diff --git a/cmd/gh/main.go b/cmd/gh/main.go index 7ee00b061..236943475 100644 --- a/cmd/gh/main.go +++ b/cmd/gh/main.go @@ -159,11 +159,17 @@ func mainRun() exitCode { cs := cmdFactory.IOStreams.ColorScheme() - if cmd != nil && cmdutil.IsAuthCheckEnabled(cmd) && !cmdutil.CheckAuth(cfg) { - fmt.Fprintln(stderr, cs.Bold("Welcome to GitHub CLI!")) - fmt.Fprintln(stderr) - fmt.Fprintln(stderr, "To authenticate, please run `gh auth login`.") - return exitAuth + authError := errors.New("authError") + rootCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error { + // require that the user is authenticated before running most commands + if cmdutil.IsAuthCheckEnabled(cmd) && !cmdutil.CheckAuth(cfg) { + fmt.Fprintln(stderr, cs.Bold("Welcome to GitHub CLI!")) + fmt.Fprintln(stderr) + fmt.Fprintln(stderr, "To authenticate, please run `gh auth login`.") + return authError + } + + return nil } rootCmd.SetArgs(expandedArgs) @@ -177,6 +183,8 @@ func mainRun() exitCode { fmt.Fprint(stderr, "\n") } return exitCancel + } else if errors.Is(err, authError) { + return exitAuth } printError(stderr, err, cmd, hasDebug) diff --git a/pkg/cmd/actions/actions.go b/pkg/cmd/actions/actions.go index 356ca1581..7880a9a6c 100644 --- a/pkg/cmd/actions/actions.go +++ b/pkg/cmd/actions/actions.go @@ -9,39 +9,30 @@ import ( "github.com/spf13/cobra" ) -type ActionsOptions struct { - IO *iostreams.IOStreams -} - func NewCmdActions(f *cmdutil.Factory) *cobra.Command { - opts := ActionsOptions{ - IO: f.IOStreams, - } + cs := f.IOStreams.ColorScheme() cmd := &cobra.Command{ Use: "actions", Short: "Learn about working with GitHub actions", - Long: actionsExplainer(nil), + Long: actionsExplainer(cs), Run: func(cmd *cobra.Command, args []string) { - actionsRun(opts) + fmt.Fprintln(f.IOStreams.Out, actionsExplainer(cs)) }, Annotations: map[string]string{ "IsActions": "true", }, } + cmdutil.DisableAuthCheck(cmd) + return cmd } func actionsExplainer(cs *iostreams.ColorScheme) string { - header := "Welcome to GitHub Actions on the command line." - runHeader := "Interacting with workflow runs" - workflowHeader := "Interacting with workflow files" - if cs != nil { - header = cs.Bold(header) - runHeader = cs.Bold(runHeader) - workflowHeader = cs.Bold(workflowHeader) - } + header := cs.Bold("Welcome to GitHub Actions on the command line.") + runHeader := cs.Bold("Interacting with workflow runs") + workflowHeader := cs.Bold("Interacting with workflow files") return heredoc.Docf(` %s @@ -70,8 +61,3 @@ func actionsExplainer(cs *iostreams.ColorScheme) string { `, header, runHeader, workflowHeader) } - -func actionsRun(opts ActionsOptions) { - cs := opts.IO.ColorScheme() - fmt.Fprintln(opts.IO.Out, actionsExplainer(cs)) -} diff --git a/pkg/cmdutil/auth_check.go b/pkg/cmdutil/auth_check.go index 10df9fade..ab536f6b2 100644 --- a/pkg/cmdutil/auth_check.go +++ b/pkg/cmdutil/auth_check.go @@ -5,9 +5,6 @@ import ( "github.com/spf13/cobra" ) -// TODO can have this set a PersistentPreRun so we don't have to set for all child commands of auth, -// config - func DisableAuthCheck(cmd *cobra.Command) { if cmd.Annotations == nil { cmd.Annotations = map[string]string{} @@ -37,7 +34,7 @@ func CheckAuth(cfg config.Config) bool { } func IsAuthCheckEnabled(cmd *cobra.Command) bool { - if !cmd.Runnable() { + if cmd.Name() == "help" { return false } for c := cmd; c.Parent() != nil; c = c.Parent() { diff --git a/pkg/cmdutil/repo_override.go b/pkg/cmdutil/repo_override.go index c5d996c70..6ce68011b 100644 --- a/pkg/cmdutil/repo_override.go +++ b/pkg/cmdutil/repo_override.go @@ -7,12 +7,26 @@ import ( "github.com/spf13/cobra" ) +func executeParentHooks(cmd *cobra.Command, args []string) error { + for cmd.HasParent() { + cmd = cmd.Parent() + if cmd.PersistentPreRunE != nil { + return cmd.PersistentPreRunE(cmd, args) + } + } + return nil +} + func EnableRepoOverride(cmd *cobra.Command, f *Factory) { cmd.PersistentFlags().StringP("repo", "R", "", "Select another repository using the `[HOST/]OWNER/REPO` format") - cmd.PersistentPreRun = func(cmd *cobra.Command, args []string) { + cmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error { + if err := executeParentHooks(cmd, args); err != nil { + return err + } repoOverride, _ := cmd.Flags().GetString("repo") f.BaseRepo = OverrideBaseRepoFunc(f, repoOverride) + return nil } }