Sha256: bbbc4052a13a98426b7fa9e4af9527a5a2b63f622a4c5a68de2a0a6a5805ca0b

Contents?: true

Size: 2 KB

Versions: 2

Compression:

Stored size: 2 KB

Contents

require 'singleton'

module Lane

  class Configuration

    include ::Singleton

    # The location of the default config file
    DEFAULT_CONF_FILE = Pathname.new '/etc/lane.conf'

    SAMPLE_CONF_FILE = Pathname.new(__FILE__).parent.parent.parent + 'data/lane.conf.example'

    CONF_KEYS = {
        jamf_server: nil,
        jamf_port: :to_i,
        jamf_use_ssl: nil,
        jamf_user: nil,
        jamf_password: nil
    }.freeze

    # automatically create accessors for all the CONF_KEYS
    CONF_KEYS.keys.each { |k| attr_accessor k }

    # Initialize!
    #
    def initialize
      read_global
    end

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

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


    private
    def read(file)
      available_conf_keys = CONF_KEYS.keys

      puts file
      Pathname.new(file).read.each_line do |line|

        # skip blank lines and those starting with #
        next if line =~ /^\s*(#|$)/

        line.strip =~ /^(\w+?):\s*(\S.*)$/
        key = Regexp.last_match(1)
        next unless key
        attr = key.to_sym
        next unless available_conf_keys.include? attr
        setter = "#{key}=".to_sym
        value = Regexp.last_match(2).strip

        # convert the string value read from the file
        # to the correct class
        value &&= case CONF_KEYS[attr]
                  when Proc
                    # If its a proc, pass it to the proc
                    CONF_KEYS[attr].call value

                  when Symbol
                    # otherwise its a symbol method name to call on the string
                    value.send(CONF_KEYS[attr])

                  else
                    value

                  end

        send(setter, value)
      end # do line
      true
    end # read file

  end # class Configuration

  # The single instance of Configuration
  def self.config
    Lane::Configuration.instance
  end


end # module

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
Lane-Reporter-0.1.2 lib/Lane/config.rb
Lane-Reporter-0.1.1 lib/Lane/config.rb