Skip to content

Commit 640a089

Browse files
author
Nate Smith
authored
Merge pull request cli#3850 from chemotaxis/fix-actions-help
Print help even if logged out
2 parents 654336f + b0f58d0 commit 640a089

File tree

4 files changed

+37
-32
lines changed

4 files changed

+37
-32
lines changed

cmd/gh/main.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,17 @@ func mainRun() exitCode {
159159

160160
cs := cmdFactory.IOStreams.ColorScheme()
161161

162-
if cmd != nil && cmdutil.IsAuthCheckEnabled(cmd) && !cmdutil.CheckAuth(cfg) {
163-
fmt.Fprintln(stderr, cs.Bold("Welcome to GitHub CLI!"))
164-
fmt.Fprintln(stderr)
165-
fmt.Fprintln(stderr, "To authenticate, please run `gh auth login`.")
166-
return exitAuth
162+
authError := errors.New("authError")
163+
rootCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
164+
// require that the user is authenticated before running most commands
165+
if cmdutil.IsAuthCheckEnabled(cmd) && !cmdutil.CheckAuth(cfg) {
166+
fmt.Fprintln(stderr, cs.Bold("Welcome to GitHub CLI!"))
167+
fmt.Fprintln(stderr)
168+
fmt.Fprintln(stderr, "To authenticate, please run `gh auth login`.")
169+
return authError
170+
}
171+
172+
return nil
167173
}
168174

169175
rootCmd.SetArgs(expandedArgs)
@@ -177,6 +183,8 @@ func mainRun() exitCode {
177183
fmt.Fprint(stderr, "\n")
178184
}
179185
return exitCancel
186+
} else if errors.Is(err, authError) {
187+
return exitAuth
180188
}
181189

182190
printError(stderr, err, cmd, hasDebug)

pkg/cmd/actions/actions.go

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,30 @@ import (
99
"github.com/spf13/cobra"
1010
)
1111

12-
type ActionsOptions struct {
13-
IO *iostreams.IOStreams
14-
}
15-
1612
func NewCmdActions(f *cmdutil.Factory) *cobra.Command {
17-
opts := ActionsOptions{
18-
IO: f.IOStreams,
19-
}
13+
cs := f.IOStreams.ColorScheme()
2014

2115
cmd := &cobra.Command{
2216
Use: "actions",
2317
Short: "Learn about working with GitHub actions",
24-
Long: actionsExplainer(nil),
18+
Long: actionsExplainer(cs),
2519
Run: func(cmd *cobra.Command, args []string) {
26-
actionsRun(opts)
20+
fmt.Fprintln(f.IOStreams.Out, actionsExplainer(cs))
2721
},
2822
Annotations: map[string]string{
2923
"IsActions": "true",
3024
},
3125
}
3226

27+
cmdutil.DisableAuthCheck(cmd)
28+
3329
return cmd
3430
}
3531

3632
func actionsExplainer(cs *iostreams.ColorScheme) string {
37-
header := "Welcome to GitHub Actions on the command line."
38-
runHeader := "Interacting with workflow runs"
39-
workflowHeader := "Interacting with workflow files"
40-
if cs != nil {
41-
header = cs.Bold(header)
42-
runHeader = cs.Bold(runHeader)
43-
workflowHeader = cs.Bold(workflowHeader)
44-
}
33+
header := cs.Bold("Welcome to GitHub Actions on the command line.")
34+
runHeader := cs.Bold("Interacting with workflow runs")
35+
workflowHeader := cs.Bold("Interacting with workflow files")
4536

4637
return heredoc.Docf(`
4738
%s
@@ -70,8 +61,3 @@ func actionsExplainer(cs *iostreams.ColorScheme) string {
7061
<https://docs.github.com/en/actions/guides/managing-github-actions-with-github-cli>
7162
`, header, runHeader, workflowHeader)
7263
}
73-
74-
func actionsRun(opts ActionsOptions) {
75-
cs := opts.IO.ColorScheme()
76-
fmt.Fprintln(opts.IO.Out, actionsExplainer(cs))
77-
}

pkg/cmdutil/auth_check.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ import (
55
"github.com/spf13/cobra"
66
)
77

8-
// TODO can have this set a PersistentPreRun so we don't have to set for all child commands of auth,
9-
// config
10-
118
func DisableAuthCheck(cmd *cobra.Command) {
129
if cmd.Annotations == nil {
1310
cmd.Annotations = map[string]string{}
@@ -37,7 +34,7 @@ func CheckAuth(cfg config.Config) bool {
3734
}
3835

3936
func IsAuthCheckEnabled(cmd *cobra.Command) bool {
40-
if !cmd.Runnable() {
37+
if cmd.Name() == "help" {
4138
return false
4239
}
4340
for c := cmd; c.Parent() != nil; c = c.Parent() {

pkg/cmdutil/repo_override.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,26 @@ import (
77
"github.com/spf13/cobra"
88
)
99

10+
func executeParentHooks(cmd *cobra.Command, args []string) error {
11+
for cmd.HasParent() {
12+
cmd = cmd.Parent()
13+
if cmd.PersistentPreRunE != nil {
14+
return cmd.PersistentPreRunE(cmd, args)
15+
}
16+
}
17+
return nil
18+
}
19+
1020
func EnableRepoOverride(cmd *cobra.Command, f *Factory) {
1121
cmd.PersistentFlags().StringP("repo", "R", "", "Select another repository using the `[HOST/]OWNER/REPO` format")
1222

13-
cmd.PersistentPreRun = func(cmd *cobra.Command, args []string) {
23+
cmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
24+
if err := executeParentHooks(cmd, args); err != nil {
25+
return err
26+
}
1427
repoOverride, _ := cmd.Flags().GetString("repo")
1528
f.BaseRepo = OverrideBaseRepoFunc(f, repoOverride)
29+
return nil
1630
}
1731
}
1832

0 commit comments

Comments
 (0)