@@ -3,7 +3,6 @@ package flavor
33import (
44 "encoding/json"
55 "fmt"
6- "io/ioutil"
76 "os"
87 "strings"
98
@@ -37,6 +36,7 @@ func Command(plugins func() discovery.Plugins) *cobra.Command {
3736 Short : "Access flavor plugin" ,
3837 }
3938 name := cmd .PersistentFlags ().String ("name" , "" , "Name of plugin" )
39+ flavorPropertiesURL := cmd .PersistentFlags ().String ("properties" , "" , "Properties of the flavor plugin, a url" )
4040
4141 cmd .PersistentPreRunE = func (c * cobra.Command , args []string ) error {
4242 if err := cli .EnsurePersistentPreRunE (c ); err != nil {
@@ -104,92 +104,108 @@ func Command(plugins func() discovery.Plugins) *cobra.Command {
104104 return group_types.Index {Group : group .ID (groupID ), Sequence : groupSequence }
105105 }
106106
107+ ///////////////////////////////////////////////////////////////////////////////////
108+ // validate
109+ validateTemplateFlags , toJSON , _ , validateProcessTemplate := base .TemplateProcessor (plugins )
107110 validate := & cobra.Command {
108- Use : "validate <flavor configuration file> " ,
109- Short : "validate a flavor configuration" ,
111+ Use : "validate" ,
112+ Short : "Validate a flavor configuration" ,
110113 RunE : func (cmd * cobra.Command , args []string ) error {
111114
112- if len (args ) != 1 {
115+ if len (args ) != 0 {
113116 cmd .Usage ()
114117 os .Exit (1 )
115118 }
116119
117- buff , err := ioutil . ReadFile ( args [ 0 ] )
120+ view , err := validateProcessTemplate ( * flavorPropertiesURL )
118121 if err != nil {
119- log .Warn ("error" , "err" , err )
120- os .Exit (1 )
122+ return err
123+ }
124+
125+ buff , err := toJSON ([]byte (view ))
126+ if err != nil {
127+ return err
121128 }
122129
123130 return flavorPlugin .Validate (types .AnyBytes (buff ), allocationMethodFromFlags ())
124131 },
125132 }
133+ validate .Flags ().AddFlagSet (validateTemplateFlags )
126134 addAllocationMethodFlags (validate )
127- cmd .AddCommand (validate )
128135
136+ ///////////////////////////////////////////////////////////////////////////////////
137+ // prepare
138+ prepareTemplateFlags , toJSON , _ , prepareProcessTemplate := base .TemplateProcessor (plugins )
129139 prepare := & cobra.Command {
130- Use : "prepare <flavor configuration file> < instance Spec JSON file >" ,
131- Short : "prepare provisioning inputs for an instance" ,
140+ Use : "prepare <instance Spec template url >" ,
141+ Short : "Prepare provisioning inputs for an instance. Read from stdin if url is '-' " ,
132142 RunE : func (cmd * cobra.Command , args []string ) error {
133143
134- if len (args ) != 2 {
144+ if len (args ) != 1 {
135145 cmd .Usage ()
136146 os .Exit (1 )
137147 }
138148
139- flavorProperties , err := ioutil . ReadFile ( args [ 0 ] )
149+ flavorProperties , err := prepareProcessTemplate ( * flavorPropertiesURL )
140150 if err != nil {
141- log .Warn ("error" , "err" , err )
142- os .Exit (1 )
151+ return err
143152 }
144153
145- buff , err := ioutil .ReadFile (args [1 ])
154+ view , err := base .ReadFromStdinIfElse (
155+ func () bool { return args [0 ] == "-" },
156+ func () (string , error ) { return prepareProcessTemplate (args [0 ]) },
157+ toJSON ,
158+ )
146159 if err != nil {
147- log .Warn ("error" , "err" , err )
148- os .Exit (1 )
160+ return err
149161 }
150162
151163 spec := instance.Spec {}
152- if err := json . Unmarshal ( buff , & spec ); err != nil {
164+ if err := types . AnyString ( view ). Decode ( & spec ); err != nil {
153165 return err
154166 }
155167
156168 spec , err = flavorPlugin .Prepare (
157- types .AnyBytes (flavorProperties ),
169+ types .AnyString (flavorProperties ),
158170 spec ,
159171 allocationMethodFromFlags (),
160172 indexFromFlags (),
161173 )
162- if err == nil {
163- buff , err = json .MarshalIndent (spec , " " , " " )
164- if err == nil {
165- fmt .Println (string (buff ))
166- }
174+ if err != nil {
175+ return err
176+ }
177+
178+ if buff , err := json .MarshalIndent (spec , " " , " " ); err == nil {
179+ fmt .Println (string (buff ))
167180 }
168181 return err
169182 },
170183 }
184+ prepare .Flags ().AddFlagSet (prepareTemplateFlags )
171185 addAllocationMethodFlags (prepare )
172186 indexFlags (prepare )
173- cmd .AddCommand (prepare )
174187
188+ ///////////////////////////////////////////////////////////////////////////////////
189+ // healthy
190+ healthyTemplateFlags , toJSON , _ , healthyProcessTemplate := base .TemplateProcessor (plugins )
175191 healthy := & cobra.Command {
176- Use : "healthy <flavor configuration file> " ,
192+ Use : "healthy" ,
177193 Short : "checks if an instance is considered healthy" ,
178194 }
179195 tags := healthy .Flags ().StringSlice ("tags" , []string {}, "Tags to filter" )
180196 id := healthy .Flags ().String ("id" , "" , "ID of resource" )
181197 logicalID := healthy .Flags ().String ("logical-id" , "" , "Logical ID of resource" )
198+ healthy .Flags ().AddFlagSet (healthyTemplateFlags )
182199 healthy .RunE = func (cmd * cobra.Command , args []string ) error {
183200
184- if len (args ) != 1 {
201+ if len (args ) != 0 {
185202 cmd .Usage ()
186203 os .Exit (1 )
187204 }
188205
189- flavorProperties , err := ioutil . ReadFile ( args [ 0 ] )
206+ flavorProperties , err := healthyProcessTemplate ( * flavorPropertiesURL )
190207 if err != nil {
191- log .Warn ("error" , "err" , err )
192- os .Exit (1 )
208+ return err
193209 }
194210
195211 filter := map [string ]string {}
@@ -214,13 +230,18 @@ func Command(plugins func() discovery.Plugins) *cobra.Command {
214230 desc .LogicalID = & logical
215231 }
216232
217- healthy , err := flavorPlugin .Healthy (types .AnyBytes (flavorProperties ), desc )
233+ healthy , err := flavorPlugin .Healthy (types .AnyString (flavorProperties ), desc )
218234 if err == nil {
219235 fmt .Printf ("%v\n " , healthy )
220236 }
221237 return err
222238 }
223- cmd .AddCommand (healthy )
239+
240+ cmd .AddCommand (
241+ validate ,
242+ prepare ,
243+ healthy ,
244+ )
224245
225246 return cmd
226247}
0 commit comments