Class: Metanorma::Cli::Commands::Config

Inherits:
Thor
  • Object
show all
Defined in:
lib/metanorma/cli/commands/config.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


47
# File 'lib/metanorma/cli/commands/config.rb', line 47

def self.exit_on_failure?() true end

.load_configs(options, configs = [Metanorma::Cli.global_config_path, Metanorma::Cli.local_config_path]) ⇒ Object

priority: IDEAL: thor defaults -> global conf -> local conf -> env vars -> passed arguments ACTUAL: all arguments -> global conf -> local conf

  • thor doesn’t provide to differentiate default values against passed args

  • thor doesn’t allow to get all args available for current command



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

def self.load_configs(options, configs = [Metanorma::Cli.global_config_path, Metanorma::Cli.local_config_path])
  result = options.dup
  configs.each do |config_path|
    next unless File.exist?(config_path)

    config_values = ::YAML::load_file(config_path).symbolize_all_keys[:cli] || {}
    result.merge!(config_values)
  end

  result
end

Instance Method Details

#get(name = nil) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/metanorma/cli/commands/config.rb', line 16

def get(name = nil)
  config, config_path = load_config(options[:global], create_default_config: false)

  if name.nil? && File.exist?(config_path)
    UI.say File.read(config_path, encoding: "utf-8")
  else
    UI.say(config.dig(*dig_path(name)) || "nil")
  end
end

#set(name, value = nil) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/metanorma/cli/commands/config.rb', line 27

def set(name, value = nil)
  config, config_path = load_config(options[:global])

  value = try_convert_to_bool(value)
  ypath = dig_path(name)
  deep_set(config, value, *ypath)

  save_config(config, config_path)
end

#unset(name) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/metanorma/cli/commands/config.rb', line 38

def unset(name)
  config, config_path = load_config(options[:global])

  ypath = dig_path(name)
  deep_unset(config, *ypath)

  save_config(config, config_path)
end