-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
202 lines (179 loc) · 3.63 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
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
package main
import (
"context"
"flag"
"fmt"
"log"
"net"
"net/http"
"os"
"strings"
)
type repositoriesList []string
func (r *repositoriesList) String() string {
return strings.Join(*r, ",")
}
func (r *repositoriesList) Set(value string) error {
*r = append(*r, value)
return nil
}
// Config represents the configuration of the CLI tool
type Config struct {
caseInsensitive bool
colorize bool
contextLinesAfter int
contextLinesBefore int
findInBody bool
findInFilename bool
fixedStrings bool
noPrintHeaders bool
noPrintLineNumber bool
numLines int
pattern string
printFilename bool
printTree bool
repositories repositoriesList
}
func initFlags() Config {
c := &Config{}
var contextLines int
flag.IntVar(
&contextLines,
"C",
0,
"Print num lines of leading and trailing context surrounding each match.",
)
flag.IntVar(
&c.contextLinesAfter,
"A",
0,
"Print num lines of trailing context after each match.",
)
flag.IntVar(
&c.contextLinesBefore,
"B",
0,
"Print num lines of trailing context before each match.",
)
flag.IntVar(
&c.numLines,
"c",
-1,
"Only a count of selected lines is written to standard output.",
)
flag.BoolVar(
&c.colorize,
"color",
true,
"Mark up the matching text in color",
)
flag.StringVar(
&c.pattern,
"e",
"",
"Specify a pattern used during the search of the input: an input line is"+
"selected if it matches any of the specified patterns.",
)
flag.BoolVar(
&c.fixedStrings,
"F",
false,
"Interpret pattern as a set of fixed strings",
)
flag.BoolVar(
&c.noPrintHeaders,
"h",
false,
"Never print filename headers (i.e. filenames) with output lines.",
)
flag.BoolVar(
&c.caseInsensitive,
"i",
false,
"Perform case insensitive matching.",
)
flag.BoolVar(
&c.noPrintLineNumber,
"N",
false,
"Do not precede each output line is by its relative line number in the file.",
)
flag.BoolVar(
&c.findInFilename,
"f",
false,
"Look in the names of files for matches (like find)",
)
flag.BoolVar(
&c.findInBody,
"b",
false,
"Look in the contents of files for matches (like grep)",
)
flag.BoolVar(
&c.printTree,
"t",
false,
"Print tree in addition to file path",
)
flag.Var(
&c.repositories,
"r",
"Repositories to search in. Specify -r multiple times to specify a list of repositories.",
)
var version bool
flag.BoolVar(&version, "v", false, "Print version and exit")
flag.Parse()
if version {
fmt.Printf("0.0.1\n")
os.Exit(0)
}
if contextLines > c.contextLinesAfter {
c.contextLinesAfter = contextLines
}
if contextLines > c.contextLinesBefore {
c.contextLinesBefore = contextLines
}
return *c
}
func main() {
config := initFlags()
if len(flag.Args()) == 0 {
os.Exit(0)
}
query := flag.Args()[0]
var host string
if os.Getenv("LIVEGREP_HOST") != "" {
host = os.Getenv("LIVEGREP_HOST")
} else {
host = "livegrep.com"
}
l := NewLivegrep(host)
if os.Getenv("LIVEGREP_USE_HTTPS") == "false" {
l.UseHTTPS = false
}
unixSocket := os.Getenv("LIVEGREP_UNIX_SOCKET")
if unixSocket != "" {
transport := &http.Transport{
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
return net.Dial("unix", unixSocket)
},
}
httpClient := &http.Client{
Transport: transport,
}
l.Client = httpClient
}
q := l.NewQuery(query)
q.Repositories = config.repositories
q.FoldCase = config.caseInsensitive
q.Regex = !config.fixedStrings
if len(config.pattern) > 0 {
q.Term = config.pattern
}
response, err := l.Query(q)
if err != nil {
log.Fatalln(err)
}
Print(config, q, response)
}