Sha256: 0b6d07ca8ac53afd7a96539835562ac4dbbeea74d85190a82127ef50af2f80cf

Contents?: true

Size: 1.59 KB

Versions: 6

Compression:

Stored size: 1.59 KB

Contents

module BackupJenkins
  class Config
    attr_reader :s3

    def initialize(path = default_config_file_path)
      @config = Hashie::Mash.new(config_file(path))
    end

    def valid?
      !verbose.nil? && jenkins_valid? && aws_valid? && backup_valid?
    end

    def base_file_name
      "#{backup.file_name_base}_#{hostname}"
    end

    def override(options = {})
      @config.merge!(options)
    end

    private

    attr_reader :config

    def default_config_file_path
      "#{ENV['HOME']}/.config/backup_jenkins/config.yml"
    end

    def config_file(config_file_path)
      YAML.load_file(config_file_path)
    rescue Errno::ENOENT
      STDERR.puts "Please create a config file in #{default_config_file_path}"
      STDERR.puts "\nIt should look like:\n\n#{config_file_example}"

      exit 1
    end

    def config_file_example
      File.read(config_file_example_path)
    end

    def config_file_example_path
      File.expand_path('../../../config/config-example.yml', __FILE__)
    end

    def aws_valid?
      aws.access_key &&
        aws.secret &&
        aws.bucket_name
    end

    def jenkins_valid?
      !!jenkins.home
    end

    def backup_valid?
      backup.dir_base &&
        backup.file_name_base &&
        backup.backups_to_keep &&
        backup.backups_to_keep.remote &&
        backup.backups_to_keep.local
    end

    def hostname
      %x{hostname -s}.chomp
    end

    def method_missing(meth, *args, &block)
      return config[meth.to_s] if config.has_key?(meth.to_s)
      super
    end

    def respond_to?(meth)
      config.has_key?(meth.to_s) || super
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
backup_jenkins-0.0.16 lib/backup_jenkins/config.rb
backup_jenkins-0.0.15 lib/backup_jenkins/config.rb
backup_jenkins-0.0.14 lib/backup_jenkins/config.rb
backup_jenkins-0.0.13 lib/backup_jenkins/config.rb
backup_jenkins-0.0.12 lib/backup_jenkins/config.rb
backup_jenkins-0.0.11 lib/backup_jenkins/config.rb