Class: DPL::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/dpl/cli.rb

Constant Summary collapse

OPTION_PATTERN =
/\A--([a-z][a-z_\-]*)(?:=(.+))?\z/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ CLI

Returns a new instance of CLI.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/dpl/cli.rb', line 13

def initialize(*args)
  options = {}
  args.flatten.each do |arg|
    next options.update(arg) if arg.is_a? Hash
    die("invalid option %p" % arg) unless match = OPTION_PATTERN.match(arg)
    key = match[1].tr('-', '_').to_sym
    if options.include? key
      options[key] = Array(options[key]) << match[2]
    else
      options[key] = match[2] || true
    end
  end

  self.fold_count = 0
  self.options    = default_options.merge(options)
end

Instance Attribute Details

#fold_countObject

Returns the value of attribute fold_count.



11
12
13
# File 'lib/dpl/cli.rb', line 11

def fold_count
  @fold_count
end

#optionsObject

Returns the value of attribute options.



11
12
13
# File 'lib/dpl/cli.rb', line 11

def options
  @options
end

Class Method Details

.run(*args) ⇒ Object



6
7
8
# File 'lib/dpl/cli.rb', line 6

def self.run(*args)
  new(args).run
end

Instance Method Details

#default_optionsObject



46
47
48
49
50
51
# File 'lib/dpl/cli.rb', line 46

def default_options
  {
    :app      => File.basename(Dir.pwd),
    :key_name => %x[hostname].strip
  }
end

#die(message) ⇒ Object



57
58
59
60
# File 'lib/dpl/cli.rb', line 57

def die(message)
  $stderr.puts(message)
  exit 1
end

#envObject



62
63
64
# File 'lib/dpl/cli.rb', line 62

def env
  ENV
end

#fold(message) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/dpl/cli.rb', line 37

def fold(message)
  self.fold_count += 1
  print "travis_fold:start:dpl.#{fold_count}\r" if options[:fold]
  puts "\e[33m#{message}\e[0m"
  yield
ensure
  print "\ntravis_fold:end:dpl.#{fold_count}\r" if options[:fold]
end

#runObject



30
31
32
33
34
35
# File 'lib/dpl/cli.rb', line 30

def run
  provider = Provider.new(self, options)
  provider.deploy
rescue Error => error
  options[:debug] ? raise(error) : die(error.message)
end

#shell(command) ⇒ Object



53
54
55
# File 'lib/dpl/cli.rb', line 53

def shell(command)
  system(command)
end