Skip to content

Commit

Permalink
support templating in survey variables
Browse files Browse the repository at this point in the history
  • Loading branch information
StarpTech committed Apr 12, 2018
1 parent 1127876 commit 435dbf8
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 25 deletions.
62 changes: 45 additions & 17 deletions commands/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,8 @@ func (t *Templating) confirmPackTemplate(msg string) (bool, error) {
return packTemplate, nil
}

func (t *Templating) startTemplateSurvey(surveys *Survey) error {
questions, err := BuildSurveys(surveys)
func (t *Templating) startTemplateSurvey() error {
questions, err := BuildSurveys(t.templateConfig)
if err != nil {
return errors.Wrap(err, "build survey from template config")
}
Expand Down Expand Up @@ -442,7 +442,7 @@ func (t *Templating) runSurveyTemplateHooks(cmdDir string) error {
Parse("{if " + hook.Enabled + "}true{end}")

if err != nil {
return errors.Wrap(err, "create template")
return errors.Wrap(err, "parse hook template")
}

var buf bytes.Buffer
Expand All @@ -461,7 +461,30 @@ func (t *Templating) runSurveyTemplateHooks(cmdDir string) error {
cmd.Stdin = os.Stdin
err := cmd.Run()
if err != nil {
return errors.Wrapf(err, "Command %d ('%s') could not be executed", i, hook.Cmd)
return errors.Wrapf(err, "command %d ('%s') could not be executed", i, hook.Cmd)
}
}

return nil
}

// parseSurveyTemplateVariables template all survey variables
func (t *Templating) parseSurveyTemplateVariables() error {
for k, v := range t.Variables {
if varString, ok := v.(string); ok {
if strings.TrimSpace(varString) == "" {
return nil
}
tpl, err := template.New(k).
Delims("{", "}").
Funcs(t.templateFuncMap).
Parse(varString)
if err != nil {
return errors.Wrap(err, "parse variable template")
}
var buf bytes.Buffer
tpl.Execute(&buf, t.TemplateData)
t.Variables[k] = buf.String()
}
}

Expand Down Expand Up @@ -787,17 +810,34 @@ func (t *Templating) Run() (err error) {
return err
}

err = t.startTemplateSurvey(templateConfig)
err = t.startTemplateSurvey()
if err != nil {
ctx.WithError(err).Error("start template survey")
return err
}

t.TemplateData = &TemplateData{
t.CommandData,
time.Now().Format(time.RFC3339),
time.Now().Year(),
t.Variables,
}

t.generateTempFuncs()
t.parseSurveyTemplateVariables()

} else {
err := t.startProjectSurvey()
if err != nil {
ctx.WithError(err).Error("start project survey")
return err
}
t.TemplateData = &TemplateData{
t.CommandData,
time.Now().Format(time.RFC3339),
time.Now().Year(),
t.Variables,
}
}

// spinner progress
Expand All @@ -812,18 +852,6 @@ func (t *Templating) Run() (err error) {
*/
t.TaskTracker.Track("Template")

t.TemplateData = &TemplateData{
t.CommandData,
time.Now().Format(time.RFC3339),
time.Now().Year(),
t.Variables,
}

// create getter for survey result
if t.surveyResult != nil {
t.generateTempFuncs()
}

logy.Debugf("dir walk in path '%s'", tempDir)

// iterate through all directorys
Expand Down
30 changes: 22 additions & 8 deletions docs/templateSyntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,6 @@ _Butler maintain a [list](https://github.com/netzkern/butler/blob/master/command
* `butler{ .Date }` Return the date (RFC3339)
* `butler{ .Year }` Return the year (4-digits)

## Access to custom variables

Custom variables can be defined in the local `butler.yml` file or in the template `butler-survey.yml` file.

```
butler{ .Vars.company }
```

## Trim spaces around template actions

```go
Expand Down Expand Up @@ -99,6 +91,28 @@ butler{$id := uuid} // generate id
butler{$id} // print id
```

## Access to custom variables

Custom variables can be defined in the local `butler.yml` file or in the template `butler-survey.yml` file.

```
butler{ .Vars.company }
```

## Interpolate survey variables

You have access to the survey result as well as template helper in `butler-survey.yml` file.

```yml
variables:
projectName: "{ toUpperCase .Project.Name }"
dbName: "{ toPascalCase getDb }"
```
```
butler{ .Vars.projectName }
```

## Get survey results

We generate getter functions to provide an easier access to survey results. If you have a question with the name `color` the result is accessible by:
Expand Down

0 comments on commit 435dbf8

Please sign in to comment.