-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcat.go
129 lines (111 loc) · 2.91 KB
/
gcat.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
package main
import (
"fmt"
"go/format"
"io/fs"
"os"
"path/filepath"
"regexp"
"github.com/alecthomas/chroma/quick"
"github.com/mmpx12/optionparser"
)
const version = "v1.0.1"
var DisableSyntax bool
func ListFunctions(file string) {
var re = regexp.MustCompile(`(?m)^func.*?{$`)
for _, match := range re.FindAllString(file, -1) {
PrintRes(match + "}\n")
}
}
func CatFunction(funcName, file string) {
var re = regexp.MustCompile(`(?ms)^func (\([A-Za-z0-9_]+.*?\)[ |\t])?` + funcName + `.*?{\n.*?^}(\s+?)$`)
for _, match := range re.FindAllString(file, -1) {
PrintRes(match)
}
}
func ListTypes(file string) {
var re = regexp.MustCompile(`(?ms)^( +|\t+)?type\s[A-Za-z0-9_]+\sstruct\s{\n.*?^( +|\t+)?}$`)
for _, match := range re.FindAllString(file, -1) {
PrintRes(match + "\n")
}
}
func ListMethod(Type, file string) {
var re = regexp.MustCompile(`(?ms)^func (\([A-Za-z0-9_]+ [\*]?` + Type + `\)[ |\t])[A-Za-z0-9_].*?{$`)
for _, match := range re.FindAllString(file, -1) {
PrintRes(match + "}\n")
}
}
func GoFiles() []string {
var files []string
filepath.WalkDir(".", func(s string, d fs.DirEntry, e error) error {
if filepath.Ext(d.Name()) == ".go" {
files = append(files, s)
}
return nil
})
return files
}
func PrintVersion() {
fmt.Println("gcat version:", version)
os.Exit(1)
}
func PrintRes(res string) {
if !DisableSyntax {
quick.Highlight(os.Stdout, res, "go", "terminal256", "monokai")
} else {
fmt.Printf(res)
}
}
func main() {
var list, listtype bool
var cat, method string
op := optionparser.NewOptionParser()
op.Banner = `List or print functions, type, method in go files
Usage: gcat [OPTION] FILE1 FILE2 ...
Check recursivly on current directory if no files are passed
options:`
op.On("-l", "--list-functions", "List all functions", &list)
op.On("-p", "--print-function FUNC", "Cat FUNC function", &cat)
op.On("-m", "--method TYPE", "List TYPE method", &method)
op.On("-t", "--list-types", "List types", &listtype)
op.On("-d", "--disable-syntax", "Disable syntax highlighting", &DisableSyntax)
op.On("-v", "--version", "Print version and exit", PrintVersion)
op.Exemple("List all function in all go files in current directory:")
op.Exemple("gcat -l\n")
op.Exemple("Print main function in file.go")
op.Exemple("gcat -p main file.go")
op.Parse()
var files []string
if !list && !listtype && cat == "" && method == "" {
op.Help()
os.Exit(1)
}
if len(op.Extra) == 0 || op.Extra[0] == "." {
files = GoFiles()
} else {
files = op.Extra
}
for _, i := range files {
f, err := os.ReadFile(i)
if err != nil {
panic(err)
}
formated, err := format.Source(f)
if err != nil {
panic(err)
}
if len(files) > 1 {
fmt.Println(i + ":")
}
switch {
case list:
ListFunctions(string(formated))
case cat != "":
CatFunction(cat, string(formated))
case method != "":
ListMethod(method, string(formated))
case listtype:
ListTypes(string(formated))
}
}
}