-
Notifications
You must be signed in to change notification settings - Fork 76
feat: add json formatter #336
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
base: main
Are you sure you want to change the base?
Conversation
|
@Kashugoyal Thanks for the PR - please address all Golang-CI comments and fix failing unit tests |
dcf1526 to
2061a41
Compare
@kehoecj PR is ready for review. |
kehoecj
left a comment
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.
Works as advertised but needs some architectural changes
cmd/validator/validator.go
Outdated
| groupOutputPtr = flag.String("groupby", "", "Group output by filetype, directory, pass-fail. Supported for Standard and JSON reports") | ||
| quietPtr = flag.Bool("quiet", false, "If quiet flag is set. It doesn't print any output to stdout.") | ||
| globbingPrt = flag.Bool("globbing", false, "If globbing flag is set, check for glob patterns in the arguments.") | ||
| formatPtr = flag.Bool("format", false, "Format all supported file types in place. Only json is supported currently.") |
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.
Thinking ahead to when multiple types are supported:
- Users need to be able to specify which config types they want to format. All can be the default but needs to be configurable
- Need to think about how we'll allow users to specify formatting settings. I know you had identified it in future work but I think it needs to be part of this PR. I don't think we'll be able to specify this on the command line, will probably have to be a config file (or multiple config files)
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 do flags like --format-all, --format-json, --format-yaml, etc?
format-xxx to enable formatter for a specific file type,
format-all for all
format-config to follow config specification, e.g. --format-config ./formatter_config.json
For the settings specification, it can be a JSON file with top level keys for file types.
e.g.:
"formatters": ["json", "yaml", ..],
"json": { "indent": 2},
"toml": {..}
...The implementation for the config would be a larger change and maybe we can address as a separate PR.
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 about passing options to -format? Example: -format=all OR -format=json,yaml,ini?
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.
I like the settings JSON - that makes sense to me. Agree that would be part of a larger change later, just wanted to ensure the initial design didn't constrain the later implementation
pkg/cli/cli.go
Outdated
| if ShouldFormat { | ||
| if exitCode, err := formatFiles(filesToFormat); err != nil { | ||
| return exitCode, fmt.Errorf("unable to format files: %w", err) | ||
| } | ||
| } |
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.
The design of this could be refactored to be more efficient:
- Call the formatter in the initial foundFiles loop rather than adding it to a list then looping again.
- The formatFiles function logic should be moved down into the formatter rather than in the CLI
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.
I made the changes as requested. Please take another look.
3bc273e to
81b0d3a
Compare
|
@ccoVeille PR is ready for another look |
|
Instead of reviewing the PR, I have just shared my thoughts and concerns on the issue that this PR is about. Your code is OK. I could review it. But for now, I prefered to step out. Note to make it clear I'm not against what you coded or the way you coded, I just feel it shouldn't be added to the tool |
|
@Kashugoyal Please make the following changes to the implementation based off the discussion in #159 :
Let me know if that makes sense. |
@kehoecj what design do you prefer? I can list a few options below:
|
Good timing, I am going through the same design decision now that I'm adding schema. I was leaning toward I think I'm leaning toward interface composition: type SyntaxValidator interface {
ValidateSyntax(b []byte) (bool, error)
}
type FormatValidator interface {
ValidateFormat(Validate(b []byte, options interface{}) (bool, error)
}
// coming in my Sarif PR
type SchemaValidator interface {
ValidateSchema(Validate(b []byte, schemaDefinition interface{}) (bool, error)
}
type Validator interface {
SyntaxValidator
FormatValidator
SchemaValidator
}That way, when required, the different validator interfaces can be fulfilled and referenced. What do you think? |
I like your idea. Will do that! |
f367ea7 to
ff68e7d
Compare
a0c1e76 to
4f986c7
Compare
…to json-formatter
|
@kehoecj marking the MR ready for review. It has been a lot of back and forth therefore I may have missed some things in my MR, please review 😄 |
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.
During functional testing the following issue was found:
When passing the --check-format, the provided path is not honored by the confi-file-validator tool and it instead scans the current working directory:
~/src/github.com/boeing/pr/formatting/config-file-validator$ ./validator --check-for
mat test/fixtures/bad_format.json
✓ /src/github.com/boeing/pr/formatting/config-file-validator/.github/dependabot.yml
✓ /src/github.com/boeing/pr/formatting/config-file-validator/.github/workflows/changelog.yml
✓ /src/github.com/boeing/pr/formatting/config-file-validator/.github/workflows/go.yml
✓ /src/github.com/boeing/pr/formatting/config-file-validator/.github/workflows/golangci-lint.yml
✓ /src/github.com/boeing/pr/formatting/config-file-validator/.github/workflows/goreportcard.yaml
✓ /src/github.com/boeing/pr/formatting/config-file-validator/.github/workflows/mega-linter.yml
✓ /src/github.com/boeing/pr/formatting/config-file-validator/.github/workflows/release.yml
✓ /src/github.com/boeing/pr/formatting/config-file-validator/.github/workflows/scorecard.ymlWhen I remove the --check-format flag, the tools works as expected
In the above case, the |
Resolves #159
This PR:
Left for Future work: