-
Notifications
You must be signed in to change notification settings - Fork 43
/
main.go
200 lines (184 loc) · 4.56 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
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"go/ast"
"go/importer"
"go/parser"
"go/token"
"go/types"
"log"
"os"
"path/filepath"
"strings"
"github.com/cespare/subcmd"
"github.com/quasilyte/go-ruleguard/ruleguard"
"github.com/quasilyte/go-ruleguard/ruleguard/irconv"
"github.com/quasilyte/go-ruleguard/ruleguard/irprint"
)
func main() {
cmds := []subcmd.Command{
{
Name: "doc",
Description: "query rules documentation",
Do: docMain,
},
{
Name: "precompile",
Description: "generate a precompiled rules object",
Do: precompileMain,
},
}
subcmd.Run(cmds)
}
func docMain(args []string) {
if err := docCommand(args); err != nil {
log.Fatal(err)
}
}
func precompileMain(args []string) {
if err := precompileCommand(args); err != nil {
log.Fatal(err)
}
}
func docCommand(args []string) error {
type JsonListEntry struct {
Name string
Filename string
Line int
DocSummary string
DocBefore string
DocAfter string
DocTags []string
DocNote string
}
type JsonList struct {
List []JsonListEntry
}
fs := flag.NewFlagSet("gorules doc", flag.ExitOnError)
flagRules := fs.String("rules", "", `comma-separated list of ruleguard file paths`)
flagJson := fs.Bool("json", false, `format the output as JSON`)
fs.Parse(args)
var groupName string
extraArgs := fs.Args()
if len(extraArgs) != 0 {
groupName = extraArgs[0]
}
e := ruleguard.NewEngine()
fset := token.NewFileSet()
ctx := &ruleguard.LoadContext{
Fset: fset,
}
filenames := strings.Split(*flagRules, ",")
for _, filename := range filenames {
filename = strings.TrimSpace(filename)
data, err := os.ReadFile(filename)
if err != nil {
return fmt.Errorf("read rules file: %v", err)
}
if err := e.Load(ctx, filename, bytes.NewReader(data)); err != nil {
return fmt.Errorf("parse rules file: %v", err)
}
}
if *flagJson {
var result JsonList
for _, g := range e.LoadedGroups() {
result.List = append(result.List, JsonListEntry{
Name: g.Name,
Line: g.Line,
Filename: g.Filename,
DocSummary: g.DocSummary,
DocBefore: g.DocBefore,
DocAfter: g.DocAfter,
DocTags: g.DocTags,
DocNote: g.DocNote,
})
}
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
enc.SetEscapeHTML(false)
if err := enc.Encode(result); err != nil {
return err
}
var pretty bytes.Buffer
if err := json.Indent(&pretty, buf.Bytes(), "", " "); err != nil {
return err
}
fmt.Print(pretty.String())
return nil
}
if groupName != "" {
var g *ruleguard.GoRuleGroup
groups := e.LoadedGroups()
for i := range groups {
if groups[i].Name == groupName {
g = &groups[i]
break
}
}
if g == nil {
return fmt.Errorf("the requested %s group was not loaded", groupName)
}
fmt.Printf("%s:%d: %s\n\n", filepath.Base(g.Filename), g.Line, g.Name)
fmt.Printf("Full path: %s\n\n", g.Filename)
if g.DocSummary != "" {
fmt.Printf("Summary: %s\n\n", g.DocSummary)
}
if len(g.DocTags) != 0 {
fmt.Printf("Tags: %v\n\n", g.DocTags)
}
if g.DocBefore != "" && g.DocAfter != "" {
fmt.Printf("Before:\n")
fmt.Printf("\t%s\n", g.DocBefore)
fmt.Printf("After:\n")
fmt.Printf("\t%s\n", g.DocAfter)
}
} else {
for _, g := range e.LoadedGroups() {
fmt.Printf("%s:%d: %s\n", filepath.Base(g.Filename), g.Line, g.Name)
}
}
return nil
}
func precompileCommand(args []string) error {
fs := flag.NewFlagSet("gorules precompile", flag.ExitOnError)
flagRules := fs.String("rules", "", `a single ruleguard file path`)
fs.Parse(args)
fset := token.NewFileSet()
filename := strings.TrimSpace(*flagRules)
fileData, err := os.ReadFile(filename)
if err != nil {
return fmt.Errorf("read %s: %v", filename, err)
}
r := bytes.NewReader(fileData)
parserFlags := parser.ParseComments
f, err := parser.ParseFile(fset, filename, r, parserFlags)
if err != nil {
return fmt.Errorf("parse %s: %v", filename, err)
}
imp := importer.For("source", nil)
typechecker := types.Config{Importer: imp}
types := &types.Info{
Types: map[ast.Expr]types.TypeAndValue{},
Uses: map[*ast.Ident]types.Object{},
Defs: map[*ast.Ident]types.Object{},
}
pkg, err := typechecker.Check("gorules", fset, []*ast.File{f}, types)
if err != nil {
return fmt.Errorf("typecheck %s: %v", filename, err)
}
irconvCtx := &irconv.Context{
Pkg: pkg,
Types: types,
Fset: fset,
Src: fileData,
}
irfile, err := irconv.ConvertFile(irconvCtx, f)
if err != nil {
return fmt.Errorf("compile %s: %v", filename, err)
}
irprint.File(os.Stdout, irfile)
return nil
}