diff --git a/pkg/cmd/auth/logout/logout.go b/pkg/cmd/auth/logout/logout.go index 5192f2bf7..6f047a079 100644 --- a/pkg/cmd/auth/logout/logout.go +++ b/pkg/cmd/auth/logout/logout.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "net/http" + "os" "github.com/AlecAivazis/survey/v2" "github.com/MakeNowJust/heredoc" @@ -62,7 +63,10 @@ func NewCmdLogout(f *cmdutil.Factory, runF func(*LogoutOptions) error) *cobra.Co } func logoutRun(opts *LogoutOptions) error { - // TODO check for GITHUB_TOKEN and error if found + if os.Getenv("GITHUB_TOKEN") != "" { + return errors.New("GITHUB_TOKEN is set in your environment. If you no longer want to use it with gh, please unset it.") + } + isTTY := opts.IO.IsStdinTTY() && opts.IO.IsStdoutTTY() hostname := opts.Hostname diff --git a/pkg/cmd/auth/logout/logout_test.go b/pkg/cmd/auth/logout/logout_test.go index 83d13aad3..50e1bed2a 100644 --- a/pkg/cmd/auth/logout/logout_test.go +++ b/pkg/cmd/auth/logout/logout_test.go @@ -3,6 +3,7 @@ package logout import ( "bytes" "net/http" + "os" "regexp" "testing" @@ -183,6 +184,7 @@ func Test_logoutRun_nontty(t *testing.T) { cfgHosts []string wantHosts string wantErr *regexp.Regexp + ghtoken string }{ { name: "no arguments", @@ -211,10 +213,21 @@ func Test_logoutRun_nontty(t *testing.T) { }, wantErr: regexp.MustCompile(`not logged in to any hosts`), }, + { + name: "gh token is set", + opts: &LogoutOptions{}, + ghtoken: "abc123", + wantErr: regexp.MustCompile(`GITHUB_TOKEN is set in your environment`), + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { + ghtoken := os.Getenv("GITHUB_TOKEN") + defer func() { + os.Setenv("GITHUB_TOKEN", ghtoken) + }() + os.Setenv("GITHUB_TOKEN", tt.ghtoken) io, _, _, stderr := iostreams.Test() io.SetStdinTTY(false)