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
Changes from 1 commit
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
Next Next commit
feat(cli): read variable from stdin
Signed-off-by: francois  samin <francois.samin@corp.ovh.com>
fsamin committed May 18, 2021

Verified

This commit was signed with the committer’s verified signature.
tfcollins Travis F. Collins
commit 21ad6bcb1eff1cba44b29b09748b72d37e5f2fbd
8 changes: 7 additions & 1 deletion cli/cdsctl/config.go
Original file line number Diff line number Diff line change
@@ -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
}

46 changes: 46 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"
@@ -32,8 +36,16 @@ var projectVariableCreateCmd = cli.Command{
Args: []cli.Arg{
{Name: "variable-name"},
{Name: "variable-type"},
},
OptionalArgs: []cli.Arg{
{Name: "variable-value"},
},
Flags: []cli.Flag{
{
Name: "stdin",
Type: cli.FlagBool,
},
},
}

func projectCreateVariableRun(v cli.Values) error {
@@ -42,6 +54,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 +126,16 @@ var projectVariableUpdateCmd = cli.Command{
{Name: "variable-oldname"},
{Name: "variable-name"},
{Name: "variable-type"},
},
OptionalArgs: []cli.Arg{
{Name: "variable-value"},
},
Flags: []cli.Flag{
{
Name: "stdin",
Type: cli.FlagBool,
},
},
}

func projectUpdateVariableRun(v cli.Values) error {
@@ -113,5 +146,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)
}