-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
89 lines (76 loc) · 1.82 KB
/
main.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
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"regexp"
"github.com/fatih/color"
)
var filename = "Runfile.yaml"
var list = false
var debug = false
var requests []string
var envRe *regexp.Regexp
var varRe *regexp.Regexp
var cmdRe *regexp.Regexp
var context map[string]string
func init() {
flag.StringVar(&filename, "file", filename, "The file to run commands from")
flag.BoolVar(&list, "list", list, "List available Runfiles and their commands")
flag.BoolVar(&debug, "debug", debug, "Whether to show debugging information")
flag.Parse()
path, err := filepath.Abs(filename)
if err != nil {
log.Fatalf("error: %v", err)
}
filename = path
requests = flag.Args()
envRe = regexp.MustCompile(`\${(?P<Key>\S+)}`)
varRe = regexp.MustCompile(`{{2}\.(?P<Key>\S+)}{2}`)
cmdRe = regexp.MustCompile(`["'].+?["']|\S+`)
context = make(map[string]string)
}
func main() {
files := findRunfiles()
if list {
for _, file := range files {
fmt.Printf("Runfile: %s\n", file.Path+file.Filename)
for _, command := range file.Commands {
fmt.Printf("└ %s\n", command.Key)
}
}
os.Exit(0)
}
if len(files) > 0 {
if len(requests) == 0 {
runfile := files[0]
debugPrintf("Runfile: %s\n", runfile.Path+runfile.Filename)
for _, command := range runfile.Commands {
c, err := runfile.FindCommand(command.Key.(string))
if err != nil {
continue
}
runfile.ProcessCommand(c)
}
} else {
for _, request := range requests {
found := false
for _, runfile := range files {
debugPrintf("Runfile: %s\n", runfile.Path+runfile.Filename)
c, err := runfile.FindCommand(request)
if err != nil {
continue
}
found = true
runfile.ProcessCommand(c)
break
}
if !found {
color.Red("command \"%s\" not found.\n", request)
}
}
}
}
}