-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
auth_check.go
66 lines (52 loc) · 1.4 KB
/
auth_check.go
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package cmdutil
import (
"reflect"
"github.com/cli/cli/v2/internal/gh"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
const skipAuthCheckAnnotation = "skipAuthCheck"
func DisableAuthCheck(cmd *cobra.Command) {
if cmd.Annotations == nil {
cmd.Annotations = map[string]string{}
}
cmd.Annotations[skipAuthCheckAnnotation] = "true"
}
func DisableAuthCheckFlag(flag *pflag.Flag) {
if flag.Annotations == nil {
flag.Annotations = map[string][]string{}
}
flag.Annotations[skipAuthCheckAnnotation] = []string{"true"}
}
func CheckAuth(cfg gh.Config) bool {
if cfg.Authentication().HasEnvToken() {
return true
}
if len(cfg.Authentication().Hosts()) > 0 {
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() {
// Check whether any command marked as DisableAuthCheck is set
if c.Annotations != nil && c.Annotations[skipAuthCheckAnnotation] == "true" {
return false
}
// Check whether any flag marked as DisableAuthCheckFlag is set
var skipAuthCheck bool
c.Flags().Visit(func(f *pflag.Flag) {
if f.Annotations != nil && reflect.DeepEqual(f.Annotations[skipAuthCheckAnnotation], []string{"true"}) {
skipAuthCheck = true
}
})
if skipAuthCheck {
return false
}
}
return true
}