Class: Confidant::Configurator

Inherits:
Object
  • Object
show all
Defined in:
lib/confidant/configurator.rb

Overview

Builds configuration for the Confidant client

Constant Summary collapse

DEFAULT_OPTS =

Default configration options for the Confidant module and this Configurator class, and not for the Client. Pass these through to the CLI for use in the ‘pre` hook, and strip them out of the final config hash used by the Client.

{
  config_files: %w(~/.confidant /etc/confidant/config),
  profile: 'default',
  log_level: 'info'
}.freeze
DEFAULTS =

Default configuration options for the Client.

{
  token_lifetime: 10,
  token_version: 2,
  user_type: 'service',
  region: 'us-east-1'
}.freeze
MANDATORY_CONFIG_KEYS =

Keys that must exist in the final config in order for the Client to be able to function.

{
  global: [:url, :auth_key, :from, :to],
  get_service: [:service]
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}, command = nil) ⇒ Configurator

Instantiate with a hash of configuration opts,and optionally the name of a command that may have mandatory config options that should be validated along with the global config.



37
38
39
# File 'lib/confidant/configurator.rb', line 37

def initialize(opts = {}, command = nil)
  configure(opts, command)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



32
33
34
# File 'lib/confidant/configurator.rb', line 32

def config
  @config
end

Instance Method Details

#configure(opts, command = nil) ⇒ Object

Given a hash of configuration opts, and optionally the name of a command that may have mandatory config options that should be validated along with the global config, load configuration from files, merge config keys together, and validate the presence of sufficient top-level config keys and command-specific config keys to be able to use the client.

Saves and returns the final merged config.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/confidant/configurator.rb', line 49

def configure(opts, command = nil)
  # Merge 'opts' onto DEFAULT_OPTS so that we can self-configure.
  # This is a noop if we were called from CLI,
  # as those keys are defaults in GLI and guaranteed to exist in 'opts',
  # but this is necessary if we were invoked as a lib.
  config = DEFAULT_OPTS.dup.merge(opts)
  log.debug "Local config: #{config}"

  # Merge local config over the profile config from file.
  config = profile_config(
    config[:config_files],
    config[:profile]
  ).dup.merge(config)

  # We don't need any of the internal DEFAULT_OPTS any longer
  DEFAULT_OPTS.keys.each { |k| config.delete(k) }

  # Merge config onto local DEFAULTS
  # to backfill any keys that are needed for KMS.
  config = DEFAULTS.dup.merge(config)

  @config = config
  validate_config(command)
  log.debug "Authoritative config: #{config}"
  @config
end

#validate_config(command = nil) ⇒ Object

Validate the instance’s @config for the presence of all global mandatory config keys. If command is provided, validate the presence of all mandatory config keys specific to that command, otherwise validate that mandatory config keys exist for any command keys that exist in the top-level hash. Raises ConfigurationError if mandatory config options are missing.

Raises:



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/confidant/configurator.rb', line 82

def validate_config(command = nil)
  missing_keys = MANDATORY_CONFIG_KEYS[:global] - @config.keys

  commands_to_verify = if command
                         [command.to_sym]
                       else
                         (MANDATORY_CONFIG_KEYS.keys & @config.keys)
                       end

  commands_to_verify.each do |cmd|
    missing = missing_keys_for_command(cmd)
    next if missing.empty?
    missing_keys << "#{cmd}[#{missing.join(',')}]"
  end

  return true if missing_keys.empty?
  raise ConfigurationError,
        "Missing required config keys: #{missing_keys.join(', ')}"
end