Skip to content

Commit

Permalink
add template validation
Browse files Browse the repository at this point in the history
  • Loading branch information
StarpTech committed Jan 29, 2018
1 parent 68418f6 commit 12f2251
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
30 changes: 25 additions & 5 deletions commands/template/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,31 @@ import (
"io/ioutil"

logy "github.com/apex/log"
validator "gopkg.in/go-playground/validator.v9"
yaml "gopkg.in/yaml.v2"
)

// Question represents a question in the yml file
type Question struct {
Type string `json:"type"`
Name string `json:"name"`
Type string `json:"type" validate:"required"`
Name string `json:"name" validate:"required"`
Default interface{} `json:"default"`
Options []string `json:"options"`
Message string `json:"message"`
Message string `json:"message" validate:"required"`
Required bool `json:"required"`
Help string `json:"help"`
}

// Hook represent a hook in the yml file
type Hook struct {
Cmd string `json:"cmd"`
Cmd string `json:"cmd" validate:"required"`
Args []string `json:"args"`
Enabled string `json:"enabled"`
}

// Survey represents in the yml file
type Survey struct {
Questions []Question `yaml:"questions"`
Questions []Question `yaml:"questions" validate:"required,dive"`
AfterHooks []Hook `yaml:"afterHooks"`
Variables map[string]interface{} `yaml:"variables"`
Deprecated bool `yaml:"deprecated"`
Expand All @@ -49,5 +50,24 @@ func ReadSurveyConfig(path string) (*Survey, error) {
return survey, err
}

if err = validate(survey); err != nil {
logy.WithError(err).Error("invalid butler-survey.yml configuration")
return nil, err
}

return survey, nil
}

func validate(cfg interface{}) error {
validate := validator.New()
validate.RegisterStructValidation(questionStructHasOptions, Question{})
return validate.Struct(cfg)
}

func questionStructHasOptions(sl validator.StructLevel) {
question := sl.Current().Interface().(Question)

if (question.Type == "select" || question.Type == "multiselect") && len(question.Options) == 0 {
sl.ReportError(question.Options, "options", "foptions", "optionsRequired", "")
}
}
2 changes: 1 addition & 1 deletion commands/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ func (t *Templating) confirmPackTemplate() (bool, error) {
func (t *Templating) startTemplateSurvey(surveys *Survey) error {
questions, err := BuildSurveys(surveys)
if err != nil {
return errors.Wrap(err, "build surveys")
return errors.Wrap(err, "build survey from template config")
}

t.surveyResult = map[string]interface{}{}
Expand Down

0 comments on commit 12f2251

Please sign in to comment.