@@ -9,28 +9,45 @@ import (
99 "os"
1010 "os/exec"
1111 "strings"
12+ "sync"
1213
1314 "github.com/github/gh-aw/pkg/console"
1415 "github.com/github/gh-aw/pkg/logger"
1516 "github.com/github/gh-aw/pkg/tty"
1617)
1718
1819var githubCLILog = logger .New ("workflow:github_cli" )
20+ var defaultGHHost struct {
21+ mu sync.RWMutex
22+ host string
23+ }
24+
25+ // SetDefaultGHHost sets the default host used by gh CLI helper commands when GH_HOST
26+ // is not set in the process environment.
27+ func SetDefaultGHHost (host string ) {
28+ defaultGHHost .mu .Lock ()
29+ defer defaultGHHost .mu .Unlock ()
30+ defaultGHHost .host = host
31+ }
32+
33+ func getDefaultGHHost () string {
34+ defaultGHHost .mu .RLock ()
35+ defer defaultGHHost .mu .RUnlock ()
36+ return defaultGHHost .host
37+ }
1938
2039// setupGHCommand creates an exec.Cmd for gh CLI with proper token configuration.
2140// This is the core implementation shared by ExecGH and ExecGHContext.
22- // When ctx is nil, it uses exec.Command; when ctx is provided, it uses exec.CommandContext .
41+ // When ctx is nil, it falls back to context.TODO() .
2342func setupGHCommand (ctx context.Context , args ... string ) * exec.Cmd {
2443 // Check if GH_TOKEN or GITHUB_TOKEN is available
2544 ghToken := os .Getenv ("GH_TOKEN" )
2645 githubToken := os .Getenv ("GITHUB_TOKEN" )
2746
28- var cmd * exec.Cmd
29- if ctx != nil {
30- cmd = exec .CommandContext (ctx , "gh" , args ... )
31- } else {
32- cmd = exec .Command ("gh" , args ... )
47+ if ctx == nil {
48+ ctx = context .TODO ()
3349 }
50+ cmd := exec .CommandContext (ctx , "gh" , args ... )
3451
3552 if ghToken != "" || githubToken != "" {
3653 githubCLILog .Printf ("Token detected, using gh CLI for command: gh %v" , args )
@@ -44,6 +61,9 @@ func setupGHCommand(ctx context.Context, args ...string) *exec.Cmd {
4461 githubCLILog .Printf ("GH_TOKEN not set, using GITHUB_TOKEN for gh CLI" )
4562 cmd .Env = append (os .Environ (), "GH_TOKEN=" + githubToken )
4663 }
64+ if os .Getenv ("GH_HOST" ) == "" {
65+ SetGHHostEnv (cmd , getDefaultGHHost ())
66+ }
4767
4868 return cmd
4969}
0 commit comments