Skip to content

Commit 5cbb664

Browse files
committed
feat: allow pact-message update to receive JSON via the standard input
1 parent 7c95115 commit 5cbb664

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

lib/pact/message/cli.rb

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,23 @@ class CLI < Thor
99
method_option :pact_dir, required: true, desc: "The Pact directory"
1010
method_option :pact_specification_version, required: false, default: "2.0.0", desc: "The Pact Specification version"
1111

12-
desc 'update MESSAGE_JSON', 'Update a pact with the given message, or create the pact if it does not exist. The MESSAGE_JSON may be in the legacy Ruby JSON format or the v2+ format.'
13-
def update(message)
12+
# Update a pact with the given message, or create the pact if it does not exist
13+
desc 'update MESSAGE_JSON', "Update/create a pact. If MESSAGE_JSON is omitted or '-', it is read from stdin"
14+
long_desc <<-MSG, wrapping: false
15+
Update a pact with the given message, or create the pact if it does not exist.
16+
The MESSAGE_JSON may be in the legacy Ruby JSON format or the v2+ format.
17+
If MESSAGE_JSON is not provided or is '-', the content will be read from
18+
standard input.
19+
MSG
20+
def update(maybe_json = '-')
1421
require 'pact/message'
1522
require 'pact/message/consumer/write_pact'
23+
24+
message_json = JSON.parse(maybe_json == '-' ? $stdin.read : maybe_json)
25+
1626
pact_specification_version = Pact::SpecificationVersion.new(options.pact_specification_version)
17-
message = Pact::Message.from_hash(JSON.load(message), { pact_specification_version: pact_specification_version })
18-
Pact::Message::Consumer::WritePact.call(message, options.pact_dir, options.consumer, options.provider, options.pact_specification_version, :update)
27+
message_hash = Pact::Message.from_hash(message_json, { pact_specification_version: pact_specification_version })
28+
Pact::Message::Consumer::WritePact.call(message_hash, options.pact_dir, options.consumer, options.provider, options.pact_specification_version, :update)
1929
end
2030

2131
desc 'reify', "Take a JSON document with embedded pact matchers and return a concrete example"

0 commit comments

Comments
 (0)