/
main.go
80 lines (64 loc) · 1.46 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
package main
import (
"flag"
"fmt"
"log"
"os"
)
var (
showV bool
showVersion bool
flagConfigPath string
shouldMakeFrcFile bool
shouldPrint bool
)
func init() {
flag.BoolVar(&showV, "v", false, "show version info and exit(0)")
flag.BoolVar(&showVersion, "version", false, "show version info and exit(0)")
flag.StringVar(&flagConfigPath, "config", "", "path to config file, defaults to ~/.frc")
flag.BoolVar(&shouldMakeFrcFile, "init", false, "Attempt to create config file.")
flag.BoolVar(&shouldPrint, "print", false, "Print command to stdout, do not run in separate process")
appendDefaultPaths()
}
func main() {
flag.Parse()
if showVersion || showV {
fmt.Println(VersionInfo())
os.Exit(0)
}
prependConfigPath(flagConfigPath)
if shouldMakeFrcFile {
err := createConfigFile()
if err != nil {
log.Fatal(err)
}
return
}
err := findAndParseConfigFile()
if err != nil {
log.Fatal(err)
}
// fmt.Println("\n", allTriplets)
// get the non flag args:
args := flag.Args()
if len(args) == 0 {
PrintMenu(allTriplets)
return
}
primaryArg := args[0]
// search for the command by shortcut:
t := FindTripletByShortcut(allTriplets, primaryArg)
if t != nil {
t.RunCommand()
return
}
// search for the command by number:
t = FindTripletByNumber(allTriplets, primaryArg)
if t != nil {
t.RunCommand()
return
}
// if not found,
fmt.Println("No shortcut or number:" + primaryArg)
PrintMenu(allTriplets)
}