Skip to content

Commit 352cde0

Browse files
committed
do not process filename arguments
1 parent a5a043c commit 352cde0

File tree

2 files changed

+25
-57
lines changed

2 files changed

+25
-57
lines changed

pkg/cmd/secret/set/set.go

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,11 @@ func NewCmdSet(f *cmdutil.Factory, runF func(*SetOptions) error) *cobra.Command
4545
Short: "Create or update secrets",
4646
Long: "Locally encrypt a new or updated secret at either the repository or organization level and send it to GitHub for storage.",
4747
Example: heredoc.Doc(`
48-
$ cat SECRET.txt | gh secret set NEW_SECRET
49-
$ gh secret set NEW_SECRET -b"some literal value"
50-
$ gh secret set NEW_SECRET -b"@file.json"
51-
$ gh secret set ORG_SECRET --org
52-
$ gh secret set ORG_SECRET --org=anotherOrg --visibility=selected -r="repo1,repo2,repo3"
53-
$ gh secret set ORG_SECRET --org=anotherOrg --visibility="all"
48+
$ gh secret set FROM_FLAG -b"some literal value"
49+
$ gh secret set FROM_ENV -b"${ENV_VALUE}"
50+
$ gh secret set FROM_FILE < file.json
51+
$ gh secret set ORG_SECRET -bval --org=anOrg --visibility=all
52+
$ gh secret set ORG_SECRET -bval --org=anOrg --repos="repo1,repo2,repo3"
5453
`),
5554
Args: func(cmd *cobra.Command, args []string) error {
5655
if len(args) != 1 {
@@ -108,7 +107,7 @@ func NewCmdSet(f *cmdutil.Factory, runF func(*SetOptions) error) *cobra.Command
108107
cmd.Flags().StringVarP(&opts.OrgName, "org", "o", "", "List secrets for an organization")
109108
cmd.Flags().StringVarP(&opts.Visibility, "visibility", "v", "private", "Set visibility for an organization secret: `all`, `private`, or `selected`")
110109
cmd.Flags().StringSliceVarP(&opts.RepositoryNames, "repos", "r", []string{}, "List of repository names for `selected` visibility")
111-
cmd.Flags().StringVarP(&opts.Body, "body", "b", "-", "Provide either a literal string or a file path; prepend file paths with an @. Reads from STDIN if not provided.")
110+
cmd.Flags().StringVarP(&opts.Body, "body", "b", "", "A value for the secret. Reads from STDIN if not specified.")
112111

113112
return cmd
114113
}
@@ -196,23 +195,14 @@ func validSecretName(name string) error {
196195
return nil
197196
}
198197

199-
func getBody(opts *SetOptions) (body []byte, err error) {
200-
if opts.Body == "-" {
201-
body, err = ioutil.ReadAll(opts.IO.In)
198+
func getBody(opts *SetOptions) ([]byte, error) {
199+
if opts.Body == "" {
200+
body, err := ioutil.ReadAll(opts.IO.In)
202201
if err != nil {
203202
return nil, fmt.Errorf("failed to read from STDIN: %w", err)
204203
}
205204

206-
return
207-
}
208-
209-
if strings.HasPrefix(opts.Body, "@") {
210-
body, err = opts.IO.ReadUserFile(opts.Body[1:])
211-
if err != nil {
212-
return nil, fmt.Errorf("failed to read file %s: %w", opts.Body[1:], err)
213-
}
214-
215-
return
205+
return body, nil
216206
}
217207

218208
return []byte(opts.Body), nil

pkg/cmd/secret/set/set_test.go

Lines changed: 15 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"fmt"
77
"io/ioutil"
88
"net/http"
9-
"os"
109
"testing"
1110

1211
"github.com/cli/cli/internal/ghrepo"
@@ -64,34 +63,34 @@ func TestNewCmdSet(t *testing.T) {
6463
},
6564
{
6665
name: "repos without vis",
67-
cli: "cool_secret --org coolOrg -rcoolRepo",
66+
cli: "cool_secret -bs --org coolOrg -rcoolRepo",
6867
wants: SetOptions{
6968
SecretName: "cool_secret",
7069
Visibility: shared.Selected,
7170
RepositoryNames: []string{"coolRepo"},
72-
Body: "-",
71+
Body: "s",
7372
OrgName: "coolOrg",
7473
},
7574
},
7675
{
7776
name: "org with selected repo",
78-
cli: "-ocoolOrg -vselected -rcoolRepo cool_secret",
77+
cli: "-ocoolOrg -bs -vselected -rcoolRepo cool_secret",
7978
wants: SetOptions{
8079
SecretName: "cool_secret",
8180
Visibility: shared.Selected,
8281
RepositoryNames: []string{"coolRepo"},
83-
Body: "-",
82+
Body: "s",
8483
OrgName: "coolOrg",
8584
},
8685
},
8786
{
8887
name: "org with selected repos",
89-
cli: `--org=coolOrg -vselected -r="coolRepo,radRepo,goodRepo" cool_secret`,
88+
cli: `--org=coolOrg -bs -vselected -r="coolRepo,radRepo,goodRepo" cool_secret`,
9089
wants: SetOptions{
9190
SecretName: "cool_secret",
9291
Visibility: shared.Selected,
9392
RepositoryNames: []string{"coolRepo", "goodRepo", "radRepo"},
94-
Body: "-",
93+
Body: "s",
9594
OrgName: "coolOrg",
9695
},
9796
},
@@ -107,11 +106,11 @@ func TestNewCmdSet(t *testing.T) {
107106
},
108107
{
109108
name: "vis all",
110-
cli: `cool_secret --org coolOrg -b"@cool.json" -vall`,
109+
cli: `cool_secret --org coolOrg -b"cool" -vall`,
111110
wants: SetOptions{
112111
SecretName: "cool_secret",
113112
Visibility: shared.All,
114-
Body: "@cool.json",
113+
Body: "cool",
115114
OrgName: "coolOrg",
116115
},
117116
},
@@ -286,27 +285,20 @@ func Test_setRun_org(t *testing.T) {
286285

287286
func Test_getBody(t *testing.T) {
288287
tests := []struct {
289-
name string
290-
bodyArg string
291-
want string
292-
stdin string
293-
fromFile bool
288+
name string
289+
bodyArg string
290+
want string
291+
stdin string
294292
}{
295293
{
296294
name: "literal value",
297295
bodyArg: "a secret",
298296
want: "a secret",
299297
},
300298
{
301-
name: "from stdin",
302-
bodyArg: "-",
303-
want: "a secret",
304-
stdin: "a secret",
305-
},
306-
{
307-
name: "from file",
308-
fromFile: true,
309-
want: "a secret from a file",
299+
name: "from stdin",
300+
want: "a secret",
301+
stdin: "a secret",
310302
},
311303
}
312304

@@ -319,27 +311,13 @@ func Test_getBody(t *testing.T) {
319311
_, err := stdin.WriteString(tt.stdin)
320312
assert.NoError(t, err)
321313

322-
if tt.fromFile {
323-
dir := os.TempDir()
324-
tmpfile, err := ioutil.TempFile(dir, "testfile*")
325-
assert.NoError(t, err)
326-
_, err = tmpfile.WriteString(tt.want)
327-
assert.NoError(t, err)
328-
tt.bodyArg = fmt.Sprintf("@%s", tmpfile.Name())
329-
}
330-
331314
body, err := getBody(&SetOptions{
332315
Body: tt.bodyArg,
333316
IO: io,
334317
})
335318
assert.NoError(t, err)
336319

337320
assert.Equal(t, string(body), tt.want)
338-
339321
})
340-
341322
}
342-
343323
}
344-
345-
// TODO test updating org secret's repo lists

0 commit comments

Comments
 (0)