Class: CemAcpt::Config::Base
- Inherits:
-
Object
- Object
- CemAcpt::Config::Base
- Defined in:
- lib/cem_acpt/config/base.rb
Overview
Base class for other config classes Child classes should provide the following constant:
- VALID_KEYS - provide an array of valid top-level keys for the config as symbols
Child classes should implement the following methods:
- defaults - provide a hash of default values for the config
- env_var_prefix - provide a string to prefix environment variables with (defaults to 'CEM_ACPT').
This will be converted to uppercase, have all non-alphanumeric characters replaced with
underscores, and be joined with the key name with an underscore to form the environment
variable name.
However, they can override any of the methods in this class.
Direct Known Subclasses
Constant Summary collapse
- BASE_VALID_KEYS =
%i[ ci_mode config_file log_level log_file log_format no_destroy_nodes no_ephemeral_ssh_key platform provisioner puppet quiet terraform user_config verbose ].freeze
- DEEP_MERGE_OPTS =
{ overwrite_arrays: true, merge_nil_values: true, }.freeze
Instance Attribute Summary collapse
-
#config ⇒ Object
(also: #to_h)
readonly
Returns the value of attribute config.
-
#env_vars ⇒ Object
readonly
Returns the value of attribute env_vars.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #ci_mode? ⇒ Boolean (also: #ci?)
- #debug_mode? ⇒ Boolean (also: #debug?)
-
#defaults ⇒ Hash
The default configuration.
- #empty? ⇒ Boolean
-
#env_var_prefix ⇒ String
The prefix for environment variables.
-
#explain ⇒ Object
Returns a string representation of how the config was loaded.
- #get(dot_key) ⇒ Object (also: #dget)
- #has?(dot_key) ⇒ Boolean
-
#initialize(opts: {}, config_file: nil, load_user_config: true) ⇒ Base
constructor
A new instance of Base.
- #inspect ⇒ Object
-
#load(opts: {}, config_file: nil) ⇒ self
Load the configuration from the environment variables, config file, and opts The order of precedence is: 1.
- #quiet_mode? ⇒ Boolean (also: #quiet?)
- #to_json(*args) ⇒ Object
- #to_yaml ⇒ Object
-
#valid_keys ⇒ Array<Symbol>
Valid top-level keys for the config.
- #verbose_mode? ⇒ Boolean (also: #verbose?)
Constructor Details
#initialize(opts: {}, config_file: nil, load_user_config: true) ⇒ Base
Returns a new instance of Base.
51 52 53 54 |
# File 'lib/cem_acpt/config/base.rb', line 51 def initialize(opts: {}, config_file: nil, load_user_config: true) @load_user_config = load_user_config load(opts: opts, config_file: config_file) end |
Instance Attribute Details
#config ⇒ Object (readonly) Also known as: to_h
Returns the value of attribute config.
49 50 51 |
# File 'lib/cem_acpt/config/base.rb', line 49 def config @config end |
#env_vars ⇒ Object (readonly)
Returns the value of attribute env_vars.
49 50 51 |
# File 'lib/cem_acpt/config/base.rb', line 49 def env_vars @env_vars end |
Instance Method Details
#[](key) ⇒ Object
126 127 128 129 130 131 132 133 134 |
# File 'lib/cem_acpt/config/base.rb', line 126 def [](key) if key.is_a?(Symbol) @config[key].dup elsif key.is_a?(String) get(key) else raise ArgumentError, "Invalid key type '#{key.class}'" end end |
#ci_mode? ⇒ Boolean Also known as: ci?
149 150 151 |
# File 'lib/cem_acpt/config/base.rb', line 149 def ci_mode? !!get('ci_mode') || !!(ENV['GITHUB_ACTIONS'] || ENV['CI']) end |
#debug_mode? ⇒ Boolean Also known as: debug?
154 155 156 |
# File 'lib/cem_acpt/config/base.rb', line 154 def debug_mode? get('log_level') == 'debug' end |
#defaults ⇒ Hash
Returns The default configuration.
75 76 77 |
# File 'lib/cem_acpt/config/base.rb', line 75 def defaults {} end |
#empty? ⇒ Boolean
145 146 147 |
# File 'lib/cem_acpt/config/base.rb', line 145 def empty? @config.empty? end |
#env_var_prefix ⇒ String
Returns The prefix for environment variables.
61 62 63 |
# File 'lib/cem_acpt/config/base.rb', line 61 def env_var_prefix 'CEM_ACPT' end |
#explain ⇒ Object
Returns a string representation of how the config was loaded
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/cem_acpt/config/base.rb', line 108 def explain explanation = {} %i[defaults env_vars user_config config_from_file options].each do |source| source_vals = send(source).dup next if source_vals.nil? || source_vals.empty? # The loop below will overwrite the value of explanation[key] if the same key is found in multiple sources # This is intentional, as the last source to set the value is the one that should be used source_vals.each do |key, value| explanation[key] = source if @config.dget(key.to_s) == value end end explained = explanation.each_with_object([]) do |(key, value), ary| ary << "Key '#{key}' from source '#{value}'" end explained.join("\n") end |
#get(dot_key) ⇒ Object Also known as: dget
136 137 138 |
# File 'lib/cem_acpt/config/base.rb', line 136 def get(dot_key) @dot_key_cache[dot_key] ||= @config.dget(dot_key).dup end |
#has?(dot_key) ⇒ Boolean
141 142 143 |
# File 'lib/cem_acpt/config/base.rb', line 141 def has?(dot_key) !!get(dot_key) end |
#inspect ⇒ Object
56 57 58 |
# File 'lib/cem_acpt/config/base.rb', line 56 def inspect "#<#{self.class}:#{object_id.to_s(16)}>" end |
#load(opts: {}, config_file: nil) ⇒ self
Load the configuration from the environment variables, config file, and opts The order of precedence is:
1. environment variables
2. user config file (config.yaml in user_config_dir)
3. specified config file (if it exists)
4. opts
5. static options (set in this class)
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/cem_acpt/config/base.rb', line 89 def load(opts: {}, config_file: nil) create_config_dirs! init_config!(opts: opts, config_file: config_file) add_env_vars!(@config) @config.deep_merge!(user_config, **DEEP_MERGE_OPTS) if user_config && @load_user_config @config.deep_merge!(config_from_file, **DEEP_MERGE_OPTS) if config_from_file @config.deep_merge!(@options, **DEEP_MERGE_OPTS) if @options (@config) @config.format! # Symbolize keys of all hashes validate_config! @dot_key_cache = {} # Freeze the config so it can't be modified # This helps with thread safety and deterministic behavior @config.freeze self end |
#quiet_mode? ⇒ Boolean Also known as: quiet?
164 165 166 |
# File 'lib/cem_acpt/config/base.rb', line 164 def quiet_mode? !!get('quiet') end |
#to_json(*args) ⇒ Object
173 174 175 |
# File 'lib/cem_acpt/config/base.rb', line 173 def to_json(*args) @config.to_json(*args) end |
#to_yaml ⇒ Object
169 170 171 |
# File 'lib/cem_acpt/config/base.rb', line 169 def to_yaml @config.to_yaml end |
#valid_keys ⇒ Array<Symbol>
Returns Valid top-level keys for the config.
66 67 68 69 70 71 72 |
# File 'lib/cem_acpt/config/base.rb', line 66 def valid_keys if self.class.const_defined?(:VALID_KEYS) (BASE_VALID_KEYS + self.class.const_get(:VALID_KEYS)).uniq else BASE_VALID_KEYS end end |
#verbose_mode? ⇒ Boolean Also known as: verbose?
159 160 161 |
# File 'lib/cem_acpt/config/base.rb', line 159 def verbose_mode? !!get('verbose') end |