Skip to content

Commit

Permalink
Don't need the local default function if Sprig is available
Browse files Browse the repository at this point in the history
Signed-off-by: Brian Topping <[email protected]>
  • Loading branch information
briantopping committed Aug 10, 2023
1 parent 07a0955 commit d6ee09c
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 48 deletions.
1 change: 0 additions & 1 deletion docs-v2/content/en/docs/environment/templating.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,4 @@ List of variables that are available for templating:

### Template functions
In addition to the [built-in template functions](https://pkg.go.dev/text/template) in Golang and the [Sprig template functions](http://masterminds.github.io/sprig/), Skaffold also provides the following:
- `default` : Default values can be specified using the `{{default "bar" .FOO}}` expression syntax, which results in "bar" if `.FOO` is nil or a zero value.
- `cmd`: This allows users to use the result from external commands in template, for example `{{cmd "bash" "-c" "xxx xxx xxx"}}` can be used to execute bash script and get the result into the template.
28 changes: 1 addition & 27 deletions pkg/skaffold/util/env_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"fmt"
"os"
"os/exec"
"reflect"
"sort"
"strings"
"text/template"
Expand All @@ -35,8 +34,7 @@ import (
var (
OSEnviron = os.Environ
funcsMap = template.FuncMap{
"default": defaultFunc,
"cmd": runCmdFunc,
"cmd": runCmdFunc,
}
)

Expand Down Expand Up @@ -140,30 +138,6 @@ func MapToFlag(m map[string]*string, flag string) ([]string, error) {
return kvFlags, nil
}

// defaultFunc is a template function that behaves as sprig's default function.
// See https://masterminds.github.io/sprig/defaults.html#default
func defaultFunc(dflt, value interface{}) interface{} {
if value == nil {
return dflt
}
v := reflect.ValueOf(value)
switch v.Kind() {
case reflect.Array, reflect.Slice:
if v.Len() == 0 {
return dflt
}
case reflect.Ptr:
if v.IsNil() {
return dflt
}
default:
if v.IsZero() {
return dflt
}
}
return value
}

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

func TestDefaultFunc(t *testing.T) {
for _, empty := range []interface{}{nil, false, 0, "", []string{}} {
t.Run(fmt.Sprintf("empties: %v (%T)", empty, empty), func(t *testing.T) {
dflt := "default"
if defaultFunc(dflt, empty) != dflt {
t.Error("did not return default")
}
})
}
s := "string"
for _, nonEmpty := range []interface{}{&s, true, 1, "hoot", []string{"hoot"}} {
t.Run(fmt.Sprintf("non-empty: %v (%T)", nonEmpty, nonEmpty), func(t *testing.T) {
dflt := "default"
if defaultFunc(dflt, nonEmpty) == dflt {
t.Error("should not return default")
}
})
}
}

func TestRunCmdFunc(t *testing.T) {
tests := []struct {
description string
Expand Down

0 comments on commit d6ee09c

Please sign in to comment.