-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.go
More file actions
228 lines (195 loc) · 5.78 KB
/
main.go
File metadata and controls
228 lines (195 loc) · 5.78 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package main
import (
"context"
_ "embed"
"flag"
"fmt"
"log"
"os"
"os/signal"
"runtime/debug"
"strings"
"syscall"
"github.com/kitproj/kit/internal"
"github.com/kitproj/kit/internal/types"
"sigs.k8s.io/yaml"
)
func init() {
log.SetOutput(os.Stdout)
log.SetFlags(0)
}
func main() {
help := false
printVersion := false
workingDir := "."
configFile := ""
tasksToSkip := ""
port := -1 // -1 means unspecified, 0 means disabled, >0 means specified
openBrowser := false
rewrite := false
completion := ""
flag.BoolVar(&help, "h", false, "print help and exit")
flag.BoolVar(&printVersion, "v", false, "print version and exit")
flag.StringVar(&workingDir, "C", ".", "working directory (default current directory)")
flag.StringVar(&configFile, "f", "tasks.yaml", "config file (default tasks.yaml)")
flag.StringVar(&tasksToSkip, "s", "", "tasks to skip (comma separated)")
flag.IntVar(&port, "p", port, "port to start UI on (default 3000, zero disables)")
flag.BoolVar(&openBrowser, "b", false, "open the UI in the browser (default false)")
flag.BoolVar(&rewrite, "w", false, "rewrite the config file")
flag.StringVar(&completion, "completion", "", "generate shell completion script (bash, zsh, fish)")
flag.Parse()
taskNames := flag.Args()
if help {
flag.Usage()
os.Exit(0)
}
if printVersion {
info, _ := debug.ReadBuildInfo()
fmt.Printf("%v\n", info.Main.Version)
os.Exit(0)
}
if completion != "" {
if err := printCompletion(completion, configFile); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%s\n", err.Error())
os.Exit(1)
}
os.Exit(0)
}
if err := os.Chdir(workingDir); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "failed to change to directory %s: %v\n", workingDir, err)
os.Exit(1)
}
err := func() error {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM, syscall.SIGHUP)
defer cancel()
wf := &types.Workflow{}
in, err := os.ReadFile(configFile)
if err != nil {
return fmt.Errorf("failed to read %s: %w", configFile, err)
}
if err = yaml.UnmarshalStrict(in, wf); err != nil {
return fmt.Errorf("failed to parse %s: %w", configFile, err)
}
if rewrite {
out, err := yaml.Marshal(wf)
if err != nil {
return fmt.Errorf("failed to marshal %s: %w", configFile, err)
}
return os.WriteFile(configFile, out, 0644)
}
// if wf.Port is specified, use that, unless the user has specified a port on the command line
if port == -1 {
if wf.Port != nil {
port = int(*wf.Port)
} else {
port = 3000 // default port
}
}
// split the tasks on comma, but don't end up with a single entry of ""
split := strings.Split(tasksToSkip, ",")
if len(split) == 1 && split[0] == "" {
split = []string{}
}
if len(taskNames) == 0 {
for taskName, task := range wf.Tasks {
if task.Default {
taskNames = []string{taskName}
break
}
}
}
return internal.RunSubgraph(
ctx,
cancel,
port,
openBrowser,
log.Default(),
wf,
taskNames,
split,
)
}()
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%s\n", err.Error())
os.Exit(1)
}
}
func printCompletion(shell, configFile string) error {
switch shell {
case "bash":
fmt.Print(bashCompletion(configFile))
case "zsh":
fmt.Print(zshCompletion(configFile))
case "fish":
fmt.Print(fishCompletion(configFile))
default:
return fmt.Errorf("unsupported shell: %s (supported: bash, zsh, fish)", shell)
}
return nil
}
func bashCompletion(configFile string) string {
return fmt.Sprintf(`_kit_completions() {
local cur="${COMP_WORDS[COMP_CWORD]}"
local tasks=""
if [[ -f "%s" ]]; then
tasks=$(grep -E '^ [a-zA-Z0-9_-]+:\s*$' "%s" 2>/dev/null | sed 's/:.*//' | tr -d ' ' | tr '\n' ' ')
fi
if [[ "${cur}" == -* ]]; then
COMPREPLY=($(compgen -W "-h -v -C -f -s -p -b -w --completion" -- "${cur}"))
else
COMPREPLY=($(compgen -W "${tasks}" -- "${cur}"))
fi
}
complete -F _kit_completions kit
`, configFile, configFile)
}
func zshCompletion(configFile string) string {
return fmt.Sprintf(`#compdef kit
_kit() {
local -a tasks
local -a opts
opts=(
'-h[print help and exit]'
'-v[print version and exit]'
'-C[working directory]:directory:_files -/'
'-f[config file]:file:_files'
'-s[tasks to skip]:tasks:'
'-p[port to start UI on]:port:'
'-b[open the UI in the browser]'
'-w[rewrite the config file]'
'--completion[generate shell completion script]:shell:(bash zsh fish)'
)
if [[ -f "%s" ]]; then
tasks=(${(f)"$(grep -E '^ [a-zA-Z0-9_-]+:\s*$' "%s" 2>/dev/null | sed 's/:.*//' | tr -d ' ')"})
fi
_arguments -s $opts '*:task:'"(${tasks[*]})"
}
# don't run the completion function when being source-ed or eval-ed
if [ "$funcstack[1]" = "_kit" ]; then
_kit
fi
# register the completion function (requires compinit to have been run)
if [[ -n ${_comps+x} ]]; then
compdef _kit kit
fi
`, configFile, configFile)
}
func fishCompletion(configFile string) string {
return fmt.Sprintf(`function __fish_kit_tasks
if test -f "%s"
grep -E '^ [a-zA-Z0-9_-]+:\s*$' "%s" 2>/dev/null | sed 's/:.*//' | string trim
end
end
complete -c kit -f
complete -c kit -s h -d 'print help and exit'
complete -c kit -s v -d 'print version and exit'
complete -c kit -s C -d 'working directory' -r -a '(__fish_complete_directories)'
complete -c kit -s f -d 'config file' -r -F
complete -c kit -s s -d 'tasks to skip'
complete -c kit -s p -d 'port to start UI on'
complete -c kit -s b -d 'open the UI in the browser'
complete -c kit -s w -d 'rewrite the config file'
complete -c kit -l completion -d 'generate shell completion script' -r -a 'bash zsh fish'
complete -c kit -n 'not string match -q -- "-*" (commandline -ct)' -a '(__fish_kit_tasks)'
`, configFile, configFile)
}