Module: Sockd

Defined in:
lib/sockd.rb,
lib/sockd/runner.rb,
lib/sockd/version.rb

Defined Under Namespace

Classes: ConfigFileError, ParseError, Runner

Constant Summary collapse

VERSION =
"0.3.1"

Class Method Summary collapse

Class Method Details

.define(name = nil, options = {}, &block) ⇒ Object Also known as: new



13
14
15
# File 'lib/sockd.rb', line 13

def define(name = nil, options = {}, &block)
  Runner.define(name = nil, options, &block)
end

.parse(runner, argv = ARGV, &block) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/sockd.rb', line 23

def parse(runner, argv = ARGV, &block)
  raise ArgumentError, 'You must provide an instance of Sockd::Runner' unless runner.class <= Runner
  options = {}
  parser = optparser(runner.name, options, &block)
  command, *message = parser.parse(argv)

  if options[:config_save]
    save_path = options[:config_path] || runner.options[:config_path]
    raise ParseError, 'no config file path specified, unable to save' unless save_path
    save_yaml options, save_path

    puts "config saved to: #{save_path}"
    exit
  end

  if options[:config_path]
    read_yaml options, options[:config_path]
  elsif runner.options[:config_path] && File.file?(runner.options[:config_path])
    read_yaml options, runner.options[:config_path]
  end

  runner.options.merge! options

  case command
  when nil
    runner.options[:daemonize] = false
    runner.start
  when 'start', 'stop', 'restart'
    raise ParseError, "invalid arguments for #{command}" unless message.empty?
    runner.public_send command.to_sym
  else
    message.unshift command unless command == 'send'
    raise ParseError, 'no message provided' if message.empty?
    puts runner.send message.join(' ')
  end
rescue OptionParser::ParseError => e
  puts "Error: #{e.message}"
  puts parser
  puts ''
  exit 1
rescue ConfigFileError, Runner::ServiceError => e
  puts "Error: #{e.message}"
  exit 1
end

.run(name = nil, options = {}, &block) ⇒ Object



19
20
21
# File 'lib/sockd.rb', line 19

def run(name = nil, options = {}, &block)
  parse define(name, options, &block)
end