Skip to content

Commit

Permalink
Merge pull request #34 from jistr/output_filtering_rev2
Browse files Browse the repository at this point in the history
Output: select fields you want for listings
  • Loading branch information
Petr Blaho committed Jan 23, 2013
2 parents 952bcf7 + 063c5e9 commit a46ca02
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 2 deletions.
2 changes: 2 additions & 0 deletions features/usage.feature
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Feature: Usage
aeolus provider list
Options:
[--fields=FIELDS] # Fields (attributes) to print in the listing
[--conductor-url=CONDUCTOR_URL]
[--username=USERNAME]
[--password=PASSWORD]
Expand Down Expand Up @@ -85,6 +86,7 @@ Feature: Usage
aeolus provider_account list
Options:
[--fields=FIELDS] # Fields (attributes) to print in the listing
[--conductor-url=CONDUCTOR_URL]
[--username=USERNAME]
[--password=PASSWORD]
Expand Down
20 changes: 20 additions & 0 deletions lib/aeolus_cli/common_cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ def banner(task, namespace = nil, subcommand = false)
# aeolus list
#"#{basename} #{task.formatted_usage(self, $thor_runner, subcommand)}"
end

def method_options_for_resource_list
method_option_fields
# TODO: method_option_sort_by
end

def method_option_fields
method_option :fields,
:type => :string,
:desc => 'Fields (attributes) to print in the listing'
end
end

def load_aeolus_config(options)
Expand Down Expand Up @@ -125,6 +136,15 @@ def set_output_format(options)
@output_format = AeolusCli::Formatting.create_format(shell, options)
end

# Transforms e.g. 'name,status' into [:name, :status]
def resource_fields(fields_option)
return nil unless fields_option
if fields_option == ''
raise Thor::MalformattedArgumentError.new("Option 'fields' cannot be empty.")
end
fields_option.split(',').map { |option| option.to_sym }
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
3 changes: 2 additions & 1 deletion lib/aeolus_cli/provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
class AeolusCli::Provider < AeolusCli::CommonCli

desc "list", "List all providers"
method_options_for_resource_list
def list
providers = AeolusCli::Model::Provider.all
output_format.list(providers)
output_format.list(providers, resource_fields(options[:fields]))
end

desc "add PROVIDER_NAME", "Add a provider"
Expand Down
3 changes: 2 additions & 1 deletion lib/aeolus_cli/provider_account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
class AeolusCli::ProviderAccount < AeolusCli::CommonCli

desc "list", "list provider accounts"
method_options_for_resource_list
# TODO maybe an optional variable for provider_type
def list
accounts = AeolusCli::Model::ProviderAccount.all_full_detail
output_format.list(accounts)
output_format.list(accounts, resource_fields(options[:fields]))
end

desc "add PROVIDER_ACCOUNT_LABEL", "Add a provider account"
Expand Down
24 changes: 24 additions & 0 deletions spec/aeolus_cli/common_cli_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'aeolus_cli/common_cli'

describe AeolusCli::CommonCli do
let(:common_cli) { AeolusCli::CommonCli.new() }

context "#resource_fields" do
context "non-empty fields" do
subject { common_cli.send(:resource_fields, "name,status,is_cool") }
it { should == [:name, :status, :is_cool] }
end

context "empty fields" do
subject { common_cli.send(:resource_fields, "") }
it do
expect { subject }.to raise_error Thor::MalformattedArgumentError
end
end

context "nil fields" do
subject { common_cli.send(:resource_fields, nil) }
it { should == nil }
end
end
end
23 changes: 23 additions & 0 deletions spec/aeolus_cli/formatting_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'aeolus_cli/formatting'

describe AeolusCli::Formatting do
let(:formatting) { AeolusCli::Formatting }
let(:shell) { double('shell') }

context ".create_format" do
subject { formatting.create_format(shell, { :format => 'human' }) }
let(:human_format) { double('human format') }
let(:human_format_class) do
double('human format class').tap do |clazz|
clazz.stub(:new).with(shell).and_return(human_format)
end
end

before do
formatting.should_receive(:require).with("aeolus_cli/formatting/human_format").and_return(true)
formatting.should_receive(:const_get).with("HumanFormat").and_return(human_format_class)
end

it { should == human_format }
end
end

0 comments on commit a46ca02

Please sign in to comment.