Class: Chook::Configuration

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/chook/configuration.rb

Overview

The configuration object

Constant Summary collapse

DEFAULT_CONF_FILE =

The location of the default config file

Pathname.new '/etc/chook.conf'
SAMPLE_CONF_FILE =
Pathname.new(__FILE__).parent.parent.parent + 'data/chook.conf.example'
CONF_KEYS =

The attribute keys we maintain, and how they should be converted to the value used by chook internally.

For descriptions of the keys, see data/chook.conf.example

{
  port: :to_i,
  concurrency: Chook::Procs::STRING_TO_BOOLEAN,
  handler_dir: Chook::Procs::STRING_TO_PATHNAME,
  use_ssl: Chook::Procs::STRING_TO_BOOLEAN,
  ssl_cert_path: Chook::Procs::STRING_TO_PATHNAME,
  ssl_private_key_path: Chook::Procs::STRING_TO_PATHNAME,
  log_file: Chook::Procs::STRING_TO_PATHNAME,
  log_level: Chook::Procs::STRING_TO_LOG_LEVEL,
  log_max_megs:  :to_i,
  logs_to_keep: :to_i,
  webhooks_user: nil,
  webhooks_user_pw: nil,
  admin_user: nil,
  admin_pw: nil,
  admin_session_expires: :to_i,
  jamf_server: nil,
  jamf_port: :to_i,
  jamf_use_ssl: Chook::Procs::STRING_TO_BOOLEAN,
  jamf_verify_cert: Chook::Procs::STRING_TO_BOOLEAN
}.freeze

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Initialize!



84
85
86
# File 'lib/chook/configuration.rb', line 84

def initialize
  read_global
end

Instance Method Details

#clear_allvoid

This method returns an undefined value.

Clear all values



95
96
97
# File 'lib/chook/configuration.rb', line 95

def clear_all
  CONF_KEYS.keys.each { |k| send "#{k}=".to_sym, nil }
end

This method returns an undefined value.

Print out the current settings to stdout



164
165
166
# File 'lib/chook/configuration.rb', line 164

def print
  CONF_KEYS.keys.sort.each { |k| puts "#{k}: #{send k}" }
end

#read_globalBoolean

(Re)read the global prefs, if it exists.

Returns:

  • (Boolean)

    was the file loaded?



103
104
105
106
# File 'lib/chook/configuration.rb', line 103

def read_global
  return false unless DEFAULT_CONF_FILE.file? && DEFAULT_CONF_FILE.readable?
  read DEFAULT_CONF_FILE
end

#reload(file = DEFAULT_CONF_FILE) ⇒ Boolean

Clear the settings and reload the prefs file, or another file if provided

Parameters:

  • file (String, Pathname) (defaults to: DEFAULT_CONF_FILE)

    a non-standard prefs file to load

Returns:

  • (Boolean)

    was the file reloaded?



114
115
116
117
118
119
# File 'lib/chook/configuration.rb', line 114

def reload(file = DEFAULT_CONF_FILE)
  file = Pathname.new file
  return false unless file.file? && file.readable?
  clear_all
  read file
end

#save(file) ⇒ void

This method returns an undefined value.

Save the prefs into a file

Parameters:

  • file (Symbol, String, Pathname)

    either :user, :global, or an arbitrary file to save.



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/chook/configuration.rb', line 127

def save(file)
  path = Pathname.new(file)

  # file already exists? read it in and update the values.
  # Don't overwrite it, since the user might have comments
  # in there.
  if path.readable?
    data = path.read

    # go thru the known attributes/keys
    CONF_KEYS.keys.sort.each do |k|
      # if the key exists, update it.
      if data =~ /^#{k}:/
        data.sub!(/^#{k}:.*$/, "#{k}: #{send k}")

      # if not, add it to the end unless it's nil
      else
        data += "\n#{k}: #{send k}" unless send(k).nil?
      end # if data =~ /^#{k}:/
    end # each do |k|

  else # not readable, make a new file
    data = ''
    CONF_KEYS.keys.sort.each do |k|
      data << "#{k}: #{send k}\n" unless send(k).nil?
    end
  end # if path readable

  # make sure we end with a newline, the save it.
  data << "\n" unless data.end_with?("\n")
  path.open('w') { |f| f.write data }
end