Skip to content

Commit

Permalink
modifying to support a broader definition of configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
wbailey committed May 21, 2012
1 parent 9a40344 commit e0a556a
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 110 deletions.
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -52,7 +52,8 @@ Usage: claws [options]

* Add filtering by regular expression for any field

* Integrate with your projects Capistrano definitions so that one can filter by environment and roles.
* Integrate with your projects Capistrano definitions so that one can filter by environment and
roles.

* Add RDS and ELB support

Expand Down
2 changes: 1 addition & 1 deletion lib/claws/command/ec2.rb
Expand Up @@ -13,7 +13,7 @@ def self.exec(options)
exit 1
end

Claws::Collection::EC2.connect( config.aws_credentials )
Claws::Collection::EC2.connect( config.aws )

begin
instances = Claws::Collection::EC2.get
Expand Down
81 changes: 43 additions & 38 deletions lib/claws/command/initialize.rb
Expand Up @@ -5,49 +5,54 @@ module Command
class Initialize
def self.exec
h = {
'aws_user' => nil,
'capistrano_home' => nil,
'access_key_id' => nil,
'secret_access_key' => nil,
'fields' => {
'id' => {
'width' => 10,
'title' => 'ID',
},
'name' => {
'width' => 20,
'title' => 'Name',
},
'status' => {
'width' => 8,
'title' => 'Status',
},
'dns_name' => {
'width' => 42,
'title' => 'DNS Name',
},
'instance_type' => {
'width' => 13,
'title' => 'Instance Type',
},
'public_ip_address' => {
'width' => 16,
'title' => 'Public IP',
},
'private_ip_address' => {
'width' => 16,
'title' => 'Private IP',
},
'tags' => {
'width' => 30,
'title' => 'tags',
'capistrano' => {
'home' => nil,
},
'aws' => {
'aws_user' => nil,
'access_key_id' => nil,
'secret_access_key' => nil,
},
'ec2' => {
'fields' => {
'id' => {
'width' => 10,
'title' => 'ID',
},
'name' => {
'width' => 20,
'title' => 'Name',
},
'status' => {
'width' => 8,
'title' => 'Status',
},
'dns_name' => {
'width' => 42,
'title' => 'DNS Name',
},
'instance_type' => {
'width' => 13,
'title' => 'Instance Type',
},
'public_ip_address' => {
'width' => 16,
'title' => 'Public IP',
},
'private_ip_address' => {
'width' => 16,
'title' => 'Private IP',
},
'tags' => {
'width' => 30,
'title' => 'tags',
},
},
}
}

puts "Creating configuration file: #{conf}\n..."
conf = File.join(ENV['HOME'], '.claws.yml')
puts "Creating configuration file: #{conf}"
puts '...'
File.open(conf, 'w').write(h.to_yaml)
puts 'Complete!'
end
Expand Down
22 changes: 4 additions & 18 deletions lib/claws/configuration.rb
Expand Up @@ -5,7 +5,7 @@ module Claws
class ConfigurationError < StandardError; end

class Configuration
attr_accessor :path, :capistrano_home, :access_key_id, :secret_access_key, :aws_user
attr_accessor :path, :capistrano, :aws, :ec2

def initialize(use_path = nil)
self.path = use_path || File.join(ENV['HOME'], '.claws.yml')
Expand All @@ -16,23 +16,9 @@ def initialize(use_path = nil)
raise ConfigurationError, "Unable to locate configuration file: #{self.path}"
end

self.capistrano_home = yaml['capistrano_home']
self.access_key_id = yaml['access_key_id']
self.secret_access_key = yaml['secret_access_key']
self.aws_user = yaml['aws_user']
self.fields = yaml['fields'] || {}
end

def fields= fields
@fields = fields.inject({}) {|h,v| h.merge({v[0] => OpenStruct.new(v[1])})}
end

def fields
@fields
end

def aws_credentials
{:access_key_id => self.access_key_id, :secret_access_key => self.secret_access_key}
self.capistrano = OpenStruct.new( yaml['capistrano'] )
self.aws = yaml['aws']
self.ec2 = OpenStruct.new( { :fields => yaml['ec2']['fields'] })
end
end
end
8 changes: 4 additions & 4 deletions lib/claws/report/ec2.rb
Expand Up @@ -17,9 +17,9 @@ def run
row :header => true do
column 'Choice', :width => 6, :color => 'blue', :bold => true, :align => 'right'

self.config.fields.each do |field, properties|
text = properties.title || field
width = properties.width || nil
self.config.ec2.fields.each do |field, properties|
text = properties['title'] || field
width = properties['width'] || nil
column text, :width => width, :color => 'blue', :bold => true
end
end
Expand All @@ -39,7 +39,7 @@ def run
row do
column choice

self.config.fields.each do |field, properties|
self.config.ec2.fields.each do |field, properties|
props = ( field == 'status' ) ? {:color => color} : {}
column i.send( field ), props
end
Expand Down
116 changes: 68 additions & 48 deletions spec/configuration_spec.rb
Expand Up @@ -4,74 +4,94 @@
describe Claws::Configuration do
let (:yaml) do
{
'capistrano_home' => 'test',
'access_key_id' => 'asdf',
'secret_access_key' => 'qwer',
'aws_user' => 'test',
'fields' => {
'id' => {
'width' => 10,
'title' => 'ID'
},
'name' => {
'width' => 20,
'title' => 'Name'
},
'capistrano' => {
'home' => 'test',
},
'aws' => {
'access_key_id' => 'asdf',
'secret_access_key' => 'qwer',
'aws_user' => 'test',
},
'ec2' => {
'fields' => {
'id' => {
'width' => 10,
'title' => 'ID'
},
'name' => {
'width' => 20,
'title' => 'Name'
},
}
}
}
end

let (:config) { Claws::Configuration.new }

context 'defines capistrano home' do
it 'using default path' do
YAML.should_receive(:load_file).with("#{ENV['HOME']}/.claws.yml").and_return(yaml)
config.capistrano_home.should == 'test'
describe '#initialize' do
it 'defines default path' do
c = Claws::Configuration.new
c.path.should == File.join(ENV['HOME'], '.claws.yml')
end

it 'using custom path' do
YAML.should_receive(:load_file).and_return(yaml)
config.capistrano_home.should == 'test'
it 'defines a custom path' do
YAML.should_receive(:load_file).and_return( yaml )
c = Claws::Configuration.new '/home/test'
c.path.should == '/home/test'
end
end

context 'defines AWS credentials' do
before :each do
YAML.should_receive(:load_file).and_return(yaml)
end
it 'raises ConfigurationError' do
YAML.should_receive(:load_file).and_raise( Exception.new )

it 'access key id' do
config.access_key_id.should == 'asdf'
expect {
Claws::Configuration.new
}.should raise_exception Claws::ConfigurationError
end

it 'secret access key' do
config.secret_access_key.should == 'qwer'
context 'Capistrano' do
it 'defines home' do
YAML.should_receive(:load_file).and_return(yaml)
config.capistrano.home.should == 'test'
end
end

it 'user' do
config.aws_user.should == 'test'
end
context 'AWS' do
before :each do
YAML.should_receive(:load_file).and_return(yaml)
end

it 'hash' do
config.aws_credentials.should == {:access_key_id => 'asdf', :secret_access_key => 'qwer'}
end
end
it 'defines user' do
config.aws['aws_user'].should == 'test'
end

context 'defines display fields' do
before :each do
YAML.should_receive(:load_file).and_return(yaml)
end
it 'defines secret access key' do
config.aws['secret_access_key'].should == 'qwer'
end

it 'id' do
id = config.fields['id']
id.width.should == 10
id.title.should == 'ID'
it 'defines access key id' do
config.aws['access_key_id'].should == 'asdf'
end
end

it 'name' do
name = config.fields['name']
name.width.should == 20
name.title.should == 'Name'
context 'EC2' do
before :each do
YAML.should_receive(:load_file).and_return(yaml)
end

context 'fields' do
it 'defines id hash' do
id = config.ec2.fields['id']
id['width'].should == 10
id['title'].should == 'ID'
end

it 'defines name hash' do
name = config.ec2.fields['name']
name['width'].should == 20
name['title'].should == 'Name'
end
end
end
end
end

0 comments on commit e0a556a

Please sign in to comment.