class CLI::Kit::Config
Constants
- XDG_CONFIG_HOME
Public Class Methods
# File lib/cli/kit/config.rb, line 9 def initialize(tool_name:) @tool_name = tool_name end
Public Instance Methods
The path on disk at which the configuration is stored:
`$XDG_CONFIG_HOME/<toolname>/config`
if ENV is not set, we default to ~/.config, e.g.:
~/.config/tool/config
# File lib/cli/kit/config.rb, line 106 def file config_home = ENV.fetch(XDG_CONFIG_HOME, '~/.config') File.expand_path(File.join(@tool_name, 'config'), config_home) end
Returns the config corresponding to `name` from the config file `false` is returned if it doesn't exist
#### Parameters `section` : the section of the config value you are looking for `name` : the name of the config value you are looking for
#### Returns `value` : the value of the config variable (false if none)
#### Example Usage `config.get('name.of.config')`
# File lib/cli/kit/config.rb, line 26 def get(section, name, default: false) all_configs.dig("[#{section}]", name) || default end
Coalesce and enforce the value of a config to a boolean
# File lib/cli/kit/config.rb, line 31 def get_bool(section, name, default: false) case get(section, name, default: default).to_s when "true" true when "false" false else raise CLI::Kit::Abort, "Invalid config: #{section}.#{name} is expected to be true or false" end end
Returns a path from config in expanded form e.g. shopify corresponds to ~/src/shopify, but is expanded to /Users/name/src/shopify
#### Example Usage `config.get_path('srcpath', 'shopify')`
#### Returns `path` : the expanded path to the corrsponding value
# File lib/cli/kit/config.rb, line 92 def get_path(section, name = nil) v = get(section, name) false == v ? v : File.expand_path(v) end
Gets the hash for the entire section
#### Parameters `section` : the section of the config you are getting
#### Example Usage `config.get_section('section')`
# File lib/cli/kit/config.rb, line 79 def get_section(section) (all_configs["[#{section}]"] || {}).dup end
Sets the config value in the config file
#### Parameters `section` : the section of the config you are setting `name` : the name of the config you are setting `value` : the value of the config you are setting
#### Example Usage `config.set('section', 'name.of.config', 'value')`
# File lib/cli/kit/config.rb, line 52 def set(section, name, value) all_configs["[#{section}]"] ||= {} all_configs["[#{section}]"][name] = value.nil? ? nil : value.to_s write_config end
# File lib/cli/kit/config.rb, line 97 def to_s ini.to_s end
Unsets a config value in the config file
#### Parameters `section` : the section of the config you are deleting `name` : the name of the config you are deleting
#### Example Usage `config.unset('section', 'name.of.config')`
# File lib/cli/kit/config.rb, line 67 def unset(section, name) set(section, name, nil) end
Private Instance Methods
# File lib/cli/kit/config.rb, line 113 def all_configs ini.ini end
# File lib/cli/kit/config.rb, line 117 def ini @ini ||= CLI::Kit::Ini .new(file, default_section: "[global]", convert_types: false) .tap(&:parse) end
# File lib/cli/kit/config.rb, line 123 def write_config all_configs.each do |section, sub_config| all_configs[section] = sub_config.reject { |_, value| value.nil? } all_configs.delete(section) if all_configs[section].empty? end FileUtils.mkdir_p(File.dirname(file)) File.write(file, to_s) end