-
Notifications
You must be signed in to change notification settings - Fork 2
/
task.go
76 lines (68 loc) · 1.98 KB
/
task.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package muts
import (
"flag"
"fmt"
"log"
"os"
"sort"
"strings"
)
// Abort is the function that is called if any error was detected.
// You can inject your own here but make sure to set it before calling any Defer(task).
var Abort = log.Fatalln
var tasks = map[string]func(){}
// LocalUse holds the value for the -local flag (default is false)
var LocalUse = flag.Bool("local", false, "Run all on your local machine")
// Task registers a function that can be called using a name as argument of the program (or via RunTask).
func Task(name string, f func()) {
// for now, use a map of string->func, this may change
tasks[name] = f
}
// RunTasks runs all the named tasks in order.
// If only one string is given then interpret that as a composition of names separated by spaces.
// After each task run, the current working directory is reset.
// Do not abort if a task is unknown.
func RunTasks(names ...string) {
defer Chdir(Workspace)
for _, each := range names {
for _, name := range strings.Split(each, " ") {
task, ok := tasks[name]
if ok {
PrintfFunc("\n----------------------\n task %q in %s\n----------------------\n", name, Workspace)
Chdir(Workspace)
task()
} else {
PrintfFunc("[RunTasks failed] unknown task %q", name)
}
}
}
}
// RunTasksFromArgs reads the command line and runs each named task.
// A task name is an argument that does not start with a dash "-"
// Make sure any flags are on the commandline are present before the task names.
func RunTasksFromArgs() {
defer deferList.run()
if len(os.Args) == 1 {
PrintTasks()
fmt.Println()
flag.PrintDefaults()
return
}
for i := 1; i < len(os.Args); i++ {
each := os.Args[i]
if !strings.HasPrefix(each, "-") {
RunTasks(os.Args[i])
}
}
}
// PrintTasks lists (in sort order) the names of all registered tasks.
func PrintTasks() {
names := []string{}
for each, _ := range tasks {
names = append(names, each)
}
sort.Strings(names)
for _, each := range names {
fmt.Printf("\t%s\n", each)
}
}