diff --git a/cli/cdsctl/config.go b/cli/cdsctl/config.go index ad0e96931a..10aefcd974 100644 --- a/cli/cdsctl/config.go +++ b/cli/cdsctl/config.go @@ -181,7 +181,13 @@ func withAutoConf() cli.CommandModifier { func(c *cli.Command, args *[]string) error { // if args length equals or over context args length means that all // context args were given so ignore discover conf - if len(*args) >= len(c.Ctx)+len(c.Args) { + var expectedArgs = len(c.Ctx) + for _, a := range c.Args { + if !a.AllowEmpty { + expectedArgs++ + } + } + if len(*args) >= expectedArgs { return nil } diff --git a/cli/cdsctl/project_variable.go b/cli/cdsctl/project_variable.go index aa96a1c45f..a8365e023e 100644 --- a/cli/cdsctl/project_variable.go +++ b/cli/cdsctl/project_variable.go @@ -1,6 +1,10 @@ package main import ( + "io/ioutil" + "os" + + "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/ovh/cds/cli" @@ -32,8 +36,17 @@ var projectVariableCreateCmd = cli.Command{ Args: []cli.Arg{ {Name: "variable-name"}, {Name: "variable-type"}, + }, + OptionalArgs: []cli.Arg{ {Name: "variable-value"}, }, + Flags: []cli.Flag{ + { + Name: "stdin", + Usage: "read the variable value from stdin", + Type: cli.FlagBool, + }, + }, } func projectCreateVariableRun(v cli.Values) error { @@ -42,6 +55,19 @@ func projectCreateVariableRun(v cli.Values) error { Type: v.GetString("variable-type"), Value: v.GetString("variable-value"), } + + if variable.Value == "" && v.GetBool("stdin") { + btes, err := ioutil.ReadAll(os.Stdin) + if err != nil { + return err + } + variable.Value = string(btes) + } + + if variable.Value == "" { + return errors.New("missing value") + } + return client.ProjectVariableCreate(v.GetString(_ProjectKey), variable) } @@ -101,8 +127,17 @@ var projectVariableUpdateCmd = cli.Command{ {Name: "variable-oldname"}, {Name: "variable-name"}, {Name: "variable-type"}, + }, + OptionalArgs: []cli.Arg{ {Name: "variable-value"}, }, + Flags: []cli.Flag{ + { + Name: "stdin", + Usage: "read the variable value from stdin", + Type: cli.FlagBool, + }, + }, } func projectUpdateVariableRun(v cli.Values) error { @@ -113,5 +148,18 @@ func projectUpdateVariableRun(v cli.Values) error { variable.Name = v.GetString("variable-name") variable.Type = v.GetString("variable-type") variable.Value = v.GetString("variable-value") + + if variable.Value == "" && v.GetBool("stdin") { + btes, err := ioutil.ReadAll(os.Stdin) + if err != nil { + return err + } + variable.Value = string(btes) + } + + if variable.Value == "" { + return errors.New("missing value") + } + return client.ProjectVariableUpdate(v.GetString(_ProjectKey), variable) }