Skip to content
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

(maint) Replace --report-* options with --format. #24

Merged
merged 3 commits into from
Apr 5, 2017
Merged

(maint) Replace --report-* options with --format. #24

merged 3 commits into from
Apr 5, 2017

Conversation

whopper
Copy link
Contributor

@whopper whopper commented Apr 3, 2017

The first of these commits adds --format as a global option even though initially it may only
be relevant to the validate and test subcommands.

The second commit adds a new OptionParser Util class and a method to parse the report formats and targets which were provided.

The final commits update the validate subcommand to use the new format option, which specifies one or more format:target pairs.

Given multiple report formats and targets, the validators will create multiple reports, one for each.

This adds --format as a global option even though initially it may only
be relevant to the `validate` and `test` subcommands.
@whopper
Copy link
Contributor Author

whopper commented Apr 3, 2017

/cc @scotje a couple of things about this:

  1. I DRY'd up the duplicated code in the validator subclasses in my other open PR, so the duplication where I'm looping over each of the reports won't exist once that's merged and this is rebased.

  2. I wasn't sure what the default target should be when someone does --formats=text, so I assumed stdout for now.

  3. My strategy here for multiple reports (i.e, --format=text:foo.text --format=junit:bar.junit is to create an array of Report objects and simply invoke each of their write methods after validation has been completed.

module PDK
module CLI
module Util
class OptionParser
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: I know the name OptionParser kind of sucks. Suggestions welcome :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a built-in OptionParser class so we probably shouldn't use that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to OptionNormalizer

format, target = f, PDK::Report.default_target
end

reports << Report.new(target, format)
Copy link
Contributor Author

@whopper whopper Apr 3, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My plan here is to always work with an array of one or more reports, and we'll then just invoke write on however many we've got at the end of validation / testing. Each Report will deal with the specifics of writing its format to its target.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That seems reasonable. :)

@@ -13,6 +13,10 @@ def self.default_format
'junit'
end

def self.default_target
'stdout' # TODO: What's the default target?
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

¯_(ツ)_/¯

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, everything should report to STDOUT unless otherwise specified.

PDK.logger.info("Running #{cmd}")
output = PDK::CLI::Exec.execute(cmd)
report.write(output) if report
if reports
reports.each do |r|
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of this duplicated code across the 4 validators will go away after my other PR is merged and this fun stuff is merged into a base Validator class.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I'm wondering if it would be better for the components to just return a structure with like the exit-code, stdout, and stderr from the invoked command and then have the CLI part be responsible for passing that into the various formatters. Output formatting seems like it belongs in the interface layer of the application and then we won't have to pass the reports array in.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to do just that. I fixed up the exec method to create a CommandResult struct and return it. I'm not sure if an actual struct is the approach we'll want to ultimately take, but I suppose it does give us a consistent interface for command data (as opposed to just returning a plain old hash with {exit, stdout, stderr})

process.start

# wait indefinitely for process to exit...
process.wait

# get the exit code
process.exit_code #=> 0
stdout = process.io.stdout.open.read
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@scotje here's some fun with Tempfile. I wonder if I need to do and validation that it's actually open and I can read?

This commit updates the `validate` subcommand to use the new
`format` option, which specifies one or more <format>:<target>
pairs.

Given multiple report formats and targets, the validators will
create multiple reports, one for each.
@scotje scotje merged commit 5022b2e into puppetlabs:master Apr 5, 2017
@DavidS DavidS added the feature label Jun 16, 2017
@DavidS DavidS changed the title (MAINT) Replace --report-* options with --format. (maint) Replace --report-* options with --format. Jun 16, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants