MCPcopy Create free account
hub / github.com/ejoffe/spr / findToken

Function findToken

github/githubclient/client.go:83–126  ·  view source on GitHub ↗
(githubHost string)

Source from the content-addressed store, hash-verified

81}
82
83func findToken(githubHost string) string {
84 // Try environment variable first
85 token := os.Getenv("GITHUB_TOKEN")
86 if token != "" {
87 return token
88 }
89
90 // Try ~/.config/gh/hosts.yml
91 cfg, err := readGhCLIConfig()
92 if err != nil {
93 log.Warn().Err(err).Msg("failed to read gh cli config file")
94 } else {
95 for host, user := range *cfg {
96 if host == githubHost {
97 secret, err := keyring.Get("gh:github.com", user.User)
98 if err != nil {
99 return user.OauthToken
100 }
101 return secret
102 }
103 }
104 }
105
106 // Try ~/.config/hub
107 hubCfg, err := readHubCLIConfig()
108 if err != nil {
109 log.Warn().Err(err).Msg("failed to read hub config file")
110 return ""
111 }
112
113 if c, ok := hubCfg["github.com"]; ok {
114 if len(c) == 0 {
115 log.Warn().Msg("no token found in hub config file")
116 return ""
117 }
118 if len(c) > 1 {
119 log.Warn().Msgf("multiple tokens found in hub config file, using first one: %s", c[0].User)
120 }
121
122 return c[0].OauthToken
123 }
124
125 return ""
126}
127
128const tokenHelpText = `
129No GitHub OAuth token found! You can either create one

Callers 1

NewGitHubClientFunction · 0.85

Calls 2

readGhCLIConfigFunction · 0.85
readHubCLIConfigFunction · 0.85

Tested by

no test coverage detected