Class: AptControl::CLI::Root

Inherits:
Object
  • Object
show all
Extended by:
AptControl::ConfigDSL
Defined in:
lib/apt_control/cli.rb

Constant Summary collapse

DEFAULT_CONFIG_FILE_LOCATION =
'/etc/apt_control/config.yaml'

Instance Method Summary collapse

Methods included from AptControl::ConfigDSL

configs

Instance Method Details

#apt_siteObject



144
145
146
# File 'lib/apt_control/cli.rb', line 144

def apt_site
  @apt_site ||= AptSite.new(config[:apt_site_dir], logger)
end

#build_archiveObject



152
153
154
# File 'lib/apt_control/cli.rb', line 152

def build_archive
  @build_archive ||= BuildArchive.new(config[:build_archive_dir], logger)
end

#build_configObject

Read yaml file if one exists, then apply overrides from the command line



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/apt_control/cli.rb', line 105

def build_config
  file = [options[:config_file], DEFAULT_CONFIG_FILE_LOCATION].
    compact.find {|f| File.exists?(f) }

  hash =
    if file
      YAML.load_file(file).each do |key, value|
      stderr.puts("Warn: Unknown key in config file: #{key}") unless
        self.class.configs.find {|opt| opt.first.to_s == key.to_s }
    end
    else
      {}
    end

  options[:config_option].map {|str| str.split('=') }.
    inject(hash) {|m, (k,v)| m.merge(k.to_sym => v) }
end

#configObject



98
99
100
# File 'lib/apt_control/cli.rb', line 98

def config
  @config ||= build_config
end

#control_fileObject



148
149
150
# File 'lib/apt_control/cli.rb', line 148

def control_file
  @control_file ||= ControlFile.new(config[:control_file], logger)
end

#loggerObject



138
139
140
141
142
# File 'lib/apt_control/cli.rb', line 138

def logger
  @logger ||= Logger.new(config[:log_file] || STDOUT).tap do |logger|
    logger.level = Logger::DEBUG
  end
end

#notifierObject



156
157
158
159
# File 'lib/apt_control/cli.rb', line 156

def notifier
  @notify ||= Notify::Jabber.new(:jid => config[:jabber_id], :logger => logger,
    :password => config[:jabber_password], :room_jid => config[:jabber_chatroom_id])
end

#notify(message) ⇒ Object



161
162
163
164
165
# File 'lib/apt_control/cli.rb', line 161

def notify(message)
  logger.info("notify: #{message}")
  return unless config[:jabber_enabled]
  notifier.message(message)
end

#validate_config!Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/apt_control/cli.rb', line 123

def validate_config!
  self.class.configs.each do |key, desc, options|
    if options[:required]
      config[key] or raise Climate::ExitException, "Error: No config supplied for #{key}"
    end
  end

  if config[:jabber_enabled]
    self.class.configs.each do |key, desc, options|
      next unless key.to_s['jabber_']
      config[key] or raise Climate::ExitException, "Error: you must supply all jabber options if jabber is enabled"
    end
  end
end