Merge pull request #3850 from chemotaxis/fix-actions-help

Print help even if logged out
This commit is contained in:
Nate Smith 2021-06-22 14:24:06 -07:00 committed by GitHub
commit 640a089e55
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 32 deletions

View file

@ -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)

View file

@ -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 {
<https://docs.github.com/en/actions/guides/managing-github-actions-with-github-cli>
`, header, runHeader, workflowHeader)
}
func actionsRun(opts ActionsOptions) {
cs := opts.IO.ColorScheme()
fmt.Fprintln(opts.IO.Out, actionsExplainer(cs))
}

View file

@ -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() {

View file

@ -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
}
}