Class: Heighliner::Config

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

Constant Summary collapse

POSSIBLE_STEERFILES =

Later entries win over earlier ones, so the legacy Kaiser name comes first: a repo with both files uses the Steerfile.

%w[
  Kaiserfile
  Steerfile
  Heighliner.config
  heighliner.config
].freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configObject (readonly)

Returns the value of attribute config.



9
10
11
# File 'lib/heighliner/config.rb', line 9

def config
  @config
end

.config_dirObject (readonly)

Returns the value of attribute config_dir.



9
10
11
# File 'lib/heighliner/config.rb', line 9

def config_dir
  @config_dir
end

.config_fileObject (readonly)

Returns the value of attribute config_file.



9
10
11
# File 'lib/heighliner/config.rb', line 9

def config_file
  @config_file
end

.info_outObject

Returns the value of attribute info_out.



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

def info_out
  @info_out
end

.outObject

Returns the value of attribute out.



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

def out
  @out
end

.steerfileObject (readonly)

Returns the value of attribute steerfile.



9
10
11
# File 'lib/heighliner/config.rb', line 9

def steerfile
  @steerfile
end

.work_dirObject (readonly)

Returns the value of attribute work_dir.



9
10
11
# File 'lib/heighliner/config.rb', line 9

def work_dir
  @work_dir
end

Class Method Details

.always_verbose?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/heighliner/config.rb', line 69

def always_verbose?
  @config[:always_verbose]
end

.detect_config_dirObject

Heighliner used to be called Kaiser. If a user has an old ~/.kaiser directory and no ~/.heighliner directory yet, keep using the old one instead of starting from scratch.



76
77
78
79
80
81
82
83
# File 'lib/heighliner/config.rb', line 76

def detect_config_dir
  heighliner_dir = "#{home_dir}/.heighliner"
  kaiser_dir = "#{home_dir}/.kaiser"

  return kaiser_dir if !Dir.exist?(heighliner_dir) && Dir.exist?(kaiser_dir)

  heighliner_dir
end

.detect_steerfile_nameObject

Kaiserfile is the name Heighliner used when it was called Kaiser. It has the same contents as a Steerfile, so it is loaded the same way.



65
66
67
# File 'lib/heighliner/config.rb', line 65

def detect_steerfile_name
  POSSIBLE_STEERFILES.select { |x| File.exist?(x) }.last
end

.home_dirObject



85
86
87
# File 'lib/heighliner/config.rb', line 85

def home_dir
  ENV['HOME']
end

.load(work_dir, use_steerfile: true) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/heighliner/config.rb', line 15

def load(work_dir, use_steerfile: true)
  @work_dir = work_dir
  @config_dir = detect_config_dir

  migrate_dotted_config_files

  FileUtils.mkdir_p @config_dir
  @config_file = "#{@config_dir}/config.yml"

  @config = {
    envnames: {},
    envs: {},
    networkname: 'heighliner_net',
    shared_names: {
      nginx: 'heighliner-nginx',
      chrome: 'heighliner-chrome',
      dns: 'heighliner-dns',
      certs: 'heighliner-certs'
    },
    largest_port: 9000,
    always_verbose: false
  }

  load_config

  if use_steerfile
    @fname = detect_steerfile_name

    Optimist.die <<~ERROR if @fname.nil?
      No Steerfile in current directory.
      Possible names are #{POSSIBLE_STEERFILES.join(', ')}"
    ERROR

    @steerfile = Steerfile.new("#{@work_dir}/#{@fname}")
  end

  @config
end

.load_configObject



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/heighliner/config.rb', line 104

def load_config
  loaded = YAML.load_file(@config_file) if File.exist?(@config_file)

  config_shared_names = @config[:shared_names] if @config
  loaded_shared_names = loaded[:shared_names] if loaded

  @config = {
    **(@config || {}),
    **(loaded || {}),
    shared_names: { **(config_shared_names || {}), **(loaded_shared_names || {}) }
  }
end

.migrate_dotted_config_filesObject

Up until version 0.5.1, heighliner used dotfiles for all of it configuration. It makes sense of hide the configuration directory itself but hiding the files inside of it just causes confusion.

Heighliner 0.5.2 started using non-dotted files instead. This method renames the old files in case you have just upgraded from an older version.



95
96
97
98
99
100
101
102
# File 'lib/heighliner/config.rb', line 95

def migrate_dotted_config_files
  return unless File.exist?("#{@config_dir}/.config.yml")

  Dir["#{@config_dir}/**/.*"].each do |x|
    dest = x.sub(%r{/\.([a-z.]+)$}, '/\1')
    FileUtils.mv x, dest
  end
end