Skip to content

Commit

Permalink
Implement after hooks, update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
StarpTech committed Jan 25, 2018
1 parent dbf7f52 commit 9df6a5b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
9 changes: 8 additions & 1 deletion commands/template/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@ type Question struct {
Help string `json:"help"`
}

type Hook struct {
Cmd string `json:"cmd"`
Args []string `json:"args"`
}

type Survey struct {
Questions []Question `json:"questions"`
Questions []Question `yaml:"questions"`
AfterHooks []Hook `yaml:"afterHooks"`
}

// ReadSurveyConfig reads the config and return a new survey
Expand All @@ -31,6 +37,7 @@ func ReadSurveyConfig(path string) (*Survey, error) {
}

err = yaml.Unmarshal(dat, &survey)

if err != nil {
logy.Errorf("could not unmarshal %s", err.Error())
return survey, err
Expand Down
16 changes: 16 additions & 0 deletions commands/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -565,6 +567,20 @@ func (t *Templating) Run() error {
return err
}

// run after hooks
for i, hook := range t.surveys.AfterHooks {
logy.Debugf("Run cmd %s with %v", hook.Cmd, hook.Args)
cmd := exec.Command(hook.Cmd, hook.Args...)
cmd.Dir = path.Clean(t.CommandData.Path)
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
err := cmd.Run()
if err != nil {
logy.WithError(err).Errorf("Command %d ('%s') could not be executed", i, hook.Cmd)
log.Fatal(err)
}
}

return nil
}

Expand Down
13 changes: 12 additions & 1 deletion docs/templateSurveys.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,24 @@ questions:
message: "Choose your database:"
options: ["mongodb", "mssql", "redis"]
required: true

# Are executed after the project was created completly.
afterHooks:
- cmd: node
args: ["v"]
```
**Configuration**
### Configuration
#### Questions
- type: input, select, multiselect `string`
- name: the indentifier of your question to access it in your template `string`
- message: the question or statement `string`
- options: the choices if you use select or mulitselect questions `[]string`
- default: depends on the type `string` for select and `[]string` for multiselect questions
- required: `boolean`
- help: `string`

#### afterHooks
- cmd: command to execute `string`
- args: arguments `[]string`
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ var (
)

func init() {
logy.SetLevel(logy.ErrorLevel)
logy.SetLevel(logy.WarnLevel)
cfg = config.ParseConfig(configName)

// Windows comaptible symbols
Expand Down

0 comments on commit 9df6a5b

Please sign in to comment.