class Object

Public Instance Methods

[](*key, type: nil, default: nil)
Alias for: get
[]=(*key, value) click to toggle source

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
cli_ARGV_looks_like_bash_comp?() click to toggle source

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
cli_log_config() click to toggle source

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
cli_log_dest() click to toggle source

@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
cli_log_level() click to toggle source

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
env_key_for(*key) click to toggle source
# 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
from_env(*key, type: nil) click to toggle source
# 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
from_files(*key, type: nil) click to toggle source

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
get(*key, type: nil, default: nil) click to toggle source
# 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
Also aliased as: []
home_dir() click to toggle source

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
key_path_for(*key) click to toggle source
# File lib/locd/config.rb, line 176
def key_path_for *key
  self.class.key_path_for *key
end
log_dir() click to toggle source

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
parse_and_check(key, value, type: nil) click to toggle source
# 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
set(*key, value, type: nil) click to toggle source
# 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
tmp_dir() click to toggle source
# File lib/locd/config.rb, line 327
def tmp_dir
  self[
    :tmp,
    :dir,
    default: ( home_dir / 'tmp' )
  ]
end
to_h() click to toggle source
# 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
user_config_path() click to toggle source

@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