check parent annotatiosn for auth skip

This commit is contained in:
vilmibm 2020-08-19 10:21:19 -05:00
parent 9975cbf291
commit cba401deb0
8 changed files with 17 additions and 16 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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