Class: SugarJar::Config

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

Overview

This parses SugarJar configs (not to be confused with repoconfigs). This is stuff like log level, github-user, etc.

Constant Summary collapse

DEFAULTS =
{
  'github_user' => ENV.fetch('USER'),
  'gitlab_user' => ENV.fetch('USER'),
  'pr_autofill' => true,
  'pr_autostack' => nil,
  'color' => true,
  'ignore_deprecated_options' => [],
}.freeze

Class Method Summary collapse

Class Method Details

._find_ordered_filesObject



17
18
19
20
21
22
# File 'lib/sugarjar/config.rb', line 17

def self._find_ordered_files
  [
    '/etc/sugarjar/config.yaml',
    "#{Dir.home}/.config/sugarjar/config.yaml",
  ].select { |f| File.exist?(f) }
end

.configObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/sugarjar/config.rb', line 24

def self.config
  SugarJar::Log.debug("Defaults: #{DEFAULTS}")
  c = DEFAULTS.dup
  _find_ordered_files.each do |f|
    SugarJar::Log.debug("Loading config #{f}")
    data = YAML.safe_load_file(f)
    warn_on_deprecated_configs(data, f)
    if data['github_host']
      data['forge_host'] = data['github_host'] if data['forge_host'].nil?
      data.delete('github_host')
    end
    # an empty file is a `nil` which you can't merge
    c.merge!(YAML.safe_load_file(f)) if data
    SugarJar::Log.debug("Modified config: #{c}")
  end
  c
end

.warn_on_deprecated_configs(data, fname) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/sugarjar/config.rb', line 42

def self.warn_on_deprecated_configs(data, fname)
  ignore_deprecated_options = data['ignore_deprecated_options'] || []
  %w{fallthru gh_cli}.each do |opt|
    next unless data.key?(opt)

    if ignore_deprecated_options.include?(opt)
      SugarJar::Log.debug(
        "#{fname}: Not warning about deprecated option `#{opt}` due to " +
        '`ignore_deprecated_options` in that file.',
      )
      next
    end
    SugarJar::Log.warn(
      "#{fname}: contains deprecated option `#{opt}`. You can " +
      'suppress this warning with `ignore_deprecated_options`.',
    )
  end

  # github_host has special handling
  return unless data['github_host']

  if ignore_deprecated_options.include?('github_host')
    SugarJar::Log.debug(
      "#{fname}: Deprecated option `github_host` found, but not " +
      'warning due to `ignore_deprecated_options` in that file.',
    )
  elsif data.key?('forge_host')
    SugarJar::Log.warn(
      "#{fname}: Deprecated option `github_host` found. " +
      'Ignoring in favor of newer `force_host` option. You can ' +
      'suppress this warning with `ignore_deprecated_options`.',
    )
  else
    SugarJar::Log.warn(
      "#{fname}: Deprecated option `github_host` found. " +
      'Treating it as if it was `forge_host` for now. Please update ' +
      'your config file to use this new option. You can suppress ' +
      'this warning with `ignore_deprecated_options`.',
    )
  end
end