Skip to content

Commit

Permalink
feat(cli): read variable from stdin (#5812)
Browse files Browse the repository at this point in the history
  • Loading branch information
fsamin authored May 18, 2021
1 parent 8303faa commit be677c9
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
8 changes: 7 additions & 1 deletion cli/cdsctl/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
48 changes: 48 additions & 0 deletions cli/cdsctl/project_variable.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package main

import (
"io/ioutil"
"os"

"github.com/pkg/errors"
"github.com/spf13/cobra"

"github.com/ovh/cds/cli"
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}

0 comments on commit be677c9

Please sign in to comment.