Class: Checklister::Configuration
- Inherits:
-
Object
- Object
- Checklister::Configuration
- 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:
- gitlab.com (Official Hosted Service)
- privately hosted gitlab server
- github.com
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/benichuservice, queryingChecklister.config.usernamewill return the appropriate valuegithub_user - But, if the user selects next the
https://gitlab.intello.comservice, queryingChecklister.config.usernamewill return the appropriate valuegitlab_user
Constant Summary collapse
- ATTRIBUTES =
List of all the configuration attributes stored for use within the gem
[:endpoint, :private_token, :label, :kind]
Instance Attribute Summary collapse
-
#endpoint ⇒ Object
List of accessor attributes.
-
#kind ⇒ Object
List of accessor attributes.
-
#label ⇒ String
The label value, if not specifically set, we infer it from the given endpoint url (we use the host).
-
#private_token ⇒ Object
List of accessor attributes.
Instance Method Summary collapse
-
#apply(attributes = {}) ⇒ Object
Apply a configuration hash to a configuration instance.
-
#to_hash ⇒ Hash
The configuration instance formatted as a stringified hash.
-
#to_stdout ⇒ Object
Write a configuration summary to STDOUT, useful for output in the CLI.
Instance Attribute Details
#endpoint ⇒ Object
List of accessor attributes
77 78 79 |
# File 'lib/checklister/configuration.rb', line 77 def endpoint @endpoint end |
#kind ⇒ Object
List of accessor attributes
77 78 79 |
# File 'lib/checklister/configuration.rb', line 77 def kind @kind end |
#label ⇒ String
The label value, if not specifically set, we infer it from the given endpoint url (we use the host)
104 105 106 107 108 109 110 |
# File 'lib/checklister/configuration.rb', line 104 def label if instance_variable_get "@label" @label elsif instance_variable_get "@endpoint" URI.parse(@endpoint).host end end |
#private_token ⇒ Object
List of accessor attributes
77 78 79 |
# File 'lib/checklister/configuration.rb', line 77 def private_token @private_token end |
Instance Method Details
#apply(attributes = {}) ⇒ Object
Apply a configuration hash to a configuration instance
91 92 93 94 95 96 97 |
# File 'lib/checklister/configuration.rb', line 91 def apply(attributes = {}) prepared_attributes = prepare_attributes attributes prepared_attributes.each_pair do |attribute, value| send("#{attribute}=", value) end self end |
#to_hash ⇒ Hash
The configuration instance formatted as a stringified hash
120 121 122 123 124 125 126 |
# File 'lib/checklister/configuration.rb', line 120 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 ⇒ Object
Write a configuration summary to STDOUT, useful for output in the CLI
130 131 132 133 134 135 |
# File 'lib/checklister/configuration.rb', line 130 def to_stdout to_hash.each_pair do |attribute, value| puts "%-20s %-50s" % ["#{attribute}:", value] end nil end |