feat(template): int func to coerce value to int

This commit is contained in:
Lewis Wynne 2025-11-20 01:18:14 +00:00
parent 9fdc831832
commit 99abdf4a31

View file

@ -27,6 +27,7 @@ import (
"os" "os"
"os/exec" "os/exec"
"slices" "slices"
"strconv"
"strings" "strings"
"text/template" "text/template"
@ -157,6 +158,14 @@ func applyTemplate(tplBytes []byte, substitutions []string) ([]byte, error) {
} }
return "", fmt.Errorf("invalid value %q (allowed: %v)", s, allowed) return "", fmt.Errorf("invalid value %q (allowed: %v)", s, allowed)
}, },
"int": func(v any) (int, error) {
s := fmt.Sprint(v)
i, err := strconv.Atoi(s)
if err != nil {
return 0, fmt.Errorf("failed to convert to int: %w", err)
}
return i, nil
},
} }
tpl, err := template.New("cmd"). tpl, err := template.New("cmd").
Delims("{{", "}}"). Delims("{{", "}}").