-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.go
164 lines (144 loc) · 2.96 KB
/
cli.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"fmt"
"io"
"os"
"strings"
"github.com/zakuro9715/nzflag"
"github.com/zakuro9715/z/config"
"github.com/zakuro9715/z/runner"
)
var exit = os.Exit
var version = "unset" // Set via ldflags
const helpTextBase = `
z - Z Task runner
Usage:
z [OPTIONS] task... [ARGS]
`
func isHelpFlag(s string) bool {
return s == "-h" || s == "--help"
}
func showTextAndExit(code int, text string) {
if code == 0 {
fmt.Println(text)
} else {
fmt.Fprintln(os.Stderr, text)
}
exit(code)
}
func fprintTasks(w io.Writer, tasks map[string]*config.Task) {
fmt.Fprintln(w, "Tasks:")
maxNameLen := 0
for k := range tasks {
if len(k) > maxNameLen {
maxNameLen = len(k)
}
}
for k, t := range tasks {
fmt.Fprintf(w,
" %v%v - %v\n",
k,
strings.Repeat(" ", maxNameLen-len(k)),
t.Description,
)
}
}
func fprintHelp(w io.Writer, config *config.Config) {
var sb strings.Builder
sb.WriteString(helpTextBase)
if config != nil {
sb.WriteString("\n")
fprintTasks(&sb, config.Tasks)
}
fmt.Fprintln(w, sb.String())
}
func fprintTaskHelp(w io.Writer, task *config.Task) {
var sb strings.Builder
sb.WriteString(task.Description)
if len(task.Tasks) > 0 {
fprintTasks(&sb, task.Tasks)
}
fmt.Fprint(w, sb.String())
}
func fprintVersion(w io.Writer) {
fmt.Fprintf(w, "z task runner %v\n", version)
}
func realMain(args []string) int {
i := 0
configPath := "z.yaml"
if p, ok := os.LookupEnv("ZCONFIG"); ok {
configPath = p
}
nzargs := (&nzflag.App{
FlagOption: map[string]nzflag.FlagOption{
"c": nzflag.HasValue,
"config": nzflag.HasValue,
},
}).Normalize(args)
helpFlag := false
unknownFlag := false
for ; i < len(nzargs); i++ {
arg := nzargs[i]
if arg.Type() != nzflag.TypeFlag {
break
}
switch {
case isHelpFlag(arg.String()):
helpFlag = true
case arg.Flag().Name == "v" || arg.Flag().Name == "--version":
fprintVersion(os.Stdout)
case arg.Flag().Name == "c" || arg.Flag().Name == "config":
configPath = arg.Flag().Values[0]
default: // unknow flag
unknownFlag = true
}
}
config, err := config.LoadConfig(configPath)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return 1
}
if unknownFlag {
fprintHelp(os.Stderr, config)
return 0
}
if helpFlag {
fprintHelp(os.Stdout, config)
return 0
}
task := config.FindTask(config.Default)
if i >= len(nzargs) {
if len(config.Default) == 0 {
fprintHelp(os.Stderr, config)
return 1
}
} else {
fullName := ""
for ; i < len(nzargs); i++ {
arg := nzargs[i]
if isHelpFlag(nzargs[i].String()) {
fprintTaskHelp(os.Stdout, task)
return 0
}
if arg.String() == "--" {
i++
break
}
if len(fullName) > 0 {
fullName += "."
}
fullName += arg.String()
newTask := config.FindTask(fullName)
if newTask == nil {
break
}
task = newTask
}
}
taskArgs := make([]string, len(nzargs[i:]))
for i, arg := range nzargs[i:] {
taskArgs[i] = arg.String()
}
runner.New(task).Run(taskArgs)
return 0
}