Skip to content

Commit

Permalink
Pass configs in context for reference to other tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
apoorvam committed May 15, 2019
1 parent 3f1f0ab commit 0dd5d1e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
2 changes: 1 addition & 1 deletion internal/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var HomeDir = os.Getenv("HOME")

// DirExists returns true if the given param is a valid existing directory
func DirExists(dir string) bool {
if dir[0] == '~' {
if strings.HasPrefix(dir, "~") {
dir = path.Join(HomeDir, strings.Trim(dir, "~"))
}
src, err := os.Stat(dir)
Expand Down
18 changes: 12 additions & 6 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"context"
"fmt"
"io/ioutil"
"os"
Expand Down Expand Up @@ -36,7 +37,7 @@ var (
type customValidation struct {
tag string
translation string
validationFn func(fl validator.FieldLevel) bool
validationFn func(context.Context, validator.FieldLevel) bool
}

var customValidations = []customValidation{
Expand All @@ -58,9 +59,9 @@ type Task struct {
Name string `yaml:"name"`
Image string `yaml:"image" validate:"required"`
SubDir string `yaml:"dir"`
Command []string `yaml:"command" validate:"required,min=1,dive,required"`
Command []string `yaml:"command" validate:"omitempty,dive,required"`
Envs []string `yaml:"envs"`
Mounts []string `yaml:"mounts" validate:"omitempty,dive,mountdir"`
Mounts []string `yaml:"mounts" validate:"omitempty,dive,min=1,mountdir"`
Args []string `yaml:"args"`
}

Expand All @@ -69,6 +70,10 @@ type Configs struct {
Tasks map[string][]Task `validate:"required,min=1,dive,keys,required,endkeys,required,min=1,required"`
}

type contextKey string

var configsKey = contextKey("dunnerConfigs")

// Validate validates config and returns errors.
func (configs *Configs) Validate() []error {
err := initValidator(customValidations)
Expand All @@ -77,10 +82,11 @@ func (configs *Configs) Validate() []error {
}
valErrs := govalidator.Struct(configs)
errs := formatErrors(valErrs, "")
ctx := context.WithValue(context.Background(), configsKey, configs)

// Each task is validated separately so that task name can be added in error messages
for taskName, tasks := range configs.Tasks {
taskValErrs := govalidator.Var(tasks, "dive")
taskValErrs := govalidator.VarCtx(ctx, tasks, "dive")
errs = append(errs, formatErrors(taskValErrs, taskName)...)
}
return errs
Expand Down Expand Up @@ -126,7 +132,7 @@ func initValidator(customValidations []customValidation) error {

// Register Custom validators and translations
for _, t := range customValidations {
err := govalidator.RegisterValidation(t.tag, t.validationFn)
err := govalidator.RegisterValidationCtx(t.tag, t.validationFn)
if err != nil {
return fmt.Errorf("failed to register validation: %s", err.Error())
}
Expand All @@ -140,7 +146,7 @@ func initValidator(customValidations []customValidation) error {

// ValidateMountDir verifies that mount values are in proper format <src>:<dest>:<mode>
// Format should match, <mode> is optional which is `readOnly` by default and `src` directory exists in host machine
func ValidateMountDir(fl validator.FieldLevel) bool {
func ValidateMountDir(ctx context.Context, fl validator.FieldLevel) bool {
value := fl.Field().String()
f := func(c rune) bool { return c == ':' }
mountValues := strings.FieldsFunc(value, f)
Expand Down
6 changes: 3 additions & 3 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ func TestConfigs_ValidateWithNoTasks(t *testing.T) {
}
}

func TestConfigs_ValidateWithParseErrors(t *testing.T) {
func TestConfigs_ValidateWithEmptyImageAndCommand(t *testing.T) {
tasks := make(map[string][]Task, 0)
task := Task{Image: "", Command: []string{}}
task := Task{Image: "", Command: []string{""}}
tasks["stats"] = []Task{task}
configs := &Configs{Tasks: tasks}

Expand All @@ -96,7 +96,7 @@ func TestConfigs_ValidateWithParseErrors(t *testing.T) {
}

expected1 := "task 'stats': image is a required field"
expected2 := "task 'stats': command must contain at least 1 item"
expected2 := "task 'stats': command[0] is a required field"
if errs[0].Error() != expected1 {
t.Fatalf("expected: %s, got: %s", expected1, errs[0].Error())
}
Expand Down

0 comments on commit 0dd5d1e

Please sign in to comment.