Skip to content

Commit

Permalink
Merge branch 'release/1.0.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
khash committed Feb 10, 2020
2 parents 6636b21 + baf78c1 commit 0937f87
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 33 deletions.
8 changes: 8 additions & 0 deletions samples/metadata.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: 1
metadata:
some_key: $HOME/ff97e4c6
steps:
- name: meta
metadata:
step_key: $HOME/hello
command: "{{ index .MergedMetadata \"step_key\" }}/middle/{{ index .MergedMetadata \"some_key\"}}"
13 changes: 10 additions & 3 deletions utils/log_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package utils

import (
"bytes"
"context"
"fmt"
"html/template"
"io/ioutil"
Expand Down Expand Up @@ -62,7 +63,7 @@ func NewLoggingContext(workflow *Workflow, step *Step) *LoggingContext {
}

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

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

logger := logrus.New()
Expand All @@ -118,10 +120,15 @@ func NewLogger(baseDefinition *LogDefinition, loggingContext *LoggingContext) (*
} else if definition.Type == "discard" {
logger.SetOutput(ioutil.Discard)
} else if definition.Type == "file" {
filename, err := loggingContext.Parse(definition.Destination)
if err != nil {
var filename string
var err error
if filename, err = loggingContext.parse(definition.Destination); err != nil {
return nil, err
}
if filename, err = expandEnvVars(ctx, filename); err != nil {
return nil, err
}

file, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0755)
if err != nil {
return nil, err
Expand Down
164 changes: 134 additions & 30 deletions utils/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,32 +97,6 @@ func (s *Step) shouldRun() bool {
return true
}

func (s *Step) parseCommand(ctx context.Context) error {
buf := &bytes.Buffer{}
tmpl, err := template.New("t1").Parse(s.Command)
if err != nil {
return err
}

err = tmpl.Execute(buf, s)
if err != nil {
return err
}

s.Command = buf.String()

return nil
}

func (s *Step) expandEnvVars(ctx context.Context) {
expandedCommand := os.ExpandEnv(s.Command)
s.Command = expandedCommand

if s.Workdir != "" {
s.Workdir = os.ExpandEnv(s.Workdir)
}
}

func (s *Step) isDone() bool {
return s.status == stepDone
}
Expand Down Expand Up @@ -153,13 +127,10 @@ func (s *Step) Run(ctx context.Context) error {
return nil
}

err := s.parseCommand(ctx)
err := s.EnrichStep(ctx)
if err != nil {
// a failure here is down to workflow errors so
// continue on failure doesn't apply
return err
}
s.expandEnvVars(ctx)

spinner, err := NewSpinnerForStep(ctx, *s)
if err != nil {
Expand Down Expand Up @@ -200,3 +171,136 @@ func (s *Step) Run(ctx context.Context) error {

return nil
}

// EnrichStep resolves environment variables and parses the command for the step
// on all applicable attributes
func (s *Step) EnrichStep(ctx context.Context) error {
var err error
// parse for meta data
if s.Command, err = s.parseAttribute(ctx, s.Command); err != nil {
return err
}
if s.Name, err = s.parseAttribute(ctx, s.Name); err != nil {
return err
}
if s.Workdir, err = s.parseAttribute(ctx, s.Workdir); err != nil {
return err
}
if s.Probe != nil {
if s.Probe.Command, err = s.parseAttribute(ctx, s.Probe.Command); err != nil {
return err
}
if s.Probe.Workdir, err = s.parseAttribute(ctx, s.Probe.Workdir); err != nil {
return err
}
}
if s.Logger != nil {
if s.Logger.Destination, err = s.parseAttribute(ctx, s.Logger.Destination); err != nil {
return err
}
if s.Logger.Format, err = s.parseAttribute(ctx, s.Logger.Format); err != nil {
return err
}
if s.Logger.Level, err = s.parseAttribute(ctx, s.Logger.Level); err != nil {
return err
}
if s.Logger.Type, err = s.parseAttribute(ctx, s.Logger.Type); err != nil {
return err
}
}
if s.Preflights != nil {
for idx, preFlight := range s.Preflights {
if s.Preflights[idx].Command, err = s.parseAttribute(ctx, preFlight.Command); err != nil {
return err
}
if s.Preflights[idx].Workdir, err = s.parseAttribute(ctx, preFlight.Workdir); err != nil {
return err
}
if s.Preflights[idx].Message, err = s.parseAttribute(ctx, preFlight.Message); err != nil {
return err
}
}
}

// expand env var
if s.Command, err = expandEnvVars(ctx, s.Command); err != nil {
return err
}
if s.Workdir, err = expandEnvVars(ctx, s.Workdir); err != nil {
return err
}
if s.Command, err = expandEnvVars(ctx, s.Command); err != nil {
return err
}
if s.Name, err = expandEnvVars(ctx, s.Name); err != nil {
return err
}
if s.Workdir, err = expandEnvVars(ctx, s.Workdir); err != nil {
return err
}
if s.Probe != nil {
if s.Probe.Command, err = expandEnvVars(ctx, s.Probe.Command); err != nil {
return err
}
if s.Probe.Workdir, err = expandEnvVars(ctx, s.Probe.Workdir); err != nil {
return err
}
}
if s.Logger != nil {
if s.Logger.Destination, err = expandEnvVars(ctx, s.Logger.Destination); err != nil {
return err
}
if s.Logger.Format, err = expandEnvVars(ctx, s.Logger.Format); err != nil {
return err
}
if s.Logger.Level, err = expandEnvVars(ctx, s.Logger.Level); err != nil {
return err
}
if s.Logger.Type, err = expandEnvVars(ctx, s.Logger.Type); err != nil {
return err
}
}
if s.Preflights != nil {
for idx, preFlight := range s.Preflights {
if s.Preflights[idx].Command, err = expandEnvVars(ctx, preFlight.Command); err != nil {
return err
}
if s.Preflights[idx].Workdir, err = expandEnvVars(ctx, preFlight.Workdir); err != nil {
return err
}
if s.Preflights[idx].Message, err = expandEnvVars(ctx, preFlight.Message); err != nil {
return err
}
}
}

return nil
}

func (s *Step) parseAttribute(ctx context.Context, value string) (string, error) {
if value == "" {
return "", nil
}

buf := &bytes.Buffer{}
tmpl, err := template.New("step").Parse(value)
if err != nil {
return "", err
}

err = tmpl.Execute(buf, s)
if err != nil {
return "", err
}

return buf.String(), nil
}

func expandEnvVars(ctx context.Context, value string) (string, error) {
if value == "" {
return "", nil
}

expandedCommand := os.ExpandEnv(value)
return expandedCommand, nil
}

0 comments on commit 0937f87

Please sign in to comment.