Class: UpdateRepo::CmdConfig

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/update_repo/cmd_config.rb

Overview

This class takes care of reading and parsing the command line and conf file

Instance Method Summary collapse

Methods included from Helpers

#print_log, #trunc_dir

Constructor Details

#initializeCmdConfig

Returns a new instance of CmdConfig.



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/update_repo/cmd_config.rb', line 15

def initialize
  # read the options from Trollop and store in temp variable.
  # we do it this way around otherwise if configuration file is missing it
  # gives the error messages even on '--help' and '--version'
  temp_opt = set_options
  @conf = Confoog::Settings.new(filename: CONFIG_FILE,
                                prefix: 'update_repo',
                                autoload: true, autosave: false)
  @conf['cmd'] = temp_opt
  check_params
  config_error unless @conf.status[:errors] == Status::INFO_FILE_LOADED
end

Instance Method Details

#check_paramsObject

make sure the parameter combinations are valid



53
54
55
56
57
58
59
60
# File 'lib/update_repo/cmd_config.rb', line 53

def check_params
  if true_cmd(:dump) && true_cmd(:import)
    Trollop.die 'Sorry, you cannot specify --dump AND --import'.red
  end
  if true_cmd(:dump) && true_cmd(:dump_remote)
    Trollop.die 'Sorry, you cannot specify --dump AND --dump-remote'.red
  end
end

#getconfigObject

return the configuration hash variable



29
30
31
# File 'lib/update_repo/cmd_config.rb', line 29

def getconfig
  @conf
end

#true_cmd(command) ⇒ Object

This will return the ‘true’ version of a command, taking into account both command line (given preference) and the configuration file. parameter is a :symbol



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/update_repo/cmd_config.rb', line 36

def true_cmd(command)
  cmd_given = @conf['cmd'][(command.to_s + '_given').to_sym]
  cmd_line = @conf['cmd'][command.to_sym]

  if cmd_given
    # if we specify something on the cmd line, that takes precedence
    cmd_line
  elsif !@conf[command.to_s].nil?
    # if we have a value in the config file we use that.
    @conf[command.to_s]
  else
    # this will catch any 'default' values in the cmd setup.
    cmd_line
  end
end