Skip to content

Commit

Permalink
feat: add describe-environment command
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Jun 5, 2021
1 parent e32e4e5 commit cd11ebb
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 13 deletions.
9 changes: 9 additions & 0 deletions lib/pact_broker/client/cli/pacticipant_commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ def list_pacticipants
execute_pacticipant_command(params_from_options(PACTICIPANT_PARAM_NAMES), 'List')
end

desc 'describe-pacticipant', "Describe a pacticipant"
method_option :name, type: :string, required: true, desc: "Pacticipant name"
output_option_json_or_text
shared_authentication_options
verbose_option
def describe_pacticipant
execute_pacticipant_command({ name: options.name }, 'Describe')
end

no_commands do
def execute_pacticipant_command(params, command_class_name)
require 'pact_broker/client/pacticipants'
Expand Down
23 changes: 23 additions & 0 deletions lib/pact_broker/client/describe_text_formatter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'yaml'
require 'pact_broker/client/generate_display_name'

module PactBroker
module Client
class DescribeTextFormatter
extend GenerateDisplayName

def self.call(properties)
YAML.dump(displayify_keys(properties)).gsub("---\n", "")
end

def self.displayify_keys(thing)
case thing
when Hash then thing.each_with_object({}) { | (key, value), new_hash | new_hash[generate_display_name(key)] = displayify_keys(value) }
when Array then thing.collect{ | value | displayify_keys(value) }
else
thing
end
end
end
end
end
16 changes: 3 additions & 13 deletions lib/pact_broker/client/environments/describe_environment.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
require 'pact_broker/client/environments/environment_command'
require 'pact_broker/client/generate_display_name'
require 'yaml'
require 'pact_broker/client/describe_text_formatter'

module PactBroker
module Client
module Environments
class DescribeEnvironment < PactBroker::Client::Environments::EnvironmentCommand
include PactBroker::Client::GenerateDisplayName
private

def do_call
Expand All @@ -18,16 +16,8 @@ def result_message
if json_output?
existing_environment_resource.response.raw_body
else
YAML.dump(displayify_keys(existing_environment_resource.response.body.except("_links"))).gsub("---\n", "")
end
end

def displayify_keys(thing)
case thing
when Hash then thing.each_with_object({}) { | (key, value), new_hash | new_hash[generate_display_name(key)] = displayify_keys(value) }
when Array then thing.collect{ | value | displayify_keys(value) }
else
thing
properties = existing_environment_resource.response.body.except("_links", "_embedded")
PactBroker::Client::DescribeTextFormatter.call(properties)
end
end
end
Expand Down
33 changes: 33 additions & 0 deletions lib/pact_broker/client/pacticipants/describe.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require 'pact_broker/client/base_command'
require 'pact_broker/client/describe_text_formatter'

module PactBroker
module Client
module Pacticipants2
class Describe < PactBroker::Client::BaseCommand

private

def do_call
PactBroker::Client::CommandResult.new(true, result_message)
end

def pacticipant_entity
@pacticipant_entity ||= index_resource._link('pb:pacticipant').expand('pacticipant' => params[:name]).get!
end

def result_message
if json_output?
pacticipant_entity.response.raw_body
else
properties = pacticipant_entity.response.body.except("_links", "_embedded")
if pacticipant_entity._embedded["labels"] && pacticipant_entity._embedded["labels"].any?
properties["labels"] = pacticipant_entity._embedded["labels"]
end
PactBroker::Client::DescribeTextFormatter.call(properties)
end
end
end
end
end
end

0 comments on commit cd11ebb

Please sign in to comment.