mirror of
https://github.com/go-task/task.git
synced 2024-12-04 10:24:45 +02:00
Move path expanding logic to shell.Expand
This commit is contained in:
parent
18961e3d07
commit
67105b332f
@ -1,22 +0,0 @@
|
||||
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
|
||||
}
|
@ -4,9 +4,8 @@ import (
|
||||
"path/filepath"
|
||||
"sort"
|
||||
|
||||
"github.com/go-task/task/internal/osext"
|
||||
|
||||
"github.com/mattn/go-zglob"
|
||||
"mvdan.cc/sh/shell"
|
||||
)
|
||||
|
||||
func glob(dir string, globs []string) (files []string, err error) {
|
||||
@ -14,7 +13,7 @@ func glob(dir string, globs []string) (files []string, err error) {
|
||||
if !filepath.IsAbs(g) {
|
||||
g = filepath.Join(dir, g)
|
||||
}
|
||||
g, err = osext.Expand(g)
|
||||
g, err = shell.Expand(g, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
20
task_test.go
20
task_test.go
@ -12,6 +12,7 @@ import (
|
||||
"github.com/go-task/task"
|
||||
"github.com/go-task/task/internal/taskfile"
|
||||
|
||||
"github.com/mitchellh/go-homedir"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
@ -412,3 +413,22 @@ func TestTaskVersion(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestExpand(t *testing.T) {
|
||||
const dir = "testdata/expand"
|
||||
|
||||
home, err := homedir.Dir()
|
||||
if err != nil {
|
||||
t.Errorf("Couldn't get $HOME: %v", err)
|
||||
}
|
||||
var buff bytes.Buffer
|
||||
|
||||
e := task.Executor{
|
||||
Dir: dir,
|
||||
Stdout: &buff,
|
||||
Stderr: &buff,
|
||||
}
|
||||
assert.NoError(t, e.Setup())
|
||||
assert.NoError(t, e.Run(taskfile.Call{Task: "pwd"}))
|
||||
assert.Equal(t, home, strings.TrimSpace(buff.String()))
|
||||
}
|
||||
|
8
testdata/expand/Taskfile.yml
vendored
Normal file
8
testdata/expand/Taskfile.yml
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
version: '2'
|
||||
|
||||
tasks:
|
||||
pwd:
|
||||
cmds:
|
||||
- pwd
|
||||
dir: '~'
|
||||
silent: true
|
@ -3,9 +3,10 @@ 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"
|
||||
|
||||
"mvdan.cc/sh/shell"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -40,7 +41,7 @@ func (e *Executor) CompiledTask(call taskfile.Call) (*taskfile.Task, error) {
|
||||
Method: r.Replace(origTask.Method),
|
||||
Prefix: r.Replace(origTask.Prefix),
|
||||
}
|
||||
new.Dir, err = osext.Expand(new.Dir)
|
||||
new.Dir, err = shell.Expand(new.Dir, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user