1
0
mirror of https://github.com/go-task/task.git synced 2025-06-15 00:15:10 +02:00

refactor: minor improvements to setCurrentDir

This commit is contained in:
Pete Davison
2023-09-14 22:15:54 +00:00
committed by Andrey Nering
parent ba299aa71f
commit d1d312f396
2 changed files with 11 additions and 15 deletions

View File

@ -55,6 +55,11 @@ func (e *Executor) Setup() error {
} }
func (e *Executor) setCurrentDir() error { func (e *Executor) setCurrentDir() error {
// If the entrypoint is already set, we don't need to do anything
if e.Entrypoint != "" {
return nil
}
// Default the directory to the current working directory // Default the directory to the current working directory
if e.Dir == "" { if e.Dir == "" {
wd, err := os.Getwd() wd, err := os.Getwd()
@ -64,22 +69,13 @@ func (e *Executor) setCurrentDir() error {
e.Dir = wd e.Dir = wd
} }
// Ensure we have an absolute path // Search for a taskfile
abs, err := filepath.Abs(e.Dir)
if err != nil {
return err
}
e.Dir = abs
// If no entrypoint is specified, we need to search for a taskfile
if e.Entrypoint == "" {
root, err := read.ExistsWalk(e.Dir) root, err := read.ExistsWalk(e.Dir)
if err != nil { if err != nil {
return err return err
} }
e.Dir = filepath.Dir(root) e.Dir = filepath.Dir(root)
e.Entrypoint = filepath.Base(root) e.Entrypoint = filepath.Base(root)
}
return nil return nil
} }

View File

@ -311,13 +311,13 @@ func Exists(path string) (string, error) {
return "", err return "", err
} }
if fi.Mode().IsRegular() { if fi.Mode().IsRegular() {
return path, nil return filepath.Abs(path)
} }
for _, n := range defaultTaskfiles { for _, n := range defaultTaskfiles {
fpath := filepathext.SmartJoin(path, n) fpath := filepathext.SmartJoin(path, n)
if _, err := os.Stat(fpath); err == nil { if _, err := os.Stat(fpath); err == nil {
return fpath, nil return filepath.Abs(fpath)
} }
} }