-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
122 lines (110 loc) · 2.81 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
package main
import (
"errors"
"flag"
"fmt"
"io"
"log"
"os"
"strings"
)
const cmdName = "afa"
func main() {
initCommand, err := GetInitCommand()
if err != nil {
log.Fatal(fmt.Sprintf("Error: Failed to get init command. %v", err))
}
newCommand, err := GetNewCommand()
if err != nil {
log.Fatal(fmt.Sprintf("Error: Failed to get new command. %v", err))
}
sourceCommand, err := GetSourceCommand()
if err != nil {
log.Fatal(fmt.Sprintf("Error: Failed to get source command. %v", err))
}
resumeCommand, err := GetResumeCommand()
if err != nil {
log.Fatal(fmt.Sprintf("Error: Failed to get resume command. %v", err))
}
listCommand, err := GetListCommand()
if err != nil {
log.Fatal(fmt.Sprintf("Error: Failed to get list command. %v", err))
}
showCommand, err := GetShowCommand()
if err != nil {
log.Fatal(fmt.Sprintf("Error: Failed to get show command. %v", err))
}
cmds := []Command{
initCommand,
newCommand,
sourceCommand,
resumeCommand,
listCommand,
showCommand,
}
defaultSubCommandIdx := 0
for i, cmd := range cmds {
if cmd.Default() {
defaultSubCommandIdx = i
}
}
defaultSubCommand := []string{cmds[defaultSubCommandIdx].Name(), "-script"}
args := os.Args[1:]
if len(args) == 0 {
args = defaultSubCommand
}
flagSet := flag.NewFlagSet(cmdName, flag.ContinueOnError)
flagSet.Usage = func() {}
flagSetOutput := flagSet.Output()
flagSet.SetOutput(io.Discard)
ver := flagSet.Bool("version", false, "Display version")
if err := flagSet.Parse(args); err != nil {
if errors.Is(err, flag.ErrHelp) {
fmt.Fprintf(flagSetOutput, "Usage of %s:\n", cmdName)
flagSet.PrintDefaults()
for _, cmd := range cmds {
isDefaultSubCommand := ""
if cmd.Default() {
isDefaultSubCommand = " (default)"
}
fmt.Fprintf(flagSetOutput, " %s\n\t%s%s\n", cmd.Name(), cmd.Description(), isDefaultSubCommand)
}
os.Exit(0)
}
// No subcommand and unknown flag
args = append(defaultSubCommand, args...)
}
if *ver {
fmt.Fprintf(flagSetOutput, "%s v%s (rev:%s)\n", cmdName, version, revision)
os.Exit(0)
}
names := []string{}
for _, cmd := range cmds {
names = append(names, cmd.Name())
}
if len(os.Args) == 1 {
log.Fatal(subCommandNotFoundError(names))
}
subCommand := args[0]
match := false
for _, cmd := range cmds {
if cmd.Name() == subCommand {
if err := cmd.Parse(args[1:]); err != nil {
log.Fatal(fmt.Sprintf("Error: Failed to parse flags. %v", err))
}
if err := cmd.Run(); err != nil {
log.Fatal(fmt.Sprintf("Error: Failed to run. %v", err))
}
match = true
}
}
if !match {
log.Fatal(subCommandNotFoundError(names))
}
}
func subCommandNotFoundError(subcommands []string) error {
return fmt.Errorf(
"Error: No subcommand specified. Please provide one of the following subcommands: %s.",
strings.Join(subcommands, ", "),
)
}