Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): read variable from stdin #5812

Merged
merged 2 commits into from
May 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}