From 5aaf6b27f0e8766cbe617145caa3d77048383171 Mon Sep 17 00:00:00 2001 From: Valentin Maerten Date: Mon, 29 Apr 2024 11:07:46 +0200 Subject: [PATCH] use templating system instead of os.ExpandEnv --- task_test.go | 2 ++ taskfile/node_http.go | 4 +--- taskfile/reader.go | 5 ++++- .../include_with_env_variable/Taskfile.yml | 4 ++++ website/docs/experiments/remote_taskfiles.mdx | 4 ++-- 5 files changed, 13 insertions(+), 6 deletions(-) create mode 100644 testdata/includes_interpolation/include_with_env_variable/Taskfile.yml diff --git a/task_test.go b/task_test.go index fe6e7ec7ef..58a96c58f8 100644 --- a/task_test.go +++ b/task_test.go @@ -1200,8 +1200,10 @@ func TestIncludesInterpolation(t *testing.T) { expectedOutput string }{ {"include", "include", false, "include\n"}, + {"include_with_env_variable", "include-with-env-variable", false, "include_with_env_variable\n"}, {"include_with_dir", "include-with-dir", false, "included\n"}, } + t.Setenv("MODULE", "included") for _, test := range tests { t.Run(test.name, func(t *testing.T) { diff --git a/taskfile/node_http.go b/taskfile/node_http.go index 746c13da2c..5931d10f19 100644 --- a/taskfile/node_http.go +++ b/taskfile/node_http.go @@ -5,7 +5,6 @@ import ( "io" "net/http" "net/url" - "os" "path/filepath" "time" @@ -30,8 +29,7 @@ func NewHTTPNode( opts ...NodeOption, ) (*HTTPNode, error) { base := NewBaseNode(dir, opts...) - entrypointWithEnv := os.ExpandEnv(entrypoint) - url, err := url.Parse(entrypointWithEnv) + url, err := url.Parse(entrypoint) if err != nil { return nil, err } diff --git a/taskfile/reader.go b/taskfile/reader.go index e9fa8b61bf..0da8606d46 100644 --- a/taskfile/reader.go +++ b/taskfile/reader.go @@ -3,6 +3,7 @@ package taskfile import ( "context" "fmt" + "github.com/go-task/task/v3/internal/compiler" "os" "time" @@ -97,9 +98,11 @@ func (r *Reader) include(node Node) error { // Loop over each included taskfile _ = vertex.Taskfile.Includes.Range(func(namespace string, include ast.Include) error { + vars := compiler.GetEnviron() + vars.Merge(vertex.Taskfile.Vars, nil) // Start a goroutine to process each included Taskfile g.Go(func() error { - cache := &templater.Cache{Vars: vertex.Taskfile.Vars} + cache := &templater.Cache{Vars: vars} include = ast.Include{ Namespace: include.Namespace, Taskfile: templater.Replace(include.Taskfile, cache), diff --git a/testdata/includes_interpolation/include_with_env_variable/Taskfile.yml b/testdata/includes_interpolation/include_with_env_variable/Taskfile.yml new file mode 100644 index 0000000000..e9a8dea0de --- /dev/null +++ b/testdata/includes_interpolation/include_with_env_variable/Taskfile.yml @@ -0,0 +1,4 @@ +version: "3" + +includes: + include-with-env-variable: '../{{.MODULE}}/Taskfile.yml' diff --git a/website/docs/experiments/remote_taskfiles.mdx b/website/docs/experiments/remote_taskfiles.mdx index 9bc20b1fd1..ba2e825bf0 100644 --- a/website/docs/experiments/remote_taskfiles.mdx +++ b/website/docs/experiments/remote_taskfiles.mdx @@ -48,13 +48,13 @@ tasks: and you run `task my-remote-namespace:hello`, it will print the text: "Hello from the remote Taskfile!" to your console. -You can also reference environment variable in your URL to have authentication, for example : +URL is processed by the templating system, so you can reference environment variable in your URL to have authentication, for example : ```yaml version: '3' includes: - my-remote-namespace: https://${TOKEN}@raw.githubusercontent.com/my-org/my-repo/main/Taskfile.yml + my-remote-namespace: https://{{.TOKEN}}@raw.githubusercontent.com/my-org/my-repo/main/Taskfile.yml ``` `TOKEN=my-token task my-remote-namespace:hello` will be resolved by Task to `https://my-token@raw.githubusercontent.com/my-org/my-repo/main/Taskfile.yml`