class Checklister::Configuration

This class maintains all system-wide configuration for checklister. It properly applies the correct source of configuration values based on the different contexts and also makes sure that compulsory data are not missing.

Some values are dependent on a context, for example: `host` can have different values based on what service the user wants to connect to. Other values are shared by all those contexts, for example `log_file` is defined once and for all and the service selected by our user does not matter.

checklister can work with several publishing destinations, for example:

## Setting/Selecting the configuration values

When using the checklister binary, you can set one of the many publishing destinations you have access to, via two possible ways:

### 1. CLI Configuration Values

No configuration file required, you can pass your service credentials directly inline as options.

For example, you might do:

“`bash $ checklister –endpoint=api.github.com –private_token==supersecret create … “` NOTE: Every time you pass credentials via command line options, they will override any configuration file you have previously set .

### 2. Configuration File

You can use the command line to add publishing destinations credentials and answer the questions:

“`bash $ checklister setup “`

By default, that configuration file will be saved at `/path/to/home/.checklister.json`. But you can easily use another one by using that option before any checklister commands:

“`bash $ checklister –config=/another/path/to/my_checklister.json setup “` As soon as you have set up one or many publishing destinations, every time you will be using a checklister command you will be prompted to select which service to use, for example:

“`bash $ checklister create …

Select which service to use:
[1] https://github.com/benichu
[2] https://github.com/mdeloupy
[3] https://gitlab.intello.com

“`

## DEVELOPMENT: Using the configuration values

Any time you need to access the credentials, you just need to confidently query the value without worrying about the context selected by the user.

For example:

Constants

ATTRIBUTES

List of all the configuration attributes stored for use within the gem

Attributes

client_certificate_password[RW]
client_certificate_path[RW]
endpoint[RW]
endpoint_certificate_path[RW]
kind[RW]
label[W]
private_token[RW]

Public Instance Methods

apply(attributes = {}) click to toggle source

Apply a configuration hash to a configuration instance

@example Override one of the configuration attributes

config = Checklister::Configuration.new
config.apply(private_token: 'supersecret')
config.private_token #=> "supersecret"

@param attributes [Hash] list of key/values to apply to the configuration @return [Object] the configuration object

# File lib/checklister/configuration.rb, line 89
def apply(attributes = {})
  prepared_attributes = prepare_attributes attributes
  prepared_attributes.each_pair do |attribute, value|
    send("#{attribute}=", value)
  end
  self
end
label() click to toggle source

The label value, if not specifically set, we infer it from the given endpoint url (we use the host)

@return [String] the label string

# File lib/checklister/configuration.rb, line 102
def label
  if instance_variable_get "@label"
    @label
  elsif instance_variable_get "@endpoint"
    URI.parse(@endpoint).host
  end
end
to_hash() click to toggle source

The configuration instance formatted as a stringified hash

@example Override one of the configuration attributes

config = Checklister::Configuration.new
config.to_hash #=> { "endpoint" => "https://gitlab.example.com/api/v3", ..., "private_token" => "supersecret" }

@return [Hash] the configuration object as a Hash

# File lib/checklister/configuration.rb, line 118
def to_hash
  config_hash = ATTRIBUTES.inject({}) do |hash, attr|
    hash["#{attr}"] = instance_variable_get("@#{attr}")
    hash
  end
  Checklister::Sanitizer.symbolize config_hash
end
to_stdout() click to toggle source

Write a configuration summary to STDOUT, useful for output in the CLI

# File lib/checklister/configuration.rb, line 128
def to_stdout
  to_hash.each_pair do |attribute, value|
    puts "%-20s %-50s" % ["#{attribute}:", value]
  end
  nil
end

Private Instance Methods

prepare_attributes(attributes) click to toggle source

Symbolize keys and remove nil or duplicate attributes The attributes usually passed to our configuration class by the CLI are usually full of duplicates and unconsistant keys, we make sure to clean up that input, before doing any configuration work.

@param attributes [Hash] list of key/values @return [Hash] a clean list of key/values

# File lib/checklister/configuration.rb, line 145
def prepare_attributes(attributes)
  # Convert string keys to symbols
  symboled_attributes = Checklister::Sanitizer.symbolize attributes
  # Clean up user_attributes from unwanted, nil and duplicate options
  symboled_attributes.select { |key, _| ATTRIBUTES.include? key }
                     .delete_if { |_, v| v.nil? }
end