Class: CLI::Kit::Config
- Inherits:
-
Object
- Object
- CLI::Kit::Config
- Defined in:
- lib/cli/kit/config.rb
Constant Summary collapse
- XDG_CONFIG_HOME =
'XDG_CONFIG_HOME'
Instance Method Summary collapse
-
#file ⇒ Object
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.
-
#get(section, name, default: nil) ⇒ Object
Returns the config corresponding to ‘name` from the config file `false` is returned if it doesn’t exist.
-
#get_bool(section, name, default: false) ⇒ Object
Coalesce and enforce the value of a config to a boolean.
-
#get_path(section, name = nil) ⇒ Object
Returns a path from config in expanded form e.g.
-
#get_section(section) ⇒ Object
Gets the hash for the entire section.
-
#initialize(tool_name:) ⇒ Config
constructor
A new instance of Config.
-
#set(section, name, value) ⇒ Object
Sets the config value in the config file.
- #to_s ⇒ Object
-
#unset(section, name) ⇒ Object
Unsets a config value in the config file.
Constructor Details
#initialize(tool_name:) ⇒ Config
Returns a new instance of Config.
9 10 11 |
# File 'lib/cli/kit/config.rb', line 9 def initialize(tool_name:) @tool_name = tool_name end |
Instance Method Details
#file ⇒ Object
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
108 109 110 111 |
# File 'lib/cli/kit/config.rb', line 108 def file config_home = ENV.fetch(XDG_CONFIG_HOME, '~/.config') File.(File.join(@tool_name, 'config'), config_home) end |
#get(section, name, default: nil) ⇒ Object
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 (nil if none)
#### Example Usage ‘config.get(’name.of.config’)‘
26 27 28 |
# File 'lib/cli/kit/config.rb', line 26 def get(section, name, default: nil) all_configs.dig("[#{section}]", name) || default end |
#get_bool(section, name, default: false) ⇒ Object
Coalesce and enforce the value of a config to a boolean
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/cli/kit/config.rb', line 31 def get_bool(section, name, default: false) case get(section, name, default: default) when 'true' true when 'false' false when default default else raise CLI::Kit::Abort, "Invalid config: #{section}.#{name} is expected to be true or false" end end |
#get_path(section, name = nil) ⇒ Object
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
94 95 96 97 |
# File 'lib/cli/kit/config.rb', line 94 def get_path(section, name = nil) v = get(section, name) false == v ? v : File.(v) end |
#get_section(section) ⇒ Object
Gets the hash for the entire section
#### Parameters ‘section` : the section of the config you are getting
#### Example Usage ‘config.get_section(’section’)‘
81 82 83 |
# File 'lib/cli/kit/config.rb', line 81 def get_section(section) (all_configs["[#{section}]"] || {}).dup end |
#set(section, name, value) ⇒ Object
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’)‘
54 55 56 57 58 |
# File 'lib/cli/kit/config.rb', line 54 def set(section, name, value) all_configs["[#{section}]"] ||= {} all_configs["[#{section}]"][name] = value.nil? ? nil : value.to_s write_config end |
#to_s ⇒ Object
99 100 101 |
# File 'lib/cli/kit/config.rb', line 99 def to_s ini.to_s end |
#unset(section, name) ⇒ Object
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’)‘
69 70 71 |
# File 'lib/cli/kit/config.rb', line 69 def unset(section, name) set(section, name, nil) end |