-
Notifications
You must be signed in to change notification settings - Fork 29
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 config and README entry with regal new rule
command
#304
Conversation
Signed-off-by: Ronnie Personal <76408835+Ronnie-personal@users.noreply.github.com>
Signed-off-by: Ronnie Personal <76408835+Ronnie-personal@users.noreply.github.com>
Signed-off-by: Ronnie Personal <76408835+Ronnie-personal@users.noreply.github.com>
Signed-off-by: Ronnie Personal <76408835+Ronnie-personal@users.noreply.github.com>
Signed-off-by: Ronnie Personal <76408835+Ronnie-personal@users.noreply.github.com>
@anderseknert |
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.
Looks good to me! I don't think the change in new.go
belongs in this PR though, but should be in another one for that issue, right?
Also left a few comments, but mostly just details. Good work!
Oh wait, I was confused by the code from both of the PRs being included here, so I mistakenly commented on this as if it was the print-sprintf PR. Can you have all code relating to that PR removed from here so we can review this separately? |
Sorry about the confusion, I will remove the print-sprintf code change from this PR. |
Signed-off-by: Ronnie Personal <76408835+Ronnie-personal@users.noreply.github.com>
Signed-off-by: Ronnie-personal <76408835+Ronnie-personal@users.noreply.github.com>
Signed-off-by: Ronnie-personal <76408835+Ronnie-personal@users.noreply.github.com>
Signed-off-by: Ronnie-personal <76408835+Ronnie-personal@users.noreply.github.com>
Could you please help me with a question? I'm trying to read data.ymal and unmarshal the file's content to config.Config type.
There are no failures, however existingConfig only has 'level' field for each rule. Other attributes, such as 'max-file-length', are dropped. Is there any other function I can call to retain all the fields of the rule? |
Signed-off-by: Ronnie-personal <76408835+Ronnie-personal@users.noreply.github.com>
@Ronnie-personal I'll take a look tonight 👍 |
Thank you! I also tried to unmarshal the yaml content to |
Could you please try and import Once we have the unmarshalling right, we can look into the problems with marshalling. |
I also noticed that "ignore" and "level" persist in extra attributes after being unmarshalled. This is fixed in #335, so make sure to rebase from main once that's been merged. |
Thank you, I will work on it tonight. |
Signed-off-by: Ronnie-personal <76408835+Ronnie-personal@users.noreply.github.com>
Signed-off-by: Ronnie-personal <76408835+Ronnie-personal@users.noreply.github.com>
Signed-off-by: Ronnie-personal <76408835+Ronnie-personal@users.noreply.github.com>
Signed-off-by: Ronnie-personal <76408835+Ronnie-personal@users.noreply.github.com>
Can I clarify the requirement? Do we need to add builtin rule only to the README.md and bundle/regal/config/provided/data.yaml? What about the custom type rule? |
Yes, only for |
Signed-off-by: Ronnie-personal <76408835+Ronnie-personal@users.noreply.github.com>
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 shaping up really nice! I've left some comments and suggestions, and it would probably be good if we had an e2e test, but that's not a hard requirement.
cmd/new.go
Outdated
return scaffoldBuiltinRule(params) | ||
} | ||
|
||
return fmt.Errorf("unsupported type %v", params.type_) | ||
} | ||
|
||
func addToDataYAML(params newRuleCommandParams) error { | ||
// Read the YAML content from the file | ||
yamlContent, err := os.ReadFile("bundle/regal/config/provided/data.yaml") |
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 assumes the command is run from the project root directory. I guess that's an OK assumption to make, but perhaps we should check for that and fail with a message saying that regal new rule --type builtin
must be run from that location?
cmd/new.go
Outdated
} | ||
|
||
// Add the new entry to the Rules map within the Config struct | ||
if existingConfig.Rules == 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.
This should never be nil, as the provided configuration will always have rules, right?
cmd/new.go
Outdated
return os.WriteFile("bundle/regal/config/provided/data.yaml", b.Bytes(), 0o600) | ||
} | ||
|
||
func addRuleToREADME(params newRuleCommandParams) 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.
This is clever! I wonder though, won't the "startPattern" break if we introduce a rule with a longer name than what we've previously had?
There's some code from before to render the table in the README over here: https://github.com/StyraInc/regal/blob/main/cmd/table.go
Do you think it would be worth refactoring that into a common function that we could reuse here?
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.
Thanks for the comments, shall I use function createTable instead?
I think recreate the entire table is simpler than adding the new rule delta.
I assume the function read all categories and rules from data.yaml and construct table. So I tried to use the function, but I don't understand line #91, when I debug the code, result.Modules appears to empty.
Thanks for the review! It's a great idea to have an e2e test, I'm looking into it. I would like to modify 'addToDataYAML' to use full path instead of relative path. I think we can obtain the current executable path from main.go (e.g os.Executable()) and pass along. I assume that it's mandatory to place 'regal' executable file in the top level directory of our repo. `func TestCreateNewBuiltinRuleYamlReadme(t *testing.T) {
}` |
Yes, the e2e tests require that, and the |
Signed-off-by: Ronnie-personal <76408835+Ronnie-personal@users.noreply.github.com>
Co-authored-by: tony-2023 <138949958+tony-2023@users.noreply.github.com> Signed-off-by: Ronnie-personal <76408835+Ronnie-personal@users.noreply.github.com>
Signed-off-by: Ronnie-personal <76408835+Ronnie-personal@users.noreply.github.com>
@anderseknert Thanks. |
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.
Looks great! The only thing I'm not sure about is using os.Executable
and setting the result to an EXECUTABLE_PATH
env var. I have my Regal executable in ~/bin
, and I would not want config/README to be stored there, but rather in the project root directory.
Could we have this logic updated to simply say:
"If we find bundle/regal/config/provided/data.yaml
under the current working directory, we consider it a project root, and if we don't we exit with an error"?
Or is there something I forgot to consider? Other than that, this is good to merge. Well done! And thanks for being so patient :)
Thanks for the review, I will work on it tonight. |
Signed-off-by: Ronnie-personal <76408835+Ronnie-personal@users.noreply.github.com>
Signed-off-by: Ronnie-personal <76408835+Ronnie-personal@users.noreply.github.com>
What's the status here @Ronnie-personal? I'm looking to cut a new release sometime early next week, and would love to have this included 🙂 |
@anderseknert Sorry about the late response, I'm done the code change, please review again when you have chance. |
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.
LGTM. Just not sure about the test. Also, please rebase from main. I think there's some change to the config file that's causing the conflict, but should be trivial to merge.
e2e/cli_test.go
Outdated
tmpDir := t.TempDir() | ||
|
||
err := regal(&stdout, &stderr)("new", "rule", "--type", "builtin", "--category", "naming", "--name", "foo-bar-baz", "--output", tmpDir) | ||
if exp, act := 1, ExitStatus(err); exp != act { |
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 I understand this test :) Do we expect the command to exit with an exit code of 1?
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.
Good question and sorry about the confusion.
The working directory for e2e test is ./e2e.
The new.go expects the project top level folder as the current working directory, otherwise it will fail with error message "data.yaml file not found. Please run this command from the top-level directory of the Regal repository"
I once tried to change the e2e test's working directory, but the entire test stack is based on ./e2e directory, I'm not sure if I can do the change.
I also tried to use the full path in new.go instead of the relative path, but it requires to pass along the executable 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.
Ah, I see. I think you can just go ahead and remove this test then.
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.
Sure, I'm doing it right now.
I will take care of the rebase in one hour as soon as I logon to my personal desktop. |
Signed-off-by: Ronnie-personal <76408835+Ronnie-personal@users.noreply.github.com>
Signed-off-by: Ronnie-personal <76408835+Ronnie-personal@users.noreply.github.com>
@anderseknert |
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.
Excellent! Thanks for sticking around 😃
regal new rule
command
Fixes #231