mirror of
https://github.com/ahmetb/kubectx.git
synced 2025-07-21 10:39:03 +00:00
feat(kubens): add unset option
This commit is contained in:
parent
013b6bc252
commit
4f4a0418f9
@ -52,6 +52,8 @@ func parseArgs(argv []string) Op {
|
|||||||
return VersionOp{}
|
return VersionOp{}
|
||||||
case "--current", "-c":
|
case "--current", "-c":
|
||||||
return CurrentOp{}
|
return CurrentOp{}
|
||||||
|
case "--unset", "-u":
|
||||||
|
return UnsetOp{}
|
||||||
default:
|
default:
|
||||||
return getSwitchOp(v, false)
|
return getSwitchOp(v, false)
|
||||||
}
|
}
|
||||||
|
@ -45,6 +45,12 @@ func Test_parseArgs_new(t *testing.T) {
|
|||||||
{name: "current long form",
|
{name: "current long form",
|
||||||
args: []string{"--current"},
|
args: []string{"--current"},
|
||||||
want: CurrentOp{}},
|
want: CurrentOp{}},
|
||||||
|
{name: "unset shorthand",
|
||||||
|
args: []string{"-u"},
|
||||||
|
want: UnsetOp{}},
|
||||||
|
{name: "unset long form",
|
||||||
|
args: []string{"--unset"},
|
||||||
|
want: UnsetOp{}},
|
||||||
{name: "switch by name",
|
{name: "switch by name",
|
||||||
args: []string{"foo"},
|
args: []string{"foo"},
|
||||||
want: SwitchOp{Target: "foo"}},
|
want: SwitchOp{Target: "foo"}},
|
||||||
|
@ -39,7 +39,8 @@ func printUsage(out io.Writer) error {
|
|||||||
%PROG% - : switch to the previous namespace in this context
|
%PROG% - : switch to the previous namespace in this context
|
||||||
%PROG% -c, --current : show the current namespace
|
%PROG% -c, --current : show the current namespace
|
||||||
%PROG% -h,--help : show this message
|
%PROG% -h,--help : show this message
|
||||||
%PROG% -V,--version : show version`
|
%PROG% -V,--version : show version
|
||||||
|
%PROG% -u,--unset : unset the current context`
|
||||||
|
|
||||||
// TODO this replace logic is duplicated between this and kubectx
|
// TODO this replace logic is duplicated between this and kubectx
|
||||||
help = strings.ReplaceAll(help, "%PROG%", selfName())
|
help = strings.ReplaceAll(help, "%PROG%", selfName())
|
||||||
|
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-context 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")
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := clearNamespace(kc)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = printer.Success(stderr, "Active namespace unset for kubens.")
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func clearNamespace(kc *kubeconfig.Kubeconfig) (string, error) {
|
||||||
|
ctx := kc.GetCurrentContext()
|
||||||
|
ns := ""
|
||||||
|
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
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user