do not process filename arguments

This commit is contained in:
vilmibm 2020-12-14 10:44:26 -08:00
parent a5a043c5a5
commit 352cde0563
2 changed files with 25 additions and 57 deletions

View file

@ -45,12 +45,11 @@ func NewCmdSet(f *cmdutil.Factory, runF func(*SetOptions) error) *cobra.Command
Short: "Create or update secrets",
Long: "Locally encrypt a new or updated secret at either the repository or organization level and send it to GitHub for storage.",
Example: heredoc.Doc(`
$ cat SECRET.txt | gh secret set NEW_SECRET
$ gh secret set NEW_SECRET -b"some literal value"
$ gh secret set NEW_SECRET -b"@file.json"
$ gh secret set ORG_SECRET --org
$ gh secret set ORG_SECRET --org=anotherOrg --visibility=selected -r="repo1,repo2,repo3"
$ gh secret set ORG_SECRET --org=anotherOrg --visibility="all"
$ gh secret set FROM_FLAG -b"some literal value"
$ gh secret set FROM_ENV -b"${ENV_VALUE}"
$ gh secret set FROM_FILE < file.json
$ gh secret set ORG_SECRET -bval --org=anOrg --visibility=all
$ gh secret set ORG_SECRET -bval --org=anOrg --repos="repo1,repo2,repo3"
`),
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
@ -108,7 +107,7 @@ func NewCmdSet(f *cmdutil.Factory, runF func(*SetOptions) error) *cobra.Command
cmd.Flags().StringVarP(&opts.OrgName, "org", "o", "", "List secrets for an organization")
cmd.Flags().StringVarP(&opts.Visibility, "visibility", "v", "private", "Set visibility for an organization secret: `all`, `private`, or `selected`")
cmd.Flags().StringSliceVarP(&opts.RepositoryNames, "repos", "r", []string{}, "List of repository names for `selected` visibility")
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.")
cmd.Flags().StringVarP(&opts.Body, "body", "b", "", "A value for the secret. Reads from STDIN if not specified.")
return cmd
}
@ -196,23 +195,14 @@ func validSecretName(name string) error {
return nil
}
func getBody(opts *SetOptions) (body []byte, err error) {
if opts.Body == "-" {
body, err = ioutil.ReadAll(opts.IO.In)
func getBody(opts *SetOptions) ([]byte, error) {
if opts.Body == "" {
body, err := ioutil.ReadAll(opts.IO.In)
if err != nil {
return nil, fmt.Errorf("failed to read from STDIN: %w", err)
}
return
}
if strings.HasPrefix(opts.Body, "@") {
body, err = opts.IO.ReadUserFile(opts.Body[1:])
if err != nil {
return nil, fmt.Errorf("failed to read file %s: %w", opts.Body[1:], err)
}
return
return body, nil
}
return []byte(opts.Body), nil

View file

@ -6,7 +6,6 @@ import (
"fmt"
"io/ioutil"
"net/http"
"os"
"testing"
"github.com/cli/cli/internal/ghrepo"
@ -64,34 +63,34 @@ func TestNewCmdSet(t *testing.T) {
},
{
name: "repos without vis",
cli: "cool_secret --org coolOrg -rcoolRepo",
cli: "cool_secret -bs --org coolOrg -rcoolRepo",
wants: SetOptions{
SecretName: "cool_secret",
Visibility: shared.Selected,
RepositoryNames: []string{"coolRepo"},
Body: "-",
Body: "s",
OrgName: "coolOrg",
},
},
{
name: "org with selected repo",
cli: "-ocoolOrg -vselected -rcoolRepo cool_secret",
cli: "-ocoolOrg -bs -vselected -rcoolRepo cool_secret",
wants: SetOptions{
SecretName: "cool_secret",
Visibility: shared.Selected,
RepositoryNames: []string{"coolRepo"},
Body: "-",
Body: "s",
OrgName: "coolOrg",
},
},
{
name: "org with selected repos",
cli: `--org=coolOrg -vselected -r="coolRepo,radRepo,goodRepo" cool_secret`,
cli: `--org=coolOrg -bs -vselected -r="coolRepo,radRepo,goodRepo" cool_secret`,
wants: SetOptions{
SecretName: "cool_secret",
Visibility: shared.Selected,
RepositoryNames: []string{"coolRepo", "goodRepo", "radRepo"},
Body: "-",
Body: "s",
OrgName: "coolOrg",
},
},
@ -107,11 +106,11 @@ func TestNewCmdSet(t *testing.T) {
},
{
name: "vis all",
cli: `cool_secret --org coolOrg -b"@cool.json" -vall`,
cli: `cool_secret --org coolOrg -b"cool" -vall`,
wants: SetOptions{
SecretName: "cool_secret",
Visibility: shared.All,
Body: "@cool.json",
Body: "cool",
OrgName: "coolOrg",
},
},
@ -286,11 +285,10 @@ func Test_setRun_org(t *testing.T) {
func Test_getBody(t *testing.T) {
tests := []struct {
name string
bodyArg string
want string
stdin string
fromFile bool
name string
bodyArg string
want string
stdin string
}{
{
name: "literal value",
@ -298,15 +296,9 @@ func Test_getBody(t *testing.T) {
want: "a secret",
},
{
name: "from stdin",
bodyArg: "-",
want: "a secret",
stdin: "a secret",
},
{
name: "from file",
fromFile: true,
want: "a secret from a file",
name: "from stdin",
want: "a secret",
stdin: "a secret",
},
}
@ -319,15 +311,6 @@ func Test_getBody(t *testing.T) {
_, err := stdin.WriteString(tt.stdin)
assert.NoError(t, err)
if tt.fromFile {
dir := os.TempDir()
tmpfile, err := ioutil.TempFile(dir, "testfile*")
assert.NoError(t, err)
_, err = tmpfile.WriteString(tt.want)
assert.NoError(t, err)
tt.bodyArg = fmt.Sprintf("@%s", tmpfile.Name())
}
body, err := getBody(&SetOptions{
Body: tt.bodyArg,
IO: io,
@ -335,11 +318,6 @@ func Test_getBody(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, string(body), tt.want)
})
}
}
// TODO test updating org secret's repo lists