@@ -97,32 +97,6 @@ func (s *Step) shouldRun() bool {
9797 return true
9898}
9999
100- func (s * Step ) parseCommand (ctx context.Context ) error {
101- buf := & bytes.Buffer {}
102- tmpl , err := template .New ("t1" ).Parse (s .Command )
103- if err != nil {
104- return err
105- }
106-
107- err = tmpl .Execute (buf , s )
108- if err != nil {
109- return err
110- }
111-
112- s .Command = buf .String ()
113-
114- return nil
115- }
116-
117- func (s * Step ) expandEnvVars (ctx context.Context ) {
118- expandedCommand := os .ExpandEnv (s .Command )
119- s .Command = expandedCommand
120-
121- if s .Workdir != "" {
122- s .Workdir = os .ExpandEnv (s .Workdir )
123- }
124- }
125-
126100func (s * Step ) isDone () bool {
127101 return s .status == stepDone
128102}
@@ -153,13 +127,10 @@ func (s *Step) Run(ctx context.Context) error {
153127 return nil
154128 }
155129
156- err := s .parseCommand (ctx )
130+ err := s .EnrichStep (ctx )
157131 if err != nil {
158- // a failure here is down to workflow errors so
159- // continue on failure doesn't apply
160132 return err
161133 }
162- s .expandEnvVars (ctx )
163134
164135 spinner , err := NewSpinnerForStep (ctx , * s )
165136 if err != nil {
@@ -200,3 +171,136 @@ func (s *Step) Run(ctx context.Context) error {
200171
201172 return nil
202173}
174+
175+ // EnrichStep resolves environment variables and parses the command for the step
176+ // on all applicable attributes
177+ func (s * Step ) EnrichStep (ctx context.Context ) error {
178+ var err error
179+ // parse for meta data
180+ if s .Command , err = s .parseAttribute (ctx , s .Command ); err != nil {
181+ return err
182+ }
183+ if s .Name , err = s .parseAttribute (ctx , s .Name ); err != nil {
184+ return err
185+ }
186+ if s .Workdir , err = s .parseAttribute (ctx , s .Workdir ); err != nil {
187+ return err
188+ }
189+ if s .Probe != nil {
190+ if s .Probe .Command , err = s .parseAttribute (ctx , s .Probe .Command ); err != nil {
191+ return err
192+ }
193+ if s .Probe .Workdir , err = s .parseAttribute (ctx , s .Probe .Workdir ); err != nil {
194+ return err
195+ }
196+ }
197+ if s .Logger != nil {
198+ if s .Logger .Destination , err = s .parseAttribute (ctx , s .Logger .Destination ); err != nil {
199+ return err
200+ }
201+ if s .Logger .Format , err = s .parseAttribute (ctx , s .Logger .Format ); err != nil {
202+ return err
203+ }
204+ if s .Logger .Level , err = s .parseAttribute (ctx , s .Logger .Level ); err != nil {
205+ return err
206+ }
207+ if s .Logger .Type , err = s .parseAttribute (ctx , s .Logger .Type ); err != nil {
208+ return err
209+ }
210+ }
211+ if s .Preflights != nil {
212+ for idx , preFlight := range s .Preflights {
213+ if s .Preflights [idx ].Command , err = s .parseAttribute (ctx , preFlight .Command ); err != nil {
214+ return err
215+ }
216+ if s .Preflights [idx ].Workdir , err = s .parseAttribute (ctx , preFlight .Workdir ); err != nil {
217+ return err
218+ }
219+ if s .Preflights [idx ].Message , err = s .parseAttribute (ctx , preFlight .Message ); err != nil {
220+ return err
221+ }
222+ }
223+ }
224+
225+ // expand env var
226+ if s .Command , err = expandEnvVars (ctx , s .Command ); err != nil {
227+ return err
228+ }
229+ if s .Workdir , err = expandEnvVars (ctx , s .Workdir ); err != nil {
230+ return err
231+ }
232+ if s .Command , err = expandEnvVars (ctx , s .Command ); err != nil {
233+ return err
234+ }
235+ if s .Name , err = expandEnvVars (ctx , s .Name ); err != nil {
236+ return err
237+ }
238+ if s .Workdir , err = expandEnvVars (ctx , s .Workdir ); err != nil {
239+ return err
240+ }
241+ if s .Probe != nil {
242+ if s .Probe .Command , err = expandEnvVars (ctx , s .Probe .Command ); err != nil {
243+ return err
244+ }
245+ if s .Probe .Workdir , err = expandEnvVars (ctx , s .Probe .Workdir ); err != nil {
246+ return err
247+ }
248+ }
249+ if s .Logger != nil {
250+ if s .Logger .Destination , err = expandEnvVars (ctx , s .Logger .Destination ); err != nil {
251+ return err
252+ }
253+ if s .Logger .Format , err = expandEnvVars (ctx , s .Logger .Format ); err != nil {
254+ return err
255+ }
256+ if s .Logger .Level , err = expandEnvVars (ctx , s .Logger .Level ); err != nil {
257+ return err
258+ }
259+ if s .Logger .Type , err = expandEnvVars (ctx , s .Logger .Type ); err != nil {
260+ return err
261+ }
262+ }
263+ if s .Preflights != nil {
264+ for idx , preFlight := range s .Preflights {
265+ if s .Preflights [idx ].Command , err = expandEnvVars (ctx , preFlight .Command ); err != nil {
266+ return err
267+ }
268+ if s .Preflights [idx ].Workdir , err = expandEnvVars (ctx , preFlight .Workdir ); err != nil {
269+ return err
270+ }
271+ if s .Preflights [idx ].Message , err = expandEnvVars (ctx , preFlight .Message ); err != nil {
272+ return err
273+ }
274+ }
275+ }
276+
277+ return nil
278+ }
279+
280+ func (s * Step ) parseAttribute (ctx context.Context , value string ) (string , error ) {
281+ if value == "" {
282+ return "" , nil
283+ }
284+
285+ buf := & bytes.Buffer {}
286+ tmpl , err := template .New ("step" ).Parse (value )
287+ if err != nil {
288+ return "" , err
289+ }
290+
291+ err = tmpl .Execute (buf , s )
292+ if err != nil {
293+ return "" , err
294+ }
295+
296+ return buf .String (), nil
297+ }
298+
299+ func expandEnvVars (ctx context.Context , value string ) (string , error ) {
300+ if value == "" {
301+ return "" , nil
302+ }
303+
304+ expandedCommand := os .ExpandEnv (value )
305+ return expandedCommand , nil
306+ }
0 commit comments