Skip to content

Commit

Permalink
Add text/template
Browse files Browse the repository at this point in the history
  • Loading branch information
rdsubhas committed Nov 14, 2016
1 parent d0b3570 commit 232d1dc
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 15 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ myke tag-name/task-name
* Environment files
* Projects can define environment variables using multiple methods (see section below)
* Runtime parameters
* If your build task command is: `echo {{key1|required}} {{key2|required}}`
* You can run it as: `myke build --key1=value1 --key2=value2`
* If your build task command is: `echo {{.key1}} {{.key2}}`
* You can run it as: `myke build[key1=value1,key2=value2]`
* Use runtime parameters to pass values that are dynamic each time you run `myke <project/task>`, otherwise prefer environment variables
* Template inheritance
* Projects can extend other template(s) using `extends` keyword
Expand Down
32 changes: 20 additions & 12 deletions core/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,20 @@ import (

type Execution struct {
Workspace *Workspace
Query *Query
Project *Project
Task *Task
}

func ExecuteQuery(w *Workspace, q Query) error {
matches := q.Search(w)
for _, match := range matches {
e := Execution{Workspace:w, Project:&match.Project, Task:&match.Task}
e := Execution{
Workspace:w,
Query: &q,
Project:&match.Project,
Task:&match.Task,
}
err := e.Execute()
if err != nil {
return err
Expand Down Expand Up @@ -48,7 +54,19 @@ func (e *Execution) Execute() error {
}

func (e *Execution) ExecuteSelf() error {
return e.Cmd().Run()
vars := mergeEnv(e.Project.Env, e.Query.Params)
cmd, err := commandTemplate(e.Task.Cmd, vars)
if err != nil {
return err
}

proc := exec.Command("sh", "-exc", cmd)
proc.Dir = e.Project.Cwd
proc.Env = envList(e.Project.Env)
proc.Stdin = os.Stdin
proc.Stdout = os.Stdout
proc.Stderr = os.Stderr
return proc.Run()
}

func (e *Execution) ExecuteDependent(qs []string) error {
Expand All @@ -70,16 +88,6 @@ func (e *Execution) ExecuteDependent(qs []string) error {
return nil
}

func (e *Execution) Cmd() *exec.Cmd {
cmd := exec.Command("sh", "-exc", e.Task.Cmd)
cmd.Dir = e.Project.Cwd
cmd.Env = envList(e.Project.Env)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd
}

func envList(env map[string]string) []string {
env["PATH"] = env["PATH"] + string(os.PathListSeparator) + os.Getenv("PATH")
envList := make([]string, len(env))
Expand Down
21 changes: 21 additions & 0 deletions core/template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package core

import (
"text/template"
"bytes"
)

func commandTemplate(tmpl string, params map[string]string) (string, error) {
t, err := template.New("cmd").Parse(tmpl)
if err != nil {
return "", err
}

var doc bytes.Buffer
err = t.Execute(&doc, params)
if err != nil {
return "", err
} else {
return doc.String(), nil
}
}
2 changes: 1 addition & 1 deletion examples/params/myke.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ project: params
desc: demonstrates use of templates and parameters in command
tasks:
render:
cmd: echo {{from | required}} {{to | default("something_to")}}
cmd: echo {{.from}} {{or .to "something_to"}}
desc: run as s2do render (or) params/render with parameters --from=... --to=...

0 comments on commit 232d1dc

Please sign in to comment.