Skip to content

Commit

Permalink
chore: support-external-cmd-call-in-template (#8296)
Browse files Browse the repository at this point in the history
  • Loading branch information
ericzzzzzzz authored Jan 5, 2023
1 parent 420b790 commit 285c697
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
8 changes: 8 additions & 0 deletions pkg/skaffold/util/env_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"context"
"fmt"
"os"
"os/exec"
"reflect"
"sort"
"strings"
Expand All @@ -34,6 +35,7 @@ var (
OSEnviron = os.Environ
funcsMap = template.FuncMap{
"default": defaultFunc,
"cmd": runCmdFunc,
}
)

Expand Down Expand Up @@ -160,3 +162,9 @@ func defaultFunc(dflt, value interface{}) interface{} {
}
return value
}

func runCmdFunc(name string, args ...string) (string, error) {
cmd := exec.Command(name, args...)
out, err := RunCmdOut(context.TODO(), cmd)
return string(out), err
}
34 changes: 34 additions & 0 deletions pkg/skaffold/util/env_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,37 @@ func TestDefaultFunc(t *testing.T) {
})
}
}

func TestRunCmdFunc(t *testing.T) {
tests := []struct {
description string
commandName string
args []string
output string
expectedCommand string
err error
}{
{
description: "test running command succeeds",
commandName: "bash",
args: []string{"-c", "git rev-parse --verify HEAD"},
output: "123",
expectedCommand: "bash -c git rev-parse --verify HEAD",
},
{
description: "test running command fails",
commandName: "bash",
args: []string{"-c", "gib rev-parse --verify HEAD"},
output: "",
expectedCommand: "bash -c gib rev-parse --verify HEAD",
err: fmt.Errorf("command not found"),
},
}
for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
t.Override(&DefaultExecCommand, testutil.CmdRunOut(test.expectedCommand, test.output))
out, _ := runCmdFunc(test.commandName, test.args...)
t.CheckErrorAndDeepEqual(test.err != nil, test.err, test.output, out)
})
}
}

0 comments on commit 285c697

Please sign in to comment.