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

Add an additional argument to save the json to file #148

Open
j-puri opened this issue Oct 30, 2023 · 9 comments
Open

Add an additional argument to save the json to file #148

j-puri opened this issue Oct 30, 2023 · 9 comments
Labels
awaiting feedback Awaiting Feedback from OP

Comments

@j-puri
Copy link

j-puri commented Oct 30, 2023

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).

@j-puri j-puri changed the title An additional argument to save the json to file Add an additional argument to save the json to file Oct 30, 2023
@bethesque
Copy link
Member

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

@bethesque
Copy link
Member

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

@j-puri
Copy link
Author

j-puri commented Nov 9, 2023

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.

@mefellows
Copy link
Member

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?

@mefellows
Copy link
Member

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

@bethesque
Copy link
Member

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.

@mefellows
Copy link
Member

$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 .

@mefellows mefellows added the awaiting feedback Awaiting Feedback from OP label Nov 16, 2023
@j-puri
Copy link
Author

j-puri commented Nov 22, 2023

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.

@mefellows
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting feedback Awaiting Feedback from OP
Projects
None yet
Development

No branches or pull requests

3 participants