-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
179 lines (158 loc) · 5.21 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
// This file is part of grepp.
//
// Copyright (C) 2012-2024 David Gamba Rios
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package main
import (
"context"
"errors"
"fmt"
"io"
"os"
l "github.com/DavidGamba/dgtools/grepp/logging"
"github.com/DavidGamba/dgtools/grepp/runInPager"
"github.com/DavidGamba/dgtools/grepp/semver"
"github.com/DavidGamba/go-getoptions"
)
// Buffer Size used to read files when searching through them.
// Default value should cover most cases.
var bufferSize int
var g grepp
func main() {
os.Exit(program(os.Args))
}
func program(args []string) int {
l.LogInit(io.Discard, io.Discard, os.Stdout, os.Stderr, os.Stderr)
opt := getoptions.New()
opt.SetUnknownMode(getoptions.Pass)
opt.SetCommandFn(Run)
opt.Bool("version", false) // version info
opt.BoolVar(&g.ignoreBinary, "ignore-binary", true, opt.Alias("I"))
opt.BoolVar(&g.caseSensitive, "case-sensitive", false, opt.Alias("c"))
opt.String("color", "auto", opt.Description("When to use colors: always, auto, never"), opt.ValidValues("always", "auto", "never"), opt.ArgName("always|auto|never"))
opt.BoolVar(&g.useNumber, "line-number", true, opt.Alias("n"))
opt.BoolVar(&g.filenameOnly, "files-with-matches", false, opt.Alias("l"))
opt.StringVar(&g.replace, "replace", "", opt.Alias("r"), opt.Description(`In-place replace matches with the given text.
Use \1, \2, \3, \4, \5 to replace captures.
Requires --force to apply changes`))
opt.BoolVar(&g.force, "force", false, opt.Alias("f", "apply"), opt.Description("Apply changes when using --replace"))
opt.IntVar(&g.context, "context", 0, opt.Alias("C"), opt.Description("Number of lines of context to show"), opt.ArgName("number"))
opt.IntVar(&bufferSize, "buffer", 16384)
opt.BoolVar(&g.showBufferSizeErrors, "show-buffer-errors", false, opt.Alias("sbe"))
opt.Bool("no-pager", false, opt.Description("Do not use pager"))
opt.HelpSynopsisArg("<pattern>", "Pattern to search for")
opt.HelpSynopsisArg("[<search_path>]", "Base path to search in, defaults to current directory")
opt.StringSlice("ignore-extension", 1, 1, opt.Alias("ie"), opt.ArgName("ext"))
// "fp" // fullPath - Used to show the file full path instead of the relative to the current dir.
// "name" // filePattern - Use to further filter the search to files matching that pattern.
// "ignore" // ignoreFilePattern - Use to further filter the search to files not matching that pattern.
// "spacing" // keepSpacing - Do not remove initial spacing.
opt.Bool("debug", false, opt.Description("Enable debug logging"))
opt.HelpCommand("help", opt.Alias("?"))
remaining, err := opt.Parse(args[1:])
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
return 1
}
if opt.Called("version") {
version := semver.Version{Major: 0, Minor: 2, Patch: 0, PreReleaseLabel: "dev"}
fmt.Println(version)
return 0
}
if opt.Called("debug") {
l.Debug.SetOutput(os.Stderr)
l.Trace.SetOutput(os.Stderr)
}
l.Debug.Println(remaining)
ctx, cancel, done := getoptions.InterruptContext()
defer func() { cancel(); <-done }()
err = opt.Dispatch(ctx, remaining)
if err != nil {
if errors.Is(err, getoptions.ErrorHelpCalled) {
return 1
}
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
if errors.Is(err, getoptions.ErrorParsing) {
fmt.Fprintf(os.Stderr, "\n"+opt.Help())
}
return 1
}
return 0
}
func Run(ctx context.Context, opt *getoptions.GetOpt, args []string) error {
noPager := opt.Value("no-pager").(bool)
ie := opt.Value("ignore-extension").([]string)
color := opt.Value("color").(string)
// TODO: Read from ~/.grepprc
g.ignoreExtensionList = []string{
".un~", // vim
".swp", // vim
".a", // c
".so", // c
".db", // Database file
".base64", // encoded_data
".svg", // image
".png", // image
".PNG", // image
".jpg", // image
".ttf", // font
".pdf", // pdf
".tfstate", // terraform state
}
g.ignoreExtensionList = append(g.ignoreExtensionList, ie...)
pattern, args, err := opt.GetRequiredArg(args)
if err != nil {
return err
}
g.pattern = pattern
g.searchBase = "."
if len(args) > 0 {
g.searchBase = args[0]
}
searchBaseInfo, err := os.Stat(g.searchBase)
if err != nil {
return fmt.Errorf("cannot stat %s: %w", g.searchBase, err)
}
if searchBaseInfo.IsDir() {
g.showFile = true
} else {
g.showFile = false
// If filename provided, don't skip it
g.ignoreBinary = false
}
l.Debug.Printf("pattern: %s, searchBase: %s, replace: %s", g.pattern, g.searchBase, g.replace)
l.Debug.Printf(fmt.Sprintln(g))
isDevice := isDevice()
switch color {
case "always":
g.useColor = true
case "never":
g.useColor = false
default:
if isDevice {
g.useColor = true
} else {
g.useColor = false
}
}
if !noPager && isDevice {
l.Debug.Println("runInPager")
err = runInPager.Command(ctx, &g)
} else if noPager && isDevice {
g.Stdout = os.Stdout
g.Stderr = os.Stderr
err = g.Run(ctx)
} else {
g.Stdout = os.Stdout
g.Stderr = os.Stderr
err = g.Run(ctx)
}
return err
}
func isDevice() bool {
statStdout, _ := os.Stdout.Stat()
return (statStdout.Mode() & os.ModeDevice) != 0
}