forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth_check.go
More file actions
49 lines (39 loc) · 888 Bytes
/
auth_check.go
File metadata and controls
49 lines (39 loc) · 888 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package cmdutil
import (
"github.com/cli/cli/v2/internal/config"
"github.com/spf13/cobra"
)
func DisableAuthCheck(cmd *cobra.Command) {
if cmd.Annotations == nil {
cmd.Annotations = map[string]string{}
}
cmd.Annotations["skipAuthCheck"] = "true"
}
func CheckAuth(cfg config.Config) bool {
if config.AuthTokenProvidedFromEnv() {
return true
}
hosts, err := cfg.Hosts()
if err != nil {
return false
}
for _, hostname := range hosts {
token, _ := cfg.Get(hostname, "oauth_token")
if token != "" {
return true
}
}
return false
}
func IsAuthCheckEnabled(cmd *cobra.Command) bool {
switch cmd.Name() {
case "help", cobra.ShellCompRequestCmd, cobra.ShellCompNoDescRequestCmd:
return false
}
for c := cmd; c.Parent() != nil; c = c.Parent() {
if c.Annotations != nil && c.Annotations["skipAuthCheck"] == "true" {
return false
}
}
return true
}