Skip to content

Commit

Permalink
Merge pull request #99 from davidovich/adds-destination-for-templated…
Browse files Browse the repository at this point in the history
…-summon

teach the summon templ func to allow dest
  • Loading branch information
davidovich committed Jan 14, 2024
2 parents bea948f + 5a2dd4d commit a57503c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 10 deletions.
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,15 @@ bash /tmp/hello.sh
> Note that `hello.sh` could also contain templates that will be
rendered at instantiation time.

##### Arg and Args Function
Destination

> new in v0.16.0
>
> The summon function takes an additional parameter specifying where the summoned
> file should be written. If not specified, it will be written to the temp
> directory.
##### `{{ arg }}` and `{{ args }}` Function

> New in v0.12.0
Expand Down Expand Up @@ -583,9 +591,9 @@ exec:
cmd: *bash-c
completion: '{{ (split ":" (run "bash-c" (printf "kubectl __complete %s ''%s''" (join " " (rest (initial args))) (last args))))._0 }}'

bash --norc --noprofile -c: # this is a simple bash environment to delegate calls
bash-c:
hidden: true
bash: # this is a simple bash environment to delegate calls
cmd: [bash, --norc, --noprofile, -c]
hidden: true
```

#### Removing the run subcommand
Expand Down
20 changes: 16 additions & 4 deletions pkg/summon/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func TestRun(t *testing.T) {
contains [][]string
args []string
wantErr bool
replaceFS bool
}{
{
name: "composite-invoker", // python -c
Expand All @@ -47,10 +48,17 @@ func TestRun(t *testing.T) {
wantErr: false,
},
{
name: "self-reference-invoker", // bash
cmd: []string{"bash-self-ref"},
expect: [][]string{{"bash", filepath.Join(os.TempDir(), "hello.sh")}},
wantErr: false,
name: "self-reference-invoker", // bash
cmd: []string{"bash-self-ref"},
expect: [][]string{{"bash", filepath.Join(os.TempDir(), "hello.sh")}},
wantErr: false,
replaceFS: true,
},
{
name: "summon-function-with-destination",
cmd: []string{"summon-with-destination"},
expect: [][]string{{"cat", filepath.Join("dest-dir", "hello.sh")}},
replaceFS: true,
},
{
name: "self-reference-run", // bash
Expand Down Expand Up @@ -179,6 +187,10 @@ func TestRun(t *testing.T) {
tt.helper = "TestSummonRunHelper"
}

if tt.replaceFS {
defer testutil.ReplaceFs()()
}

program := append([]string{"summon"}, tt.cmd...)
args := append(program, tt.args...)
execCmd := testutil.FakeExecCommand(tt.helper)
Expand Down
11 changes: 9 additions & 2 deletions pkg/summon/summon.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"os"
"path"
"path/filepath"
"reflect"
"strings"
"text/template"

Expand Down Expand Up @@ -194,8 +195,14 @@ func summonFuncMap(d *Driver) template.FuncMap {
}
return strings.TrimSpace(b.String()), err
},
"summon": func(path string) (string, error) {
return d.Summon(Filename(path), Dest(os.TempDir()))
"summon": func(path string, arg ...any) (string, error) {
dest := os.TempDir()
if len(arg) > 0 {
if reflect.TypeOf(arg[0]).Kind() == reflect.String {
dest = arg[0].(string)
}
}
return d.Summon(Filename(path), Dest(dest))
},
"flagValue": func(flag string) (string, error) {
for _, toRender := range d.flagsToRender {
Expand Down
1 change: 1 addition & 0 deletions pkg/summon/testdata/summon.config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ exec:
# These handles are setup for testing
hello-bash: [bash, hello.sh ]
bash-self-ref: [bash, '{{ summon "hello.sh" }}']
summon-with-destination: [cat, '{{ summon "hello.sh" "dest-dir" }}']
run-example: [bash, '{{ run "hello-bash" }}']
args: [bash, 'args:', '{{ arg 0 "" }}']
one-arg: [bash, 'args:', '{{arg 0 "" }}']
Expand Down

0 comments on commit a57503c

Please sign in to comment.