Class: CLI::Kit::Config

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

Defined Under Namespace

Classes: ConfigWriteError

Constant Summary collapse

XDG_CONFIG_HOME =
'XDG_CONFIG_HOME'

Instance Method Summary collapse

Constructor Details

#initialize(tool_name:) ⇒ Config

: (tool_name: String) -> void



120
121
122
# File 'lib/cli/kit/config.rb', line 120

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

: -> String



218
219
220
221
# File 'lib/cli/kit/config.rb', line 218

def file
  config_home = ENV.fetch(XDG_CONFIG_HOME, '~/.config')
  File.expand_path(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')

: (String section, String name, ?default: String?) -> String?



138
139
140
# File 'lib/cli/kit/config.rb', line 138

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 : (String section, String name, ?default: bool?) -> bool?



144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/cli/kit/config.rb', line 144

def get_bool(section, name, default: false)
  case get(section, name)
  when 'true'
    true
  when 'false'
    false
  when nil
    default
  else
    raise CLI::Kit::Abort, "Invalid config: #{section}.#{name} is expected to be true or false"
  end
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')

: (String section) -> Hash[String, String]



203
204
205
# File 'lib/cli/kit/config.rb', line 203

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')

: (String section, String name, (String | bool)? value) -> void



168
169
170
171
172
173
174
175
176
177
178
# File 'lib/cli/kit/config.rb', line 168

def set(section, name, value)
  all_configs["[#{section}]"] ||= {}
  section = all_configs["[#{section}]"] #: as !nil
  case value
  when nil
    section.delete(name)
  else
    section[name] = value.to_s
  end
  write_config
end

#to_s ⇒ Object

: -> String



208
209
210
# File 'lib/cli/kit/config.rb', line 208

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')

: (String section, String name) -> void



190
191
192
# File 'lib/cli/kit/config.rb', line 190

def unset(section, name)
  set(section, name, nil)
end