Class: DevDock::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/dev_dock/options.rb

Overview

Handles parsing options with the following syntax: dev_dock subcommand [options..] image [command..]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Options

Returns a new instance of Options.



13
14
15
16
17
18
19
20
21
22
# File 'lib/dev_dock/options.rb', line 13

def initialize(argv)
  @volumes = []
  @environment = []
  @subcommand = nil
  @error = nil
  @image_name = nil
  @host_home = nil
  @argv = argv
  @run_command = ['tmux', 'new']
end

Instance Attribute Details

#environmentObject (readonly)

Returns the value of attribute environment.



10
11
12
# File 'lib/dev_dock/options.rb', line 10

def environment
  @environment
end

#errorObject (readonly)

Returns the value of attribute error.



10
11
12
# File 'lib/dev_dock/options.rb', line 10

def error
  @error
end

#host_homeObject (readonly)

Returns the value of attribute host_home.



10
11
12
# File 'lib/dev_dock/options.rb', line 10

def host_home
  @host_home
end

#image_nameObject (readonly)

Returns the value of attribute image_name.



10
11
12
# File 'lib/dev_dock/options.rb', line 10

def image_name
  @image_name
end

#run_commandObject (readonly)

Returns the value of attribute run_command.



10
11
12
# File 'lib/dev_dock/options.rb', line 10

def run_command
  @run_command
end

#subcommandObject (readonly)

Returns the value of attribute subcommand.



10
11
12
# File 'lib/dev_dock/options.rb', line 10

def subcommand
  @subcommand
end

#volumesObject (readonly)

Returns the value of attribute volumes.



10
11
12
# File 'lib/dev_dock/options.rb', line 10

def volumes
  @volumes
end

Instance Method Details

#inspectObject



89
90
91
# File 'lib/dev_dock/options.rb', line 89

def inspect
  "Subcommand: #{@subcommand}, Image: #{@image_name}, Volumes: #{@volumes}, Environment: #{@environment}, Run Command: #{@run_command}"
end

#parseObject



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/dev_dock/options.rb', line 24

def parse
  parse_subcommand

  if not @error
    if @subcommand == "start"
      parse_start
    else
      parse_end
    end
  end

  parse_env
end

#parse_endObject



42
43
44
45
46
47
48
# File 'lib/dev_dock/options.rb', line 42

def parse_end
  if @argv.length != 2
    @error = 'Invalid number of arguments'
  else
    @image_name = @argv[1]
  end
end

#parse_envObject



38
39
40
# File 'lib/dev_dock/options.rb', line 38

def parse_env
  @host_home = ENV['DEV_DOCK_HOST_HOME'] || ENV['HOME']
end

#parse_startObject



50
51
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
# File 'lib/dev_dock/options.rb', line 50

def parse_start
  parser = OptionParser.new 'Usage: dev_dock start [options..] image [command..]'

  parser.on('-e', '--env ENVIRONMENT') do |env|
    self.environment.push(env)
  end
  parser.on('-v', '--volume VOLUME') do |volume|
    self.volumes.push(volume)
  end
  argv = @argv.slice(1, @argv.length - 1)
  begin
    parser.parse!(argv)
  rescue OptionParser::ParseError => err
    @error = err.reason
  end

  if not @error
    if argv.length == 0
      @error = 'Missing argument: image'
    else
      @image_name = argv.shift()
      if argv.length > 0
        @run_command = argv
      end
    end
  end
end

#parse_subcommandObject



78
79
80
81
82
83
84
85
86
87
# File 'lib/dev_dock/options.rb', line 78

def parse_subcommand
  arg = @argv[0]
  if arg == 'start' or arg == 's'
    @subcommand = 'start'
  elsif arg == 'purge' or arg == 'p'
    @subcommand = 'purge'
  else
    @error = "Invalid subcommand #{arg}"
  end
end