pact-foundation/pact_broker-client

Add an additional argument to save the json to file

Opened this issue · 9 comments

Feature Request

An additional argument to save the json output to file. This would be particularly useful for can-i-deploy in CI so that the result json doesn't need to be extracted from parsed container logs (if using the Dockerised form of the CLI).

Is there a reason that adding > output.json on the end of the command does not work for you?

Why do you need to parse the JSON? What are you trying to do that it is not supported by the CLI currently?

Is there a reason that adding > output.json on the end of the command does not work for you?

This was my original plan but I happened to run with -e PACT_DISABLE_SSL_VERIFICATION=true and the output contained log entries (WARN: SSL verification has been disabled by a dodgy hack (reassigning the VERIFY_PEER constant to VERIFY_NONE). You acknowledge that you do this at your own risk!).

Why do you need to parse the JSON? What are you trying to do that it is not supported by the CLI currently?

It's just a pattern we tend to use. We use Jenkins libraries to create common building blocks for pipelines. For CLI tools this provides an abstraction for teams where instead of them having to deal with raw JSON from the library function, we instead return a Groovy object based on the JSON and relevant to our organisational usage.

This was my original plan but I happened to run with -e PACT_DISABLE_SSL_VERIFICATION=true and the output contained log entries (WARN: SSL verification has been disabled by a dodgy hack (reassigning the VERIFY_PEER constant to VERIFY_NONE). You acknowledge that you do this at your own risk!).

Perhaps this should be printed to stderr, and this way you could split out stdout from stderr?

In the mean time @j-puri, I suppose you could filter out that message before JSON parsing?

Yes, the warning should be to stderr. That's a rookie error. If you can find where that's printed, it should be easy to fix.

$stderr.puts "WARN: SSL verification has been disabled by a dodgy hack (reassigning the VERIFY_PEER constant to VERIFY_NONE). You acknowledge that you do this at your own risk!"

hmm actually it already is stderr. This means you should be able to split stdout (the JSON) from stderr. I just tried this and it works as you'd expect - the warning is printed to the console using stderr and the JSON is correctly parsed by jq:

PACT_DISABLE_SSL_VERIFICATION=true pact-broker can-i-deploy \
                --pacticipant pactflow-example-consumer \
                --latest --verbose --output json > output.json

cat output.json | jq .

Thanks for the help, all.

It nearly works for me. I'm using the docker image to run the client which may be why I still get error logs in output.json. For example, in the case where can-i-deploy fails because an environment doesn't exist, my output.json is:

�[33m[dry-run] �[0mError retrieving matrix. PactBroker::Client::Hal::ErrorResponseReturned - Error making request to https://abcdefgh.pactflow.io/matrix status=400 {"errors":["Environment with name 'uat' does not exist"]}
�[33m[dry-run] �[0m/usr/lib/ruby/gems/3.2.0/gems/pact_broker-client-1.74.0/lib/pact_broker/client/hal/entity.rb:197:in `assert_success!'
�[33m[dry-run] �[0m/usr/lib/ruby/gems/3.2.0/gems/pact_broker-client-1.74.0/lib/pact_broker/client/hal/link.rb:45:in `get!'
�[33m[dry-run] �[0m/usr/lib/ruby/gems/3.2.0/gems/pact_broker-client-1.74.0/lib/pact_broker/client/matrix/query.rb:11:in `call'
�[33m[dry-run] �[0m/usr/lib/ruby/gems/3.2.0/gems/pact_broker-client-1.74.0/lib/pact_broker/client/base_command.rb:13:in `call'
�[33m[dry-run] �[0m/usr/lib/ruby/gems/3.2.0/gems/pact_broker-client-1.74.0/lib/pact_broker/client/can_i_deploy.rb:107:in `block in fetch_matrix'
�[33m[dry-run] �[0m/usr/lib/ruby/gems/3.2.0/gems/pact_broker-client-1.74.0/lib/pact_broker/client/retry.rb:22:in `while_error'
�[33m[dry-run] �[0m/usr/lib/ruby/gems/3.2.0/gems/pact_broker-client-1.74.0/lib/pact_broker/client/can_i_deploy.rb:107:in `fetch_matrix'
�[33m[dry-run] �[0m/usr/lib/ruby/gems/3.2.0/gems/pact_broker-client-1.74.0/lib/pact_broker/client/can_i_deploy.rb:111:in `fetch_matrix_with_retries'
�[33m[dry-run] �[0m/usr/lib/ruby/gems/3.2.0/gems/pact_broker-client-1.74.0/lib/pact_broker/client/can_i_deploy.rb:34:in `call'
�[33m[dry-run] �[0m/usr/lib/ruby/gems/3.2.0/gems/pact_broker-client-1.74.0/lib/pact_broker/client/can_i_deploy.rb:23:in `call'
�[33m[dry-run] �[0m/usr/lib/ruby/gems/3.2.0/gems/pact_broker-client-1.74.0/lib/pact_broker/client/cli/matrix_commands.rb:41:in `can_i_deploy'
�[33m[dry-run] �[0m/usr/lib/ruby/gems/3.2.0/gems/thor-1.3.0/lib/thor/command.rb:28:in `run'
�[33m[dry-run] �[0m/usr/lib/ruby/gems/3.2.0/gems/thor-1.3.0/lib/thor/invocation.rb:127:in `invoke_command'
�[33m[dry-run] �[0m/usr/lib/ruby/gems/3.2.0/gems/thor-1.3.0/lib/thor.rb:527:in `dispatch'
�[33m[dry-run] �[0m/usr/lib/ruby/gems/3.2.0/gems/thor-1.3.0/lib/thor/base.rb:584:in `start'
�[33m[dry-run] �[0m/usr/lib/ruby/gems/3.2.0/gems/pact_broker-client-1.74.0/lib/pact_broker/client/cli/custom_thor.rb:34:in `start'
�[33m[dry-run] �[0m/usr/lib/ruby/gems/3.2.0/gems/pact_broker-client-1.74.0/bin/pact-broker:10:in `<top (required)>'
�[33m[dry-run] �[0m/usr/bin/pact-broker:25:in `load'
�[33m[dry-run] �[0m/usr/bin/pact-broker:25:in `<main>'
�[33m[dry-run] �[0m
�[33m[dry-run] �[0m�[32mDry run enabled - ignoring any failures�[0m

When there's no errors, however, redirecting the output to a json file works as expected.

That seems like a bug in the JSON output, or at least an inconsistency in how the --output json argument works. If I specify an invalid version I get a JSON response, but if I specify an unknown environment I don't.