diff --git a/cmd/gh/main.go b/cmd/gh/main.go index 17bce66fc..2e8513384 100644 --- a/cmd/gh/main.go +++ b/cmd/gh/main.go @@ -93,15 +93,15 @@ func main() { } } - _, skipAuthCheck := cmd.Annotations["skipAuthCheck"] + authCheckEnabled := cmdutil.IsAuthCheckEnabled(cmd) // TODO support other names ghtoken := os.Getenv("GITHUB_TOKEN") if ghtoken != "" { - skipAuthCheck = true + authCheckEnabled = false } - if !skipAuthCheck { + if authCheckEnabled { hasAuth := false cfg, err := cmdFactory.Config() diff --git a/pkg/cmd/auth/login/login.go b/pkg/cmd/auth/login/login.go index 219999d8c..7d53839c6 100644 --- a/pkg/cmd/auth/login/login.go +++ b/pkg/cmd/auth/login/login.go @@ -95,8 +95,6 @@ func NewCmdLogin(f *cmdutil.Factory, runF func(*LoginOptions) error) *cobra.Comm }, } - cmdutil.DisableAuthCheck(cmd) - cmd.Flags().StringVarP(&opts.Hostname, "hostname", "h", "", "The hostname of the GitHub instance to authenticate with") cmd.Flags().Bool("with-token", false, "Read token from standard input") diff --git a/pkg/cmd/auth/logout/logout.go b/pkg/cmd/auth/logout/logout.go index db9a57165..6f047a079 100644 --- a/pkg/cmd/auth/logout/logout.go +++ b/pkg/cmd/auth/logout/logout.go @@ -57,8 +57,6 @@ func NewCmdLogout(f *cmdutil.Factory, runF func(*LogoutOptions) error) *cobra.Co }, } - cmdutil.DisableAuthCheck(cmd) - cmd.Flags().StringVarP(&opts.Hostname, "hostname", "h", "", "The hostname of the GitHub instance to log out of") return cmd diff --git a/pkg/cmd/auth/refresh/refresh.go b/pkg/cmd/auth/refresh/refresh.go index e85aa195c..8a69ea0e2 100644 --- a/pkg/cmd/auth/refresh/refresh.go +++ b/pkg/cmd/auth/refresh/refresh.go @@ -57,8 +57,6 @@ func NewCmdRefresh(f *cmdutil.Factory, runF func(*RefreshOptions) error) *cobra. }, } - cmdutil.DisableAuthCheck(cmd) - cmd.Flags().StringVarP(&opts.Hostname, "hostname", "h", "", "The GitHub host to use for authentication") cmd.Flags().StringSliceVarP(&opts.Scopes, "scopes", "s", nil, "Additional authentication scopes for gh to have") diff --git a/pkg/cmd/auth/status/status.go b/pkg/cmd/auth/status/status.go index 7995f40e6..09d2f2fde 100644 --- a/pkg/cmd/auth/status/status.go +++ b/pkg/cmd/auth/status/status.go @@ -57,8 +57,6 @@ func NewCmdStatus(f *cmdutil.Factory, runF func(*StatusOptions) error) *cobra.Co }, } - cmdutil.DisableAuthCheck(cmd) - cmd.Flags().StringVarP(&opts.Hostname, "hostname", "h", "", "Check a specific hostname's auth status") return cmd diff --git a/pkg/cmd/config/config.go b/pkg/cmd/config/config.go index ee5c45b9f..3fa86ddcb 100644 --- a/pkg/cmd/config/config.go +++ b/pkg/cmd/config/config.go @@ -58,8 +58,6 @@ func NewCmdConfigGet(f *cmdutil.Factory) *cobra.Command { }, } - cmdutil.DisableAuthCheck(cmd) - cmd.Flags().StringVarP(&hostname, "host", "h", "", "Get per-host setting") return cmd @@ -96,8 +94,6 @@ func NewCmdConfigSet(f *cmdutil.Factory) *cobra.Command { }, } - cmdutil.DisableAuthCheck(cmd) - cmd.Flags().StringVarP(&hostname, "host", "h", "", "Set per-host setting") return cmd diff --git a/pkg/cmd/factory/http.go b/pkg/cmd/factory/http.go index 7a4ede424..1cccfda9c 100644 --- a/pkg/cmd/factory/http.go +++ b/pkg/cmd/factory/http.go @@ -31,7 +31,7 @@ func httpClient(io *iostreams.IOStreams, cfg config.Config, appVersion string, s token, err := cfg.Get(hostname, "oauth_token") if err != nil || token == "" { // Users shouldn't see this because of the pre-execute auth check on commands - return "", fmt.Errorf("authentication required for %s; please run `gh auth login -h %s", hostname, hostname) + return "", fmt.Errorf("authentication required for %s; please run `gh auth login -h %s`", hostname, hostname) } return fmt.Sprintf("token %s", token), nil diff --git a/pkg/cmdutil/auth_check.go b/pkg/cmdutil/auth_check.go index 008ea998d..5d40d0143 100644 --- a/pkg/cmdutil/auth_check.go +++ b/pkg/cmdutil/auth_check.go @@ -31,3 +31,16 @@ func CheckAuth(cfg config.Config) bool { return false } + +func IsAuthCheckEnabled(cmd *cobra.Command) bool { + if !cmd.Runnable() { + return false + } + for c := cmd; c.Parent() != nil; c = c.Parent() { + if c.Annotations != nil && c.Annotations["skipAuthCheck"] == "true" { + return false + } + } + + return true +}