-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredoc.go
203 lines (160 loc) · 4.18 KB
/
redoc.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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package main
import (
"flag"
"fmt"
"os"
"sort"
"strings"
)
type Command struct {
Name string
Summary string
Arguments string
Group string
Since string
Description string
}
var (
// what to display for each command
description = flag.Bool("d", false, "display long description")
since = flag.Bool("s", false, "display since")
// general
colors = flag.Bool("c", true, "use colors")
listCommands = flag.Bool("lc", false, "list available Redis commands")
listGroups = flag.Bool("lg", false, "list available Redis groups")
)
func usage() {
fmt.Fprintf(os.Stderr, "usage: redoc [flags] [command|@group]\n")
flag.PrintDefaults()
os.Exit(2)
}
func (c *Command) printCommand() {
var formats map[string]string
if *colors {
formats = map[string]string{
"name": " \x1b[1m%s\x1b[0m \x1b[90m%s\x1b[0m\n",
"summary": " \x1b[38;5;31msummary:\x1b[0m %s\n",
"since": " \x1b[38;5;31msince:\x1b[0m %s\n",
"group": " \x1b[38;5;31mgroup:\x1b[0m %s\n",
"description": " \x1b[38;5;31mdescription:\x1b[0m\n\n\x1b[90m%s\x1b[0m\n",
}
} else {
formats = map[string]string{
"name": " %s %s\n",
"summary": " summary: %s\n",
"since": " since: %s\n",
"group": " group: %s\n",
"description": " description:\n\n%s\n",
}
}
fmt.Fprintf(os.Stdout, formats["name"], strings.ToUpper(c.Name), c.Arguments)
fmt.Fprintf(os.Stdout, formats["summary"], c.Summary)
if *since {
fmt.Fprintf(os.Stdout, formats["since"], c.Since)
}
fmt.Fprintf(os.Stdout, formats["group"], c.Group)
if *description {
fmt.Fprintf(os.Stdout, formats["description"], c.Description)
}
fmt.Fprintf(os.Stdout, "\n")
}
func printName(name string) bool {
if c, ok := Commands[name]; ok {
c.printCommand()
return ok
}
return false
}
func printCommands() {
var commands []string
for k := range Commands {
commands = append(commands, k)
}
sort.Strings(commands)
var format string
if *colors {
format = "%s\x1b[38;5;196m|\x1b[0m"
} else {
format = "%s "
}
for _, name := range commands {
fmt.Fprintf(os.Stdout, format, name)
}
fmt.Fprintf(os.Stdout, "\n")
}
func printGroup(name string) bool {
g := orderByGroup()
for k, v := range g {
if k == name {
printGrouped(map[string][]string{k: v})
}
}
return false
}
func printGroups() {
found := map[string]bool{}
var groups []string
for _, v := range Commands {
if _, ok := found[v.Group]; !ok {
found[v.Group] = true
groups = append(groups, v.Group)
}
}
sort.Strings(groups)
var format string
if *colors {
format = "%s\x1b[38;5;196m|\x1b[0m"
} else {
format = "%s "
}
for _, name := range groups {
fmt.Fprintf(os.Stdout, format, name)
}
fmt.Fprintf(os.Stdout, "\n")
}
func printGrouped(grouped map[string][]string) {
for _, v := range grouped {
for _, name := range v {
c := Commands[name]
c.printCommand()
}
}
}
func orderByGroup() map[string][]string {
grouped := make(map[string][]string)
for k, v := range Commands {
grouped[v.Group] = append(grouped[v.Group], k)
}
for _, v := range grouped {
sort.Strings(v)
}
return grouped
}
func main() {
flag.Usage = usage
flag.Parse()
args := flag.Args()
if *listGroups {
printGroups()
os.Exit(0)
}
if *listCommands {
printCommands()
os.Exit(0)
}
if len(args) > 0 {
typ := strings.ToLower(strings.Join(args, " "))
if strings.HasPrefix(typ, "@") && printGroup(typ[1:]) {
os.Exit(0)
}
if !printName(typ) {
if !printGroup(typ) {
os.Exit(1)
}
}
os.Exit(0)
}
g := orderByGroup()
printGrouped(g)
os.Exit(0)
}