mirror of
https://github.com/ahmetb/kubectx.git
synced 2026-03-11 08:22:11 +00:00
Compare commits
7 Commits
feature/ky
...
add_unset_
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bc1b8fe34d | ||
|
|
e8ea180cee | ||
|
|
cef93629f0 | ||
|
|
7fc9c21f4b | ||
|
|
63ea64cdc7 | ||
|
|
a6cf1728fe | ||
|
|
80bbe8306a |
21
.github/dependabot.yml
vendored
Normal file
21
.github/dependabot.yml
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
version: 2
|
||||
updates:
|
||||
- package-ecosystem: github-actions
|
||||
directory: /
|
||||
schedule:
|
||||
interval: weekly
|
||||
commit-message:
|
||||
prefix: chore
|
||||
include: scope
|
||||
|
||||
- package-ecosystem: gomod
|
||||
directory: /
|
||||
schedule:
|
||||
interval: weekly
|
||||
commit-message:
|
||||
prefix: chore
|
||||
include: scope
|
||||
groups:
|
||||
kubernetes:
|
||||
patterns:
|
||||
- "k8s.io/*"
|
||||
24
.github/workflows/dependabot.yml
vendored
Normal file
24
.github/workflows/dependabot.yml
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
name: Dependabot
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
|
||||
permissions:
|
||||
contents: write
|
||||
pull-requests: write
|
||||
|
||||
jobs:
|
||||
auto-merge:
|
||||
runs-on: ubuntu-latest
|
||||
if: ${{ github.actor == 'dependabot[bot]' }}
|
||||
steps:
|
||||
- name: Dependabot metadata
|
||||
id: metadata
|
||||
uses: dependabot/fetch-metadata@v2
|
||||
|
||||
- name: Enable auto-merge for Dependabot PRs
|
||||
if: ${{ steps.metadata.outputs.update-type != 'version-update:semver-major' }}
|
||||
run: gh pr merge --auto --merge "$PR_URL"
|
||||
env:
|
||||
PR_URL: ${{ github.event.pull_request.html_url }}
|
||||
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
@@ -52,6 +52,8 @@ func parseArgs(argv []string) Op {
|
||||
return VersionOp{}
|
||||
case "--current", "-c":
|
||||
return CurrentOp{}
|
||||
case "--unset", "-u":
|
||||
return UnsetOp{}
|
||||
default:
|
||||
return getSwitchOp(v, false)
|
||||
}
|
||||
|
||||
@@ -45,6 +45,12 @@ func Test_parseArgs_new(t *testing.T) {
|
||||
{name: "current long form",
|
||||
args: []string{"--current"},
|
||||
want: CurrentOp{}},
|
||||
{name: "unset shorthand",
|
||||
args: []string{"-u"},
|
||||
want: UnsetOp{}},
|
||||
{name: "unset long form",
|
||||
args: []string{"--unset"},
|
||||
want: UnsetOp{}},
|
||||
{name: "switch by name",
|
||||
args: []string{"foo"},
|
||||
want: SwitchOp{Target: "foo"}},
|
||||
|
||||
@@ -39,6 +39,7 @@ func printUsage(out io.Writer) error {
|
||||
%PROG% - : switch to the previous namespace in this context
|
||||
%PROG% -c, --current : show the current namespace
|
||||
%PROG% -h,--help : show this message
|
||||
%PROG% -u,--unset : unset the namespace choice (set to 'default')
|
||||
%PROG% -V,--version : show version`
|
||||
|
||||
// TODO this replace logic is duplicated between this and kubectx
|
||||
|
||||
58
cmd/kubens/unset.go
Normal file
58
cmd/kubens/unset.go
Normal file
@@ -0,0 +1,58 @@
|
||||
// Copyright 2021 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/ahmetb/kubectx/internal/kubeconfig"
|
||||
"github.com/ahmetb/kubectx/internal/printer"
|
||||
)
|
||||
|
||||
// UnsetOp indicates intention to remove current namespace preference.
|
||||
type UnsetOp struct{}
|
||||
|
||||
func (_ UnsetOp) Run(_, stderr io.Writer) error {
|
||||
kc := new(kubeconfig.Kubeconfig).WithLoader(kubeconfig.DefaultLoader)
|
||||
defer kc.Close()
|
||||
if err := kc.Parse(); err != nil {
|
||||
return errors.Wrap(err, "kubeconfig error")
|
||||
}
|
||||
|
||||
ns, err := clearNamespace(kc)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = printer.Success(stderr, "Active namespace is \"%s\".", printer.SuccessColor.Sprint(ns))
|
||||
return err
|
||||
}
|
||||
|
||||
func clearNamespace(kc *kubeconfig.Kubeconfig) (string, error) {
|
||||
ctx := kc.GetCurrentContext()
|
||||
ns := "default"
|
||||
if ctx == "" {
|
||||
return "", errors.New("current-context is not set")
|
||||
}
|
||||
|
||||
if err := kc.SetNamespace(ctx, ns); err != nil {
|
||||
return "", errors.Wrapf(err, "failed to clear namespace")
|
||||
}
|
||||
if err := kc.Save(); err != nil {
|
||||
return "", errors.Wrap(err, "failed to save kubeconfig file")
|
||||
}
|
||||
return ns, nil
|
||||
}
|
||||
Reference in New Issue
Block a user