Skip to content

Commit be677c9

Browse files
authored
feat(cli): read variable from stdin (#5812)
1 parent 8303faa commit be677c9

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

cli/cdsctl/config.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,13 @@ func withAutoConf() cli.CommandModifier {
181181
func(c *cli.Command, args *[]string) error {
182182
// if args length equals or over context args length means that all
183183
// context args were given so ignore discover conf
184-
if len(*args) >= len(c.Ctx)+len(c.Args) {
184+
var expectedArgs = len(c.Ctx)
185+
for _, a := range c.Args {
186+
if !a.AllowEmpty {
187+
expectedArgs++
188+
}
189+
}
190+
if len(*args) >= expectedArgs {
185191
return nil
186192
}
187193

cli/cdsctl/project_variable.go

+48
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package main
22

33
import (
4+
"io/ioutil"
5+
"os"
6+
7+
"github.com/pkg/errors"
48
"github.com/spf13/cobra"
59

610
"github.com/ovh/cds/cli"
@@ -32,8 +36,17 @@ var projectVariableCreateCmd = cli.Command{
3236
Args: []cli.Arg{
3337
{Name: "variable-name"},
3438
{Name: "variable-type"},
39+
},
40+
OptionalArgs: []cli.Arg{
3541
{Name: "variable-value"},
3642
},
43+
Flags: []cli.Flag{
44+
{
45+
Name: "stdin",
46+
Usage: "read the variable value from stdin",
47+
Type: cli.FlagBool,
48+
},
49+
},
3750
}
3851

3952
func projectCreateVariableRun(v cli.Values) error {
@@ -42,6 +55,19 @@ func projectCreateVariableRun(v cli.Values) error {
4255
Type: v.GetString("variable-type"),
4356
Value: v.GetString("variable-value"),
4457
}
58+
59+
if variable.Value == "" && v.GetBool("stdin") {
60+
btes, err := ioutil.ReadAll(os.Stdin)
61+
if err != nil {
62+
return err
63+
}
64+
variable.Value = string(btes)
65+
}
66+
67+
if variable.Value == "" {
68+
return errors.New("missing value")
69+
}
70+
4571
return client.ProjectVariableCreate(v.GetString(_ProjectKey), variable)
4672
}
4773

@@ -101,8 +127,17 @@ var projectVariableUpdateCmd = cli.Command{
101127
{Name: "variable-oldname"},
102128
{Name: "variable-name"},
103129
{Name: "variable-type"},
130+
},
131+
OptionalArgs: []cli.Arg{
104132
{Name: "variable-value"},
105133
},
134+
Flags: []cli.Flag{
135+
{
136+
Name: "stdin",
137+
Usage: "read the variable value from stdin",
138+
Type: cli.FlagBool,
139+
},
140+
},
106141
}
107142

108143
func projectUpdateVariableRun(v cli.Values) error {
@@ -113,5 +148,18 @@ func projectUpdateVariableRun(v cli.Values) error {
113148
variable.Name = v.GetString("variable-name")
114149
variable.Type = v.GetString("variable-type")
115150
variable.Value = v.GetString("variable-value")
151+
152+
if variable.Value == "" && v.GetBool("stdin") {
153+
btes, err := ioutil.ReadAll(os.Stdin)
154+
if err != nil {
155+
return err
156+
}
157+
variable.Value = string(btes)
158+
}
159+
160+
if variable.Value == "" {
161+
return errors.New("missing value")
162+
}
163+
116164
return client.ProjectVariableUpdate(v.GetString(_ProjectKey), variable)
117165
}

0 commit comments

Comments
 (0)