Sha256: 02b2f09e6b2a78fff3b7e9399ce54608a37753e15f61070301c8751140803cb1

Contents?: true

Size: 1.24 KB

Versions: 11

Compression:

Stored size: 1.24 KB

Contents

require 'aptly_cli/version'
require 'yaml'
require 'logger'

module AptlyCli
  # Load aptly-cli configuration
  class AptlyLoad
    def config
      @config
    end

    def initialize
      @log = Logger.new(STDOUT)
      @log.level = Logger::WARN

      # Configuration defaults
      @config = {
        proto: 'http',
        server: '127.0.0.1',
        port: 8082
      }

      @valid_config_keys = @config.keys + [:username, :password, :debug]
    end

    # Configure through hash
    def configure(opts = {})
      opts.each do |k, v|
        config[k.to_sym] = v if @valid_config_keys.include? k.to_sym
      end

      @config
    end

    # Configure through yaml file
    def configure_with(path_to_yaml_file)
      if path_to_yaml_file
        begin
          config = YAML.load(IO.read(path_to_yaml_file))
        rescue Errno::ENOENT
          @log.warn(
            "YAML configuration file couldn\'t be found at " \
             "#{path_to_yaml_file}. Using defaults.")
          return @config
        rescue Psych::SyntaxError
          @log.warn(
            'YAML configuration file contains invalid syntax. Using defaults.')
          return @config
        end
      else
        config = {}
      end

      configure(config)
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
aptly_cli-0.6.1 lib/aptly_load.rb
aptly_cli-0.6.0 lib/aptly_load.rb
aptly_cli-0.5.0 lib/aptly_load.rb
aptly_cli-0.4.2 lib/aptly_load.rb
aptly_cli-0.4.0 lib/aptly_load.rb
aptly_cli-0.3.10 lib/aptly_load.rb
aptly_cli-0.3.8 lib/aptly_load.rb
aptly_cli-0.3.7 lib/aptly_load.rb
aptly_cli-0.3.6 lib/aptly_load.rb
aptly_cli-0.3.5 lib/aptly_load.rb
aptly_cli-0.3.4 lib/aptly_load.rb