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

Constant Summary collapse

CONFIG_PATH =

This constant holds the path to the config file, default to home dir.

'~/'
CONFIG_FILE =

This constant holds the filename of the config file.

'.updaterepo'

Instance Method Summary collapse

Methods included from Helpers

#trunc_dir

Constructor Details

#initializeCmdConfig

Returns a new instance of CmdConfig.



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/update_repo/cmd_config.rb', line 22

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,
                                location: CONFIG_PATH,
                                prefix: 'update_repo',
                                autoload: true, autosave: false)
  config_error unless @conf.status[:errors] == Status::INFO_FILE_LOADED
  @conf['cmd'] = temp_opt
  check_params
end

Instance Method Details

#[](key) ⇒ various

return the specified TRUE configuration variable, using ‘[]’ notation

Examples:

quiet = @cmd[quiet]

Parameters:

  • key (string)

    Which cmd variable to return

Returns:

  • (various)

    The value of the specified cmd variable



50
51
52
# File 'lib/update_repo/cmd_config.rb', line 50

def [](key)
  true_cmd(key.to_sym)
end

#check_paramsvoid

This method returns an undefined value.

rubocop:disable Layout/LineLength make sure the parameter combinations are valid, terminating otherwise.

Parameters:

  • (none)


79
80
81
82
83
84
# File 'lib/update_repo/cmd_config.rb', line 79

def check_params
  return unless true_cmd(:dump)

  Optimist.die 'You cannot use --dump AND --import'.red if true_cmd(:import)
  Optimist.die 'You cannot use --dump AND --dump-remote'.red if true_cmd(:dump_remote)
end

#getconfigClass

return the configuration hash variable

Examples:

@config = @cmd.getconfig

Parameters:

  • (none)

Returns:

  • (Class)

    Returns the base ‘confoog’ class to the caller.



41
42
43
# File 'lib/update_repo/cmd_config.rb', line 41

def getconfig
  @conf
end

#true_cmd(command) ⇒ various

This will return the ‘true’ version of a command, taking into account both command line (given preference) and the configuration file. ignore the :reek:NilCheck for this function

Parameters:

  • command (symbol)

    The symbol of the defined command

Returns:

  • (various)

    Returns the true value of the comamnd symbol



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/update_repo/cmd_config.rb', line 59

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