Skip to content

Commit 289a63c

Browse files
authored
Add tag exclude (#34)
Co-authored-by: Tony Meehan <[email protected]>
1 parent 88a697f commit 289a63c

File tree

2 files changed

+25
-30
lines changed

2 files changed

+25
-30
lines changed

cmd/ruler/ruler.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ var cli struct {
1616
Num int `name:"num" short:"n" help:"Number to generate." default:"1"`
1717
} `cmd:"" name:"id" short:"i" help:"Generate random id."`
1818
BuildCmd struct {
19-
InPath string `name:"path" short:"p" help:"Path to read rules" default:"rules"`
20-
OutPath string `name:"out" short:"o" help:"Optional path to write files; default curdir"`
21-
Version string `name:"vers" short:"v" help:"Optional semantic version override"`
19+
InPath string `name:"path" short:"p" help:"Path to read rules" default:"rules"`
20+
OutPath string `name:"out" short:"o" help:"Optional path to write files; default curdir"`
21+
Version string `name:"vers" short:"v" help:"Optional semantic version override"`
22+
Exclude []string `name:"exclude" short:"x" help:"Exclude rules by tag"`
2223
} `cmd:"" name:"build" short:"b" help:"Build rules package."`
2324
VersionCmd struct {
24-
} `cmd:"" name:"version" short:"v" help:"Print version information." default:1`
25-
Version bool `name:"version" short:"v" help:"Version"`
26-
Level string `short:"l" help:"Log level." default:"info"`
25+
} `cmd:"" name:"version" short:"v" help:"Print version information."`
26+
Level string `short:"l" help:"Log level." default:"info"`
2727
}
2828

2929
func initLogger() {
@@ -47,6 +47,6 @@ func main() {
4747
case "id":
4848
ruler.RunId(cli.IdCmd.Num)
4949
case "build":
50-
ruler.RunBuild(cli.BuildCmd.InPath, cli.BuildCmd.OutPath, cli.BuildCmd.Version)
50+
ruler.RunBuild(cli.BuildCmd.InPath, cli.BuildCmd.OutPath, cli.BuildCmd.Version, cli.BuildCmd.Exclude)
5151
}
5252
}

pkg/ruler/build.go

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"os"
88
"path/filepath"
9+
"slices"
910
"sort"
1011
"strings"
1112

@@ -28,13 +29,7 @@ var (
2829
catsYaml = "categories.yaml"
2930
)
3031

31-
type BuildCmd struct {
32-
InPath string `name:"path" short:"p" help:"Path to read rules" default:"rules"`
33-
OutPath string `name:"out" short:"o" help:"Optional path to write files; default curdir"`
34-
Version string `name:"vers" short:"v" help:"Optional semantic version override"`
35-
}
36-
37-
func RunBuild(inPath, outPath, vers string) error {
32+
func RunBuild(inPath, outPath, vers string, exclude []string) error {
3833

3934
if outPath == "" {
4035
var err error
@@ -64,7 +59,7 @@ func RunBuild(inPath, outPath, vers string) error {
6459
}
6560
}
6661

67-
if err := _build(vers, inPath, outPath, packageName); err != nil {
62+
if err := _build(vers, inPath, outPath, packageName, exclude); err != nil {
6863
return err
6964
}
7065

@@ -200,7 +195,17 @@ func processRules(path string, ruleDupes, termDupes dupesT, tags tagsT) (*parser
200195
return allRules, nil
201196
}
202197

203-
func _build(vers, inPath, outPath, packageName string) error {
198+
func containsAny[T comparable](a, b []T) bool {
199+
for _, v := range b {
200+
log.Debug().Any("v", v).Any("a", a).Msg("containsAny")
201+
if slices.Contains(a, v) {
202+
return true
203+
}
204+
}
205+
return false
206+
}
207+
208+
func _build(vers, inPath, outPath, packageName string, exclude []string) error {
204209

205210
var (
206211
allRules = make(map[string]parser.ParseRuleT)
@@ -253,6 +258,10 @@ func _build(vers, inPath, outPath, packageName string) error {
253258
}
254259

255260
for _, rule := range r.Rules {
261+
if containsAny(rule.Cre.Tags, exclude) {
262+
log.Info().Str("id", rule.Cre.Id).Msg("Skipping rule due to exclude tag match")
263+
continue
264+
}
256265
allRules[rule.Cre.Id] = rule
257266
}
258267

@@ -284,20 +293,6 @@ func _build(vers, inPath, outPath, packageName string) error {
284293
return nil
285294
}
286295

287-
func unmarshal(data []byte) (*parser.RulesT, error) {
288-
289-
var (
290-
rules *parser.RulesT
291-
err error
292-
)
293-
294-
if rules, err = parser.Unmarshal(data); err != nil {
295-
return nil, err
296-
}
297-
298-
return rules, nil
299-
}
300-
301296
func compile(rules *parser.RulesT) error {
302297

303298
var (

0 commit comments

Comments
 (0)