@@ -18,6 +18,7 @@ import (
1818 "github.com/cashapp/hermit"
1919 "github.com/cashapp/hermit/cache"
2020 "github.com/cashapp/hermit/github"
21+ "github.com/cashapp/hermit/github/auth"
2122 "github.com/cashapp/hermit/state"
2223 "github.com/cashapp/hermit/ui"
2324 "github.com/cashapp/hermit/util/debug"
@@ -167,14 +168,6 @@ func Main(config Config) {
167168 cli = & unactivated {cliBase : common }
168169 }
169170
170- githubToken := os .Getenv ("HERMIT_GITHUB_TOKEN" )
171- if githubToken == "" {
172- githubToken = os .Getenv ("GITHUB_TOKEN" )
173- p .Tracef ("GitHub token set from GITHUB_TOKEN" )
174- } else {
175- p .Tracef ("GitHub token set from HERMIT_GITHUB_TOKEN" )
176- }
177-
178171 kongOptions := []kong.Option {
179172 kong.Groups {
180173 "env" : "Environment:\n Commands for creating and managing environments." ,
@@ -209,6 +202,23 @@ func Main(config Config) {
209202 log .Fatalf ("failed to initialise CLI: %s" , err )
210203 }
211204
205+ ctx , err := parser .Parse (os .Args [1 :])
206+ parser .FatalIfErrorf (err )
207+ configureLogging (cli , ctx .Command (), p )
208+
209+ var userConfig UserConfig
210+ userConfigPath := cli .getUserConfigFile ()
211+
212+ if IsUserConfigExists (userConfigPath ) {
213+ p .Tracef ("Loading user config from: %s" , userConfigPath )
214+ userConfig , err = LoadUserConfig (userConfigPath )
215+ if err != nil {
216+ log .Printf ("%s: %s" , userConfigPath , err )
217+ }
218+ } else {
219+ p .Tracef ("No user config found at: %s" , userConfigPath )
220+ }
221+
212222 var envInfo * hermit.EnvInfo
213223 if isActivated {
214224 envInfo , err = hermit .LoadEnvInfo (envPath )
@@ -217,13 +227,36 @@ func Main(config Config) {
217227 }
218228 }
219229
230+ // Initialize GitHub token
231+ var githubToken string
232+ if envInfo != nil && len (envInfo .Config .GitHubTokenAuth .Match ) > 0 {
233+ // Determine provider based on user config
234+ providerType := "env"
235+ if userConfig .GHCliAuth {
236+ providerType = "gh-cli"
237+ }
238+
239+ provider , err := auth .NewProvider (providerType , p )
240+ if err != nil {
241+ p .Tracef ("Failed to create GitHub token provider: %v" , err )
242+ } else {
243+ token , err := provider .GetToken ()
244+ if err != nil {
245+ p .Tracef ("Failed to get GitHub token from provider %s: %v" , providerType , err )
246+ } else {
247+ githubToken = token
248+ p .Tracef ("GitHub token set from provider: %s" , providerType )
249+ }
250+ }
251+ }
252+
220253 getSource := config .PackageSourceSelector
221254 if config .PackageSourceSelector == nil {
222255 getSource = cache .GetSource
223256 }
224257 defaultHTTPClient := config .defaultHTTPClient (p )
225258
226- ghClient := github .New (defaultHTTPClient , githubToken )
259+ ghClient := github .New (p , defaultHTTPClient , githubToken )
227260 if envInfo != nil {
228261 // If the environment has been configured to use GitHub token
229262 // authentication for any patterns, wrap the
@@ -245,23 +278,6 @@ func Main(config Config) {
245278 log .Fatalf ("failed to open cache: %s" , err )
246279 }
247280
248- ctx , err := parser .Parse (os .Args [1 :])
249- parser .FatalIfErrorf (err )
250- configureLogging (cli , ctx .Command (), p )
251-
252- var userConfig UserConfig
253- userConfigPath := cli .getUserConfigFile ()
254-
255- if IsUserConfigExists (userConfigPath ) {
256- p .Tracef ("Loading user config from: %s" , userConfigPath )
257- userConfig , err = LoadUserConfig (userConfigPath )
258- if err != nil {
259- log .Printf ("%s: %s" , userConfigPath , err )
260- }
261- } else {
262- p .Tracef ("No user config found at: %s" , userConfigPath )
263- }
264-
265281 config .State .LockTimeout = cli .getLockTimeout ()
266282 sta , err = state .Open (hermit .UserStateDir , config .State , cache )
267283 if err != nil {
0 commit comments