X Tutup
Skip to content

Commit 2eb40f8

Browse files
committed
Empty auth token env variables are equal to being unset
1 parent 414de33 commit 2eb40f8

File tree

3 files changed

+17
-38
lines changed

3 files changed

+17
-38
lines changed

cmd/gh/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,8 @@ func basicClient(currentVersion string) (*api.Client, error) {
243243
}
244244
opts = append(opts, api.AddHeader("User-Agent", fmt.Sprintf("GitHub CLI %s", currentVersion)))
245245

246-
token, _, found := config.AuthTokenFromEnv(ghinstance.Default())
247-
if !found {
246+
token, _ := config.AuthTokenFromEnv(ghinstance.Default())
247+
if token == "" {
248248
if c, err := config.ParseDefaultConfig(); err == nil {
249249
token, _ = c.Get(ghinstance.Default(), "oauth_token")
250250
}

internal/config/from_env.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ func (c *envConfig) Hosts() ([]string, error) {
3030
hasDefault = true
3131
}
3232
}
33-
_, _, found := AuthTokenFromEnv(ghinstance.Default())
34-
if (err != nil || !hasDefault) && found {
33+
token, _ := AuthTokenFromEnv(ghinstance.Default())
34+
if (err != nil || !hasDefault) && token != "" {
3535
hosts = append([]string{ghinstance.Default()}, hosts...)
3636
return hosts, nil
3737
}
@@ -45,7 +45,7 @@ func (c *envConfig) Get(hostname, key string) (string, error) {
4545

4646
func (c *envConfig) GetWithSource(hostname, key string) (string, string, error) {
4747
if hostname != "" && key == "oauth_token" {
48-
if token, env, found := AuthTokenFromEnv(hostname); found {
48+
if token, env := AuthTokenFromEnv(hostname); token != "" {
4949
return token, env, nil
5050
}
5151
}
@@ -55,28 +55,26 @@ func (c *envConfig) GetWithSource(hostname, key string) (string, string, error)
5555

5656
func (c *envConfig) CheckWriteable(hostname, key string) error {
5757
if hostname != "" && key == "oauth_token" {
58-
if _, env, found := AuthTokenFromEnv(hostname); found {
58+
if token, env := AuthTokenFromEnv(hostname); token != "" {
5959
return fmt.Errorf("read-only token in %s cannot be modified", env)
6060
}
6161
}
6262

6363
return c.Config.CheckWriteable(hostname, key)
6464
}
6565

66-
func AuthTokenFromEnv(hostname string) (string, string, bool) {
66+
func AuthTokenFromEnv(hostname string) (string, string) {
6767
if ghinstance.IsEnterprise(hostname) {
68-
if token, found := os.LookupEnv(GH_ENTERPRISE_TOKEN); found {
69-
return token, GH_ENTERPRISE_TOKEN, found
68+
if token := os.Getenv(GH_ENTERPRISE_TOKEN); token != "" {
69+
return token, GH_ENTERPRISE_TOKEN
7070
}
7171

72-
token, found := os.LookupEnv(GITHUB_ENTERPRISE_TOKEN)
73-
return token, GITHUB_ENTERPRISE_TOKEN, found
72+
return os.Getenv(GITHUB_ENTERPRISE_TOKEN), GITHUB_ENTERPRISE_TOKEN
7473
}
7574

76-
if token, found := os.LookupEnv(GH_TOKEN); found {
77-
return token, GH_TOKEN, found
75+
if token := os.Getenv(GH_TOKEN); token != "" {
76+
return token, GH_TOKEN
7877
}
7978

80-
token, found := os.LookupEnv(GITHUB_TOKEN)
81-
return token, GITHUB_TOKEN, found
79+
return os.Getenv(GITHUB_TOKEN), GITHUB_TOKEN
8280
}

internal/config/from_env_test.go

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -260,29 +260,10 @@ func TestInheritEnv(t *testing.T) {
260260
}
261261
for _, tt := range tests {
262262
t.Run(tt.name, func(t *testing.T) {
263-
if tt.GITHUB_TOKEN != "" {
264-
os.Setenv("GITHUB_TOKEN", tt.GITHUB_TOKEN)
265-
} else {
266-
os.Unsetenv("GITHUB_TOKEN")
267-
}
268-
269-
if tt.GITHUB_ENTERPRISE_TOKEN != "" {
270-
os.Setenv("GITHUB_ENTERPRISE_TOKEN", tt.GITHUB_ENTERPRISE_TOKEN)
271-
} else {
272-
os.Unsetenv("GITHUB_ENTERPRISE_TOKEN")
273-
}
274-
275-
if tt.GH_TOKEN != "" {
276-
os.Setenv("GH_TOKEN", tt.GH_TOKEN)
277-
} else {
278-
os.Unsetenv("GH_TOKEN")
279-
}
280-
281-
if tt.GH_ENTERPRISE_TOKEN != "" {
282-
os.Setenv("GH_ENTERPRISE_TOKEN", tt.GH_ENTERPRISE_TOKEN)
283-
} else {
284-
os.Unsetenv("GH_ENTERPRISE_TOKEN")
285-
}
263+
os.Setenv("GITHUB_TOKEN", tt.GITHUB_TOKEN)
264+
os.Setenv("GITHUB_ENTERPRISE_TOKEN", tt.GITHUB_ENTERPRISE_TOKEN)
265+
os.Setenv("GH_TOKEN", tt.GH_TOKEN)
266+
os.Setenv("GH_ENTERPRISE_TOKEN", tt.GH_ENTERPRISE_TOKEN)
286267

287268
baseCfg := NewFromString(tt.baseConfig)
288269
cfg := InheritEnv(baseCfg)

0 commit comments

Comments
 (0)
X Tutup