diff --git a/predictor/predictor.go b/predictor/predictor.go index 2e072417..f6fdbd2f 100644 --- a/predictor/predictor.go +++ b/predictor/predictor.go @@ -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 { @@ -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 != "" { + 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.