X Tutup
Skip to content

Commit f7ee39d

Browse files
committed
bonus: logout error if GITHUB_TOKEN is set
1 parent e6ae0a1 commit f7ee39d

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

pkg/cmd/auth/logout/logout.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66
"net/http"
7+
"os"
78

89
"github.com/AlecAivazis/survey/v2"
910
"github.com/MakeNowJust/heredoc"
@@ -62,7 +63,10 @@ func NewCmdLogout(f *cmdutil.Factory, runF func(*LogoutOptions) error) *cobra.Co
6263
}
6364

6465
func logoutRun(opts *LogoutOptions) error {
65-
// TODO check for GITHUB_TOKEN and error if found
66+
if os.Getenv("GITHUB_TOKEN") != "" {
67+
return errors.New("GITHUB_TOKEN is set in your environment. If you no longer want to use it with gh, please unset it.")
68+
}
69+
6670
isTTY := opts.IO.IsStdinTTY() && opts.IO.IsStdoutTTY()
6771

6872
hostname := opts.Hostname

pkg/cmd/auth/logout/logout_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package logout
33
import (
44
"bytes"
55
"net/http"
6+
"os"
67
"regexp"
78
"testing"
89

@@ -183,6 +184,7 @@ func Test_logoutRun_nontty(t *testing.T) {
183184
cfgHosts []string
184185
wantHosts string
185186
wantErr *regexp.Regexp
187+
ghtoken string
186188
}{
187189
{
188190
name: "no arguments",
@@ -211,10 +213,21 @@ func Test_logoutRun_nontty(t *testing.T) {
211213
},
212214
wantErr: regexp.MustCompile(`not logged in to any hosts`),
213215
},
216+
{
217+
name: "gh token is set",
218+
opts: &LogoutOptions{},
219+
ghtoken: "abc123",
220+
wantErr: regexp.MustCompile(`GITHUB_TOKEN is set in your environment`),
221+
},
214222
}
215223

216224
for _, tt := range tests {
217225
t.Run(tt.name, func(t *testing.T) {
226+
ghtoken := os.Getenv("GITHUB_TOKEN")
227+
defer func() {
228+
os.Setenv("GITHUB_TOKEN", ghtoken)
229+
}()
230+
os.Setenv("GITHUB_TOKEN", tt.ghtoken)
218231
io, _, _, stderr := iostreams.Test()
219232

220233
io.SetStdinTTY(false)

0 commit comments

Comments
 (0)
X Tutup