-
Notifications
You must be signed in to change notification settings - Fork 0
/
root.go
86 lines (75 loc) · 1.86 KB
/
root.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
// Package cmd provides the root command for the vai CLI.
package cmd
import (
"os"
"runtime/debug"
"github.com/Noxsios/vai"
"github.com/charmbracelet/log"
"github.com/spf13/cobra"
)
var w map[string]string
var level string
var ver bool
var list bool
var rootCmd = &cobra.Command{
Use: "vai",
Short: "A simple task runner",
PersistentPreRunE: func(_ *cobra.Command, _ []string) error {
l, err := log.ParseLevel(level)
if err != nil {
return err
}
vai.SetLogLevel(l)
return nil
},
RunE: func(_ *cobra.Command, args []string) error {
logger := vai.Logger()
if ver {
bi, ok := debug.ReadBuildInfo()
if ok {
logger.Printf("%s", bi.Main.Version)
}
return nil
}
wf, err := vai.ReadAndValidate(vai.DefaultFileName)
if err != nil {
return err
}
if len(args) == 0 {
args = append(args, vai.DefaultTaskName)
}
with := make(vai.With)
for k, v := range w {
with[k] = v
}
if list {
logger.Print("Available:\n")
for k := range wf {
logger.Printf("- %s", k)
}
return nil
}
for _, call := range args {
if err := vai.Run(wf, call, with); err != nil {
return err
}
}
return nil
},
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
rootCmd.SilenceUsage = true
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}
func init() {
rootCmd.Flags().StringToStringVarP(&w, "with", "w", nil, "variables to pass to the called task(s)")
rootCmd.Flags().StringVarP(&level, "log-level", "l", "info", "log level")
rootCmd.Flags().BoolVarP(&ver, "version", "V", false, "print version")
rootCmd.Flags().BoolVarP(&vai.Force, "force", "F", false, "ignore checksum mismatch for cached remote files")
rootCmd.Flags().BoolVar(&list, "list", false, "list available tasks")
}