mirror of
https://github.com/go-task/task.git
synced 2025-07-15 01:35:00 +02:00
Protect creation of "dir:" with a mutex
This commit is contained in:
@ -1,5 +1,8 @@
|
|||||||
package taskfile
|
package taskfile
|
||||||
|
|
||||||
|
import "os"
|
||||||
|
import "sync"
|
||||||
|
|
||||||
// Tasks represents a group of tasks
|
// Tasks represents a group of tasks
|
||||||
type Tasks map[string]*Task
|
type Tasks map[string]*Task
|
||||||
|
|
||||||
@ -14,6 +17,7 @@ type Task struct {
|
|||||||
Generates []string
|
Generates []string
|
||||||
Status []string
|
Status []string
|
||||||
Dir string
|
Dir string
|
||||||
|
mkdirMutex sync.Mutex
|
||||||
Vars Vars
|
Vars Vars
|
||||||
Env Vars
|
Env Vars
|
||||||
Silent bool
|
Silent bool
|
||||||
@ -21,3 +25,22 @@ type Task struct {
|
|||||||
Prefix string
|
Prefix string
|
||||||
IgnoreError bool `yaml:"ignore_error"`
|
IgnoreError bool `yaml:"ignore_error"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Mkdir creates the directory Task.Dir.
|
||||||
|
// Safe to be called concurrently.
|
||||||
|
func (t *Task) Mkdir() error {
|
||||||
|
if t.Dir == "" {
|
||||||
|
// No "dir:" attribute, so we do nothing.
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
t.mkdirMutex.Lock()
|
||||||
|
defer t.mkdirMutex.Unlock()
|
||||||
|
|
||||||
|
if _, err := os.Stat(t.Dir); os.IsNotExist(err) {
|
||||||
|
if err := os.MkdirAll(t.Dir, 0755); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
7
task.go
7
task.go
@ -202,13 +202,8 @@ func (e *Executor) RunTask(ctx context.Context, call taskfile.Call) error {
|
|||||||
|
|
||||||
// When using the "dir:" attribute it can happen that the directory doesn't exist.
|
// When using the "dir:" attribute it can happen that the directory doesn't exist.
|
||||||
// If so, we create it.
|
// If so, we create it.
|
||||||
if t.Dir != "" {
|
if err := t.Mkdir(); err != nil {
|
||||||
if _, err := os.Stat(t.Dir); os.IsNotExist(err) {
|
|
||||||
if err := os.MkdirAll(t.Dir, 0755); err != nil {
|
|
||||||
e.Logger.Errf("task: cannot make directory %q: %v", t.Dir, err)
|
e.Logger.Errf("task: cannot make directory %q: %v", t.Dir, err)
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := range t.Cmds {
|
for i := range t.Cmds {
|
||||||
|
Reference in New Issue
Block a user