setupGHCommand creates an exec.Cmd for gh CLI with proper token configuration. This is the core implementation shared by ExecGH and ExecGHContext. When ctx is nil, it falls back to context.TODO().
(ctx context.Context, args ...string)
| 40 | // This is the core implementation shared by ExecGH and ExecGHContext. |
| 41 | // When ctx is nil, it falls back to context.TODO(). |
| 42 | func setupGHCommand(ctx context.Context, args ...string) *exec.Cmd { |
| 43 | // Check if GH_TOKEN or GITHUB_TOKEN is available |
| 44 | ghToken := os.Getenv("GH_TOKEN") |
| 45 | githubToken := os.Getenv("GITHUB_TOKEN") |
| 46 | |
| 47 | if ctx == nil { |
| 48 | ctx = context.TODO() |
| 49 | } |
| 50 | cmd := exec.CommandContext(ctx, "gh", args...) |
| 51 | |
| 52 | if ghToken != "" || githubToken != "" { |
| 53 | githubCLILog.Printf("Token detected, using gh CLI for command: gh %v", args) |
| 54 | } else { |
| 55 | githubCLILog.Printf("No token available, using default gh CLI for command: gh %v", args) |
| 56 | } |
| 57 | |
| 58 | // Set up environment to ensure token is available |
| 59 | // Only add GH_TOKEN if it's not set but GITHUB_TOKEN is available |
| 60 | if ghToken == "" && githubToken != "" { |
| 61 | githubCLILog.Printf("GH_TOKEN not set, using GITHUB_TOKEN for gh CLI") |
| 62 | cmd.Env = append(os.Environ(), "GH_TOKEN="+githubToken) |
| 63 | } |
| 64 | if os.Getenv("GH_HOST") == "" { |
| 65 | SetGHHostEnv(cmd, getDefaultGHHost()) |
| 66 | } |
| 67 | |
| 68 | return cmd |
| 69 | } |
| 70 | |
| 71 | // ExecGH wraps gh CLI calls and ensures proper token configuration. |
| 72 | // It uses go-gh/v2 to execute gh commands when GH_TOKEN or GITHUB_TOKEN is available, |