-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
384 lines (348 loc) · 9.27 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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
package main
import (
"embed"
"fmt"
"os"
"regexp"
"strings"
"github.com/fasibio/gomake/command"
"github.com/fasibio/gomake/interpreter"
nearfinder "github.com/fasibio/gomake/nearFinder"
"github.com/urfave/cli/v2"
)
const (
MakeFileCli = "makefile"
CommandCli = "command"
ExecuterCli = "executer"
App = "GOMAKE"
DryRunCli = "dry-run"
VarsCli = "var"
ShellAutocompleteCli = "shell"
PersistAutocompleteCli = "persist"
)
const (
GomakeDefaultFile = "gomake.yml"
)
func getFlagEnvByFlagName(flagName string) string {
return fmt.Sprintf("%s_%s", App, strings.ToUpper(flagName))
}
var VariableFlagRegex *regexp.Regexp
func init() {
VariableFlagRegex = regexp.MustCompile(`^[0-9aA-zZ]*=[0-9,aA-zZ]*$`)
}
//go:embed autocomplete/*
var autocompleteFiles embed.FS
//go:embed gomake_ini.yml
var gomakeIni []byte
func main() {
runner := Runner{
cmdHandler: command.NewCommandHandler(App),
}
app1 := &cli.App{
Usage: "A helm like makefile",
EnableBashCompletion: true,
CommandNotFound: runner.CommandNotFound,
Flags: []cli.Flag{
&cli.PathFlag{
Name: MakeFileCli,
Aliases: []string{"f"},
EnvVars: []string{getFlagEnvByFlagName(MakeFileCli)},
Value: GomakeDefaultFile,
Usage: "gomake file to use",
},
&cli.StringFlag{
Name: ExecuterCli,
Aliases: []string{"sh"},
EnvVars: []string{getFlagEnvByFlagName(ExecuterCli)},
Value: "/bin/sh",
Usage: "Shell to execute gomakefile config",
},
},
Commands: []*cli.Command{
{
Name: "autocomplete",
Usage: "Set Autocomplete helper stuff to current shell session",
Action: runner.Autocomplete,
Flags: []cli.Flag{
&cli.StringFlag{
Name: ShellAutocompleteCli,
EnvVars: []string{"SHELL"},
Usage: "Shell inside this terminal",
},
&cli.BoolFlag{
Name: PersistAutocompleteCli,
Aliases: []string{"p"},
EnvVars: []string{getFlagEnvByFlagName(PersistAutocompleteCli)},
Usage: "To make autocomplete for this programm persist (Linux only)",
},
},
},
{
Name: "init",
Usage: fmt.Sprintf("Crate a starter %s to current dir", GomakeDefaultFile),
Action: runner.Init,
},
{
Name: "ls",
Usage: "List all commands described at gomake yaml file",
Action: runner.List,
Before: runner.Before,
},
{
ArgsUsage: "{executed command name}",
Name: "run",
Usage: fmt.Sprintf("Run commands from %s file", GomakeDefaultFile),
BashComplete: runner.RunBashComplete,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: DryRunCli,
EnvVars: []string{getFlagEnvByFlagName(DryRunCli)},
Value: false,
Usage: fmt.Sprintf("Only show template paresed %s but not execute it", GomakeDefaultFile),
},
&cli.StringSliceFlag{
Name: VarsCli,
Aliases: []string{"v"},
EnvVars: []string{getFlagEnvByFlagName(VarsCli)},
Action: runner.ExtraVariables,
},
},
Action: runner.Run,
Before: runner.RunBefore,
},
{
ArgsUsage: "{executed command name}",
Name: "srun",
Usage: fmt.Sprintf("Run commands from %s file but it run all commands are inside the given stage and run this in parallel", GomakeDefaultFile),
BashComplete: runner.SRunBashComplete,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: DryRunCli,
EnvVars: []string{getFlagEnvByFlagName(DryRunCli)},
Value: false,
Usage: fmt.Sprintf("Only show template paresed %s but not execute it", GomakeDefaultFile),
},
&cli.StringSliceFlag{
Name: VarsCli,
Aliases: []string{"v"},
EnvVars: []string{getFlagEnvByFlagName(VarsCli)},
Action: runner.ExtraVariables,
},
},
Action: runner.SRun,
Before: runner.RunBefore,
},
},
}
if err := app1.Run(os.Args); err != nil {
fmt.Println("Error: ", err)
}
}
type Runner struct {
cmdHandler command.CommandHandler
interpreter interpreter.Interpreter
}
func (r *Runner) ExtraVariables(ctx *cli.Context, s []string) error {
for _, a := range s {
if ok := VariableFlagRegex.MatchString(a); !ok {
return fmt.Errorf("Variable does not match: %s only \"key=value\" are allowed ", a)
}
splittetVar := strings.SplitN(a, "=", 2)
r.interpreter.ExtraVariables[splittetVar[0]] = splittetVar[1]
}
return nil
}
func (r *Runner) RunBefore(c *cli.Context) error {
r.Before(c)
neededCommand := c.Args().Get(0)
dryRun := c.Bool(DryRunCli)
if neededCommand == "" {
return fmt.Errorf("need name of executing command")
}
r.interpreter.ExecuteCommand = neededCommand
r.interpreter.DryRun = dryRun
return nil
}
func (r *Runner) CommandNotFound(c *cli.Context, cmd string) {
possibleCommands := []string{}
for _, v := range c.App.Commands {
possibleCommands = append(possibleCommands, v.Names()...)
}
fmt.Printf("Command \"%s\" not found did you mean \n%s\n", cmd, nearfinder.ClosestMatch(cmd, possibleCommands, 1))
}
func (r *Runner) Before(c *cli.Context) error {
makefile := c.Path(MakeFileCli)
executer := c.String(ExecuterCli)
f, err := os.ReadFile(makefile)
if err != nil {
return err
}
r.interpreter = interpreter.NewInterpreter(App, "", executer, c.Bool(DryRunCli), r.cmdHandler, f)
return nil
}
func isFlagAtUseList(used []string, test string) bool {
for _, v := range used {
if v == test {
return true
}
}
return false
}
func (r *Runner) SRunBashComplete(c *cli.Context) {
r.Before(c)
r.RunBefore(c)
autoCompleteHelp := make([]string, 0)
for _, cs := range c.App.Commands {
if cs.Name == "srun" {
for _, csf := range cs.Flags {
for _, csfn := range csf.Names() {
if csfn == "help" || csfn == "h" {
continue
}
if csfn == DryRunCli {
if isFlagAtUseList(c.FlagNames(), csfn) {
continue
}
}
minusStr := "-"
if len(csfn) > 1 {
minusStr = "--"
}
autoCompleteHelp = append(autoCompleteHelp, fmt.Sprintf("%s%s", minusStr, csfn))
}
}
}
}
list, _, _, err := r.interpreter.GetStageMap()
if err != nil {
return
}
for k := range list {
autoCompleteHelp = append(autoCompleteHelp, k)
}
for _, v := range autoCompleteHelp {
fmt.Println(v)
}
}
func (r *Runner) RunBashComplete(c *cli.Context) {
r.Before(c)
r.RunBefore(c)
autoCompleteHelp := make([]string, 0)
for _, cs := range c.App.Commands {
if cs.Name == "run" {
for _, csf := range cs.Flags {
for _, csfn := range csf.Names() {
if csfn == "help" || csfn == "h" {
continue
}
if csfn == DryRunCli {
if isFlagAtUseList(c.FlagNames(), csfn) {
continue
}
}
minusStr := "-"
if len(csfn) > 1 {
minusStr = "--"
}
autoCompleteHelp = append(autoCompleteHelp, fmt.Sprintf("%s%s", minusStr, csfn))
}
}
}
}
list, err := r.interpreter.GetMakeScripts()
if err != nil {
return
}
for k := range list {
autoCompleteHelp = append(autoCompleteHelp, k)
}
for _, v := range autoCompleteHelp {
fmt.Println(v)
}
}
func (r *Runner) Autocomplete(c *cli.Context) error {
persist := c.Bool(PersistAutocompleteCli)
shell := c.String(ShellAutocompleteCli)
if strings.HasSuffix(shell, "zsh") {
err := os.Setenv("PROG", c.App.Name)
if err != nil {
return err
}
f, err := autocompleteFiles.ReadFile("autocomplete/zsh_autocomplete")
if err != nil {
return err
}
if persist {
path := fmt.Sprintf("/etc/bash_completion.d/%s", c.App.Name)
err = os.WriteFile(path, f, 0644)
if err != nil {
return err
}
fmt.Printf("Autocomplete was added to %s \n reload shell to activate or \n export PROG=%s \n source %s", path, c.App.Name, path)
} else {
err = os.WriteFile("/usr/tmp/zsh_autocomplete", f, 0644)
if err != nil {
return err
}
fmt.Printf("Execute Command \n export PROG=%s \n source /usr/tmp/zsh_autocomplete", c.App.Name)
}
}
if strings.HasSuffix(shell, "bash") {
err := os.Setenv("PROG", c.App.Name)
if err != nil {
return err
}
f, err := autocompleteFiles.ReadFile("autocomplete/bash_autocomplete")
if err != nil {
return err
}
err = os.WriteFile("/usr/tmp/bash_autocomplete", f, 0644)
if err != nil {
return err
}
fmt.Printf("Execute Command \n export PROG=%s \n source /usr/tmp/bash_autocomplete", c.App.Name)
}
return nil
}
func (r *Runner) Run(c *cli.Context) error {
return r.interpreter.Run()
}
func (r *Runner) SRun(c *cli.Context) error {
return r.interpreter.SRun()
}
func (r *Runner) Init(c *cli.Context) error {
_, err := os.Stat(GomakeDefaultFile)
if err != nil {
f, err := os.Create(GomakeDefaultFile)
defer f.Close()
if err != nil {
return err
}
_, err = f.Write(gomakeIni)
if err != nil {
return err
}
fmt.Printf("%s was created\nyou can execute with \ngomake run run", GomakeDefaultFile)
return nil
}
return fmt.Errorf("%s already exist", GomakeDefaultFile)
}
func (r *Runner) List(c *cli.Context) error {
list, err := r.interpreter.GetMakeScripts()
if err != nil {
return err
}
fmt.Println("List of executed Commands (for run):")
for k := range list {
fmt.Printf("%s %s \n", k, list[k].Doc)
}
fmt.Println("\nList of executed Stages (for srun):")
stages, _, _, err := r.interpreter.GetStageMap()
if err != nil {
return err
}
for k := range stages {
fmt.Println(k)
}
return nil
}