Class: Cloudkick::Command::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudkick/commands/base.rb

Direct Known Subclasses

Help, Pscp, Pssh, Version

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Base

Returns a new instance of Base.



5
6
7
# File 'lib/cloudkick/commands/base.rb', line 5

def initialize(args)
  @args = args
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



3
4
5
# File 'lib/cloudkick/commands/base.rb', line 3

def args
  @args
end

Instance Method Details

#clientObject



18
19
20
21
22
23
24
25
# File 'lib/cloudkick/commands/base.rb', line 18

def client
  if !@client
    key, secret = credentials
    @client = Cloudkick::Base.new(key, secret)
  end

  return @client
end

#credentialsObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/cloudkick/commands/base.rb', line 27

def credentials
  begin
    key = ''
    File.open('/etc/cloudkick.conf') do |f|
      f.grep(/oauth_key (\w+)/) { key = $1 }
    end

    secret = ''
    File.open('/etc/cloudkick.conf') do |f|
      f.grep(/oauth_secret (\w+)/) { secret = $1 }
    end

    return key, secret
  rescue
    raise CommandFailed, 'Unable to open /etc/cloudkick.conf'
  end
end

#display(msg, newline = true) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/cloudkick/commands/base.rb', line 9

def display(msg, newline=true)
  if newline
    puts(msg)
  else
    print(msg)
    STDOUT.flush
  end
end

#extract_option(options, default = true) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/cloudkick/commands/base.rb', line 45

def extract_option(options, default=true)
  values = options.is_a?(Array) ? options : [options]
  return unless opt_index = args.select { |a| values.include? a }.first
  opt_position = args.index(opt_index) + 1
  if args.size > opt_position && opt_value = args[opt_position]
    if opt_value.include?('--')
      opt_value = nil
    else
      args.delete_at(opt_position)
    end
  end
  opt_value ||= default
  args.delete(opt_index)
  block_given? ? yield(opt_value) : opt_value
end