-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
103 lines (97 loc) · 2.76 KB
/
Copy pathmain.go
File metadata and controls
103 lines (97 loc) · 2.76 KB
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
package main
import "fmt"
import "github.com/licensezero/cli/subcommands"
import "github.com/mitchellh/go-homedir"
import "os"
import "sort"
// Rev represents the current build revision. Set via ldflags.
var Rev string
var commands = map[string]*subcommands.Subcommand{
"backup": subcommands.Backup,
"buy": subcommands.Buy,
"bugs": subcommands.Bugs,
"contributing": subcommands.Contributing,
"identify": subcommands.Identify,
"import": subcommands.Import,
"license": subcommands.License,
"latest": subcommands.Latest,
"lock": subcommands.Lock,
"offer": subcommands.Offer,
"quote": subcommands.Quote,
"projects": subcommands.Projects,
"readme": subcommands.README,
"register": subcommands.Register,
"relicense": subcommands.Relicense,
"reprice": subcommands.Reprice,
"reset": subcommands.Reset,
"retract": subcommands.Retract,
"sponsor": subcommands.Sponsor,
"token": subcommands.Token,
"version": subcommands.Version,
"waive": subcommands.Waive,
"whoami": subcommands.WhoAmI,
}
func main() {
home, homeError := homedir.Dir()
if homeError != nil {
subcommands.Fail("Could not find home directory.")
}
cwd, cwdError := os.Getwd()
if cwdError != nil {
subcommands.Fail("Could not find working directory.")
}
paths := subcommands.Paths{Home: home, CWD: cwd}
arguments := os.Args
if len(arguments) > 1 {
subcommand := os.Args[1]
if value, ok := commands[subcommand]; ok {
if subcommand == "version" || subcommand == "latest" {
value.Handler([]string{Rev}, paths)
} else {
value.Handler(os.Args[2:], paths)
}
} else {
showUsage()
os.Exit(1)
}
} else {
showUsage()
os.Exit(0)
}
}
func showUsage() {
os.Stdout.WriteString("Manage License Zero projects and dependencies.\n\nSubcommands:\n")
buyer := map[string]*subcommands.Subcommand{}
seller := map[string]*subcommands.Subcommand{}
misc := map[string]*subcommands.Subcommand{}
for key, value := range commands {
switch value.Tag {
case "buyer":
buyer[key] = value
case "seller":
seller[key] = value
default:
misc[key] = value
}
}
listSubcommands("For Buyers", buyer)
listSubcommands("For Sellers", seller)
listSubcommands("Miscellaneous", misc)
}
func listSubcommands(header string, list map[string]*subcommands.Subcommand) {
os.Stdout.WriteString("\n " + header + ":\n\n")
longestSubcommand := 0
var names []string
for name := range list {
length := len(name) + 1
if length > longestSubcommand {
longestSubcommand = length
}
names = append(names, name)
}
sort.Strings(names)
for _, name := range names {
info := list[name]
fmt.Printf(" %-"+fmt.Sprintf("%d", longestSubcommand)+"s %s\n", name, info.Description)
}
}