Skip to content

Commit f324454

Browse files
authored
Move temp dirs, add more debug logs, and move config dir (#36)
* Move temp dirs, add more debug logs, and move config dir * Add rule id/hash gen * Update comment * Update * Update go mod --------- Co-authored-by: Tony Meehan <tonymeehan@users.noreply.github.com>
1 parent 5578177 commit f324454

9 files changed

Lines changed: 265 additions & 59 deletions

File tree

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ require (
3030
require (
3131
github.com/cqroot/prompt v0.9.4
3232
github.com/fatih/color v1.18.0
33-
github.com/prequel-dev/prequel-compiler v0.0.9
33+
github.com/prequel-dev/prequel-compiler v0.0.10
3434
github.com/spf13/cobra v1.8.1
3535
github.com/spf13/viper v1.20.1
3636
gopkg.in/yaml.v2 v2.4.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,8 @@ github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRI
192192
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
193193
github.com/posener/complete v1.2.3 h1:NP0eAhjcjImqslEwo/1hq7gpajME0fTLTezBKDqfXqo=
194194
github.com/posener/complete v1.2.3/go.mod h1:WZIdtGGp+qx0sLrYKtIRAruyNpv6hFCicSgv7Sy7s/s=
195-
github.com/prequel-dev/prequel-compiler v0.0.9 h1:t5DfnYhk6/j1uBTbnZSqL0MOmmTIh7urUg3k8jVnDm0=
196-
github.com/prequel-dev/prequel-compiler v0.0.9/go.mod h1:cA1v+gPi9/toDHQaQCYwgvDmP2UTtHOf5QU0qu2Ksts=
195+
github.com/prequel-dev/prequel-compiler v0.0.10 h1:8UyXkjohbtNcOQrN6RHxdwSecjzq/7wgwfQbhOBUup4=
196+
github.com/prequel-dev/prequel-compiler v0.0.10/go.mod h1:cA1v+gPi9/toDHQaQCYwgvDmP2UTtHOf5QU0qu2Ksts=
197197
github.com/prequel-dev/prequel-logmatch v0.0.13 h1:cPKs1FbhfDyPevGkEOVEud+HZT7r385a+ZumWvBlmQw=
198198
github.com/prequel-dev/prequel-logmatch v0.0.13/go.mod h1:Dpfd/79s8vMCmIdmNQ5kC8WAvw/XjOd2z6wEIe3Lkck=
199199
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=

internal/pkg/cli/cli.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ var Options struct {
3333
}
3434

3535
var (
36-
defaultConfigDir = filepath.Join(os.Getenv("HOME"), ".preq")
36+
// https://specifications.freedesktop.org/basedir-spec/latest/
37+
defaultConfigDir = filepath.Join(os.Getenv("HOME"), ".config", "preq")
3738
ruleToken = filepath.Join(defaultConfigDir, ".ruletoken")
3839
ruleUpdateFile = filepath.Join(defaultConfigDir, ".ruleupdate")
3940
)
@@ -75,7 +76,7 @@ func InitAndExecute(ctx context.Context) error {
7576
var (
7677
c *config.Config
7778
token string
78-
rulesPaths []string
79+
rulesPaths []rules.RulePathT
7980
err error
8081
)
8182

@@ -177,7 +178,7 @@ func InitAndExecute(ctx context.Context) error {
177178
}
178179

179180
if Options.Cron {
180-
if err := ux.PrintCronJobTemplate(Options.Name, defaultConfigDir, rulesPaths[0]); err != nil {
181+
if err := ux.PrintCronJobTemplate(Options.Name, defaultConfigDir, rulesPaths[0].Path); err != nil {
181182
log.Error().Err(err).Msg("Failed to write cronjob template")
182183
ux.ConfigError(err)
183184
return err

internal/pkg/engine/engine.go

Lines changed: 50 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/Masterminds/semver"
1515
"github.com/prequel-dev/preq/internal/pkg/matchz"
1616
"github.com/prequel-dev/preq/internal/pkg/resolve"
17+
"github.com/prequel-dev/preq/internal/pkg/rules"
1718
"github.com/prequel-dev/preq/internal/pkg/utils"
1819
"github.com/prequel-dev/preq/internal/pkg/ux"
1920
"github.com/prequel-dev/prequel-compiler/pkg/compiler"
@@ -40,6 +41,7 @@ var (
4041
ErrExpectedMatcherCb = errors.New("expected matcher callback")
4142
ErrDuplicateRule = errors.New("duplicate rule")
4243
ErrNoRules = errors.New("no rules provided")
44+
ErrMissingCreId = errors.New("missing cre id")
4345
)
4446

4547
type RuntimeT struct {
@@ -85,52 +87,70 @@ func compileRuleTree(cf compiler.RuntimeI, tree *parser.TreeT) (compiler.ObjsT,
8587
return nodeObjs, nil
8688
}
8789

88-
func compileRulePath(cf compiler.RuntimeI, path string) (compiler.ObjsT, *parser.RulesT, error) {
90+
func compileRulePath(cf compiler.RuntimeI, rp rules.RulePathT) (compiler.ObjsT, *parser.RulesT, error) {
8991
var (
90-
rules *parser.RulesT
91-
tree *parser.TreeT
92-
nodeObjs compiler.ObjsT
93-
err error
92+
rs *parser.RulesT
93+
tree *parser.TreeT
94+
nodeObjs compiler.ObjsT
95+
rdrOpts = make([]utils.ReaderOptT, 0)
96+
parseOpts = make([]parser.ParseOptT, 0)
97+
err error
9498
)
9599

96-
log.Info().Str("path", path).Msg("Parsing rules")
100+
log.Info().Str("path", rp.Path).Msg("Parsing rules")
97101

98-
if rules, err = utils.ParseRulesPath(path); err != nil {
102+
switch rp.Type {
103+
case rules.RuleTypeCre:
104+
rdrOpts = append(rdrOpts, utils.WithMultiDoc())
105+
case rules.RuleTypeUser:
106+
// Allow empty IDs in user generated content
107+
rdrOpts = append(rdrOpts, utils.WithGenIds())
108+
parseOpts = append(parseOpts, parser.WithGenIds())
109+
}
110+
111+
if rs, err = utils.ParseRulesPath(rp.Path, rdrOpts...); err != nil {
99112
log.Error().Err(err).Msg("Failed to parse rules")
100113
return nil, nil, err
101114
}
102115

103-
if tree, err = parser.ParseRules(rules); err != nil {
116+
if tree, err = parser.ParseRules(rs, parseOpts); err != nil {
104117
return nil, nil, err
105118
}
106119

107-
log.Info().Int("cres", len(rules.Rules)).Msg("Parsed rules")
108-
for _, rule := range rules.Rules {
120+
log.Info().Int("cres", len(rs.Rules)).Msg("Parsed rules")
121+
for _, rule := range rs.Rules {
109122
log.Info().Str("id", rule.Metadata.Id).Str("cre", rule.Cre.Id).Msg("Rule")
110123
}
111124

112125
nodeObjs, err = compileRuleTree(cf, tree)
113126
if err != nil {
114-
return nil, nil, pqerr.WithFile(err, path)
127+
return nil, nil, pqerr.WithFile(err, rp.Path)
115128
}
116129

117-
return nodeObjs, rules, nil
130+
return nodeObjs, rs, nil
118131
}
119132

120133
func compileRule(cf compiler.RuntimeI, data []byte) (compiler.ObjsT, *parser.RulesT, error) {
121134
var (
122-
rules *parser.RulesT
123-
tree *parser.TreeT
124-
nodeObjs compiler.ObjsT
125-
err error
135+
rules *parser.RulesT
136+
tree *parser.TreeT
137+
nodeObjs compiler.ObjsT
138+
rdrOpts = make([]utils.ReaderOptT, 0)
139+
parseOpts = make([]parser.ParseOptT, 0)
140+
err error
126141
)
127142

128-
if rules, err = utils.ParseRules(bytes.NewReader(data)); err != nil {
143+
// Generate IDs for rules if missing
144+
rdrOpts = append(rdrOpts, utils.WithGenIds())
145+
parseOpts = append(parseOpts, parser.WithGenIds())
146+
147+
if rules, err = utils.ParseRules(bytes.NewReader(data), rdrOpts...); err != nil {
129148
log.Error().Err(err).Msg("Failed to parse rules")
130149
return nil, nil, err
131150
}
132151

133-
if tree, err = parser.ParseRules(rules); err != nil {
152+
if tree, err = parser.ParseRules(rules, parseOpts); err != nil {
153+
log.Error().Err(err).Msg("Failed to parse rules")
134154
return nil, nil, err
135155
}
136156

@@ -161,20 +181,22 @@ func (r *RuntimeT) compileRules(cf compiler.RuntimeI, data []byte) (compiler.Obj
161181
)
162182

163183
if nObjs, rules, err = compileRule(cf, data); err != nil {
184+
log.Error().Err(err).Msg("Failed to compile rule")
164185
return nil, nil, err
165186
}
166187

167188
r.Ux.IncrementRuleTracker(int64(len(rules.Rules)))
168189

169190
if ok, err = validateRules(rules, nil); !ok {
191+
log.Error().Err(err).Msg("Failed to validate rules")
170192
return nil, nil, err
171193
}
172194

173195
return nObjs, rules, nil
174196

175197
}
176198

177-
func (r *RuntimeT) compileRulesPaths(cf compiler.RuntimeI, paths []string) (compiler.ObjsT, []*parser.RulesT, error) {
199+
func (r *RuntimeT) compileRulesPaths(cf compiler.RuntimeI, paths []rules.RulePathT) (compiler.ObjsT, []*parser.RulesT, error) {
178200
var (
179201
nodeObjs = make(compiler.ObjsT, 0)
180202
allRules = make([]*parser.RulesT, 0)
@@ -466,7 +488,7 @@ func (r *RuntimeT) getRuntimeCb(report *ux.ReportT) *runtimeT {
466488
return runtime
467489
}
468490

469-
func (r *RuntimeT) CompileRulesPath(rulesPaths []string, report *ux.ReportT) (*RuleMatchersT, error) {
491+
func (r *RuntimeT) CompileRulesPath(rulesPaths []rules.RulePathT, report *ux.ReportT) (*RuleMatchersT, error) {
470492

471493
var (
472494
nodeObjs compiler.ObjsT
@@ -497,27 +519,27 @@ func (r *RuntimeT) CompileRulesPath(rulesPaths []string, report *ux.ReportT) (*R
497519
return matchers, nil
498520
}
499521

500-
func (r *RuntimeT) LoadRulesPaths(rep *ux.ReportT, rulesPaths []string) (*RuleMatchersT, error) {
522+
func (r *RuntimeT) LoadRulesPaths(rep *ux.ReportT, rulesPaths []rules.RulePathT) (*RuleMatchersT, error) {
501523

502524
var (
503525
ruleMatchers *RuleMatchersT
504-
paths = make([]string, 0, len(rulesPaths))
505526
err error
506527
)
507528

508-
for _, path := range rulesPaths {
509-
if _, err = os.Stat(path); err != nil {
510-
log.Warn().Str("path", path).Msg("Failed to stat path. Continue...")
529+
for _, rp := range rulesPaths {
530+
if _, err = os.Stat(rp.Path); err != nil {
531+
log.Warn().
532+
Str("path", rp.Path).
533+
Msg("Failed to stat path. Continue...")
511534
continue
512535
}
513-
paths = append(paths, path)
514536
}
515537

516-
if len(paths) == 0 {
538+
if len(rulesPaths) == 0 {
517539
return nil, ErrNoRules
518540
}
519541

520-
if ruleMatchers, err = r.CompileRulesPath(paths, rep); err != nil {
542+
if ruleMatchers, err = r.CompileRulesPath(rulesPaths, rep); err != nil {
521543
return nil, err
522544
}
523545

0 commit comments

Comments
 (0)