Class: Caravan::Config

Inherits:
Hash
  • Object
show all
Defined in:
lib/caravan/config.rb

Constant Summary collapse

DEFAULT_CONFIG =
{
  "master" => {
    "debug" => false,
    "deploy_mode" => "rsync",
    "incremental" => true,
    "exclude" => %w(
      .git .svn
    ),
    "once" => false
  }
}.freeze
DEFAULT_CONFIG_NAME =
"caravan.yml".freeze
DEFAULT_SPEC_NAME =
"master".freeze

Class Method Summary collapse

Class Method Details

.default_confObject



21
22
23
# File 'lib/caravan/config.rb', line 21

def default_conf
  DEFAULT_CONFIG.dup
end

.default_conf_nameObject



25
26
27
# File 'lib/caravan/config.rb', line 25

def default_conf_name
  DEFAULT_CONFIG_NAME
end

.default_spec_nameObject



29
30
31
# File 'lib/caravan/config.rb', line 29

def default_spec_name
  DEFAULT_SPEC_NAME
end

.dump(user_config_path, user_config) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/caravan/config.rb', line 43

def dump(user_config_path, user_config)
  # rubocop:disable Metrics/LineLength
  File.open(user_config_path, "w") do |f|
    f.write("# Generated Caravan's configuration file.\n")
    f.write("# Use `caravan --help` for instructions on all the configuration values.\n")
    f.write("# Add `src` and `dst` to specify the source and destination.\n")
    f.write(user_config.to_yaml)
  end
  # rubocop:enable Metrics/LineLength
end

.from(user_config_path) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/caravan/config.rb', line 33

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

.merge(options, conf, spec = Caravan::Config.default_spec_name) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/caravan/config.rb', line 54

def merge(options, conf, spec = Caravan::Config.default_spec_name)
  merged_conf = if conf.nil?
                  Caravan::Message.warn("Fail to load conf. Use default instead.")
                  default_spec_name = Caravan::Config.default_spec_name
                  Caravan::Config.default_conf[default_spec_name]
                else
                  stringify_keys(conf)[spec]
                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

.pretty_puts(conf) ⇒ Object



73
74
75
76
77
# File 'lib/caravan/config.rb', line 73

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