From ed20570659d0b264f36bc2aa92ae3f3e1a77a795 Mon Sep 17 00:00:00 2001 From: Anders Eknert Date: Thu, 20 Jul 2023 13:04:04 +0200 Subject: [PATCH] Scaffolding Add `regal new rule` command to allow quickly getting started working with custom rules, or builtin rules for Regal itself. See added docs for more details. Fixes #206 Signed-off-by: Anders Eknert --- bundle/regal/result.rego | 4 +- cmd/new.go | 245 ++++++++++++++++++ docs/custom-rules.md | 22 ++ e2e/cli_test.go | 50 ++++ internal/embeds/embeds.go | 4 + .../embeds/schemas}/regal-ast.json | 0 .../embeds/templates/builtin/builtin.rego.tpl | 20 ++ .../templates/builtin/builtin_test.rego.tpl | 29 +++ .../embeds/templates/custom/custom.rego.tpl | 22 ++ .../templates/custom/custom_test.rego.tpl | 26 ++ internal/test/rego_test.go | 2 +- main.go | 4 - 12 files changed, 421 insertions(+), 7 deletions(-) create mode 100644 cmd/new.go rename {schemas => internal/embeds/schemas}/regal-ast.json (100%) create mode 100644 internal/embeds/templates/builtin/builtin.rego.tpl create mode 100644 internal/embeds/templates/builtin/builtin_test.rego.tpl create mode 100644 internal/embeds/templates/custom/custom.rego.tpl create mode 100644 internal/embeds/templates/custom/custom_test.rego.tpl diff --git a/bundle/regal/result.rego b/bundle/regal/result.rego index c1371bc3..85f98557 100644 --- a/bundle/regal/result.rego +++ b/bundle/regal/result.rego @@ -62,7 +62,7 @@ _fail_annotated(metadata, details) := violation if { "level": config.rule_level(config.for_rule(with_location)), }) - without_custom_and_scope := object.remove(with_category, ["custom", "scope"]) + without_custom_and_scope := object.remove(with_category, ["custom", "scope", "schemas"]) related_resources := resource_urls(without_custom_and_scope.related_resources, category) violation := json.patch( @@ -80,7 +80,7 @@ _fail_annotated_custom(metadata, details) := violation if { "level": config.rule_level(config.for_rule(with_location)), }) - violation := object.remove(with_category, ["custom", "scope"]) + violation := object.remove(with_category, ["custom", "scope", "schemas"]) } fail(metadata, details) := _fail_annotated(metadata, details) diff --git a/cmd/new.go b/cmd/new.go new file mode 100644 index 00000000..bcedf29a --- /dev/null +++ b/cmd/new.go @@ -0,0 +1,245 @@ +// nolint:wrapcheck +package cmd + +import ( + "fmt" + "log" + "os" + "path/filepath" + "regexp" + "strings" + "text/template" + + "github.com/spf13/cobra" + + "github.com/styrainc/regal/internal/embeds" +) + +// The revive check will warn about using underscore in struct names, but it's seemingly not aware of keywords. +// +//nolint:revive +type newRuleCommandParams struct { + type_ string // 'type' is a keyword + category string + name string + output string +} + +type TemplateValues struct { + Category string + NameOriginal string + Name string + NameTest string +} + +var ( + categoryRegex = regexp.MustCompile(`^[a-z]+$`) + nameRegex = regexp.MustCompile(`^[a-z_]+[a-z0-9_\-]*$`) +) + +//nolint:lll +func init() { + newCommand := &cobra.Command{ + Hidden: true, + Use: "new