class Object
Public Instance Methods
Proxy to {#set} orthogonal to {#[]} / {#get} (though in this case we need to do a little more work than an alias on account of how `#[]=` handled keyword args).
@example Single string key
Locd.config[ 'cli.bash_comp.log.level' ] = :debug
@example List key
Locd.config[ :cli, :bash_comp, :log, :level ] = :debug
@example Checking the type
Locd.config[ 'home', type: t.abs_path ] = user_input
# File lib/locd/config.rb, line 278 def []= *key, value if Hash === key[-1] kwds = key[-1] key = key[0..-2] else kwds = {} end set *key, value, **kwds end
Does `ARGV` look like we're executing Bash completion?
We want to change how we're logging during Bash completion runs.
@return [Boolean]
# File lib/locd/config.rb, line 351 def cli_ARGV_looks_like_bash_comp? ARGV[0] == 'bash-complete' end
Get the logging config, taking account of the Bash completion environment.
Output from this
@return [Hash<Symbol, ]
@todo Document return value.
# File lib/locd/config.rb, line 390 def cli_log_config { application: get( 'cli.log.application' ), level: cli_log_level, dest: cli_log_dest, sync: true } end
@return [$stderr]
# File lib/locd/config.rb, line 369 def cli_log_dest dest = if cli_ARGV_looks_like_bash_comp? get 'cli.bash_comp.log.dest' else get 'cli.log.dest' end { **( IO === dest ? { io: dest } : { file_name: dest.to_s } ), formatter: NRSER::Log::Formatters::Color.new, } end
Level to log at when CLI'ing.
@return [Symbol]
One of {SemanticLogger::LEVELS}.
# File lib/locd/config.rb, line 361 def cli_log_level get( cli_ARGV_looks_like_bash_comp? ? 'bash_comp.log.level' : 'cli.log.level' ) end
# File lib/locd/config.rb, line 198 def env_key_for *key [ from_files( :namespace, :env, type: t.non_empty_str ), *key_path_for( *key ) ].join( '_' ).upcase end
# File lib/locd/config.rb, line 229 def from_env *key, type: nil env_key = env_key_for *key value = ENV[env_key] case value when nil, '' nil else parse_and_check key, value, type: type end end
Get a value from the config files only.
@param [Array<#to_s>] *key
The key to lookup.
# File lib/locd/config.rb, line 186 def from_files *key, type: nil key_path = key_path_for *key value = @from_files.dig Locd::GEM_NAME, *key_path if value.nil? nil else parse_and_check key, value, type: type end end
# File lib/locd/config.rb, line 243 def get *key, type: nil, default: nil env_value = from_env *key, type: type return env_value unless env_value.nil? files_value = from_files *key, type: type return files_value unless files_value.nil? parse_and_check key, default, type: type end
Absolute path to Loc'd home directory (it's workspace). Doesn't check that the directory exists or anything.
@return [Pathname]
# File lib/locd/config.rb, line 309 def home_dir self[:home].to_pn end
# File lib/locd/config.rb, line 176 def key_path_for *key self.class.key_path_for *key end
Directory to save system logs in (proxy, newsyslog, etc…)
@return [Pathname]
# File lib/locd/config.rb, line 318 def log_dir self[ :log, :dir, default: (home_dir / 'log') ] end
# File lib/locd/config.rb, line 206 def parse_and_check key, value, type: nil type = Types.for_key( *key ) if type.nil? return value if type.nil? begin if value.is_a?( String ) && type.has_from_s? type.from_s value else type.check! value end rescue NRSER::TypeError => error error.context.merge! \ key: key, value: value, type: type, current_config_from_files: @from_files raise error end end
# File lib/locd/config.rb, line 256 def set *key, value, type: nil value_str = value.to_s parse_and_check key, value_str, type: type ENV[ env_key_for( *key ) ] = value_str end
# File lib/locd/config.rb, line 327 def tmp_dir self[ :tmp, :dir, default: ( home_dir / 'tmp' ) ] end
# File lib/locd/config.rb, line 290 def to_h @from_files.merge \ "locd" => @from_files["locd"].map_leaves { |key_path, value| env_value = from_env *key_path if env_value.nil? value else env_value end } end
@return [Pathname]
Absolute path to the user config file, which may not exist, but will be loaded if it does.
# File lib/locd/config.rb, line 340 def user_config_path home_dir / 'config.yml' end