Skip to content
Closed
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
26 changes: 26 additions & 0 deletions predictor/predictor.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ func (r *Resource) Predict(args complete.Args) []string {
return []string{}
}
ns = org
} else {
// if there is a project set in the args use this
if p := findProject(); p != "" {
ns = p
}
}

if err := r.client.List(ctx, u, client.InNamespace(ns)); err != nil {
Expand Down Expand Up @@ -99,6 +104,27 @@ func listKindToResource(kind string) string {
return flect.Pluralize(strings.TrimSuffix(strings.ToLower(kind), listSuffix))
}

func findProject() string {
// try to find it in the full command line.
if line := os.Getenv("COMP_LINE"); line != "" {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit ugly, but the issue is that the complete.Args only include the arguments of the current subcommand, and --project is a global flag.

parts := strings.Fields(line)
if p := findProjectInSlice(parts); p != "" {
return p
}
}

return ""
}

func findProjectInSlice(args []string) string {
for i, arg := range args {
if (arg == "-p" || arg == "--project") && i+1 < len(args) {
return args[i+1]
}
}
return ""
}

func NewClient(ctx context.Context, defaultAPICluster string) (*api.Client, error) {
// the client for the predictor requires a static token in the client config
// since dynamic exec config seems to break with some shells during completion.
Expand Down