Class: Guac::Config

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

Constant Summary collapse

CONFIG_FILE =
File.join(ENV['HOME'], '.guacrc').freeze
DEFAULTS_FILE =
File.join(__dir__, 'templates/config/guacrc.yml').freeze
REQUIRED_FIELDS =
%i(repos pull_strategy default_branch).freeze
OH_NO_YOU_DONTS =
%w(push merge rebase commit clone init mv rm reset).freeze

Class Method Summary collapse

Class Method Details

.configs(reload: false) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/guac/config.rb', line 42

def configs(reload: false)
  if reload
    parse(CONFIG_FILE)
  else
    @configs ||= parse(CONFIG_FILE)
  end
end

.defaultsObject



38
39
40
# File 'lib/guac/config.rb', line 38

def defaults
  parse(DEFAULTS_FILE)
end

.loadObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/guac/config.rb', line 12

def load
  if configs.nil?
    error = "#{CONFIG_FILE} not found\n".yellow
    error += 'Please run ' + '`guac setup`'.green
    raise Guac::Commands::Error, error
  end

  missing_fields = REQUIRED_FIELDS.select { |f| configs[f].nil? || configs[f].empty? }
  if missing_fields.any?
    error = "Missing config for #{missing_fields.join(', ')} \n".yellow
    error += 'Please run ' + '`guac setup`'.green
    raise Guac::Commands::Error, error
  end

  strategy = configs[:pull_strategy]
  cmds = OH_NO_YOU_DONTS.select { |cmd| strategy.include?("git #{cmd}") }
  if cmds.any?
    error = "Your pull strategy is dangerous! `#{strategy.bold}`\n".red
    error += "You really shouldn't use #{cmds.join(', ').bold}\n".yellow
    error += 'Please run ' + '`guac setup`'.green
    raise Guac::Commands::Error, error
  end

  configs
end

.save_configs(body) ⇒ Object



50
51
52
53
54
# File 'lib/guac/config.rb', line 50

def save_configs(body)
  file = File.new(CONFIG_FILE, 'w')
  file.puts(body.to_yaml)
  file.close
end