@@ -16,9 +16,12 @@ limitations under the License.
1616package cmd
1717
1818import (
19+ "bytes"
1920 "fmt"
21+ "html/template"
2022 "io/ioutil"
2123 "os"
24+ "strings"
2225
2326 "github.com/spf13/cobra"
2427 "gopkg.in/yaml.v2"
@@ -58,9 +61,13 @@ cloud:
5861` ,
5962 Run : func (cmd * cobra.Command , args []string ) {
6063 planFile , _ := cmd .Flags ().GetString ("file" )
61- fmt . Println ( "Here we go!" , planFile )
64+ varFlag , err := cmd . Flags (). GetStringSlice ( "var" )
6265
63- _ , err := os .Stat (planFile )
66+ if err != nil {
67+ panic (err )
68+ }
69+
70+ _ , err = os .Stat (planFile )
6471 if err != nil {
6572 if os .IsNotExist (err ) {
6673 fmt .Printf ("Plan file \" %s\" does not exist.\n " , planFile )
@@ -75,6 +82,25 @@ cloud:
7582 panic (err )
7683 }
7784
85+ if len (varFlag ) > 0 {
86+ vars := make (map [string ]string )
87+ for _ , v := range varFlag {
88+ s := strings .Split (v , "=" )
89+ vars [s [0 ]] = s [1 ]
90+ }
91+
92+ var tmplBytes bytes.Buffer
93+ tmpl , err := template .New ("plan.yaml" ).Parse (string (planYaml ))
94+ if err != nil {
95+ panic (err )
96+ }
97+ err = tmpl .Execute (& tmplBytes , vars )
98+ if err != nil {
99+ panic (err )
100+ }
101+ planYaml = tmplBytes .Bytes ()
102+ }
103+
78104 var plan instance.Plan
79105 err = yaml .Unmarshal (planYaml , & plan )
80106 if err != nil {
@@ -91,5 +117,6 @@ func init() {
91117 rootCmd .AddCommand (planCmd )
92118
93119 planCmd .Flags ().String ("file" , "" , "YAML file used to define a plan" )
120+ planCmd .Flags ().StringSlice ("var" , nil , "define variable name" )
94121 planCmd .MarkFlagRequired ("file" )
95122}
0 commit comments