1package cmdutil
2
3import (
4	"github.com/cli/cli/v2/internal/config"
5	"github.com/spf13/cobra"
6)
7
8func DisableAuthCheck(cmd *cobra.Command) {
9	if cmd.Annotations == nil {
10		cmd.Annotations = map[string]string{}
11	}
12
13	cmd.Annotations["skipAuthCheck"] = "true"
14}
15
16func CheckAuth(cfg config.Config) bool {
17	if config.AuthTokenProvidedFromEnv() {
18		return true
19	}
20
21	hosts, err := cfg.Hosts()
22	if err != nil {
23		return false
24	}
25
26	for _, hostname := range hosts {
27		token, _ := cfg.Get(hostname, "oauth_token")
28		if token != "" {
29			return true
30		}
31	}
32
33	return false
34}
35
36func IsAuthCheckEnabled(cmd *cobra.Command) bool {
37	switch cmd.Name() {
38	case "help", cobra.ShellCompRequestCmd, cobra.ShellCompNoDescRequestCmd:
39		return false
40	}
41
42	for c := cmd; c.Parent() != nil; c = c.Parent() {
43		if c.Annotations != nil && c.Annotations["skipAuthCheck"] == "true" {
44			return false
45		}
46	}
47
48	return true
49}
50