Skip to content

Commit

Permalink
feat: add list-pacticipants
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed May 30, 2021
1 parent 76f323b commit fc8ce3b
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 16 deletions.
4 changes: 4 additions & 0 deletions lib/pact_broker/client/cli/custom_thor.rb
Expand Up @@ -118,6 +118,10 @@ def self.output_option_json_or_text
def self.output_option_json_or_table
method_option :output, aliases: "-o", desc: "json or table", default: 'table'
end

def params_from_options(keys)
keys.each_with_object({}) { | key, p | p[key] = options[key] }
end
end
end
end
Expand Down
16 changes: 5 additions & 11 deletions lib/pact_broker/client/cli/environment_commands.rb
Expand Up @@ -12,35 +12,30 @@ def self.shared_environment_options(name_required: false)
method_option :production, type: :boolean, default: false, desc: "Whether or not this environment is a production environment. Default: false"
method_option :contact_name, required: false, desc: "The name of the team/person responsible for this environment"
method_option :contact_email_address, required: false, desc: "The email address of the team/person responsible for this environment"
method_option :output, aliases: "-o", desc: "json or text", default: 'text'
output_option_json_or_text
end

ignored_and_hidden_potential_options_from_environment_variables
desc "create-environment", "Create an environment resource in the Pact Broker to represent a real world deployment or release environment."
shared_environment_options(name_required: true)
shared_authentication_options
def create_environment
params = ENVIRONMENT_PARAM_NAMES.each_with_object({}) { | key, p | p[key] = options[key] }
execute_command(params, "CreateEnvironment")
execute_command(params_from_options(ENVIRONMENT_PARAM_NAMES), "CreateEnvironment")
end

ignored_and_hidden_potential_options_from_environment_variables
desc "update-environment", "Update an environment resource in the Pact Broker."
method_option :uuid, required: true, desc: "The UUID of the environment to update"
shared_environment_options(name_required: false)
shared_authentication_options
def update_environment
params = (ENVIRONMENT_PARAM_NAMES + [:uuid]).each_with_object({}) { | key, p | p[key] = options[key] }
execute_command(params, "UpdateEnvironment")
execute_command(params_from_options(ENVIRONMENT_PARAM_NAMES + [:uuid]), "UpdateEnvironment")
end

desc "describe-environment", "Describe an environment"
method_option :uuid, required: true, desc: "The UUID of the environment to describe"
method_option :output, aliases: "-o", desc: "json or text", default: 'text'
shared_authentication_options
def describe_environment
params = { uuid: options.uuid }
execute_command(params, "DescribeEnvironment")
execute_command(params_from_options([:uuid]), "DescribeEnvironment")
end

desc "list-environments", "List environment"
Expand All @@ -55,8 +50,7 @@ def list_environments
method_option :output, aliases: "-o", desc: "json or text", default: 'text'
shared_authentication_options
def delete_environment
params = { uuid: options.uuid }
execute_command(params, "DeleteEnvironment")
execute_command(params_from_options([:uuid]), "DeleteEnvironment")
end

no_commands do
Expand Down
24 changes: 19 additions & 5 deletions lib/pact_broker/client/cli/pacticipant_commands.rb
Expand Up @@ -16,11 +16,25 @@ def self.included(thor)

def create_or_update_pacticipant(*required_but_ignored)
raise ::Thor::RequiredArgumentMissingError, "Pacticipant name cannot be blank" if options.name.strip.size == 0
require 'pact_broker/client/pacticipants/create'
params = PACTICIPANT_PARAM_NAMES.each_with_object({}) { | key, p | p[key] = options[key] }
result = PactBroker::Client::Pacticipants2::Create.call(params, { verbose: options.verbose, output: options.output }, pact_broker_client_options)
$stdout.puts result.message
exit(1) unless result.success
execute_command(params_from_options(PACTICIPANT_PARAM_NAMES), 'Create')
end

desc 'list-pacticipants', 'List pacticipants'
output_option_json_or_text
shared_authentication_options
verbose_option
def list_pacticipants
execute_command(params_from_options(PACTICIPANT_PARAM_NAMES), 'List')
end

no_commands do
def execute_command(params, command_class_name)
require 'pact_broker/client/pacticipants'
command_options = { verbose: options.verbose, output: options.output }
result = PactBroker::Client::Pacticipants2.const_get(command_class_name).call(params, command_options, pact_broker_client_options)
$stdout.puts result.message
exit(1) unless result.success
end
end
end
end
Expand Down
7 changes: 7 additions & 0 deletions lib/pact_broker/client/pacticipants.rb
@@ -1,3 +1,10 @@
# New code
Dir.glob(File.join(__FILE__.gsub(".rb", "/**/*.rb"))).sort.each do | path |
puts path
require path
end

# Old code
require 'pact_broker/client/base_client'

module PactBroker
Expand Down
34 changes: 34 additions & 0 deletions lib/pact_broker/client/pacticipants/list.rb
@@ -0,0 +1,34 @@
require 'pact_broker/client/base_command'
require 'pact_broker/client/pacticipants/text_formatter'

module PactBroker
module Client
module Pacticipants2
class List < PactBroker::Client::BaseCommand
private

attr_reader :environments_resource

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

def environments_resource
@environments_resource = environments_link.get!
end

def environments_link
index_resource._link!('pb:pacticipants')
end

def result_message
if json_output?
environments_resource.response.raw_body
else
TextFormatter.call(environments_resource._embedded["pacticipants"])
end
end
end
end
end
end
38 changes: 38 additions & 0 deletions lib/pact_broker/client/pacticipants/text_formatter.rb
@@ -0,0 +1,38 @@
require 'table_print'
require 'ostruct'

module PactBroker
module Client
module Pacticipants2
class TextFormatter
def self.call(pacticipants)
return "" if pacticipants.size == 0

data = pacticipants.collect do | pacticipant |
# Might add a UUID in at some stage. Backwards compatible supporting code.
OpenStruct.new({ uuid: "" }.merge(pacticipant).merge(url: pacticipant["_links"]["self"]["href"]))
end.sort_by{ | pacticipant | pacticipant.name.downcase }

uuid_width = max_width(data, :uuid)
name_width = max_width(data, :name)
display_name_width = max_width(data, :display_name)

tp_options = [
{ name: { width: name_width} },
{ displayName: { display_name: "Display name", width: display_name_width } },
]

if uuid_width > 0
tp_options.unshift({ uuid: { width: uuid_width } })
end

TablePrint::Printer.new(data, tp_options).table_print
end

def self.max_width(data, column)
data.collect{ |row| row.send(column) }.compact.collect(&:size).max
end
end
end
end
end

0 comments on commit fc8ce3b

Please sign in to comment.