Class: Shuttle::CLI

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = nil) ⇒ CLI

Returns a new instance of CLI.



12
13
14
15
# File 'lib/shuttle/cli.rb', line 12

def initialize(path=nil)
  @path    = File.expand_path(path || Dir.pwd)
  @options = default_options
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



5
6
7
# File 'lib/shuttle/cli.rb', line 5

def command
  @command
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/shuttle/cli.rb', line 5

def options
  @options
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

Class Method Details

.runObject



8
9
10
# File 'lib/shuttle/cli.rb', line 8

def self.run
  Shuttle::CLI.new.run
end

Instance Method Details

#default_optionsObject



44
45
46
47
48
49
50
# File 'lib/shuttle/cli.rb', line 44

def default_options
  {
    :path   => nil,
    :target => 'production',
    :log    => false
  }
end

#find_configObject



93
94
95
96
97
98
99
# File 'lib/shuttle/cli.rb', line 93

def find_config
  lookup_files.each { |path| break if try_config(path) }

  if @options[:path].nil?
    terminate("Please provide config with -f option.")
  end
end

#parse_commandObject



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/shuttle/cli.rb', line 79

def parse_command
  case ARGV.size
  when 0
    terminate("Command required")
  when 1
    @command = ARGV.shift
  when 2
    @options[:target] = ARGV.shift
    @command = ARGV.shift
  else
    terminate("Maximum of 2 arguments allowed")
  end
end

#parse_optionsObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/shuttle/cli.rb', line 52

def parse_options
  parser = OptionParser.new do |opts|
    opts.on('-v', '--version', 'Show version') do
      puts "Shuttle version #{Shuttle::VERSION}"
      exit 0
    end

    opts.on('-e', '--environment NAME', 'Deployment target environment') do |v|
      @options[:target] = v
    end

    opts.on('-d', '--debug', 'Enable debugging') do
      @options[:log] = true
    end

    opts.on('-f', '--file PATH', 'Configuration file path') do |v|
      @options[:path] = v
    end
  end

  begin
    parser.parse!
  rescue OptionParser::ParseError => e
    terminate(e.message)
  end
end

#runObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/shuttle/cli.rb', line 17

def run
  parse_options
  parse_command

  if @command == 'generate'
    begin
      generator = Shuttle::Generator.new.run
    rescue Exception => err
      terminate(err.message)
    end
  else
    find_config

    begin
      runner = Shuttle::Runner.new(@options)
      runner.execute(@command.dup)
    rescue Shuttle::ConfigError => err
      terminate(err.message)
    end
  end
end

#terminate(message, status = 1) ⇒ Object



39
40
41
42
# File 'lib/shuttle/cli.rb', line 39

def terminate(message, status=1)
  STDERR.puts(message)
  exit(status)
end

#try_config(path) ⇒ Object



101
102
103
104
105
106
107
108
# File 'lib/shuttle/cli.rb', line 101

def try_config(path)
  if File.exists?(path)
    @options[:path] = path
    true
  else
    false
  end
end