Skip to content

Commit

Permalink
fix: allow setting variable without using --value (profclems#881)
Browse files Browse the repository at this point in the history
  • Loading branch information
bradym committed Oct 19, 2021
1 parent 0d47c68 commit c48c53b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
18 changes: 7 additions & 11 deletions commands/variable/set/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ type SetOpts struct {
Protected bool
Masked bool
Group string

ValueSet bool
}

func NewCmdSet(f *cmdutils.Factory, runE func(opts *SetOpts) error) *cobra.Command {
Expand Down Expand Up @@ -67,20 +65,16 @@ func NewCmdSet(f *cmdutils.Factory, runE func(opts *SetOpts) error) *cobra.Comma
}

if opts.Value != "" && len(args) == 2 {
if opts.Value != "" {
err = cmdutils.FlagError{Err: errors.New("specify value either by second positional argument or --value flag")}
return
}
opts.Value = args[1]
err = cmdutils.FlagError{Err: errors.New("specify value either by second positional argument or --value flag")}
return
}

if cmd.Flags().Changed("scope") && opts.Group != "" {
err = cmdutils.FlagError{Err: errors.New("scope is not required for group variables")}
return
}

opts.ValueSet = cmd.Flags().Changed("value") || len(args) == 2
opts.Value, err = getValue(opts)
opts.Value, err = getValue(opts, args)
if err != nil {
return
}
Expand Down Expand Up @@ -156,9 +150,11 @@ func setRun(opts *SetOpts) error {
return nil
}

func getValue(opts *SetOpts) (string, error) {
if opts.Value != "" || opts.ValueSet {
func getValue(opts *SetOpts, args []string) (string, error) {
if opts.Value != "" {
return opts.Value, nil
} else if len(args) == 2 {
return args[1], nil
}

if opts.IO.IsInTTY {
Expand Down
3 changes: 2 additions & 1 deletion commands/variable/set/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,11 @@ func Test_getValue(t *testing.T) {
_, err := stdin.WriteString(tt.stdin)
assert.NoError(t, err)

args := []string{tt.valueArg}
value, err := getValue(&SetOpts{
Value: tt.valueArg,
IO: io,
})
}, args)
assert.NoError(t, err)

assert.Equal(t, value, tt.want)
Expand Down

0 comments on commit c48c53b

Please sign in to comment.