-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
68 lines (53 loc) · 1.28 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
package main
import (
"fmt"
"os"
"sort"
"strings"
"github.com/holedaemon/bot2/internal/cli"
"github.com/holedaemon/bot2/internal/cli/subcommands/bot"
"github.com/holedaemon/bot2/internal/cli/subcommands/web"
"github.com/holedaemon/bot2/internal/version"
)
// Code graciously borrowed from HortBot.
func main() {
args := os.Args[1:]
subcommands := make(map[string]cli.Command)
addCommand := func(cmd cli.Command) {
name := cmd.Name()
if subcommands[name] != nil {
panic("duplicate command " + name)
}
subcommands[name] = cmd
}
listAndExit := func() {
names := make([]string, 0, len(subcommands))
for name := range subcommands {
names = append(names, name)
}
sort.Strings(names)
fmt.Fprintln(os.Stderr, "Available subcommands:", strings.Join(names, ", "))
os.Exit(2)
}
addCommand(bot.Command())
addCommand(web.Command())
if len(args) == 0 {
fmt.Fprintln(os.Stderr, "Please specify a subcommand.")
listAndExit()
}
subcommand, args := args[0], args[1:]
if subcommand == "version" {
fmt.Println(version.Version())
return
}
if cmd := subcommands[subcommand]; cmd != nil {
cli.Run(cmd, args)
return
}
switch subcommand {
case "-h", "--help":
default:
fmt.Fprintln(os.Stderr, subcommand, "is not a valid subcommand.")
}
listAndExit()
}