-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
226 lines (198 loc) · 5.39 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package main
import (
"archive/zip"
"compress/gzip"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
"github.com/klauspost/compress/zstd"
)
// ANSI color codes
const (
Magenta = "\033[35m"
Pink = "\033[38;5;206m" // 256-color mode pink
Reset = "\033[0m" // Reset all attributes
)
func main() {
if len(os.Args) < 2 {
fmt.Println("Usage: gl [ag-options] PATTERN [dir]")
os.Exit(1)
}
// Collect command-line arguments
args := os.Args[1:]
agOptions := []string{}
var pattern, dir string
// Determine if the last argument is a directory
if len(args) > 1 && !strings.HasPrefix(args[len(args)-1], "-") {
if info, err := os.Stat(args[len(args)-1]); err == nil && info.IsDir() {
dir = args[len(args)-1]
args = args[:len(args)-1] // Remove directory from args
}
}
// Pattern is the last argument now
if len(args) > 0 {
pattern = args[len(args)-1]
agOptions = args[:len(args)-1]
}
if pattern == "" {
fmt.Println("Pattern must be provided.")
os.Exit(1)
}
// If no directory specified, use current working directory
if dir == "" {
dir, _ = os.Getwd()
}
// fmt.Printf("Pattern: %s\nDirectory: %s\nAG Options: %v\n", pattern, dir, agOptions)
// Walk through the directory structure
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.Mode().IsRegular() {
switch {
case strings.HasSuffix(path, ".zip"), strings.HasSuffix(path, ".gz"), strings.HasSuffix(path, ".tgz"), strings.HasSuffix(path, ".zstd"):
handleCompressedFile(path, pattern, agOptions)
default:
runAg(pattern, path, agOptions)
}
}
return nil
})
if err != nil {
fmt.Printf("Error walking the path %q: %v\n", dir, err)
}
cleanupLogs(dir)
}
func runAg(pattern, path string, options []string) {
cmdArgs := append(options, pattern, path)
cmd := exec.Command("ag", cmdArgs...)
output, err := cmd.CombinedOutput()
if err != nil {
if exitErr, ok := err.(*exec.ExitError); ok {
if exitErr.ExitCode() == 1 {
return
}
}
fmt.Printf("Error running ag on %s: %s\n", path, string(output))
return
}
fmt.Printf("%sResults for %s%s:\n%s\n", Magenta, path, Reset, string(output))
}
func handleCompressedFile(path, pattern string, options []string) {
// fmt.Printf("Handling compressed file: %s\n", path)
ext := filepath.Ext(path)
var reader io.ReadCloser
file, err := os.Open(path)
if err != nil {
fmt.Printf("Error opening file %s: %s\n", path, err)
return
}
defer file.Close()
switch ext {
case ".gz":
if reader, err = gzip.NewReader(file); err != nil {
fmt.Printf("Error creating gzip reader for %s: %s\n", path, err)
return
}
case ".zip":
fileInfo, err := file.Stat()
if err != nil {
fmt.Printf("Error getting file info for %s: %s\n", path, err)
return
}
zr, err := zip.NewReader(file, fileInfo.Size())
if err != nil {
fmt.Printf("Error reading zip file %s: %s\n", path, err)
return
}
for _, f := range zr.File {
if reader, err = f.Open(); err != nil {
fmt.Printf("Error opening zip file content %s: %s\n", f.Name, err)
return
}
runAgThroughReader(pattern, reader, f.Name, options)
reader.Close()
}
return
case ".zstd":
decoder, err := zstd.NewReader(file)
if err != nil {
fmt.Printf("Error creating zstd decoder for %s: %s\n", path, err)
return
}
defer decoder.Close()
reader = decoder.IOReadCloser()
default:
fmt.Printf("Unsupported file extension: %s\n", ext)
return
}
runAgThroughReader(pattern, reader, path, options)
reader.Close()
}
// runAgThroughReader reads data from a reader and directly pipes it to the `ag` command
func runAgThroughReader(pattern string, reader io.Reader, name string, options []string) {
// Prepare the ag command with given options
cmdArgs := append(options, pattern)
cmd := exec.Command("ag", cmdArgs...)
cmd.Stdin = reader // Set the command's Stdin to the reader
// Execute the command and capture the output
output, err := cmd.CombinedOutput()
if err != nil {
// if exit status is 1, it means no results were found
if exitErr, ok := err.(*exec.ExitError); ok {
if exitErr.ExitCode() == 1 {
return
}
}
fmt.Printf("Error running ag through reader for %s: %s\n", name, err)
return
}
// Print results
fmt.Printf("%sResults for %s%s (from stream):\n%s\n", Magenta, name, Reset, string(output))
}
func cleanupLogs(dir string) {
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if strings.HasSuffix(path, ".log") {
if time.Since(info.ModTime()).Hours() > 168 { // 168 hours in 7 days
fmt.Printf("Compressing and deleting old log file: %s\n", path)
compressLog(path)
}
}
return nil
})
}
func compressLog(path string) {
input, err := os.Open(path)
if err != nil {
fmt.Printf("Error opening %s: %s\n", path, err)
return
}
defer input.Close()
output, err := os.Create(path + ".zstd")
if err != nil {
fmt.Printf("Error creating compressed file for %s: %s\n", path, err)
return
}
defer output.Close()
encoder, err := zstd.NewWriter(output)
if err != nil {
fmt.Printf("Error creating zstd encoder for %s: %s\n", path, err)
return
}
defer encoder.Close()
if _, err = io.Copy(encoder, input); err != nil {
fmt.Printf("Error compressing %s: %s\n", path, err)
return
}
if err = os.Remove(path); err != nil {
fmt.Printf("Error removing original log file %s: %s\n", path, err)
return
}
}