-
Notifications
You must be signed in to change notification settings - Fork 0
/
arg.go
329 lines (262 loc) · 11.1 KB
/
arg.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package main
import (
"fmt"
"os"
"runtime"
"runtime/debug"
"sort"
"strings"
"github.com/bayashi/xfg/internal/xfglangxt"
flag "github.com/spf13/pflag"
)
const (
cmdName string = "xfg"
XFG_RC_FILE string = ".xfgrc"
defaultGroupSeparator string = "--"
defaultIndent string = " "
supportTypes = "directory (d), symlink (l), executable (x), empty (e), socket (s), pipe (p), block-device (b), char-device (c)"
)
var (
version = ""
installFrom = "Source"
)
// runtime options
type optionsExtra struct {
actualAfterContextLines uint32
actualBeforeContextLines uint32
withAfterContextLines bool
withBeforeContextLines bool
runWithNoArg bool
onlyMatchContent bool
}
type options struct {
SearchPath []string `toml:"path"`
SearchGrep []string `toml:"grep"`
SearchStart []string `toml:"start"`
SearchPathRe []string `toml:"path-regexp"`
SearchGrepRe []string `toml:"grep-regexp"`
GroupSeparator string `toml:"gourp-separator"`
Indent string `toml:"indent"`
ColorPathBase string `toml:"color-path-base"`
ColorPath string `toml:"color-path"`
ColorContent string `toml:"color-conetnt"`
XfgIgnoreFile string `toml:"xfgignore-file"`
Ignore []string `toml:"ignore"`
Type string `toml:"Type"`
Lang []string `toml:"lang"`
Ext []string `toml:"ext"`
IgnoreCase bool `toml:"ignore-case"`
NoColor bool `toml:"no-color"`
Abs bool `toml:"abs"`
ShowMatchCount bool `toml:"count"`
NoGroupSeparator bool `toml:"no-group-separator"`
NoIndent bool `toml:"no-indent"`
Hidden bool `toml:"hidden"`
SkipGitIgnore bool `toml:"skip-gitignore"`
SkipXfgIgnore bool `toml:"skip-xfgignore"`
NoDefaultSkip bool `toml:"no-default-skip"`
SearchAll bool `toml:"search-all"`
Unrestricted bool `toml:"unrestricted"`
NoPager bool `toml:"no-pager"`
Quiet bool `toml:"quiet"`
FilesWithMatches bool `toml:"files-with-matches"`
Null bool `toml:"null"`
Stats bool `toml:"stats"`
SearchOnlyName bool `toml:"search-only-name"`
NotWordBoundary bool `toml:"not-word-boundary"`
IgnorePermissionError bool `toml:"ignore-permission-error"`
flagLangList bool
ContextLines uint32 `toml:"context"`
AfterContextLines uint32 `toml:"after-context"`
BeforeContextLines uint32 `toml:"before-context"`
MaxMatchCount uint32 `toml:"max-count"`
MaxColumns uint32 `toml:"max-columns"`
extra optionsExtra
}
func (o *options) falgs(d *options) {
flag.StringArrayVarP(&o.SearchPath, "path", "p", d.SearchPath, "A string to find paths")
flag.StringArrayVarP(&o.SearchGrep, "grep", "g", d.SearchGrep, "A string to search for contents")
flag.StringArrayVarP(&o.SearchStart, "start", "s", d.SearchStart, "A location to start searching")
flag.BoolVarP(&o.IgnoreCase, "ignore-case", "i", d.IgnoreCase, "Ignore case distinctions to search. Also affects keywords of ignore option")
flag.StringArrayVarP(&o.SearchPathRe, "path-regexp", "P", d.SearchPathRe, "A string to find paths by regular expressions (RE2)")
flag.StringArrayVarP(&o.SearchGrepRe, "grep-regexp", "G", d.SearchGrepRe, "A string to grep contents by regular expressions (RE2)")
flag.BoolVarP(&o.NotWordBoundary, "not-word-boundary", "", d.NotWordBoundary, "Not care about word boundary to match by regexp")
flag.Uint32VarP(&o.ContextLines, "context", "C", d.ContextLines, "Show several lines before and after the matched one")
flag.Uint32VarP(&o.AfterContextLines, "after-context", "A", d.AfterContextLines, "Show several lines after the matched one. Override context option")
flag.Uint32VarP(&o.BeforeContextLines, "before-context", "B", d.BeforeContextLines, "Show several lines before the matched one. Override context option")
flag.BoolVarP(&o.Hidden, "hidden", ".", d.Hidden, "Enable to search hidden files")
flag.BoolVarP(&o.SkipGitIgnore, "skip-gitignore", "", d.SkipGitIgnore, "Search files and directories even if a path matches a line of .gitignore")
flag.BoolVarP(&o.SkipXfgIgnore, "skip-xfgignore", "", d.SkipXfgIgnore, "Search files and directories even if a path matches a line of .xfgignore")
flag.BoolVarP(&o.NoDefaultSkip, "no-default-skip", "", d.NoDefaultSkip, "Not skip .git, .gitkeep, .gitkeep, .svn, node_modules, vendor, *.min.js and *.mmin.css")
flag.BoolVarP(&o.SearchAll, "search-all", "a", d.SearchAll, "Search all files and directories except specific ignoring files and directories")
flag.BoolVarP(&o.Unrestricted, "unrestricted", "u", d.Unrestricted, "The alias of --search-all")
flag.StringArrayVarP(&o.Ignore, "ignore", "", d.Ignore, "Ignore path to pick up even with '--search-all'")
flag.BoolVarP(&o.SearchOnlyName, "search-only-name", "f", d.SearchOnlyName, "Search to only name instead whole path string")
flag.StringVarP(&o.Type, "type", "t", d.Type, "Filter by file type: "+supportTypes)
flag.StringArrayVarP(&o.Ext, "ext", "", d.Ext, "Only search files matching file extension")
flag.StringArrayVarP(&o.Lang, "lang", "", d.Lang, "Only search files matching language. --lang-list prints all support languages")
flag.BoolVarP(&o.flagLangList, "lang-list", "", false, "Show all supported file extensions for each language")
flag.BoolVarP(&o.Abs, "abs", "", d.Abs, "Show absolute paths")
flag.BoolVarP(&o.ShowMatchCount, "count", "c", d.ShowMatchCount, "Show a count of matching lines instead of contents")
flag.Uint32VarP(&o.MaxMatchCount, "max-count", "m", d.MaxMatchCount, "Stop reading a file after NUM matching lines")
flag.Uint32VarP(&o.MaxColumns, "max-columns", "", d.MaxColumns, "Do not print lines longer than this limit")
flag.BoolVarP(&o.FilesWithMatches, "files-with-matches", "l", d.FilesWithMatches, "Only print the names of matching files")
flag.BoolVarP(&o.Null, "null", "0", d.Null, "Separate the filenames with \\0, rather than \\n")
flag.BoolVarP(&o.NoColor, "no-color", "", d.NoColor, "Disable colors for an output")
flag.StringVarP(&o.ColorPathBase, "color-path-base", "", d.ColorPathBase, "Color name for a path")
flag.StringVarP(&o.ColorPath, "color-path", "", d.ColorPath, "Color name to highlight keywords in a path")
flag.StringVarP(&o.ColorContent, "color-content", "", d.ColorContent, "Color name to highlight keywords in a content line")
flag.StringVarP(&o.GroupSeparator, "group-separator", "", d.GroupSeparator, "Print this string instead of '--' between groups of lines")
flag.BoolVarP(&o.NoGroupSeparator, "no-group-separator", "", d.NoGroupSeparator, "Do not print a separator between groups of lines")
flag.StringVarP(&o.Indent, "indent", "", d.Indent, "Indent string for the top of each line")
flag.BoolVarP(&o.NoIndent, "no-indent", "", d.NoIndent, "Do not print an indent string")
flag.BoolVarP(&o.IgnorePermissionError, "ignore-permission-error", "", d.IgnorePermissionError, "Do not print warnings of file permission error")
flag.StringVarP(&o.XfgIgnoreFile, "xfgignore-file", "", d.XfgIgnoreFile, ".xfgignore file path if you have it except XDG base directory or HOME directory")
flag.BoolVarP(&o.NoPager, "no-pager", "", d.NoPager, "Do not invoke with the Pager")
flag.BoolVarP(&o.Quiet, "quiet", "q", d.Quiet, "Do not write anything to standard output. Exit immediately with zero status if any match is found")
flag.BoolVarP(&o.Stats, "stats", "", d.Stats, "Print runtime stats after searching result")
}
func (cli *runner) parseArgs(d *options) *options {
o := &options{}
o.extra.runWithNoArg = len(os.Args) == 1
o.falgs(d)
flag.CommandLine.SetOutput(cli.err)
flag.CommandLine.SortFlags = false
var flagHelp, flagVersion bool
flag.BoolVarP(&flagHelp, "help", "h", false, "Show help (This message) and exit")
flag.BoolVarP(&flagVersion, "version", "v", false, "Show version and build command info and exit")
flag.Parse()
if o.Type != "" && !o.validateType() {
cli.putErr(fmt.Sprintf("wrong type `%s`. Supported: %s", o.Type, supportTypes))
funcExit(exitErr)
}
if flagHelp {
cli.putHelp(fmt.Sprintf("Version %s", getVersion()))
} else if flagVersion {
cli.putErr(versionDetails())
funcExit(exitOK)
} else if o.flagLangList {
cli.putErr(showLangList())
funcExit(exitOK)
} else {
o.targetPathFromArgs()
}
return o
}
func (o *options) validateType() bool {
if len(o.Type) == 1 && strings.Contains("fdlxespbc", o.Type) {
return true // fine!
}
if o.Type == "directory" || o.Type == "symlink" || o.Type == "executable" || o.Type == "empty" ||
o.Type == "socket" || o.Type == "pipe" || o.Type == "block-device" || o.Type == "char-device" {
return true // fine!
}
return false
}
func (o *options) targetPathFromArgs() {
if len(flag.Args()) > 0 && flag.Args()[0] != "" {
o.SearchPath = append(o.SearchPath, flag.Args()[0])
}
if len(flag.Args()) > 1 {
o.SearchGrep = append(o.SearchGrep, flag.Args()[1:]...)
}
}
func (o *options) prepareContextLines(isTTY bool) {
if !isTTY {
o.ContextLines = 0
o.AfterContextLines = 0
o.BeforeContextLines = 0
o.extra.actualAfterContextLines = 0
o.extra.actualBeforeContextLines = 0
o.extra.withAfterContextLines = false
o.extra.withBeforeContextLines = false
return
}
if o.AfterContextLines > 0 {
o.extra.actualAfterContextLines = o.AfterContextLines
} else if o.ContextLines > 0 {
o.extra.actualAfterContextLines = o.ContextLines
}
o.extra.withAfterContextLines = o.ContextLines > 0 || o.AfterContextLines > 0
if o.BeforeContextLines > 0 {
o.extra.actualBeforeContextLines = o.BeforeContextLines
} else if o.ContextLines > 0 {
o.extra.actualBeforeContextLines = o.ContextLines
}
o.extra.withBeforeContextLines = o.ContextLines > 0 || o.BeforeContextLines > 0
}
func (o *options) prepareFromENV() {
if os.Getenv(XFG_NO_COLOR_ENV_KEY) != "" {
o.NoColor = true
}
}
func (o *options) prepareAliases() {
if o.Unrestricted {
o.SearchAll = true
}
}
func (o *options) prepareRuntimeFlags() {
if len(o.SearchGrep) > 0 || len(o.SearchGrepRe) > 0 {
o.extra.onlyMatchContent = true
}
}
func (o *options) validateOptions() error {
if err := validateStartPath(o.SearchStart); err != nil {
return err
}
if len(o.Lang) > 0 {
if err := validateLanguageCondition(o.Lang); err != nil {
return err
}
}
return nil
}
func showLangList() string {
m := xfglangxt.List()
languages := make([]string, 0, len(m))
for k := range m {
languages = append(languages, k)
}
sort.Strings(languages)
out := ""
for _, lang := range languages {
out = out + lang + ": " + strings.Join(m[lang], ", ") + "\n"
}
return strings.TrimRight(out, "\n")
}
func versionDetails() string {
goos := runtime.GOOS
goarch := runtime.GOARCH
compiler := runtime.Version()
return fmt.Sprintf(
"Version %s - %s.%s (compiled:%s, %s)",
getVersion(),
goos,
goarch,
compiler,
installFrom,
)
}
func getVersion() string {
if version != "" {
return version
}
i, ok := debug.ReadBuildInfo()
if !ok {
return "Unknown"
}
return i.Main.Version
}
func (cli *runner) putErr(message ...interface{}) {
fmt.Fprintln(cli.err, message...)
}
func (cli *runner) putUsage() {
cli.putErr(fmt.Sprintf("Usage: %s [SEARCH_PATH_KEYWORD] [SEARCH_CONTENT_KEYWORD] [OPTIONS]", cmdName))
}
func (cli *runner) putHelp(message string) {
cli.putErr(message)
cli.putUsage()
cli.putErr("Options:")
flag.PrintDefaults()
funcExit(exitOK)
}