auth: tab-complete context names for `auth use` · Entire

auth: tab-complete context names for auth use

69fe06f→main·

toothbrush·1mo ago·1 file·+30 added/-1 removed

Add a ValidArgsFunction to entire auth use <context> so shells complete stored context names, annotated with handle, core URL, and an (active) marker for the current context.

Co-Authored-By: Claude Opus 4.8 (1M context) noreply@anthropic.com

Sessions

b4de22d0a34cView transcript

Changes

1

29 unmodified lines

30
31
32
33
33
34
35
36
37
5 unmodified lines

43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

29 unmodified lines

"Control-plane commands (auth status/list/revoke, org/project/repo/grant) still\n" +
        "target the configured auth host (ENTIRE_AUTH_BASE_URL / the default), so\n" +
        "switching to a context on a different login server does not retarget them yet.",
        Args: cobra.ExactArgs(1),
        Args:              cobra.ExactArgs(1),
        ValidArgsFunction: completeContextNames,
        RunE: func(cmd *cobra.Command, args []string) error {
            if err := auth.SetCurrentContext(args[0]); err != nil {
                return err //nolint:wrapcheck // already a user-facing message
5 unmodified lines

}
}

// completeContextNames is the ValidArgsFunction for commands taking a single
// <context> positional. It offers the stored context names, each annotated
// (shell-completion descriptions, after a tab) with handle, core URL, and an
// "(active)" marker for the current context. Errors are swallowed because
// completion runs on every TAB press; a failed read just yields no suggestions.
func completeContextNames(_ *cobra.Command, args []string, _ string) ([]string, cobra.ShellCompDirective) {
    if len(args) != 0 {
        // <context> is a single positional; nothing to complete past it.
        return nil, cobra.ShellCompDirectiveNoFileComp
    }
    all, current, err := auth.Contexts()
    if err != nil {
        return nil, cobra.ShellCompDirectiveNoFileComp
    }
    out := make([]string, 0, len(all))
    for _, c := range all {
        desc := c.Handle
        if c.CoreURL != "" {
            desc += " " + c.CoreURL
        }
        if c.Name == current {
            desc += " (active)"
        }
        out = append(out, c.Name+"\t"+desc)
    }
    return out, cobra.ShellCompDirectiveNoFileComp
}

// warnIfCrossCoreContext warns when the now-active context authenticates
// against a different core than the control plane targets. Clone resolves
// per-cluster and is unaffected, but auth status/list/revoke and the