11package main
22
33import (
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
3952func 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
108143func 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