Skip to content

Commit

Permalink
Document rewrite clause
Browse files Browse the repository at this point in the history
  • Loading branch information
rliebz committed Aug 5, 2024
1 parent ed0bf60 commit 66780dd
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 26 deletions.
23 changes: 23 additions & 0 deletions docs/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,29 @@ options:
A private option will not accept environment variables or command line flags,
and it will not appear in the help documentation.

#### Option Rewrite

Boolean values are convenient as CLI inputs, but the interpolated output of
`true` or `false` is often not. Use the `rewrite` clause to change the
interpolation behavior from `true`/`false` to a conditional specified string:

```yaml
tasks:
greet:
options:
verbose:
type: boolean
rewrite: --level=verbose
run: mycli greet ${verbose}
```

The above will interpolate to eitehr `mycli greet` or `mycli greet --level=verbose`
depending on whether the `--verbose` flag is passed.

Note that once a boolean option has been rewritten, the output is no longer
`true`/`false`, which means `when: verbose` in the above example would never
evaluate to true.

#### Shared Options

Options may also be defined at the root of the config file to be shared between
Expand Down
91 changes: 65 additions & 26 deletions runner/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -813,13 +813,45 @@ tasks:
{
"rewrite",
`
tasks:
mytask:
options:
foo:
type: bool
rewrite: foovalue
bar:
type: bool
rewrite: barvalue
run: echo ${foo} ${bar}
`,
[]string{},
map[string]string{
"foo": "true",
},
"mytask",
marshal.Slice[*Run]{{
Command: marshal.Slice[*Command]{{
Exec: "echo foovalue",
Print: "echo foovalue",
}},
}},
},

{
"chained rewrite",
`
tasks:
mytask:
options:
foo:
type: bool
rewrite: newvalue
run: echo ${foo}
bar:
default:
when:
equal: {foo: newvalue}
value: barvalue
run: echo ${bar}
`,
[]string{},
map[string]string{
Expand All @@ -828,8 +860,38 @@ tasks:
"mytask",
marshal.Slice[*Run]{{
Command: marshal.Slice[*Command]{{
Exec: "echo newvalue",
Print: "echo newvalue",
Exec: "echo barvalue",
Print: "echo barvalue",
}},
}},
},

// This isn't really desirable, but we might as well capture that the
// behavior works the way it's expected to work.
{
"chained non-boolean rewrite",
`
tasks:
mytask:
options:
foo:
type: bool
rewrite: newvalue
bar:
default:
when: foo
value: barvalue
run: echo ${bar}
`,
[]string{},
map[string]string{
"foo": "true",
},
"mytask",
marshal.Slice[*Run]{{
Command: marshal.Slice[*Command]{{
Exec: "echo",
Print: "echo",
}},
}},
},
Expand Down Expand Up @@ -1113,29 +1175,6 @@ tasks:
type: string
rewrite: newvalue
run: echo ${bar}
`,
flags: map[string]string{
"foo": "true",
},
taskName: "mytask",
wantErr: "rewrite may only be performed on boolean values",
},

{
name: "chained rewrite",
input: `
tasks:
mytask:
options:
foo:
type: bool
rewrite: newvalue
bar:
default:
when:
equal: {foo: newvalue}
rewrite: barvalue
run: echo ${bar}
`,
flags: map[string]string{
"foo": "true",
Expand Down

0 comments on commit 66780dd

Please sign in to comment.