Class: Twin::Config

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

Constant Summary collapse

DEFAULTS =
{
  "global_excludes"           => [".DS_Store"],
  "apex_theme"                => nil,
  "apex_width"                => nil,
  "apex_code_highlight"       => nil,
  "apex_code_highlight_theme" => nil,
  "hosts"                     => {},
  "host"                      => "",
  "target"                    => "",
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Config

Returns a new instance of Config.



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/twin/config.rb', line 22

def initialize(data = {})
  merged = DEFAULTS.merge(data || {})
  @sync_dir                  = ENV["TWIN_SYNC_DIR"] || merged["sync_dir"].to_s
  @global_excludes           = merged["global_excludes"] || []
  @apex_theme                = merged["apex_theme"]
  @apex_width                = merged["apex_width"]
  @apex_code_highlight       = merged["apex_code_highlight"]
  @apex_code_highlight_theme = merged["apex_code_highlight_theme"]
  @hosts                     = merged["hosts"] || {}
  @host                      = ENV["TWIN_HOST"] || merged["host"].to_s
  @target                    = merged["target"].to_s
end

Instance Attribute Details

#apex_code_highlightObject

Returns the value of attribute apex_code_highlight.



6
7
8
# File 'lib/twin/config.rb', line 6

def apex_code_highlight
  @apex_code_highlight
end

#apex_code_highlight_themeObject

Returns the value of attribute apex_code_highlight_theme.



6
7
8
# File 'lib/twin/config.rb', line 6

def apex_code_highlight_theme
  @apex_code_highlight_theme
end

#apex_themeObject

Returns the value of attribute apex_theme.



6
7
8
# File 'lib/twin/config.rb', line 6

def apex_theme
  @apex_theme
end

#apex_widthObject

Returns the value of attribute apex_width.



6
7
8
# File 'lib/twin/config.rb', line 6

def apex_width
  @apex_width
end

#global_excludesObject

Returns the value of attribute global_excludes.



6
7
8
# File 'lib/twin/config.rb', line 6

def global_excludes
  @global_excludes
end

#hostObject

Returns the value of attribute host.



6
7
8
# File 'lib/twin/config.rb', line 6

def host
  @host
end

#hostsObject

Returns the value of attribute hosts.



6
7
8
# File 'lib/twin/config.rb', line 6

def hosts
  @hosts
end

#sync_dirObject

Returns the value of attribute sync_dir.



6
7
8
# File 'lib/twin/config.rb', line 6

def sync_dir
  @sync_dir
end

#targetObject

Returns the value of attribute target.



6
7
8
# File 'lib/twin/config.rb', line 6

def target
  @target
end

Class Method Details

.loadObject



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/twin/config.rb', line 53

def self.load
  path = ENV["TWIN_CONFIG"] || File.join(Dir.home, ".config", "twin", "config.yaml")
  data = {}
  if File.exist?(path)
    begin
      data = YAML.safe_load_file(path) || {}
    rescue Psych::SyntaxError => e
      raise "config syntax error in #{path}: #{e.message}"
    end
  end
  new(data)
end

Instance Method Details

#validate!Object



66
67
68
69
# File 'lib/twin/config.rb', line 66

def validate!
  raise "sync_dir not set (add to ~/.config/twin/config.yaml or set TWIN_SYNC_DIR)" if sync_dir.empty?
  raise "sync_dir not found: #{sync_dir}" unless Dir.exist?(sync_dir)
end

#var_mapObject

Build the flat substitution map => …, dst.home => …, dst.mount => …. Returns empty hash when no hosts are configured (substitution becomes a no-op).



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/twin/config.rb', line 37

def var_map
  return {} if hosts.empty?
  raise "host not set in config"   if host.empty?
  raise "target not set in config" if target.empty?

  src_host = hosts[host]
  dst_host = hosts[target]
  raise "unknown host #{host.inspect} (not in hosts)"     unless src_host
  raise "unknown target #{target.inspect} (not in hosts)" unless dst_host

  map = {}
  src_host.each { |k, v| map["src.#{k}"] = v.to_s }
  dst_host.each { |k, v| map["dst.#{k}"] = v.to_s }
  map
end