cloudkick 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.2.1
data/cloudkick.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{cloudkick}
8
- s.version = "0.2.0"
8
+ s.version = "0.2.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Cloudkick"]
12
- s.date = %q{2010-02-22}
12
+ s.date = %q{2010-02-23}
13
13
  s.default_executable = %q{cloudkick}
14
14
  s.description = %q{Ruby interface to the Cloudkick API}
15
15
  s.email = %q{support@cloudkick.com}
@@ -32,6 +32,7 @@ Gem::Specification.new do |s|
32
32
  "lib/cloudkick/base.rb",
33
33
  "lib/cloudkick/command.rb",
34
34
  "lib/cloudkick/commands/base.rb",
35
+ "lib/cloudkick/commands/help.rb",
35
36
  "lib/cloudkick/commands/parallel.rb",
36
37
  "lib/cloudkick/node.rb",
37
38
  "test/helper.rb",
@@ -9,7 +9,15 @@ module Cloudkick
9
9
 
10
10
  class << self
11
11
  def run(command, args)
12
- run_internal(command, args.dup)
12
+ begin
13
+ run_internal(command, args.dup)
14
+ rescue InvalidCommand
15
+ error "Unknown command. Run 'cloudkick help' for usage information."
16
+ rescue CommandFailed => e
17
+ error e.message
18
+ rescue Interrupt => e
19
+ error "\n[canceled]"
20
+ end
13
21
  end
14
22
 
15
23
  def run_internal(command, args)
@@ -19,6 +27,11 @@ module Cloudkick
19
27
  runner.send(method)
20
28
  end
21
29
 
30
+ def error(msg)
31
+ STDERR.puts(msg)
32
+ exit 1
33
+ end
34
+
22
35
  def parse(command)
23
36
  return eval("Cloudkick::Command::#{command.capitalize}"), :index
24
37
  end
@@ -6,6 +6,15 @@ module Cloudkick::Command
6
6
  @args = args
7
7
  end
8
8
 
9
+ def display(msg, newline=true)
10
+ if newline
11
+ puts(msg)
12
+ else
13
+ print(msg)
14
+ STDOUT.flush
15
+ end
16
+ end
17
+
9
18
  def client
10
19
  if !@client
11
20
  key, secret = credentials
@@ -16,18 +25,37 @@ module Cloudkick::Command
16
25
  end
17
26
 
18
27
  def credentials
19
- key = ''
20
- File.open('/etc/cloudkick.conf') do |f|
21
- f.grep(/oauth_key (\w+)/) { key = $1 }
22
- end
28
+ begin
29
+ key = ''
30
+ File.open('/etc/cloudkick.conf') do |f|
31
+ f.grep(/oauth_key (\w+)/) { key = $1 }
32
+ end
23
33
 
24
- secret = ''
25
- File.open('/etc/cloudkick.conf') do |f|
26
- f.grep(/oauth_secret (\w+)/) { secret = $1 }
27
- end
34
+ secret = ''
35
+ File.open('/etc/cloudkick.conf') do |f|
36
+ f.grep(/oauth_secret (\w+)/) { secret = $1 }
37
+ end
28
38
 
29
- return key, secret
39
+ return key, secret
40
+ rescue
41
+ raise CommandFailed, 'Unable to open /etc/cloudkick.conf'
42
+ end
30
43
  end
31
44
 
45
+ def extract_option(options, default=true)
46
+ values = options.is_a?(Array) ? options : [options]
47
+ return unless opt_index = args.select { |a| values.include? a }.first
48
+ opt_position = args.index(opt_index) + 1
49
+ if args.size > opt_position && opt_value = args[opt_position]
50
+ if opt_value.include?('--')
51
+ opt_value = nil
52
+ else
53
+ args.delete_at(opt_position)
54
+ end
55
+ end
56
+ opt_value ||= default
57
+ args.delete(opt_index)
58
+ block_given? ? yield(opt_value) : opt_value
59
+ end
32
60
  end
33
61
  end
@@ -0,0 +1,72 @@
1
+ module Cloudkick::Command
2
+ class Help < Base
3
+ class HelpGroup < Array
4
+ attr_reader :title
5
+
6
+ def initialize(title)
7
+ @title = title
8
+ end
9
+
10
+ def command(name, description)
11
+ self << [name, description]
12
+ end
13
+
14
+ def space
15
+ self << ['', '']
16
+ end
17
+ end
18
+
19
+ def self.groups
20
+ @groups ||= []
21
+ end
22
+
23
+ def self.group(title, &block)
24
+ groups << begin
25
+ group = HelpGroup.new(title)
26
+ yield group
27
+ group
28
+ end
29
+ end
30
+
31
+ def self.create_default_groups!
32
+ group 'Commands' do |group|
33
+ group.command 'help', 'show this usage'
34
+ group.command 'version', 'show the gem version'
35
+ group.space
36
+ group.command 'pssh <username> <output> <command>', 'parallel ssh your nodes'
37
+ end
38
+ end
39
+
40
+ def index
41
+ display usage
42
+ end
43
+
44
+ def version
45
+ display Cloudkick::Client.version
46
+ end
47
+
48
+ def usage
49
+ longest_command_length = self.class.groups.map do |group|
50
+ group.map { |g| g.first.length }
51
+ end.flatten.max
52
+
53
+ self.class.groups.inject(StringIO.new) do |output, group|
54
+ output.puts "=== %s" % group.title
55
+ output.puts
56
+
57
+ group.each do |command, description|
58
+ if command.empty?
59
+ output.puts
60
+ else
61
+ output.puts "%-*s # %s" % [longest_command_length, command, description]
62
+ end
63
+ end
64
+
65
+ output.puts
66
+ output
67
+ end.string
68
+ end
69
+ end
70
+ end
71
+
72
+ Cloudkick::Command::Help.create_default_groups!
@@ -3,6 +3,10 @@ require 'tempfile'
3
3
  module Cloudkick::Command
4
4
  class Pssh < Base
5
5
  def index
6
+ unless args.size == 3
7
+ raise CommandFailed, 'usage: cloudkick pssh <username> <output> <command>'
8
+ end
9
+
6
10
  file = Tempfile.new('ck')
7
11
 
8
12
  client.get('nodes').each do |node|
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cloudkick
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cloudkick
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-02-22 00:00:00 -08:00
12
+ date: 2010-02-23 00:00:00 -08:00
13
13
  default_executable: cloudkick
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -55,6 +55,7 @@ files:
55
55
  - lib/cloudkick/base.rb
56
56
  - lib/cloudkick/command.rb
57
57
  - lib/cloudkick/commands/base.rb
58
+ - lib/cloudkick/commands/help.rb
58
59
  - lib/cloudkick/commands/parallel.rb
59
60
  - lib/cloudkick/node.rb
60
61
  - test/helper.rb