From 3ccc6889bf2604e3b21d9afb4868602a14adddb8 Mon Sep 17 00:00:00 2001 From: Oleksandr Redko Date: Thu, 12 Oct 2023 19:42:16 +0300 Subject: [PATCH] commands: fix RunServerlessStatus displaying credentials (#1439) Co-authored-by: Andrew Starr-Bochicchio --- commands/serverless.go | 10 +++++----- commands/serverless_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/commands/serverless.go b/commands/serverless.go index acd15682a..1290f015b 100644 --- a/commands/serverless.go +++ b/commands/serverless.go @@ -392,17 +392,17 @@ func RunServerlessStatus(c *CmdConfig) error { credentials, _ := c.Doit.GetBool(c.NS, "credentials") if credentials { type showCreds struct { - Auth string `json:auth` - APIHost string `json:apihost` - Namespace string `json:namespace` - Path string `json:path` + Auth string `json:"auth"` + APIHost string `json:"apihost"` + Namespace string `json:"namespace"` + Path string `json:"path"` } toShow := showCreds{Auth: auth, APIHost: creds.APIHost, Namespace: creds.Namespace, Path: sls.CredentialsPath()} credsOutput, err := json.MarshalIndent(toShow, "", " ") if err != nil { return err } - fmt.Println(string(credsOutput)) + fmt.Fprintln(c.Out, string(credsOutput)) return nil } diff --git a/commands/serverless_test.go b/commands/serverless_test.go index ee0125b17..168841248 100644 --- a/commands/serverless_test.go +++ b/commands/serverless_test.go @@ -147,6 +147,33 @@ func TestServerlessStatusWhenConnected(t *testing.T) { }) } +func TestServerlessStatusDisplayCredentials(t *testing.T) { + withTestClient(t, func(config *CmdConfig, tm *tcMocks) { + buf := &bytes.Buffer{} + config.Out = buf + config.Doit.Set(config.NS, "credentials", true) + + tm.serverless.EXPECT().CheckServerlessStatus().MinTimes(1).Return(nil) + tm.serverless.EXPECT().ReadCredentials().Return(do.ServerlessCredentials{ + APIHost: "https://api.example.com", + Namespace: "hello", + Credentials: map[string]map[string]do.ServerlessCredential{ + "https://api.example.com": { + "hello": do.ServerlessCredential{ + Auth: "here-are-some-credentials", + }, + }, + }, + }, nil) + tm.serverless.EXPECT().GetNamespaceFromCluster("https://api.example.com", "here-are-some-credentials").Return("hello", nil) + tm.serverless.EXPECT().CredentialsPath().Return("/path") + + err := RunServerlessStatus(config) + require.NoError(t, err) + assert.JSONEq(t, `{"apihost":"https://api.example.com","auth":"here-are-some-credentials","namespace":"hello","path":"/path"}`, buf.String()) + }) +} + func TestServerlessStatusWithLanguages(t *testing.T) { withTestClient(t, func(config *CmdConfig, tm *tcMocks) { buf := &bytes.Buffer{}