-
Notifications
You must be signed in to change notification settings - Fork 19
/
main.go
143 lines (128 loc) · 3 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
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
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"os"
"regexp"
"strings"
)
var (
verbose = flag.Bool("v", false, "Full documentation ( default is a truncated summary )")
fuzzy = flag.Bool("f", false, "Fuzzy search (search with go regexp syntax)")
)
func lookup(item string) (s string, ok bool) {
s, ok = data[item]
if !ok {
return
}
if strings.HasPrefix(s, "-R:") {
// Follow the redirect entry
return lookup(strings.TrimPrefix(s, "-R:"))
}
return
}
func summary(item string) string {
scanner := bufio.NewScanner(strings.NewReader(item))
var output bytes.Buffer
scanner.Scan()
scanner.Scan()
output.WriteString(strings.TrimSuffix(scanner.Text(), ":"))
output.WriteString("\n\n")
for scanner.Scan() {
if strings.HasPrefix(scanner.Text(), "Description:") {
break
}
}
if scanner.Text() == "" {
return output.String()
}
output.WriteString(scanner.Text())
output.WriteString("\n")
for i := 15; i > 0; i-- {
// Output up to the first 15 lines of the first paragraph of the
// Description section
if scanner.Scan() {
// If we have hit the Operation section we have gone too far.
// Should be impossible, because we would have broken on the blank
// line.
if strings.HasPrefix(scanner.Text(), "Operation:") || len(scanner.Text()) == 0 {
break
}
output.WriteString(scanner.Text())
output.WriteString("\n")
}
if i == 1 {
output.WriteString("\n[... use -v to see full output ...]\n")
}
}
return output.String()
}
func getHeader(item string) string {
s, _ := lookup(item)
scanner := bufio.NewScanner(strings.NewReader(s))
scanner.Scan()
scanner.Scan()
header := strings.TrimSuffix(scanner.Text(), ":")
if !strings.Contains(header, " - ") {
fmt.Fprintf(os.Stderr, "[BUG] unexpected entry data for %s\n", header)
os.Exit(1)
}
return header
}
func fuzzySearch(item string) []string {
results := []string{}
if !strings.HasPrefix(item, "(?") {
item = "(?i)" + item
}
r, err := regexp.Compile(item)
if err != nil {
return results
}
for k, v := range data {
if r.MatchString(k) {
if strings.HasPrefix(v, "-R:") {
results = append(results, fmt.Sprintf("%s -> %s", k, getHeader(k)))
continue
}
results = append(results, getHeader(k))
}
}
return results
}
func main() {
flag.Parse()
if len(flag.Args()) == 0 {
flag.Usage()
os.Exit(1)
}
query := flag.Arg(0)
out, ok := lookup(strings.ToUpper(query))
if !ok {
// No exact match, try a fuzzy search
if *fuzzy {
matches := fuzzySearch(query)
if len(matches) == 0 {
fmt.Printf("%s - no documentation found.\n", query)
os.Exit(0)
}
fmt.Printf("Fuzzy matches for \"%s\" (%d):\n", query, len(matches))
for _, match := range matches {
fmt.Println(match)
}
os.Exit(0)
}
fmt.Printf("\n%s - no documentation found.\n", query)
os.Exit(0)
}
if !strings.Contains(out, "\nDescription:") {
fmt.Fprintf(os.Stderr, "[BUG] unexpected entry data for %s\n", query)
os.Exit(1)
}
if *verbose {
fmt.Println(out)
os.Exit(0)
}
fmt.Println(summary(out))
}