Skip to content

Commit

Permalink
Option --sort-by for resource lists
Browse files Browse the repository at this point in the history
  • Loading branch information
jistr committed Jan 29, 2013
1 parent f39d027 commit 2f02dae
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 3 deletions.
2 changes: 2 additions & 0 deletions features/usage.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand Down
27 changes: 26 additions & 1 deletion lib/aeolus_cli/common_cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,20 @@ 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
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)
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion lib/aeolus_cli/provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 3 additions & 1 deletion lib/aeolus_cli/provider_account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
20 changes: 20 additions & 0 deletions spec/aeolus_cli/common_cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 2f02dae

Please sign in to comment.