Skip to content

Commit

Permalink
Implement new args syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
rdsubhas committed Jan 22, 2017
1 parent 5f2e13b commit d49c97e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
10 changes: 3 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ myke solves all these problems in a single tiny binary, to avoid reinventing the
* Robust environment handling - Can be defined as keys in the YML or as dotenv files, overridden by dotenv.local files, `PATH` is always prepended, shell always takes precedence
* Built-in templating using golang text/template and 50+ functions provided by [sprig](https://github.com/Masterminds/sprig)
* Mixin ymls to share tasks, envvars, etc
* Runtime arguments like `myke ...task[key1=val1, key2=val2, ...]`
* Runtime arguments like `myke task1 --key1=val1 task2 --key2=val2 ...`
* `before/after` hooks to perform cleanups, chains with mixins, etc (`error` hook in the roadmap)
* `retry` support with max and delay for failing tasks
* and a lot of small utilities packed in
Expand All @@ -35,17 +35,13 @@ Create `myke.yml` with tasks. For example, running `myke` on this folder prints:
mixin | | task2, task3, task1
```

Using the above definition, you can invoke tasks like:
Using the above myke.yml, you can invoke tasks like:

* `myke build` runs build in all projects
* `myke <project>/build` runs build in that specific `<project>`
* `myke <tag>/build` runs build in all projects tagged `<tag>`
* `myke <tagA>/<tagB>/.../build` can match tasks by many tags (AND)

You can pass task parameters like:

* `myke template/arg[from=1,to=2]`
* `myke template/file`
* `myke task1 --key1=val1 task2 --key2=val2 ...` can pass arguments to individual tasks

## Installation

Expand Down
34 changes: 24 additions & 10 deletions core/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,37 +21,51 @@ type queryMatch struct {

// ParseQueries parses a query from the command line
func ParseQueries(qs []string) ([]Query, error) {
queries := make([]Query, len(qs))
for i, q := range qs {
queries := make([]Query, 0)
fmt.Println(qs, splitQueries(qs))
for _, q := range splitQueries(qs) {
query, err := parseQuery(q)
if err != nil {
return nil, err
}
queries[i] = query
queries = append(queries, query)
}
return queries, nil
}

func parseQuery(q string) (Query, error) {
tokens := strings.SplitN(strings.Trim(q, " ],/"), "[", 2)
if len(tokens) == 0 || len(tokens) > 2 {
return Query{}, fmt.Errorf("Bad query: %s", q)
func splitQueries(qs []string) [][]string {
if len(qs) < 2 {
return [][]string{qs}
}

res := [][]string{}
last := 0
for i, q := range qs[1:] {
if !strings.HasPrefix(q, "--") {
res = append(res, qs[last:i+1])
last = i+1
}
}

res = append(res, qs[last:])
return res
}

func parseQuery(tokens []string) (Query, error) {
tasks := strings.Split(strings.Trim(tokens[0], " /"), "/")
task, tags := tasks[len(tasks)-1], tasks[:len(tasks)-1]

params := make(map[string]string)
if len(tokens) > 1 {
for _, pair := range strings.Split(tokens[1], ",") {
kv := strings.SplitN(pair, "=", 2)
for _, pair := range tokens[1:] {
kv := strings.SplitN(strings.TrimLeft(pair, "--"), "=", 2)
if len(kv) == 2 {
params[kv[0]] = kv[1]
}
}
}

return Query{Raw: q, Task: task, Tags: tags, Params: params}, nil
return Query{Raw: strings.Join(tokens, " "), Task: task, Tags: tags, Params: params}, nil
}

func (q *Query) search(w *Workspace) []queryMatch {
Expand Down
5 changes: 3 additions & 2 deletions examples/template/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (
)

var tests = []TestTable{
{`args_from_to`, `args[from=a,to=b]`, `from=a to=b`},
{`args_from`, `args[from=a]`, `from=a to=something_to`},
{`args`, `args`, `(?s)template/args: Failed`},
{`args_from`, `args --from=a`, `from=a to=something_to`},
{`args_from_to`, `args --from=a --to=b`, `from=a to=b`},
{`args_multiple_tasks`, `args --from=a args --from=b`, `(?s).*from=a to=something_to.*from=b to=something_to`},
// Cannot invoke myke subcommand in a test
// {`file`, `file`, `(?s)I am a template.*PARAM1=value1.*PARAM2=value2`},
}
Expand Down

0 comments on commit d49c97e

Please sign in to comment.