-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
110 lines (92 loc) · 2.13 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
package main
import (
"flag"
"fmt"
"log"
"os"
"strings"
"github.com/mgutz/ansi"
"github.com/the-obsidian/mc/rollbar"
"github.com/the-obsidian/mc/term"
)
var commands = []*Command{
cmdInstall,
//cmdVersion,
cmdHelp,
//helpCommands,
//helpAbout,
// unlisted
//cmdUpdate,
}
func main() {
log.SetFlags(0)
args := os.Args[1:]
if len(args) < 1 || strings.IndexRune(args[0], '-') == 0 {
printUsageTo(os.Stderr)
os.Exit(2)
}
// Perform updates as early as possible
if args[0] == cmdUpdate.Name() {
cmdUpdate.Run(cmdUpdate, args)
return
} else if updater != nil {
defer updater.backgroundRun()
}
if !term.IsANSI(os.Stdout) {
ansi.DisableColors(true)
}
for _, cmd := range commands {
if cmd.Name() != args[0] || cmd.Run == nil {
continue
}
defer recoverPanic()
cmd.Flag.SetDisableDuplicates(true) // allow duplicate flag options
cmd.Flag.Usage = func() {
cmd.PrintUsage()
}
if cmd.NeedsServer {
if exists, err := fileExists("server.yml"); err != nil || !exists {
printFatal("server.yml not found - is this a server directory?")
}
}
if err := cmd.Flag.Parse(args[1:]); err == flag.ErrHelp {
cmdHelp.Run(cmdHelp, args[:1])
} else if err != nil {
printError(err.Error())
os.Exit(2)
}
cmd.Run(cmd, cmd.Flag.Args())
return
}
fmt.Fprintf(os.Stderr, "Unknown command: %s\n", args[0])
if g := suggest(args[0]); len(g) > 0 {
fmt.Fprintf(os.Stderr, "Possible alternatives: %v\n", strings.Join(g, " "))
}
fmt.Fprintf(os.Stderr, "Run 'mc help' for usage.\n")
os.Exit(2)
}
var rollbarClient = &rollbar.Client{
AppName: "mc",
AppVersion: Version,
Endpoint: "https://api.rollbar.com/api/1/item/",
Token: "c0fd71dc4a724934b0826c73ef3cd269",
}
func recoverPanic() {
if Version == "dev" {
return
}
if rec := recover(); rec != nil {
message := ""
switch rec := rec.(type) {
case error:
message = rec.Error()
default:
message = fmt.Sprintf("%v", rec)
}
if err := rollbarClient.Report(message); err != nil {
printError("reporting crash failed: %s", err.Error())
panic(err)
}
printFatal("mc encountered and reported an internal client issue")
}
}