X Tutup
Skip to content

Commit cba401d

Browse files
committed
check parent annotatiosn for auth skip
1 parent 9975cbf commit cba401d

File tree

8 files changed

+17
-16
lines changed

8 files changed

+17
-16
lines changed

cmd/gh/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,15 @@ func main() {
9393
}
9494
}
9595

96-
_, skipAuthCheck := cmd.Annotations["skipAuthCheck"]
96+
authCheckEnabled := cmdutil.IsAuthCheckEnabled(cmd)
9797

9898
// TODO support other names
9999
ghtoken := os.Getenv("GITHUB_TOKEN")
100100
if ghtoken != "" {
101-
skipAuthCheck = true
101+
authCheckEnabled = false
102102
}
103103

104-
if !skipAuthCheck {
104+
if authCheckEnabled {
105105
hasAuth := false
106106

107107
cfg, err := cmdFactory.Config()

pkg/cmd/auth/login/login.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,6 @@ func NewCmdLogin(f *cmdutil.Factory, runF func(*LoginOptions) error) *cobra.Comm
9595
},
9696
}
9797

98-
cmdutil.DisableAuthCheck(cmd)
99-
10098
cmd.Flags().StringVarP(&opts.Hostname, "hostname", "h", "", "The hostname of the GitHub instance to authenticate with")
10199
cmd.Flags().Bool("with-token", false, "Read token from standard input")
102100

pkg/cmd/auth/logout/logout.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ func NewCmdLogout(f *cmdutil.Factory, runF func(*LogoutOptions) error) *cobra.Co
5757
},
5858
}
5959

60-
cmdutil.DisableAuthCheck(cmd)
61-
6260
cmd.Flags().StringVarP(&opts.Hostname, "hostname", "h", "", "The hostname of the GitHub instance to log out of")
6361

6462
return cmd

pkg/cmd/auth/refresh/refresh.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ func NewCmdRefresh(f *cmdutil.Factory, runF func(*RefreshOptions) error) *cobra.
5757
},
5858
}
5959

60-
cmdutil.DisableAuthCheck(cmd)
61-
6260
cmd.Flags().StringVarP(&opts.Hostname, "hostname", "h", "", "The GitHub host to use for authentication")
6361
cmd.Flags().StringSliceVarP(&opts.Scopes, "scopes", "s", nil, "Additional authentication scopes for gh to have")
6462

pkg/cmd/auth/status/status.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ func NewCmdStatus(f *cmdutil.Factory, runF func(*StatusOptions) error) *cobra.Co
5757
},
5858
}
5959

60-
cmdutil.DisableAuthCheck(cmd)
61-
6260
cmd.Flags().StringVarP(&opts.Hostname, "hostname", "h", "", "Check a specific hostname's auth status")
6361

6462
return cmd

pkg/cmd/config/config.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,6 @@ func NewCmdConfigGet(f *cmdutil.Factory) *cobra.Command {
5858
},
5959
}
6060

61-
cmdutil.DisableAuthCheck(cmd)
62-
6361
cmd.Flags().StringVarP(&hostname, "host", "h", "", "Get per-host setting")
6462

6563
return cmd
@@ -96,8 +94,6 @@ func NewCmdConfigSet(f *cmdutil.Factory) *cobra.Command {
9694
},
9795
}
9896

99-
cmdutil.DisableAuthCheck(cmd)
100-
10197
cmd.Flags().StringVarP(&hostname, "host", "h", "", "Set per-host setting")
10298

10399
return cmd

pkg/cmd/factory/http.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func httpClient(io *iostreams.IOStreams, cfg config.Config, appVersion string, s
3131
token, err := cfg.Get(hostname, "oauth_token")
3232
if err != nil || token == "" {
3333
// Users shouldn't see this because of the pre-execute auth check on commands
34-
return "", fmt.Errorf("authentication required for %s; please run `gh auth login -h %s", hostname, hostname)
34+
return "", fmt.Errorf("authentication required for %s; please run `gh auth login -h %s`", hostname, hostname)
3535
}
3636

3737
return fmt.Sprintf("token %s", token), nil

pkg/cmdutil/auth_check.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,16 @@ func CheckAuth(cfg config.Config) bool {
3131

3232
return false
3333
}
34+
35+
func IsAuthCheckEnabled(cmd *cobra.Command) bool {
36+
if !cmd.Runnable() {
37+
return false
38+
}
39+
for c := cmd; c.Parent() != nil; c = c.Parent() {
40+
if c.Annotations != nil && c.Annotations["skipAuthCheck"] == "true" {
41+
return false
42+
}
43+
}
44+
45+
return true
46+
}

0 commit comments

Comments
 (0)
X Tutup