Skip to content

Commit 404b4a0

Browse files
authored
Merge pull request #2 from onkernel/mason/allow-unauthed-help-completion
refactor(root): extract auth exemption logic
2 parents d579cf8 + d34c5d9 commit 404b4a0

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

cmd/root.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,20 @@ func getKernelClient(cmd *cobra.Command) kernel.Client {
7171
return cmd.Context().Value(KernelClientKey).(kernel.Client)
7272
}
7373

74+
// isAuthExempt returns true if the command or any of its parents should skip auth.
75+
func isAuthExempt(cmd *cobra.Command) bool {
76+
for c := cmd; c != nil; c = c.Parent() {
77+
if c == rootCmd {
78+
return true
79+
}
80+
switch c.Name() {
81+
case "login", "logout", "auth", "help", "completion":
82+
return true
83+
}
84+
}
85+
return false
86+
}
87+
7488
func init() {
7589
rootCmd.PersistentFlags().BoolP("version", "v", false, "Print the CLI version")
7690
rootCmd.PersistentFlags().BoolP("no-color", "", false, "Disable color output")
@@ -88,9 +102,8 @@ func init() {
88102
pterm.DisableStyling()
89103
}
90104

91-
// Check if this command requires authentication
92-
// Skip auth check for root command (help) and commands that don't need it
93-
if cmd == rootCmd || cmd.Name() == "login" || cmd.Name() == "logout" || cmd.Name() == "auth" {
105+
// Skip auth check for commands that don't need it (including children, e.g., "completion zsh")
106+
if isAuthExempt(cmd) {
94107
return nil
95108
}
96109

0 commit comments

Comments
 (0)