mirror of
https://github.com/go-task/task.git
synced 2025-05-13 22:16:31 +02:00
feat: add yaml templating functions (#2219)
* feat: add yaml templating functions * docs: add yaml functions to templating reference * refactor: remove some unnecessary function wrappers
This commit is contained in:
parent
0058f18676
commit
d850d03c96
@ -7,6 +7,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
"mvdan.cc/sh/v3/shell"
|
"mvdan.cc/sh/v3/shell"
|
||||||
"mvdan.cc/sh/v3/syntax"
|
"mvdan.cc/sh/v3/syntax"
|
||||||
|
|
||||||
@ -18,58 +19,25 @@ var templateFuncs template.FuncMap
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
taskFuncs := template.FuncMap{
|
taskFuncs := template.FuncMap{
|
||||||
"OS": func() string { return runtime.GOOS },
|
"OS": os,
|
||||||
"ARCH": func() string { return runtime.GOARCH },
|
"ARCH": arch,
|
||||||
"numCPU": func() int { return runtime.NumCPU() },
|
"numCPU": runtime.NumCPU,
|
||||||
"catLines": func(s string) string {
|
"catLines": catLines,
|
||||||
s = strings.ReplaceAll(s, "\r\n", " ")
|
"splitLines": splitLines,
|
||||||
return strings.ReplaceAll(s, "\n", " ")
|
"fromSlash": filepath.FromSlash,
|
||||||
},
|
"toSlash": filepath.ToSlash,
|
||||||
"splitLines": func(s string) []string {
|
"exeExt": exeExt,
|
||||||
s = strings.ReplaceAll(s, "\r\n", "\n")
|
"shellQuote": shellQuote,
|
||||||
return strings.Split(s, "\n")
|
"splitArgs": splitArgs,
|
||||||
},
|
"IsSH": IsSH, // Deprecated
|
||||||
"fromSlash": func(path string) string {
|
"joinPath": filepath.Join,
|
||||||
return filepath.FromSlash(path)
|
"relPath": filepath.Rel,
|
||||||
},
|
"merge": merge,
|
||||||
"toSlash": func(path string) string {
|
"spew": spew.Sdump,
|
||||||
return filepath.ToSlash(path)
|
"fromYaml": fromYaml,
|
||||||
},
|
"mustFromYaml": mustFromYaml,
|
||||||
"exeExt": func() string {
|
"toYaml": toYaml,
|
||||||
if runtime.GOOS == "windows" {
|
"mustToYaml": mustToYaml,
|
||||||
return ".exe"
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
},
|
|
||||||
"shellQuote": func(str string) (string, error) {
|
|
||||||
return syntax.Quote(str, syntax.LangBash)
|
|
||||||
},
|
|
||||||
"splitArgs": func(s string) ([]string, error) {
|
|
||||||
return shell.Fields(s, nil)
|
|
||||||
},
|
|
||||||
// IsSH is deprecated.
|
|
||||||
"IsSH": func() bool { return true },
|
|
||||||
"joinPath": func(elem ...string) string {
|
|
||||||
return filepath.Join(elem...)
|
|
||||||
},
|
|
||||||
"relPath": func(basePath, targetPath string) (string, error) {
|
|
||||||
return filepath.Rel(basePath, targetPath)
|
|
||||||
},
|
|
||||||
"merge": func(base map[string]any, v ...map[string]any) map[string]any {
|
|
||||||
cap := len(v)
|
|
||||||
for _, m := range v {
|
|
||||||
cap += len(m)
|
|
||||||
}
|
|
||||||
result := make(map[string]any, cap)
|
|
||||||
maps.Copy(result, base)
|
|
||||||
for _, m := range v {
|
|
||||||
maps.Copy(result, m)
|
|
||||||
}
|
|
||||||
return result
|
|
||||||
},
|
|
||||||
"spew": func(v any) string {
|
|
||||||
return spew.Sdump(v)
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// aliases
|
// aliases
|
||||||
@ -83,3 +51,78 @@ func init() {
|
|||||||
templateFuncs = template.FuncMap(sprig.TxtFuncMap())
|
templateFuncs = template.FuncMap(sprig.TxtFuncMap())
|
||||||
maps.Copy(templateFuncs, taskFuncs)
|
maps.Copy(templateFuncs, taskFuncs)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func os() string {
|
||||||
|
return runtime.GOOS
|
||||||
|
}
|
||||||
|
|
||||||
|
func arch() string {
|
||||||
|
return runtime.GOARCH
|
||||||
|
}
|
||||||
|
|
||||||
|
func catLines(s string) string {
|
||||||
|
s = strings.ReplaceAll(s, "\r\n", " ")
|
||||||
|
return strings.ReplaceAll(s, "\n", " ")
|
||||||
|
}
|
||||||
|
|
||||||
|
func splitLines(s string) []string {
|
||||||
|
s = strings.ReplaceAll(s, "\r\n", "\n")
|
||||||
|
return strings.Split(s, "\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
func exeExt() string {
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
return ".exe"
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func shellQuote(str string) (string, error) {
|
||||||
|
return syntax.Quote(str, syntax.LangBash)
|
||||||
|
}
|
||||||
|
|
||||||
|
func splitArgs(s string) ([]string, error) {
|
||||||
|
return shell.Fields(s, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Deprecated: now always returns true
|
||||||
|
func IsSH() bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func merge(base map[string]any, v ...map[string]any) map[string]any {
|
||||||
|
cap := len(v)
|
||||||
|
for _, m := range v {
|
||||||
|
cap += len(m)
|
||||||
|
}
|
||||||
|
result := make(map[string]any, cap)
|
||||||
|
maps.Copy(result, base)
|
||||||
|
for _, m := range v {
|
||||||
|
maps.Copy(result, m)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func fromYaml(v string) any {
|
||||||
|
output, _ := mustFromYaml(v)
|
||||||
|
return output
|
||||||
|
}
|
||||||
|
|
||||||
|
func mustFromYaml(v string) (any, error) {
|
||||||
|
var output any
|
||||||
|
err := yaml.Unmarshal([]byte(v), &output)
|
||||||
|
return output, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func toYaml(v any) string {
|
||||||
|
output, _ := yaml.Marshal(v)
|
||||||
|
return string(output)
|
||||||
|
}
|
||||||
|
|
||||||
|
func mustToYaml(v any) (string, error) {
|
||||||
|
output, err := yaml.Marshal(v)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return string(output), nil
|
||||||
|
}
|
||||||
|
@ -264,6 +264,8 @@ description here for completeness. For detailed usage, please refer to the
|
|||||||
| `toJson`\* | Encodes an object as a JSON string. |
|
| `toJson`\* | Encodes an object as a JSON string. |
|
||||||
| `toPrettyJson`\* | Encodes an object as a JSON string with new lines and indentation. |
|
| `toPrettyJson`\* | Encodes an object as a JSON string with new lines and indentation. |
|
||||||
| `toRawJson`\* | Encodes an object as a JSON string with HTML characters unescaped. |
|
| `toRawJson`\* | Encodes an object as a JSON string with HTML characters unescaped. |
|
||||||
|
| `fromYaml`\* | Decodes a YAML string into an object. |
|
||||||
|
| `toYaml`\* | Encodes an object as a YAML string. |
|
||||||
| `b64enc` | Encodes a string into base 64. |
|
| `b64enc` | Encodes a string into base 64. |
|
||||||
| `b64dec` | Decodes a string from base 64. |
|
| `b64dec` | Decodes a string from base 64. |
|
||||||
| `b32enc` | Encodes a string into base 32. |
|
| `b32enc` | Encodes a string into base 32. |
|
||||||
|
Loading…
x
Reference in New Issue
Block a user