-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add mindev ruletype init
to kick off a rule type
#5228
base: main
Are you sure you want to change the base?
Conversation
This helps folks set up the basic skeleton for ruletype writing. Signed-off-by: Juan Antonio Osorio <[email protected]>
213adf2
to
daa24a2
Compare
} | ||
|
||
func createRuleTypeFile(fileName, name string) error { | ||
return createFileWithContent(fileName, fmt.Sprintf(`--- |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It feels like we should use text/template
here rather than fmt.Sprintf
, particularly if we start to input more data before outputting the rule.
# http: | ||
# body_file: HTTP_BODY_FILE | ||
# git: | ||
# repo_base: REPO_BASE_PATH |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It feels like these could benefit from a bit more documentation in a comment, e.g.
# http: | |
# body_file: HTTP_BODY_FILE | |
# git: | |
# repo_base: REPO_BASE_PATH | |
# When testing a rule, additional content can be supplied | |
# from files in the `{{ .RuleName }}.testdata` directory. | |
# File paths below are relative to this directory. | |
# http: | |
# # Input from the `http` ingest type. | |
# body_file: HTTP_BODY_FILE | |
# git: | |
# # Input from the `git` ingest type. Base paths contain | |
# # directory contents, but do not actually need to be a | |
# # git repository. | |
# repo_base: REPO_BASE_PATH |
file, err := os.Create(filepath.Clean(fileName)) | ||
if err != nil { | ||
return fmt.Errorf("error creating file %s: %w", fileName, err) | ||
} | ||
defer file.Close() | ||
|
||
if _, err := file.WriteString(content); err != nil { | ||
return fmt.Errorf("error writing to file %s: %w", fileName, err) | ||
} | ||
|
||
return nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we simply use os.WriteFile(fileName, byte[](content), 0644)
for this?
func assertFilesDontExist(files ...string) error { | ||
for _, file := range files { | ||
if _, err := os.Stat(file); err == nil { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Odd empty newline here.
for _, file := range files { | ||
if _, err := os.Stat(file); err == nil { | ||
|
||
return fmt.Errorf("file %s already exists", file) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if you want to check os.IsExist(err)
here before returning this error -- there could be a different reason the stat fails, like a missing parent directory.
return errors.New("name cannot be empty") | ||
} | ||
|
||
validName := regexp.MustCompile(`^[a-zA-Z0-9_]+$`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a different regex from https://github.com/mindersec/minder/blob/main/proto/minder/v1/minder.proto#L2496. Not sure if we want to try to re-use that validation?
return initCmd | ||
} | ||
|
||
func initCmdRun(cmd *cobra.Command, args []string) error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about having this operate in a more "wizard" style in terms of prompting for missing data rather than people needing to figure out the flag arguments up front and then getting a file with a bunch of comments to fill in?
Summary
This helps folks set up the basic skeleton for ruletype writing.
Fixes #(related issue)
Change Type
Mark the type of change your PR introduces:
Testing
Outline how the changes were tested, including steps to reproduce and any relevant configurations.
Attach screenshots if helpful.
Review Checklist: