Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: add a flag to allow prefixing a query in interactive mode #429

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions cmd/kubectx/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 an argument")}
}
} 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) {
Expand Down
4 changes: 4 additions & 0 deletions cmd/kubectx/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
20 changes: 16 additions & 4 deletions cmd/kubectx/fzf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions cmd/kubectx/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func printUsage(out io.Writer) error {
%PROG% <NEW_NAME>=. : rename current-context to <NEW_NAME>
%PROG% -u, --unset : unset the current context
%PROG% -d <NAME> [<NAME...>] : delete context <NAME> ('.' for current-context)
%PROG% -q <QUERY> : 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
Expand Down