-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.go
105 lines (93 loc) · 1.85 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
package main
import (
"fmt"
"os"
"runtime"
"runtime/pprof"
"strings"
"text/tabwriter"
"github.com/BurntSushi/ty/fun"
)
var commands = []*command{
cmdFull,
cmdShort,
cmdLoad,
cmdSearch,
cmdSize,
cmdWrite,
cmdRename,
cmdFtp,
}
func usage() {
pef("goim is a tool for interacting with a local copy of IMDB.\n")
pef("Usage:\n\n goim {command} [flags] [arguments]\n")
pef("Use 'goim help {command}' for more details on {command}.\n")
fun.Sort(func(c1, c2 *command) bool { return c1.name < c2.name }, commands)
pef("A list of the main commands:\n")
tabw := tabwriter.NewWriter(os.Stderr, 0, 0, 4, ' ', 0)
for _, c := range commands {
if c.name == "ftp" || c.other {
continue
}
fmt.Fprintf(tabw, " %s\t%s\n", c.name, c.shortHelp)
}
tabw.Flush()
pef("")
pef("A list of other commands:\n")
for _, c := range commands {
if c.name == "ftp" || !c.other {
continue
}
fmt.Fprintf(tabw, " %s\t%s\n", c.name, c.shortHelp)
}
tabw.Flush()
pef("")
os.Exit(1)
}
func main() {
var cmd string
var help bool
if len(os.Args) < 2 {
usage()
} else if strings.TrimLeft(os.Args[1], "-") == "help" {
if len(os.Args) < 3 {
usage()
} else {
cmd = os.Args[2]
help = true
}
} else {
cmd = os.Args[1]
}
for _, c := range commands {
if c.name == cmd {
c.setCommonFlags()
if c.addFlags != nil {
c.addFlags(c)
}
if help {
c.showHelp()
} else {
c.flags.Usage = c.showUsage
c.flags.Parse(os.Args[2:])
if flagCpu < 1 {
flagCpu = 1
}
runtime.GOMAXPROCS(flagCpu)
if len(flagCpuProfile) > 0 {
f := createFile(flagCpuProfile)
pprof.StartCPUProfile(f)
defer f.Close()
defer pprof.StopCPUProfile()
}
if !c.run(c) {
os.Exit(1)
}
return
}
}
}
pef("Unknown command '%s'. Run 'goim help' for a list of "+
"available commands.", cmd)
os.Exit(1)
}