-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
59 lines (46 loc) · 1.25 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
package main
import (
"bufio"
"flag"
"fmt"
"os"
"strings"
)
func main() {
// Use a verbosity flag
verbose := flag.Bool("verbose", false, "Print verbose information.")
flag.Parse()
// Read a single line from standard input
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
line := scanner.Text()
// Convert gron output to a jq query
jqQuery := gronLineToJq(line)
if *verbose {
fmt.Printf("✨ The INCOMING gron line looks like this: \r\n")
fmt.Println(line)
fmt.Printf("🎊 The 'jq' query to use would look like this: \r\n")
fmt.Println(jqQuery)
} else {
fmt.Println(jqQuery)
}
}
}
func splitSlice(input []string, delimiter string) []string {
var result []string
// Iterate over each element in the []string
for _, item := range input {
// Split the element by the delimiter
parts := strings.Split(item, delimiter)
// Append the split parts to the result
result = append(result, parts...)
}
return result
}
func gronLineToJq(line string) string {
tokens := strings.Fields(line)
midwayQuery := splitSlice(tokens,"=")[0]
jqFilter := strings.TrimPrefix(midwayQuery, "json")
jqQuery := "jq '" + jqFilter + "'"
return jqQuery
}