From 2f5ffbd60ad91820ac0c76d96aab174f9d9285b3 Mon Sep 17 00:00:00 2001 From: Nilesh Singh Date: Fri, 15 Jan 2021 13:28:57 +0530 Subject: [PATCH 1/3] Add fail message for non-existent hostname --- pkg/cmd/auth/status/status.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pkg/cmd/auth/status/status.go b/pkg/cmd/auth/status/status.go index 59603981d..96ff458fc 100644 --- a/pkg/cmd/auth/status/status.go +++ b/pkg/cmd/auth/status/status.go @@ -81,11 +81,13 @@ func statusRun(opts *StatusOptions) error { apiClient := api.NewClientFromHTTP(httpClient) var failed bool + var isHostnameFound bool for _, hostname := range hostnames { if opts.Hostname != "" && opts.Hostname != hostname { continue } + isHostnameFound = true token, tokenSource, _ := cfg.GetWithSource(hostname, "oauth_token") tokenIsWriteable := cfg.CheckWriteable(hostname, "oauth_token") == nil @@ -139,6 +141,12 @@ func statusRun(opts *StatusOptions) error { // not to since I wanted this command to be read-only. } + if !isHostnameFound { + fmt.Fprintf(stderr, + "You are not logged into any GitHub hosts. Run %s to authenticate.\n", cs.Bold(fmt.Sprintf("gh auth login -h %s", opts.Hostname))) + return cmdutil.SilentError + } + for _, hostname := range hostnames { lines, ok := statusInfo[hostname] if !ok { From f3fcaf6c9c2422b5a40636bd2d6ef1f286e7166b Mon Sep 17 00:00:00 2001 From: Nilesh Singh Date: Sun, 17 Jan 2021 14:42:49 +0530 Subject: [PATCH 2/3] Fix error message text & add test case --- pkg/cmd/auth/status/status.go | 2 +- pkg/cmd/auth/status/status_test.go | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/pkg/cmd/auth/status/status.go b/pkg/cmd/auth/status/status.go index 96ff458fc..2c4ebfd44 100644 --- a/pkg/cmd/auth/status/status.go +++ b/pkg/cmd/auth/status/status.go @@ -143,7 +143,7 @@ func statusRun(opts *StatusOptions) error { if !isHostnameFound { fmt.Fprintf(stderr, - "You are not logged into any GitHub hosts. Run %s to authenticate.\n", cs.Bold(fmt.Sprintf("gh auth login -h %s", opts.Hostname))) + "Hostname %q not found among authenticated GitHub hosts\n", opts.Hostname) return cmdutil.SilentError } diff --git a/pkg/cmd/auth/status/status_test.go b/pkg/cmd/auth/status/status_test.go index 6974770dc..f890b1672 100644 --- a/pkg/cmd/auth/status/status_test.go +++ b/pkg/cmd/auth/status/status_test.go @@ -190,6 +190,17 @@ func Test_statusRun(t *testing.T) { httpmock.StringResponse(`{"data":{"viewer":{"login":"tess"}}}`)) }, wantErrOut: regexp.MustCompile(`(?s)Token: xyz456.*Token: abc123`), + }, { + name: "missing hostname", + opts: &StatusOptions{ + Hostname: "github.example.com", + }, + cfg: func(c config.Config) { + _ = c.Set("github.com", "oauth_token", "abc123") + }, + httpStubs: func(reg *httpmock.Registry) {}, + wantErrOut: regexp.MustCompile(`(?s)Hostname "github.example.com" not found among authenticated GitHub hosts`), + wantErr: regexp.MustCompile(``), }, } @@ -241,9 +252,9 @@ func Test_statusRun(t *testing.T) { if tt.wantErr != nil { assert.True(t, tt.wantErr.MatchString(err.Error())) return - } else { - t.Fatalf("unexpected error: %s", err) } + + t.Fatalf("unexpected error: %s", err) } if tt.wantErrOut == nil { From 71fd2fa24c85fafdc70876974d3ca1852f6b143c Mon Sep 17 00:00:00 2001 From: Sam Coe Date: Fri, 22 Jan 2021 08:50:09 -0800 Subject: [PATCH 3/3] Fix up test --- pkg/cmd/auth/status/status_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/cmd/auth/status/status_test.go b/pkg/cmd/auth/status/status_test.go index e28df0816..e917f621c 100644 --- a/pkg/cmd/auth/status/status_test.go +++ b/pkg/cmd/auth/status/status_test.go @@ -200,7 +200,7 @@ func Test_statusRun(t *testing.T) { }, httpStubs: func(reg *httpmock.Registry) {}, wantErrOut: regexp.MustCompile(`(?s)Hostname "github.example.com" not found among authenticated GitHub hosts`), - wantErr: regexp.MustCompile(``), + wantErr: "SilentError", }, }