feat(template): int func to coerce value to int
This commit is contained in:
parent
9fdc831832
commit
99abdf4a31
1 changed files with 9 additions and 0 deletions
|
|
@ -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("{{", "}}").
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue