From 3be4b9951da547ad47752ee279cb5b21040a1584 Mon Sep 17 00:00:00 2001 From: Sam Coe Date: Sat, 29 Jan 2022 09:32:14 +0200 Subject: [PATCH] Trim leading and trailing whitespace when setting secrets from stdin (#5086) * Trim leading and trailing whitespace when setting secrets from stdin * Only trim newline at end of string --- pkg/cmd/secret/set/set.go | 3 ++- pkg/cmd/secret/set/set_test.go | 7 ++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/pkg/cmd/secret/set/set.go b/pkg/cmd/secret/set/set.go index 56d13b210..33874d0a7 100644 --- a/pkg/cmd/secret/set/set.go +++ b/pkg/cmd/secret/set/set.go @@ -1,6 +1,7 @@ package set import ( + "bytes" "encoding/base64" "fmt" "io" @@ -375,7 +376,7 @@ func getBody(opts *SetOptions) ([]byte, error) { return nil, fmt.Errorf("failed to read from standard input: %w", err) } - return body, nil + return bytes.TrimRight(body, "\r\n"), nil } func mapRepoNamesToIDs(client *api.Client, host, defaultOwner string, repositoryNames []string) ([]int64, error) { diff --git a/pkg/cmd/secret/set/set_test.go b/pkg/cmd/secret/set/set_test.go index 29a2c4758..7843a8b3b 100644 --- a/pkg/cmd/secret/set/set_test.go +++ b/pkg/cmd/secret/set/set_test.go @@ -459,6 +459,11 @@ func Test_getBody(t *testing.T) { want: "a secret", stdin: "a secret", }, + { + name: "from stdin with trailing newline character", + want: "a secret", + stdin: "a secret\n", + }, } for _, tt := range tests { @@ -476,7 +481,7 @@ func Test_getBody(t *testing.T) { }) assert.NoError(t, err) - assert.Equal(t, string(body), tt.want) + assert.Equal(t, tt.want, string(body)) }) } }