From 102f8ab74e77010a95a8c12957828d8be38327b1 Mon Sep 17 00:00:00 2001 From: Andrey Nering Date: Sat, 16 Jun 2018 14:25:15 -0300 Subject: [PATCH] Expand environment variables on "dir", "sources" and "generates" So a path like this works: $GOPATH/src/github.com/go-task/task Allowing of "~" was also implemented. See #74 and baac067a1a0aa0d387b60a19977c901a3863fe97 Fixes #116 --- Taskfile.yml | 1 + internal/osext/osext.go | 22 ++++++++++++++++++++++ internal/status/glob.go | 5 +++-- variables.go | 5 ++--- 4 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 internal/osext/osext.go diff --git a/Taskfile.yml b/Taskfile.yml index 55aa7d76..d8a7e7ba 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -13,6 +13,7 @@ vars: ./internal/compiler/v2 ./internal/execext ./internal/logger + ./internal/osext ./internal/output ./internal/status ./internal/taskfile diff --git a/internal/osext/osext.go b/internal/osext/osext.go new file mode 100644 index 00000000..e6932e6a --- /dev/null +++ b/internal/osext/osext.go @@ -0,0 +1,22 @@ +package osext + +import ( + "os" + + "github.com/mitchellh/go-homedir" +) + +// Expand is an improved version of os.ExpandEnv, +// that not only expand enrionment variable ($GOPATH/src/github.com/...) +// but also expands "~" as the home directory. +func Expand(s string) (string, error) { + s = os.ExpandEnv(s) + + var err error + s, err = homedir.Expand(s) + if err != nil { + return "", err + } + + return s, nil +} diff --git a/internal/status/glob.go b/internal/status/glob.go index ef8e5c50..2bbe2ae4 100644 --- a/internal/status/glob.go +++ b/internal/status/glob.go @@ -4,8 +4,9 @@ import ( "path/filepath" "sort" + "github.com/go-task/task/internal/osext" + "github.com/mattn/go-zglob" - "github.com/mitchellh/go-homedir" ) func glob(dir string, globs []string) (files []string, err error) { @@ -13,7 +14,7 @@ func glob(dir string, globs []string) (files []string, err error) { if !filepath.IsAbs(g) { g = filepath.Join(dir, g) } - g, err = homedir.Expand(g) + g, err = osext.Expand(g) if err != nil { return nil, err } diff --git a/variables.go b/variables.go index e5d8b411..ce8a4127 100644 --- a/variables.go +++ b/variables.go @@ -3,10 +3,9 @@ package task import ( "path/filepath" + "github.com/go-task/task/internal/osext" "github.com/go-task/task/internal/taskfile" "github.com/go-task/task/internal/templater" - - "github.com/mitchellh/go-homedir" ) var ( @@ -41,7 +40,7 @@ func (e *Executor) CompiledTask(call taskfile.Call) (*taskfile.Task, error) { Method: r.Replace(origTask.Method), Prefix: r.Replace(origTask.Prefix), } - new.Dir, err = homedir.Expand(new.Dir) + new.Dir, err = osext.Expand(new.Dir) if err != nil { return nil, err }