From ca72f3c3a130cee68a1d0e128ef0fc3448d503a2 Mon Sep 17 00:00:00 2001 From: Pete Davison Date: Wed, 19 Jul 2023 21:26:55 +0000 Subject: [PATCH] feat: added joinPath and relPath functions --- docs/docs/usage.md | 31 +++++++++++++++++++++++++------ internal/templater/funcs.go | 9 ++++++++- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/docs/docs/usage.md b/docs/docs/usage.md index 3dcb978b..774be626 100644 --- a/docs/docs/usage.md +++ b/docs/docs/usage.md @@ -883,17 +883,16 @@ you can use `requires`. This is useful when might not be clear to users which variables are needed, or if you want clear message about what is required. Also some tasks could have dangerous side effects if run with un-set variables. -Using `requires` you specify an array of strings in the `vars` sub-section -under `requires`, these strings are variable names which are checked prior to -running the task. If any variables are un-set the the task will error and not -run. +Using `requires` you specify an array of strings in the `vars` sub-section under +`requires`, these strings are variable names which are checked prior to running +the task. If any variables are un-set the the task will error and not run. Environmental variables are also checked. Syntax: ```yaml -requires: +requires: vars: [] # Array of strings ``` @@ -914,7 +913,7 @@ tasks: - 'docker build . -t {{.IMAGE_NAME}}:{{.IMAGE_TAG}}' # Make sure these variables are set before running - requires: + requires: vars: [IMAGE_NAME, IMAGE_TAG] ``` @@ -1042,6 +1041,26 @@ This will also work if you use globbing syntax in your sources. For example, if you specify a source for `*.txt`, the loop will iterate over all files that match that glob. +Source paths will always be returned as paths relative to the task directory. If +you need to convert this to an absolute path, you can use the built-in +`joinPath` function: + +```yaml +version: '3' + +tasks: + default: + vars: + MY_DIR: /path/to/dir + dir: '{{.MY_DIR}}' + sources: + - foo.txt + - bar.txt + cmds: + - for: sources + cmd: cat {{ joinPath .MY_DIR .ITEM }} +``` + ### Looping over variables To loop over the contents of a variable, you simply need to specify the variable diff --git a/internal/templater/funcs.go b/internal/templater/funcs.go index 21eaf8fc..d548d6a7 100644 --- a/internal/templater/funcs.go +++ b/internal/templater/funcs.go @@ -6,9 +6,10 @@ import ( "strings" "text/template" - sprig "github.com/go-task/slim-sprig" "mvdan.cc/sh/v3/shell" "mvdan.cc/sh/v3/syntax" + + sprig "github.com/go-task/slim-sprig" ) var templateFuncs template.FuncMap @@ -45,6 +46,12 @@ func init() { }, // 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) + }, } // Deprecated aliases for renamed functions. taskFuncs["FromSlash"] = taskFuncs["fromSlash"]