squidward 0.5 → 0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,4 @@
1
1
  $LOAD_PATH.unshift(File.dirname(__FILE__))
2
2
  require 'commands/base'
3
+ require 'commands/secured'
3
4
  Dir["#{File.dirname(__FILE__)}/commands/*"].each { |c| require c }
@@ -3,53 +3,7 @@ module Squidward
3
3
  # Base command from where all the other commands inherit
4
4
  # it contains the most used methods (stout, sterror, etc) and
5
5
  # also handles the configuration, hence AWS credentials
6
- class Base
7
- # Creates a new instance and prompts the user for the
8
- # credentials if they aren't stored
9
- def initialize()
10
- unless (@credentials = read_credentials)
11
- @credentials = ask_for_credentials
12
- store_credentials(@credentials)
13
- end
14
- end
15
-
16
- # Gets the AWS Credentials from the user
17
- def ask_for_credentials
18
- puts "Enter your AWS credentials."
19
-
20
- print "Access Key ID: "
21
- user = ask
22
-
23
- print "Secret Access Key: "
24
- password = ask_for_password
25
-
26
- return [user, password]
27
- end
28
-
29
- # Turns off the standard output while writting the password
30
- def echo_off
31
- system "stty -echo"
32
- end
33
-
34
- # Turns on the standard output after writting the password
35
- def echo_on
36
- system "stty echo"
37
- end
38
-
39
- # Gets trimmed input from the user
40
- def ask
41
- gets.strip
42
- end
43
-
44
- # Gets trimmed input from the user but with echoing off
45
- def ask_for_password
46
- echo_off
47
- password = ask
48
- puts
49
- echo_on
50
- return password
51
- end
52
-
6
+ class Base
53
7
  # Reads the configuration from the profile, if the reload parameter is set to true, it will
54
8
  # read it again, if not it will just return what it's already in-memory.
55
9
  def configuration(reload = false)
@@ -61,13 +15,6 @@ module Squidward
61
15
  @configuration = YAML.load_file(path)
62
16
  end
63
17
 
64
- # Reads credentials from the configuration file
65
- def read_credentials
66
- conf = self.configuration
67
- return nil unless conf[:credentials]
68
- return [conf[:credentials][:access_key], conf[:credentials][:secret_key]]
69
- end
70
-
71
18
  # Dumps configuration file contents to the user profile
72
19
  def store_configuration(new_configuration)
73
20
  FileUtils.mkpath(File.expand_path(CONF_PATH))
@@ -76,14 +23,6 @@ module Squidward
76
23
  end
77
24
  end
78
25
 
79
- # Stores the users credentials on the configuration.
80
- def store_credentials(credentials)
81
- conf = configuration
82
- conf[:credentials] = {}
83
- conf[:credentials][:access_key], conf[:credentials][:secret_key] = credentials[0], credentials[1]
84
- store_configuration(conf)
85
- end
86
-
87
26
  # Nice and clean way of echoing
88
27
  def display(msg, newline=true)
89
28
  if newline
@@ -1,7 +1,7 @@
1
1
  module Squidward
2
2
  module Command
3
3
  # Handles all the command related to AWS S3's buckets
4
- class Bucket < Base
4
+ class Bucket < Secured
5
5
  # Sets the destination bucket for the backups to be uploaded on S3
6
6
  def set(args)
7
7
  new_configuration = configuration
@@ -1,13 +1,18 @@
1
1
  module Squidward
2
2
  module Command
3
3
  # Handles all the commands related to credentials
4
- class Credentials < Base
4
+ class Credentials < Secured
5
5
  # Removes the stored credentials from the current configuration
6
6
  def clear(args = nil)
7
7
  new_configuration = configuration
8
8
  new_configuration[:credentials] = nil
9
9
  store_configuration(new_configuration)
10
10
  end
11
+
12
+ # Stores the credentials to the current configuration
13
+ def set(args = nil)
14
+ # don't do anything, already handles it the parent class
15
+ end
11
16
  end
12
17
  end
13
18
  end
@@ -1,7 +1,7 @@
1
1
  module Squidward
2
2
  module Command
3
3
  # Handles everything related to AWS SimpleDB Domains for Backup
4
- class Domain < Base
4
+ class Domain < Secured
5
5
  # Configures a SimpleDB domain for backup. When given the second parameter is
6
6
  # the virtual path inside the bucket to be stored.
7
7
  def backup(args)
@@ -4,6 +4,11 @@ module Squidward
4
4
  class Info < Base
5
5
  # Goes thru the settings and echos their values.
6
6
  def index(args = nil)
7
+ return show_info if (read_credentials)
8
+ puts("Not yet configured. Run 'squidward credentials:set' for configuring credentials.")
9
+ end
10
+
11
+ def show_info
7
12
  display("=== Current Settings")
8
13
  display("Amazon Web Services Account: #{configuration[:credentials][:access_key]}")
9
14
  display("Amazon S3 Bucket: #{configuration[:default_bucket]}")
@@ -21,6 +26,13 @@ module Squidward
21
26
  end
22
27
  display("")
23
28
  end
29
+
30
+ # Reads credentials from the configuration file
31
+ def read_credentials
32
+ conf = self.configuration
33
+ return nil unless conf[:credentials]
34
+ return [conf[:credentials][:access_key], conf[:credentials][:secret_key]]
35
+ end
24
36
  end
25
37
  end
26
38
  end
@@ -1,7 +1,7 @@
1
1
  module Squidward
2
2
  module Command
3
3
  # Core class of the whole application. It simply runs everything once it has been configured.
4
- class Run < Base
4
+ class Run < Secured
5
5
  def initialize()
6
6
  super
7
7
  aws_config = {}
@@ -0,0 +1,68 @@
1
+ module Squidward
2
+ module Command
3
+ # Base command from where all the commands
4
+ # are secured and need credentials, hence it handles AWS credentials
5
+ class Secured < Base
6
+ # Creates a new instance and prompts the user for the
7
+ # credentials if they aren't stored
8
+ def initialize()
9
+ unless (@credentials = read_credentials)
10
+ @credentials = ask_for_credentials
11
+ store_credentials(@credentials)
12
+ end
13
+ end
14
+
15
+ # Gets the AWS Credentials from the user
16
+ def ask_for_credentials
17
+ puts "Enter your AWS credentials."
18
+
19
+ print "Access Key ID: "
20
+ user = ask
21
+
22
+ print "Secret Access Key: "
23
+ password = ask_for_password
24
+
25
+ return [user, password]
26
+ end
27
+
28
+ # Turns off the standard output while writting the password
29
+ def echo_off
30
+ system "stty -echo"
31
+ end
32
+
33
+ # Turns on the standard output after writting the password
34
+ def echo_on
35
+ system "stty echo"
36
+ end
37
+
38
+ # Gets trimmed input from the user
39
+ def ask
40
+ gets.strip
41
+ end
42
+
43
+ # Gets trimmed input from the user but with echoing off
44
+ def ask_for_password
45
+ echo_off
46
+ password = ask
47
+ puts
48
+ echo_on
49
+ return password
50
+ end
51
+
52
+ # Reads credentials from the configuration file
53
+ def read_credentials
54
+ conf = self.configuration
55
+ return nil unless conf[:credentials]
56
+ return [conf[:credentials][:access_key], conf[:credentials][:secret_key]]
57
+ end
58
+
59
+ # Stores the users credentials on the configuration.
60
+ def store_credentials(credentials)
61
+ conf = configuration
62
+ conf[:credentials] = {}
63
+ conf[:credentials][:access_key], conf[:credentials][:secret_key] = credentials[0], credentials[1]
64
+ store_configuration(conf)
65
+ end
66
+ end
67
+ end
68
+ end
@@ -2,7 +2,7 @@ module Squidward
2
2
  module Command
3
3
  # Gem Versioning Helper and also displays current version to the user
4
4
  class Version < Base
5
- GEM_VERSION = "0.5"
5
+ GEM_VERSION = "0.6"
6
6
 
7
7
  # Displays current gem version to the user
8
8
  def index(args = nil)
data/rakefile CHANGED
@@ -19,6 +19,16 @@ task :specs do
19
19
  Rake::Task['specs_run'].invoke
20
20
  end
21
21
 
22
+ namespace :docs do
23
+ Rake::RDocTask.new do |t|
24
+ t.rdoc_dir = 'sdk/public/'
25
+ t.options << '--line-numbers' << '--inline-source' << '-A cattr_accessor=object'
26
+ t.options << '--charset' << 'utf-8'
27
+ t.rdoc_files.include('README.rdoc')
28
+ t.rdoc_files.include('lib/**/*.rb')
29
+ end
30
+ end
31
+
22
32
  namespace :dist do
23
33
  spec = Gem::Specification.new do |s|
24
34
  s.name = 'squidward'
@@ -48,4 +58,9 @@ namespace :dist do
48
58
  Rake::GemPackageTask.new(spec) do |pkg|
49
59
  pkg.need_tar = true
50
60
  end
61
+ end
62
+
63
+ task :cleanup do
64
+ `rm -rf pkg`
65
+ `rm -rf coverage`
51
66
  end
@@ -4,36 +4,7 @@ require 'spec_config'
4
4
 
5
5
  describe "base commands functionality" do
6
6
 
7
- it "should prompt for AWS Credentials when they do not exist" do
8
- Squidward::Command::Base.any_instance.expects(:read_credentials).returns(false)
9
- Squidward::Command::Base.any_instance.expects(:ask_for_credentials).returns(["access_key", "secret_key"])
10
- Squidward::Command::Base.any_instance.expects(:store_credentials).with(["access_key", "secret_key"])
11
- my_command = Squidward::Command::Base.new()
12
- end
13
-
14
- it "should ask for credentials" do
15
- Squidward::Command::Base.any_instance.expects(:read_credentials).returns(true)
16
- my_command = Squidward::Command::Base.new()
17
- my_command.expects(:puts).with("Enter your AWS credentials.")
18
- my_command.expects(:print).with("Access Key ID: ")
19
- my_command.expects(:print).with("Secret Access Key: ")
20
- my_command.expects(:ask).returns("user")
21
- my_command.expects(:ask_for_password).returns("password")
22
- my_command.ask_for_credentials.should == ["user", "password"]
23
- end
24
-
25
- it "when asking for password should turn off echo and turn it on" do
26
- Squidward::Command::Base.any_instance.expects(:read_credentials).returns(true)
27
- my_command = Squidward::Command::Base.new()
28
- my_command.expects(:echo_off)
29
- my_command.expects(:echo_on)
30
- my_command.expects(:puts)
31
- my_command.expects(:ask).returns("password")
32
- my_command.ask_for_password.should == "password"
33
- end
34
-
35
7
  it "should read the configuration when its prompted for config (no relaod)" do
36
- Squidward::Command::Base.any_instance.expects(:read_credentials).returns(true)
37
8
  configuration_path = File.expand_path(File.join(CONF_PATH, SETTINGS_FILE))
38
9
  File.expects(:exists?).with(configuration_path).returns(true)
39
10
  YAML::expects(:load_file).with(configuration_path)
@@ -42,7 +13,6 @@ describe "base commands functionality" do
42
13
  end
43
14
 
44
15
  it "should return the existing configuration when its prompted for config (no relaod)" do
45
- Squidward::Command::Base.any_instance.expects(:read_credentials).returns(true)
46
16
  configuration_path = File.expand_path(File.join(CONF_PATH, SETTINGS_FILE))
47
17
  File.expects(:exists?).with(configuration_path).returns(true)
48
18
  YAML::expects(:load_file).with(configuration_path).returns({:key => "value"})
@@ -53,24 +23,9 @@ describe "base commands functionality" do
53
23
  end
54
24
 
55
25
  it "should store configuration" do
56
- Squidward::Command::Base.any_instance.expects(:read_credentials).returns(true)
57
26
  File.expects(:open).with(File.expand_path(File.join(CONF_PATH, SETTINGS_FILE)), "w+")
58
27
  FileUtils.expects(:mkpath).with(File.expand_path(CONF_PATH))
59
28
  my_command = Squidward::Command::Base.new()
60
29
  my_command.store_configuration({})
61
30
  end
62
-
63
- it "should read credentials when already there" do
64
- Squidward::Command::Base.any_instance.expects(:configuration).returns({:credentials => {:secret_key => "secret", :access_key => "access"}}).twice
65
- my_command = Squidward::Command::Base.new()
66
- my_command.read_credentials.should == ["access", "secret"]
67
- end
68
-
69
- it "should store credentials after getting them" do
70
- Squidward::Command::Base.any_instance.expects(:read_credentials).returns(false)
71
- Squidward::Command::Base.any_instance.expects(:ask_for_credentials).returns(["access_key", "secret_key"])
72
- Squidward::Command::Base.any_instance.expects(:configuration).returns({:credentials => {}})
73
- Squidward::Command::Base.any_instance.expects(:store_configuration).returns()
74
- my_command = Squidward::Command::Base.new()
75
- end
76
31
  end
@@ -2,7 +2,7 @@
2
2
  $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../')
3
3
  require 'spec_config'
4
4
 
5
- describe "runner command functionality" do
5
+ describe "bucket command functionality" do
6
6
  before do
7
7
  Squidward::Command::Bucket.any_instance.expects(:read_credentials).returns(["access_key", "secret_key"])
8
8
  end
@@ -2,11 +2,20 @@
2
2
  $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../')
3
3
  require 'spec_config'
4
4
 
5
- describe "runner command functionality" do
5
+ describe "credentials command functionality" do
6
6
  it "should clear the stored credentials" do
7
7
  my_command = Squidward::Command::Credentials.new()
8
8
  my_command.expects(:configuration).returns({:credentials => ["foo", "bar"]})
9
9
  my_command.expects(:store_configuration).with({:credentials => nil})
10
10
  my_command.clear
11
11
  end
12
+
13
+ it "should store credentials" do
14
+ Squidward::Command::Credentials.any_instance.expects(:read_credentials).returns(false)
15
+ Squidward::Command::Credentials.any_instance.expects(:ask_for_credentials).returns({:credentials => ["foo", "bar"]})
16
+ Squidward::Command::Credentials.any_instance.expects(:store_credentials).returns(true)
17
+
18
+ my_command = Squidward::Command::Credentials.new()
19
+ my_command.set
20
+ end
12
21
  end
@@ -0,0 +1,13 @@
1
+ # enabling the load of files from root (on RSpec)
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../')
3
+ require 'spec_config'
4
+
5
+ describe "info command functionality" do
6
+ it "should tell that is not configured when not configured" do
7
+ Squidward::Command::Info.any_instance.expects(:read_credentials).returns(false)
8
+
9
+ my_command = Squidward::Command::Info.new()
10
+ my_command.expects(:puts).with("Not yet configured. Run 'squidward credentials:set' for configuring credentials.")
11
+ my_command.index
12
+ end
13
+ end
@@ -2,15 +2,7 @@
2
2
  $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../')
3
3
  require 'spec_config'
4
4
 
5
- describe "runner command functionality" do
6
- before do
7
- Squidward::Command::Logs.any_instance.expects(:read_credentials).returns(["access_key", "secret_key"])
8
- end
9
-
10
- it "should inherit the Base functionality" do
11
- my_command = Squidward::Command::Logs.new()
12
- end
13
-
5
+ describe "log command functionality" do
14
6
  it "should set backup default bucket" do
15
7
  my_command = Squidward::Command::Logs.new()
16
8
  my_command.expects(:system).with("tail -n 25 #{File.expand_path(File.join(CONF_PATH, LOG_FILE))}")
@@ -0,0 +1,47 @@
1
+ # enabling the load of files from root (on RSpec)
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../')
3
+ require 'spec_config'
4
+
5
+ describe "secured commands functionality" do
6
+ it "should prompt for AWS Credentials when they do not exist" do
7
+ Squidward::Command::Secured.any_instance.expects(:read_credentials).returns(false)
8
+ Squidward::Command::Secured.any_instance.expects(:ask_for_credentials).returns(["access_key", "secret_key"])
9
+ Squidward::Command::Secured.any_instance.expects(:store_credentials).with(["access_key", "secret_key"])
10
+ my_command = Squidward::Command::Secured.new()
11
+ end
12
+
13
+ it "should ask for credentials" do
14
+ Squidward::Command::Secured.any_instance.expects(:read_credentials).returns(true)
15
+ my_command = Squidward::Command::Secured.new()
16
+ my_command.expects(:puts).with("Enter your AWS credentials.")
17
+ my_command.expects(:print).with("Access Key ID: ")
18
+ my_command.expects(:print).with("Secret Access Key: ")
19
+ my_command.expects(:ask).returns("user")
20
+ my_command.expects(:ask_for_password).returns("password")
21
+ my_command.ask_for_credentials.should == ["user", "password"]
22
+ end
23
+
24
+ it "when asking for password should turn off echo and turn it on" do
25
+ Squidward::Command::Secured.any_instance.expects(:read_credentials).returns(true)
26
+ my_command = Squidward::Command::Secured.new()
27
+ my_command.expects(:echo_off)
28
+ my_command.expects(:echo_on)
29
+ my_command.expects(:puts)
30
+ my_command.expects(:ask).returns("password")
31
+ my_command.ask_for_password.should == "password"
32
+ end
33
+
34
+ it "should read credentials when already there" do
35
+ Squidward::Command::Secured.any_instance.expects(:configuration).returns({:credentials => {:secret_key => "secret", :access_key => "access"}}).twice
36
+ my_command = Squidward::Command::Secured.new()
37
+ my_command.read_credentials.should == ["access", "secret"]
38
+ end
39
+
40
+ it "should store credentials after getting them" do
41
+ Squidward::Command::Secured.any_instance.expects(:read_credentials).returns(false)
42
+ Squidward::Command::Secured.any_instance.expects(:ask_for_credentials).returns(["access_key", "secret_key"])
43
+ Squidward::Command::Secured.any_instance.expects(:configuration).returns({:credentials => {}})
44
+ Squidward::Command::Secured.any_instance.expects(:store_configuration).returns()
45
+ my_command = Squidward::Command::Secured.new()
46
+ end
47
+ end
@@ -2,11 +2,7 @@
2
2
  $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../')
3
3
  require 'spec_config'
4
4
 
5
- describe "runner command functionality" do
6
- before do
7
- Squidward::Command::Version.any_instance.expects(:read_credentials).returns(["access_key", "secret_key"])
8
- end
9
-
5
+ describe "version command functionality" do
10
6
  it "should show version string as {gem_name}-{version}" do
11
7
  my_command = Squidward::Command::Version.new()
12
8
  my_command.expects(:display).with("squidward-#{Squidward::Command::Version::GEM_VERSION}")
metadata CHANGED
@@ -4,8 +4,8 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 5
8
- version: "0.5"
7
+ - 6
8
+ version: "0.6"
9
9
  platform: ruby
10
10
  authors:
11
11
  - Johnny G. Halife
@@ -13,7 +13,7 @@ autorequire:
13
13
  bindir: bin
14
14
  cert_chain: []
15
15
 
16
- date: 2010-04-26 00:00:00 -03:00
16
+ date: 2010-05-03 00:00:00 -03:00
17
17
  default_executable: squidward
18
18
  dependencies:
19
19
  - !ruby/object:Gem::Dependency
@@ -60,14 +60,17 @@ files:
60
60
  - lib/squidward/commands/info.rb
61
61
  - lib/squidward/commands/logs.rb
62
62
  - lib/squidward/commands/run.rb
63
+ - lib/squidward/commands/secured.rb
63
64
  - lib/squidward/commands/version.rb
64
65
  - lib/squidward.rb
65
66
  - spec/commands/base_test.rb
66
67
  - spec/commands/bucket_test.rb
67
68
  - spec/commands/credentials_test.rb
68
69
  - spec/commands/domain_test.rb
70
+ - spec/commands/info_test.rb
69
71
  - spec/commands/logs_test.rb
70
72
  - spec/commands/run_test.rb
73
+ - spec/commands/secured_test.rb
71
74
  - spec/commands/version_test.rb
72
75
  - spec/mock_command.rb
73
76
  - spec/spec_config.rb