Class: Rspawn::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/rspawn/config.rb

Instance Method Summary collapse

Constructor Details

#initialize(root, key, cli_options) ⇒ Config

Returns a new instance of Config.



4
5
6
7
8
# File 'lib/rspawn/config.rb', line 4

def initialize(root, key, cli_options)
  @root = root
  @key = key
  @cli_options = cli_options
end

Instance Method Details

#config_dirObject



14
15
16
# File 'lib/rspawn/config.rb', line 14

def config_dir
  File.dirname(config_path)
end

#config_pathObject



10
11
12
# File 'lib/rspawn/config.rb', line 10

def config_path
  "#{@root}/#{@key}/config.yml"
end

#loadObject



18
19
20
# File 'lib/rspawn/config.rb', line 18

def load
  File.exist?(config_path) ? JSON.parse(File.read(config_path)) : {}
end

#option(command = nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rspawn/config.rb', line 33

def option(command = nil)
  option = {}
  save_option = load
  option[:working_dir] = @cli_options['working_dir'] || save_option['working_dir'] || Dir.pwd
  FileUtils.mkdir_p(option[:working_dir])
  Dir::chdir(option[:working_dir])

  option[:log_file] = @cli_options['log_file'] || save_option['log_file'] || config_dir + '/' + @key + '.log'
  option[:pid_file] = @cli_options['pid_file'] || save_option['pid_file'] || config_dir + '/' + @key + '.pid'
  option[:timeout] = @cli_options['timeout'] || save_option['timeout'] || 10
  option[:processes] = @cli_options['processes'] || save_option['processes'] || 1
  command = command || save_option['command']
  return option, command
end

#removeObject



29
30
31
# File 'lib/rspawn/config.rb', line 29

def remove
  FileUtils.rm_rf(config_dir)
end

#save(option, command) ⇒ Object



22
23
24
25
26
27
# File 'lib/rspawn/config.rb', line 22

def save(option, command)
  FileUtils.mkdir_p(config_dir)
  output = option.to_h
  output[:command] = command || output[:command]
  File.write(config_path, output.to_json)
end