-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.go
More file actions
136 lines (116 loc) · 3.6 KB
/
main.go
File metadata and controls
136 lines (116 loc) · 3.6 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package main
import (
"bytes"
"fmt"
"os"
"runtime"
"strings"
"github.com/benmoss/lager-cli/chug"
"github.com/benmoss/lager-cli/common"
"github.com/onsi/say"
)
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
commandGroups := []common.CommandGroup{
common.CommandGroup{
Name: "Chug",
Description: `Commands to prettify lager logs
Note: A chug-formatted time looks like 02/01/06 15:04:05.99 MST
`,
Commands: []common.Command{
chug.ChugCommand(),
chug.ServeChugCommand(),
chug.UnifyChugCommand(),
},
},
}
if len(os.Args) == 1 || os.Args[1] == "help" {
usage(commandGroups)
os.Exit(1)
}
if os.Args[1] == "completions" {
completions(commandGroups)
os.Exit(0)
}
for _, commandGroup := range commandGroups {
for _, command := range commandGroup.Commands {
if command.Name == os.Args[1] {
command.FlagSet.Parse(os.Args[2:])
command.Run(command.FlagSet.Args())
os.Exit(0)
}
}
}
say.Println(0, say.Red("Unkown command: %s", os.Args[1]))
usage(commandGroups)
}
func completions(commandGroups []common.CommandGroup) {
availableCommands := []string{}
for _, commands := range commandGroups {
for _, command := range commands.Commands {
availableCommands = append(availableCommands, command.Name)
}
}
out := fmt.Sprintf(`
function _chug() {
local cur prev commands
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
commands="%s"
if [[ "${COMP_CWORD}" == "1" ]] ; then
COMPREPLY=( $(compgen -W "${commands} help completions" -- ${cur}) );
elif [[ "${prev}" == "help" ]] ; then
COMPREPLY=( $(compgen -W "${commands}" -- ${cur}) );
else
COMPREPLY=( $(compgen -f ${cur}) );
fi
return 0
}
complete -F _chug chug
`, strings.Join(availableCommands, " "))
say.Println(0, out)
}
func usage(commandGroups []common.CommandGroup) {
if len(os.Args) > 2 {
matcher := strings.ToLower(os.Args[2])
for _, commandGroup := range commandGroups {
if strings.HasPrefix(strings.ToLower(commandGroup.Name), matcher) {
usageForCommandGroup(commandGroup, true)
return
}
for _, command := range commandGroup.Commands {
if strings.HasPrefix(strings.ToLower(command.Name), matcher) {
usageForCommand(0, command, true)
return
}
}
}
say.Fprintln(os.Stderr, 0, say.Red("Unknown command: %s", os.Args[2]))
}
say.Fprintln(os.Stderr, 0, "%s", say.Cyan("Help and Autocompletion"))
say.Fprintln(os.Stderr, 0, strings.Repeat("-", len("Help and Autocompletion")))
say.Fprintln(os.Stderr, 1, "%s %s", say.Green("help"), say.LightGray("[command] - Show this help, or detailed help for the passed in command"))
say.Fprintln(os.Stderr, 1, "%s %s", say.Green("completions"), say.LightGray("Generate BASH Completions for chug"))
say.Fprintln(os.Stderr, 0, "")
for _, commandGroup := range commandGroups {
usageForCommandGroup(commandGroup, false)
say.Println(0, "")
}
}
func usageForCommandGroup(commandGroup common.CommandGroup, includeFlags bool) {
say.Fprintln(os.Stderr, 0, "%s - %s", say.Cyan(commandGroup.Name), say.LightGray(commandGroup.Description))
say.Fprintln(os.Stderr, 0, strings.Repeat("-", len(commandGroup.Name)+3+len(commandGroup.Description)))
for _, command := range commandGroup.Commands {
usageForCommand(1, command, includeFlags)
}
}
func usageForCommand(indentation int, command common.Command, includeFlags bool) {
say.Fprintln(os.Stderr, indentation, "%s %s", say.Green(command.Name), say.LightGray(command.Description))
if includeFlags {
buffer := &bytes.Buffer{}
command.FlagSet.SetOutput(buffer)
command.FlagSet.PrintDefaults()
say.Fprintln(os.Stderr, indentation, buffer.String())
}
}