Skip to content

Commit 0937f87

Browse files
committed
Merge branch 'release/1.0.3'
2 parents 6636b21 + baf78c1 commit 0937f87

File tree

3 files changed

+152
-33
lines changed

3 files changed

+152
-33
lines changed

samples/metadata.yml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
version: 1
2+
metadata:
3+
some_key: $HOME/ff97e4c6
4+
steps:
5+
- name: meta
6+
metadata:
7+
step_key: $HOME/hello
8+
command: "{{ index .MergedMetadata \"step_key\" }}/middle/{{ index .MergedMetadata \"some_key\"}}"

utils/log_utils.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package utils
22

33
import (
44
"bytes"
5+
"context"
56
"fmt"
67
"html/template"
78
"io/ioutil"
@@ -62,7 +63,7 @@ func NewLoggingContext(workflow *Workflow, step *Step) *LoggingContext {
6263
}
6364

6465
// Parse parses the given value within this context.
65-
func (l *LoggingContext) Parse(value string) (string, error) {
66+
func (l *LoggingContext) parse(value string) (string, error) {
6667
temp, err := template.New("filename").Parse(value)
6768
if err != nil {
6869
return "", err
@@ -107,6 +108,7 @@ func DefaultLogDefinition(baseDefinition *LogDefinition) *LogDefinition {
107108

108109
// NewLogger creates a new logger instance and sets the right log level based
109110
func NewLogger(baseDefinition *LogDefinition, loggingContext *LoggingContext) (*logrus.Logger, error) {
111+
ctx := context.Background()
110112
definition := DefaultLogDefinition(baseDefinition)
111113

112114
logger := logrus.New()
@@ -118,10 +120,15 @@ func NewLogger(baseDefinition *LogDefinition, loggingContext *LoggingContext) (*
118120
} else if definition.Type == "discard" {
119121
logger.SetOutput(ioutil.Discard)
120122
} else if definition.Type == "file" {
121-
filename, err := loggingContext.Parse(definition.Destination)
122-
if err != nil {
123+
var filename string
124+
var err error
125+
if filename, err = loggingContext.parse(definition.Destination); err != nil {
126+
return nil, err
127+
}
128+
if filename, err = expandEnvVars(ctx, filename); err != nil {
123129
return nil, err
124130
}
131+
125132
file, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0755)
126133
if err != nil {
127134
return nil, err

utils/step.go

+134-30
Original file line numberDiff line numberDiff line change
@@ -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-
126100
func (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

Comments
 (0)