-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.go
196 lines (187 loc) · 4.01 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
// SPDX-License-Identifier: MIT OR Unlicense
package main
import (
"github.com/spf13/cobra"
"os"
)
const (
Version = "1.1.1"
)
func main() {
//f, _ := os.Create("profile.pprof")
//pprof.StartCPUProfile(f)
//defer pprof.StopCPUProfile()
rootCmd := &cobra.Command{
Use: "cs",
Long: "code spelunker (cs) code search.\n" +
"Version " + Version + "\n" +
"Ben Boyter <ben@boyter.org>" +
"\n\n" +
"cs recursively searches the current directory using some boolean logic\n" +
"optionally combined with regular expressions.\n" +
"\n" +
"searches by default use AND boolean syntax for all terms\n" +
" - exact match using quotes \"find this\"\n" +
" - fuzzy match within 1 or 2 distance fuzzy~1 fuzzy~2\n" +
" - negate using NOT such as pride NOT prejudice\n" +
" - regex with toothpick syntax /pr[e-i]de/\n" +
"\n" +
"example search that uses all current functionality\n" +
" - darcy NOT collins wickham~1 \"ten thousand a year\" \"/pr[e-i]de/\"\n" +
"\n",
Version: Version,
Run: func(cmd *cobra.Command, args []string) {
SearchString = args
dirFilePaths = []string{"."}
// If there are arguments we want to print straight out to the console
// otherwise we should enter interactive tui mode
if HttpServer {
// start HTTP server
StartHttpServer()
} else if len(SearchString) != 0 {
NewConsoleSearch()
} else {
NewTuiSearch()
}
},
}
flags := rootCmd.PersistentFlags()
flags.StringVar(
&Address,
"address",
":8080",
"address and port to listen to in HTTP mode",
)
flags.BoolVarP(
&HttpServer,
"http-server",
"d",
false,
"start http server for search",
)
flags.BoolVar(
&IncludeBinaryFiles,
"binary",
false,
"set to disable binary file detection and search binary files",
)
flags.BoolVar(
&IgnoreIgnoreFile,
"no-ignore",
false,
"disables .ignore file logic",
)
flags.BoolVar(
&IgnoreGitIgnore,
"no-gitignore",
false,
"disables .gitignore file logic",
)
flags.Int64VarP(
&SnippetLength,
"snippet-length",
"n",
300,
"size of the snippet to display",
)
flags.Int64VarP(
&SnippetCount,
"snippet-count",
"s",
1,
"number of snippets to display",
)
flags.BoolVar(
&IncludeHidden,
"hidden",
false,
"include hidden files",
)
flags.StringSliceVarP(
&AllowListExtensions,
"include-ext",
"i",
[]string{},
"limit to file extensions (N.B. case sensitive) [comma separated list: e.g. go,java,js,C,cpp]",
)
flags.BoolVarP(
&FindRoot,
"find-root",
"r",
false,
"attempts to find the root of this repository by traversing in reverse looking for .git or .hg",
)
flags.StringSliceVar(
&PathDenylist,
"exclude-dir",
[]string{".git", ".hg", ".svn"},
"directories to exclude",
)
flags.BoolVarP(
&CaseSensitive,
"case-sensitive",
"c",
false,
"make the search case sensitive",
)
flags.StringVar(
&SearchTemplate,
"template-search",
"",
"path to search template for custom styling",
)
flags.StringVar(
&DisplayTemplate,
"template-display",
"",
"path to display template for custom styling",
)
flags.StringSliceVarP(
&LocationExcludePattern,
"exclude-pattern",
"x",
[]string{},
"file and directory locations matching case sensitive patterns will be ignored [comma separated list: e.g. vendor,_test.go]",
)
flags.BoolVar(
&IncludeMinified,
"min",
false,
"include minified files",
)
flags.IntVar(
&MinifiedLineByteLength,
"min-line-length",
255,
"number of bytes per average line for file to be considered minified",
)
flags.Int64Var(
&MaxReadSizeBytes,
"max-read-size-bytes",
1_000_000,
"number of bytes to read into a file with the remaining content ignored",
)
flags.StringVarP(
&Format,
"format",
"f",
"text",
"set output format [text, json, vimgrep]",
)
flags.StringVar(
&Ranker,
"ranker",
"bm25",
"set ranking algorithm [simple, tfidf, tfidf2, bm25]",
)
flags.StringVarP(
&FileOutput,
"output",
"o",
"",
"output filename (default stdout)",
)
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}