Skip to content

Commit 285c697

Browse files
authored
chore: support-external-cmd-call-in-template (#8296)
1 parent 420b790 commit 285c697

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

pkg/skaffold/util/env_template.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"context"
2222
"fmt"
2323
"os"
24+
"os/exec"
2425
"reflect"
2526
"sort"
2627
"strings"
@@ -34,6 +35,7 @@ var (
3435
OSEnviron = os.Environ
3536
funcsMap = template.FuncMap{
3637
"default": defaultFunc,
38+
"cmd": runCmdFunc,
3739
}
3840
)
3941

@@ -160,3 +162,9 @@ func defaultFunc(dflt, value interface{}) interface{} {
160162
}
161163
return value
162164
}
165+
166+
func runCmdFunc(name string, args ...string) (string, error) {
167+
cmd := exec.Command(name, args...)
168+
out, err := RunCmdOut(context.TODO(), cmd)
169+
return string(out), err
170+
}

pkg/skaffold/util/env_template_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,37 @@ func TestDefaultFunc(t *testing.T) {
199199
})
200200
}
201201
}
202+
203+
func TestRunCmdFunc(t *testing.T) {
204+
tests := []struct {
205+
description string
206+
commandName string
207+
args []string
208+
output string
209+
expectedCommand string
210+
err error
211+
}{
212+
{
213+
description: "test running command succeeds",
214+
commandName: "bash",
215+
args: []string{"-c", "git rev-parse --verify HEAD"},
216+
output: "123",
217+
expectedCommand: "bash -c git rev-parse --verify HEAD",
218+
},
219+
{
220+
description: "test running command fails",
221+
commandName: "bash",
222+
args: []string{"-c", "gib rev-parse --verify HEAD"},
223+
output: "",
224+
expectedCommand: "bash -c gib rev-parse --verify HEAD",
225+
err: fmt.Errorf("command not found"),
226+
},
227+
}
228+
for _, test := range tests {
229+
testutil.Run(t, test.description, func(t *testutil.T) {
230+
t.Override(&DefaultExecCommand, testutil.CmdRunOut(test.expectedCommand, test.output))
231+
out, _ := runCmdFunc(test.commandName, test.args...)
232+
t.CheckErrorAndDeepEqual(test.err != nil, test.err, test.output, out)
233+
})
234+
}
235+
}

0 commit comments

Comments
 (0)