From 66780dd76ee3d39e11aa1794bf59787144151ff9 Mon Sep 17 00:00:00 2001 From: Robert Liebowitz Date: Mon, 5 Aug 2024 08:00:44 -0400 Subject: [PATCH] Document rewrite clause --- docs/spec.md | 23 +++++++++++ runner/parse_test.go | 91 +++++++++++++++++++++++++++++++------------- 2 files changed, 88 insertions(+), 26 deletions(-) diff --git a/docs/spec.md b/docs/spec.md index 12fdd09..5e12d2b 100644 --- a/docs/spec.md +++ b/docs/spec.md @@ -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 diff --git a/runner/parse_test.go b/runner/parse_test.go index 032902b..640c97a 100644 --- a/runner/parse_test.go +++ b/runner/parse_test.go @@ -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{ @@ -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", }}, }}, }, @@ -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",