From 6317bc2544c12ca0a2e4dc9f050ae23e43393218 Mon Sep 17 00:00:00 2001 From: Itay Kalfon Date: Sun, 22 Sep 2024 17:54:50 +0300 Subject: [PATCH 1/3] feat: added -q flag --- cmd/kubectx/flags.go | 11 +++++++++++ cmd/kubectx/fzf.go | 20 ++++++++++++++++---- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/cmd/kubectx/flags.go b/cmd/kubectx/flags.go index 060cd1c5..d07c8045 100644 --- a/cmd/kubectx/flags.go +++ b/cmd/kubectx/flags.go @@ -40,6 +40,17 @@ func parseArgs(argv []string) Op { return ListOp{} } + if argv[0] == "-q" { + if cmdutil.IsInteractiveMode(os.Stdout) { + if len(argv) > 1 { + return InteractiveSwitchOp{SelfCmd: os.Args[0], Query: argv[1]} + } else { + return UnsupportedOp{Err: fmt.Errorf("'-q' needs arguments")} + } + } else { + return UnsupportedOp{Err: fmt.Errorf("'-q' only works in interactive mode")} + } + } if argv[0] == "-d" { if len(argv) == 1 { if cmdutil.IsInteractiveMode(os.Stdout) { diff --git a/cmd/kubectx/fzf.go b/cmd/kubectx/fzf.go index 5006129d..7840973d 100644 --- a/cmd/kubectx/fzf.go +++ b/cmd/kubectx/fzf.go @@ -31,14 +31,23 @@ import ( ) type InteractiveSwitchOp struct { - SelfCmd string + SelfCmd string + Query string + FzfFlags []string } type InteractiveDeleteOp struct { SelfCmd string } -func (op InteractiveSwitchOp) Run(_, stderr io.Writer) error { +func (op InteractiveSwitchOp) Run(stdout io.Writer, stderr io.Writer) error { + if op.Query != "" { + op.FzfFlags = append(op.FzfFlags, "-q", op.Query) + } + return op._Run(stdout, stderr) +} + +func (op InteractiveSwitchOp) _Run(_, stderr io.Writer) error { // parse kubeconfig just to see if it can be loaded kc := new(kubeconfig.Kubeconfig).WithLoader(kubeconfig.DefaultLoader) if err := kc.Parse(); err != nil { @@ -49,8 +58,11 @@ func (op InteractiveSwitchOp) Run(_, stderr io.Writer) error { return errors.Wrap(err, "kubeconfig error") } kc.Close() - - cmd := exec.Command("fzf", "--ansi", "--no-preview") + fzfFlags := append([]string{"--ansi", "--no-preview"}, op.FzfFlags...) + cmd := exec.Command( + "fzf", + fzfFlags..., + ) var out bytes.Buffer cmd.Stdin = os.Stdin cmd.Stderr = stderr From 97172329004dac03dffdb380b20fd4d625ebedb4 Mon Sep 17 00:00:00 2001 From: Itay Kalfon Date: Sun, 22 Sep 2024 18:05:16 +0300 Subject: [PATCH 2/3] feat: added help --- cmd/kubectx/help.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cmd/kubectx/help.go b/cmd/kubectx/help.go index 020d2a3b..876eb635 100644 --- a/cmd/kubectx/help.go +++ b/cmd/kubectx/help.go @@ -41,6 +41,7 @@ func printUsage(out io.Writer) error { %PROG% =. : rename current-context to %PROG% -u, --unset : unset the current context %PROG% -d [] : delete context ('.' for current-context) + %PROG% -q : start the finder with the given query %SPAC% (this command won't delete the user/cluster entry %SPAC% referenced by the context entry) %PROG% -h,--help : show this message From 8857602f835043f32b03e2efec3936f84cc406a3 Mon Sep 17 00:00:00 2001 From: Itay Kalfon Date: Sun, 22 Sep 2024 18:15:08 +0300 Subject: [PATCH 3/3] feat: added test --- cmd/kubectx/flags.go | 2 +- cmd/kubectx/flags_test.go | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/cmd/kubectx/flags.go b/cmd/kubectx/flags.go index d07c8045..0a40f4d2 100644 --- a/cmd/kubectx/flags.go +++ b/cmd/kubectx/flags.go @@ -45,7 +45,7 @@ func parseArgs(argv []string) Op { if len(argv) > 1 { return InteractiveSwitchOp{SelfCmd: os.Args[0], Query: argv[1]} } else { - return UnsupportedOp{Err: fmt.Errorf("'-q' needs arguments")} + return UnsupportedOp{Err: fmt.Errorf("'-q' needs an argument")} } } else { return UnsupportedOp{Err: fmt.Errorf("'-q' only works in interactive mode")} diff --git a/cmd/kubectx/flags_test.go b/cmd/kubectx/flags_test.go index 071e3bf7..9ac14a93 100644 --- a/cmd/kubectx/flags_test.go +++ b/cmd/kubectx/flags_test.go @@ -78,6 +78,10 @@ func Test_parseArgs_new(t *testing.T) { {name: "too many args", args: []string{"a", "b", "c"}, want: UnsupportedOp{Err: fmt.Errorf("too many arguments")}}, + {name: "missing query", + args: []string{"-q"}, + want: UnsupportedOp{Err: fmt.Errorf("'-q' only works in interactive mode")}, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {