Class: Checklister::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/checklister/configuration.rb

Overview

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:

$ checklister --endpoint=https://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:

$ 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:

$ 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:

$ 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:

  • The user selected the https://github.com/benichu service, querying Checklister.config.username will return the appropriate value github_user
  • But, if the user selects next the https://gitlab.intello.com service, querying Checklister.config.username will return the appropriate value gitlab_user

Constant Summary collapse

ATTRIBUTES =

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

[:endpoint, :private_token, :label, :kind, :client_certificate_path, :client_certificate_password, :endpoint_certificate_path]

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#client_certificate_passwordObject

Returns the value of attribute client_certificate_password.



76
77
78
# File 'lib/checklister/configuration.rb', line 76

def client_certificate_password
  @client_certificate_password
end

#client_certificate_pathObject

Returns the value of attribute client_certificate_path.



76
77
78
# File 'lib/checklister/configuration.rb', line 76

def client_certificate_path
  @client_certificate_path
end

#endpointObject

Returns the value of attribute endpoint.



76
77
78
# File 'lib/checklister/configuration.rb', line 76

def endpoint
  @endpoint
end

#endpoint_certificate_pathObject

Returns the value of attribute endpoint_certificate_path.



76
77
78
# File 'lib/checklister/configuration.rb', line 76

def endpoint_certificate_path
  @endpoint_certificate_path
end

#kindObject

Returns the value of attribute kind.



76
77
78
# File 'lib/checklister/configuration.rb', line 76

def kind
  @kind
end

#labelString

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

Returns:

  • (String)

    the label string



102
103
104
105
106
107
108
# 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

#private_tokenObject

Returns the value of attribute private_token.



76
77
78
# File 'lib/checklister/configuration.rb', line 76

def private_token
  @private_token
end

Instance Method Details

#apply(attributes = {}) ⇒ Object

Apply a configuration hash to a configuration instance

Examples:

Override one of the configuration attributes

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

Parameters:

  • attributes (Hash) (defaults to: {})

    list of key/values to apply to the configuration

Returns:

  • (Object)

    the configuration object



89
90
91
92
93
94
95
# 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

#to_hashHash

The configuration instance formatted as a stringified hash

Examples:

Override one of the configuration attributes

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

Returns:

  • (Hash)

    the configuration object as a Hash



118
119
120
121
122
123
124
# 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_stdoutObject

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



128
129
130
131
132
133
# 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