Sha256: 64174201e96fc59779f887546f83bfd72410ed789d79d9fc6c7b87373ee484eb

Contents?: true

Size: 1.77 KB

Versions: 1

Compression:

Stored size: 1.77 KB

Contents

require "yaml"

module Caravan
  class Config < Hash
    DEFAULT_CONFIG = {
      "debug" => false,
      "deploy_mode" => "rsync_local",
      "incremental" => true,
      "exclude" => %w(
        .git .svn
      ),
      "once" => false
    }.freeze

    DEFAULT_CONFIG_NAME = "caravan.yml".freeze

    class << self
      def default_conf
        DEFAULT_CONFIG.dup
      end

      def default_conf_name
        DEFAULT_CONFIG_NAME
      end

      def from(user_config_path)
        if File.exist?(user_config_path)
          YAML.load_file(user_config_path)
        else
          Caravan::Message.warn("User configuration [caravan.yml] not found.")
          Caravan::Message.warn("Use `caravan --init` to generate.")
          nil
        end
      end

      def dump(user_config_path, user_config)
        File.open(user_config_path, "w") do |f|
          f.write("# Generated Caravan's configuration file.\n")
          f.write("# Use `caravan --help` to specify the values.\n")
          f.write(user_config.to_yaml)
        end
      end

      def merge(options, conf)
        if conf.nil?
          merged_conf = Caravan::Config.default_conf
        else
          merged_conf = conf
        end

        merged_conf["src"] = options[:src] if options.key?(:src)
        merged_conf["dst"] = options[:dst] if options.key?(:dst)
        merged_conf["debug"] = options[:debug] if options.key?(:debug)
        merged_conf["deploy_mode"] = options[:mode] if options.key?(:mode)
        merged_conf["exclude"] = options[:ignore] if options.key?(:ignore)
        merged_conf["once"] = options[:once] if options.key?(:once)

        merged_conf
      end

      def pretty_puts(conf)
        conf.each do |k, v|
          Caravan::Message.info("=> #{k}: #{v}")
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
caravan-0.6.1 lib/caravan/config.rb