Skip to content

Commit f98bf6c

Browse files
committed
refactor: Create executor struct to get rid of global variables
Maybe eventually help on #17
1 parent c40148a commit f98bf6c

File tree

9 files changed

+137
-109
lines changed

9 files changed

+137
-109
lines changed

cmd/task/task.go

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@ package main
22

33
import (
44
"fmt"
5+
"log"
56

67
"github.com/go-task/task"
78

89
"github.com/spf13/pflag"
910
)
1011

1112
func main() {
13+
log.SetFlags(0)
14+
1215
pflag.Usage = func() {
1316
fmt.Println(`task [target1 target2 ...]: Runs commands under targets like make.
1417
@@ -25,9 +28,40 @@ hello:
2528
`)
2629
pflag.PrintDefaults()
2730
}
28-
pflag.BoolVarP(&task.Init, "init", "i", false, "creates a new Taskfile.yml in the current folder")
29-
pflag.BoolVarP(&task.Force, "force", "f", false, "forces execution even when the task is up-to-date")
30-
pflag.BoolVarP(&task.Watch, "watch", "w", false, "enables watch of the given task")
31+
32+
var (
33+
init bool
34+
force bool
35+
watch bool
36+
)
37+
38+
pflag.BoolVarP(&init, "init", "i", false, "creates a new Taskfile.yml in the current folder")
39+
pflag.BoolVarP(&force, "force", "f", false, "forces execution even when the task is up-to-date")
40+
pflag.BoolVarP(&watch, "watch", "w", false, "enables watch of the given task")
3141
pflag.Parse()
32-
task.Run()
42+
43+
if init {
44+
if err := task.InitTaskfile(); err != nil {
45+
log.Fatal(err)
46+
}
47+
return
48+
}
49+
50+
e := task.Executor{
51+
Force: force,
52+
Watch: watch,
53+
}
54+
if err := e.ReadTaskfile(); err != nil {
55+
log.Fatal(err)
56+
}
57+
58+
args := pflag.Args()
59+
if len(args) == 0 {
60+
log.Println("task: No argument given, trying default task")
61+
args = []string{"default"}
62+
}
63+
64+
if err := e.Run(args...); err != nil {
65+
log.Fatal(err)
66+
}
3367
}

cyclic.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package task
22

33
// HasCyclicDep checks if a task tree has any cyclic dependency
4-
func HasCyclicDep(m map[string]*Task) bool {
5-
visits := make(map[string]struct{}, len(m))
4+
func (e *Executor) HasCyclicDep() bool {
5+
visits := make(map[string]struct{}, len(e.Tasks))
66

77
var checkCyclicDep func(string, *Task) bool
88
checkCyclicDep = func(name string, t *Task) bool {
@@ -13,14 +13,14 @@ func HasCyclicDep(m map[string]*Task) bool {
1313
defer delete(visits, name)
1414

1515
for _, d := range t.Deps {
16-
if !checkCyclicDep(d, m[d]) {
16+
if !checkCyclicDep(d, e.Tasks[d]) {
1717
return false
1818
}
1919
}
2020
return true
2121
}
2222

23-
for k, v := range m {
23+
for k, v := range e.Tasks {
2424
if !checkCyclicDep(k, v) {
2525
return true
2626
}

cyclic_test.go

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,34 @@ import (
77
)
88

99
func TestCyclicDepCheck(t *testing.T) {
10-
isCyclic := map[string]*task.Task{
11-
"task-a": &task.Task{
12-
Deps: []string{"task-b"},
13-
},
14-
"task-b": &task.Task{
15-
Deps: []string{"task-a"},
10+
isCyclic := &task.Executor{
11+
Tasks: map[string]*task.Task{
12+
"task-a": &task.Task{
13+
Deps: []string{"task-b"},
14+
},
15+
"task-b": &task.Task{
16+
Deps: []string{"task-a"},
17+
},
1618
},
1719
}
1820

19-
if !task.HasCyclicDep(isCyclic) {
21+
if !isCyclic.HasCyclicDep() {
2022
t.Error("Task should be cyclic")
2123
}
2224

23-
isNotCyclic := map[string]*task.Task{
24-
"task-a": &task.Task{
25-
Deps: []string{"task-c"},
26-
},
27-
"task-b": &task.Task{
28-
Deps: []string{"task-c"},
25+
isNotCyclic := &task.Executor{
26+
Tasks: map[string]*task.Task{
27+
"task-a": &task.Task{
28+
Deps: []string{"task-c"},
29+
},
30+
"task-b": &task.Task{
31+
Deps: []string{"task-c"},
32+
},
33+
"task-c": &task.Task{},
2934
},
30-
"task-c": &task.Task{},
3135
}
3236

33-
if task.HasCyclicDep(isNotCyclic) {
37+
if isNotCyclic.HasCyclicDep() {
3438
t.Error("Task should not be cyclic")
3539
}
3640
}

errors.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
package task
22

33
import (
4+
"errors"
45
"fmt"
56
)
67

8+
var (
9+
// ErrCyclicDependencyDetected is returned when a cyclic dependency was found in the Taskfile
10+
ErrCyclicDependencyDetected = errors.New("task: cyclic dependency detected")
11+
// ErrTaskfileAlreadyExists is returned on creating a Taskfile if one already exists
12+
ErrTaskfileAlreadyExists = errors.New("task: A Taskfile already exists")
13+
)
14+
715
type taskFileNotFound struct {
816
taskFile string
917
}

help.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
"text/tabwriter"
88
)
99

10-
func printExistingTasksHelp() {
11-
tasks := tasksWithDesc()
10+
func (e *Executor) printExistingTasksHelp() {
11+
tasks := e.tasksWithDesc()
1212
if len(tasks) == 0 {
1313
return
1414
}
@@ -17,13 +17,13 @@ func printExistingTasksHelp() {
1717
// Format in tab-separated columns with a tab stop of 8.
1818
w := tabwriter.NewWriter(os.Stdout, 0, 8, 0, '\t', 0)
1919
for _, task := range tasks {
20-
fmt.Fprintln(w, fmt.Sprintf("- %s:\t%s", task, Tasks[task].Desc))
20+
fmt.Fprintln(w, fmt.Sprintf("- %s:\t%s", task, e.Tasks[task].Desc))
2121
}
2222
w.Flush()
2323
}
2424

25-
func tasksWithDesc() (tasks []string) {
26-
for name, task := range Tasks {
25+
func (e *Executor) tasksWithDesc() (tasks []string) {
26+
for name, task := range e.Tasks {
2727
if task.Desc != "" {
2828
tasks = append(tasks, name)
2929
}

init.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ default:
1313
- echo "Hello, World!"
1414
`
1515

16-
func initTaskfile() {
16+
// InitTaskfile Taskfile creates a new Taskfile
17+
func InitTaskfile() error {
1718
for _, f := range []string{"Taskfile.yml", "Taskfile.toml", "Taskfile.json"} {
1819
if _, err := os.Stat(f); err == nil {
19-
log.Printf("A Taskfile already exists")
20-
os.Exit(1)
21-
return
20+
return ErrTaskfileAlreadyExists
2221
}
2322
}
2423

2524
if err := ioutil.WriteFile("Taskfile.yml", []byte(defaultTaskfile), 0666); err != nil {
26-
log.Fatalf("%v", err)
25+
return err
2726
}
2827
log.Printf("Taskfile.yml created in the current directory")
28+
return nil
2929
}

read_taskfile.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,30 @@ import (
1111
"gopkg.in/yaml.v2"
1212
)
1313

14-
func readTaskfile() (map[string]*Task, error) {
15-
initialTasks, err := readTaskfileData(TaskFilePath)
14+
// ReadTaskfile parses Taskfile from the disk
15+
func (e *Executor) ReadTaskfile() error {
16+
var err error
17+
e.Tasks, err = e.readTaskfileData(TaskFilePath)
1618
if err != nil {
17-
return nil, err
19+
return err
1820
}
19-
mergeTasks, err := readTaskfileData(fmt.Sprintf("%s_%s", TaskFilePath, runtime.GOOS))
21+
22+
osTasks, err := e.readTaskfileData(fmt.Sprintf("%s_%s", TaskFilePath, runtime.GOOS))
2023
if err != nil {
2124
switch err.(type) {
22-
default:
23-
return nil, err
2425
case taskFileNotFound:
25-
return initialTasks, nil
26+
return nil
27+
default:
28+
return err
2629
}
2730
}
28-
if err := mergo.MapWithOverwrite(&initialTasks, mergeTasks); err != nil {
29-
return nil, err
31+
if err := mergo.MapWithOverwrite(&e.Tasks, osTasks); err != nil {
32+
return err
3033
}
31-
return initialTasks, nil
34+
return nil
3235
}
3336

34-
func readTaskfileData(path string) (tasks map[string]*Task, err error) {
37+
func (e *Executor) readTaskfileData(path string) (tasks map[string]*Task, err error) {
3538
if b, err := ioutil.ReadFile(path + ".yml"); err == nil {
3639
return tasks, yaml.Unmarshal(b, &tasks)
3740
}

0 commit comments

Comments
 (0)