Class: Upstart::Exporter::Options::CommandLine

Inherits:
Hash
  • Object
show all
Includes:
Errors
Defined in:
lib/upstart-exporter/options/command_line.rb

Instance Method Summary collapse

Methods included from Errors

#error

Constructor Details

#initialize(command_line_args) ⇒ CommandLine

Returns a new instance of CommandLine.



5
6
7
8
9
10
11
12
13
# File 'lib/upstart-exporter/options/command_line.rb', line 5

def initialize(command_line_args)
  super
  self[:commands] = if command_line_args[:clear]
    {}
  else
    process_procfile(command_line_args[:procfile])
  end
  self[:app_name] = process_appname(command_line_args[:app_name])
end

Instance Method Details

#process_appname(app_name) ⇒ Object



42
43
44
45
# File 'lib/upstart-exporter/options/command_line.rb', line 42

def process_appname(app_name)
  error "Application name should contain only letters (and underscore) and be nonempty, so #{app_name.inspect} is not suitable" unless app_name =~ /^\w+$/
  app_name
end

#process_procfile(name) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/upstart-exporter/options/command_line.rb', line 15

def process_procfile(name)
  error "#{name} is not a readable file" unless FileTest.file?(name)
  commands = {}
  content = File.read(name)
  content.lines.each do |line|
    line.chomp!
    if line =~ /^(\w+?):(.*)$/
      label = $1
      command = $2
      commands[label] = command
    elsif line =~ /^\s*#/
      # do nothing, comment
    elsif line =~ /^\s*$/
      # do nothing, empty
    else
      break if commands['version'] && commands['version'].strip == '2'
      error "procfile version 1 lines should have the following format: 'some_label: command'"
    end
  end
  if commands['version'] && commands['version'].strip == '2'
    commands = YAML.load(content)
    error('procfile should include "commands" key') unless commands['commands']
    error('command names should include only letters and/or underscores') if commands['commands'].keys.find { |k| k !~ /\A[A-z\d_]*?\z/ }
  end
  commands
end