diff --git a/features/usage.feature b/features/usage.feature index 55fc86d..3d2a6c0 100644 --- a/features/usage.feature +++ b/features/usage.feature @@ -51,6 +51,7 @@ Feature: Usage Options: [--fields=FIELDS] # Fields (attributes) to print in the listing + [--sort-by=SORT_BY] # Sort output by value of field(s) [--conductor-url=CONDUCTOR_URL] [--username=USERNAME] [--password=PASSWORD] @@ -87,6 +88,7 @@ Feature: Usage Options: [--fields=FIELDS] # Fields (attributes) to print in the listing + [--sort-by=SORT_BY] # Sort output by value of field(s) [--conductor-url=CONDUCTOR_URL] [--username=USERNAME] [--password=PASSWORD] diff --git a/lib/aeolus_cli/common_cli.rb b/lib/aeolus_cli/common_cli.rb index 4c87409..a1a7528 100644 --- a/lib/aeolus_cli/common_cli.rb +++ b/lib/aeolus_cli/common_cli.rb @@ -48,7 +48,7 @@ def banner(task, namespace = nil, subcommand = false) def method_options_for_resource_list method_option_fields - # TODO: method_option_sort_by + method_option_sort_by end def method_option_fields @@ -56,6 +56,12 @@ def method_option_fields :type => :string, :desc => 'Fields (attributes) to print in the listing' end + + def method_option_sort_by + method_option :sort_by, + :type => :string, + :desc => 'Sort output by value of field(s)' + end end def load_aeolus_config(options) @@ -145,6 +151,25 @@ def resource_fields(fields_option) fields_option.split(',').map { |option| option.to_sym } end + def resource_sort_by(sort_by_option) + return nil unless sort_by_option + if sort_by_option == '' + raise Thor::MalformattedArgumentError.new("Option 'sort_by' cannot be empty.") + end + sort_by_option.split(',').map { |option| parse_one_sort_by_option(option) } + end + + def parse_one_sort_by_option(option) + case option[-1] + when '+' + [option[0..-2].to_sym, :asc] + when '-' + [option[0..-2].to_sym, :desc] + else + [option.to_sym, :asc] + end + end + def provider_type(type_s) # we need to hit the API to get the map of provider_type.name => # provider_type.id, so make sure we only do this once. diff --git a/lib/aeolus_cli/provider.rb b/lib/aeolus_cli/provider.rb index 7fcb603..9b13ad5 100644 --- a/lib/aeolus_cli/provider.rb +++ b/lib/aeolus_cli/provider.rb @@ -7,7 +7,9 @@ class AeolusCli::Provider < AeolusCli::CommonCli method_options_for_resource_list def list providers = AeolusCli::Model::Provider.all - output_format.list(providers, resource_fields(options[:fields])) + output_format.list(providers, + resource_fields(options[:fields]), + resource_sort_by(options[:sort_by])) end desc "add PROVIDER_NAME", "Add a provider" diff --git a/lib/aeolus_cli/provider_account.rb b/lib/aeolus_cli/provider_account.rb index 77fffc7..ca040e3 100644 --- a/lib/aeolus_cli/provider_account.rb +++ b/lib/aeolus_cli/provider_account.rb @@ -8,7 +8,9 @@ class AeolusCli::ProviderAccount < AeolusCli::CommonCli # TODO maybe an optional variable for provider_type def list accounts = AeolusCli::Model::ProviderAccount.all_full_detail - output_format.list(accounts, resource_fields(options[:fields])) + output_format.list(accounts, + resource_fields(options[:fields]), + resource_sort_by(options[:sort_by])) end desc "add PROVIDER_ACCOUNT_LABEL", "Add a provider account" diff --git a/spec/aeolus_cli/common_cli_spec.rb b/spec/aeolus_cli/common_cli_spec.rb index b67153f..091f9ed 100644 --- a/spec/aeolus_cli/common_cli_spec.rb +++ b/spec/aeolus_cli/common_cli_spec.rb @@ -21,4 +21,24 @@ it { should == nil } end end + + context "#resource_fields" do + context "non-empty fields" do + subject { common_cli.send(:resource_sort_by, "name+,status-,is_cool") } + it { should == [[:name, :asc], [:status, :desc], [:is_cool, :asc]] } + end + + context "empty fields" do + subject { common_cli.send(:resource_sort_by, "") } + it do + expect { subject }.to raise_error Thor::MalformattedArgumentError + end + end + + context "nil fields" do + subject { common_cli.send(:resource_sort_by, nil) } + it { should == nil } + end + end + end