Skip to content

Commit 9ce6feb

Browse files
committed
Search the project flag in COMP_LINE instead of args
The args only include the arguments of the current subcommand.
1 parent 6734c15 commit 9ce6feb

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

predictor/predictor.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,8 @@ func (r *Resource) Predict(args complete.Args) []string {
6565
ns = org
6666
}
6767
// if there is a project set in the args use this
68-
for i, arg := range args.All {
69-
if (arg == "-p" || arg == "--project") && i+1 < len(args.All) {
70-
ns = args.All[i+1]
71-
break
72-
}
68+
if p := findProject(args); p != "" {
69+
ns = p
7370
}
7471

7572
if err := r.client.List(ctx, u, client.InNamespace(ns)); err != nil {
@@ -106,6 +103,27 @@ func listKindToResource(kind string) string {
106103
return flect.Pluralize(strings.TrimSuffix(strings.ToLower(kind), listSuffix))
107104
}
108105

106+
func findProject(_ complete.Args) string {
107+
// try to find it in the full command line.
108+
if line := os.Getenv("COMP_LINE"); line != "" {
109+
parts := strings.Fields(line)
110+
if p := findProjectInSlice(parts); p != "" {
111+
return p
112+
}
113+
}
114+
115+
return ""
116+
}
117+
118+
func findProjectInSlice(args []string) string {
119+
for i, arg := range args {
120+
if (arg == "-p" || arg == "--project") && i+1 < len(args) {
121+
return args[i+1]
122+
}
123+
}
124+
return ""
125+
}
126+
109127
func NewClient(ctx context.Context, defaultAPICluster string) (*api.Client, error) {
110128
// the client for the predictor requires a static token in the client config
111129
// since dynamic exec config seems to break with some shells during completion.

0 commit comments

Comments
 (0)