Created a selfish configuration file library

Overview

I made a gem library around the configuration file with ruby. This library supports:

I had a lot of haze around the configuration file, and I wanted a library that could solve it all at once. The content of the haze is just the flip side of the above support, but I don't want to write the settings in plain text in a text file. I don't want to write different setting items for production, development, etc. and all common setting items in the same file. We have created such thoughts as a starting point.

~~ This library has not been fully tested, so the specifications and reliability have not been fully refined. If you have a better library than this, please let us know in the comments. ,,, ~~ Gefun Gefun. From now on, I will find dissatisfaction points and bugs and update them slowly and slowly for myself. (Perhaps)

How it works concretely

Read configuration file

When there is only one configuration file

Suppose you have the following configuration file:

project_config.yml


---
- :key_path: "/home/vagrant/.crypt_config/project_key.yml"
- :animal:
    :value: |-
      ?0thjXJj2wR4kzp3EidmJ+bgWtYXs7/DZpTlcDFiLjDbsSB2YWnZEB+ovMTNC
      37kx
    :desc: ''
  :fruit:
    :value: |-
      ?wIGrSC/bf5om/rlfIlz7hAIOjaZFjIgw4ulguIKI7YpBrx4JOcmUo3cLH1vC
      eDeW
    :desc: This fruit color is yellow.

When using this in the program, it can be used as follows.

require 'dinomischus'
 
# ex1 
hash = Dinomischus.load_file('config_project.yml')  
# => {:animal=>"owl", 
#     :fruit=>"Lemon"}
p hash[:animal]  # => owl  
p hash[:fruit]   # => Lemon 

If you want to get the description (desc) item as well, add `` `true``` to the argument.

# ex2 
hash = Dinomischus.load_file('project_config.yml', true)
# => {:animal=>{:value=>"owl", :desc=>""},
#     :fruit=>{:value=>"Lemon", :desc=>"This fruit color is yellow."} }

p hash[:fruit][:value]  # => Lemon
p hash[:fruit][:desc]   # => This fruit color is yellow.

When there are multiple configuration files

Even if there are multiple configuration files, they can be read at once. In that case, a separate file that defines the reading order is required. (Project_config_index.yml in the example) If the setting items are duplicated, the setting value of the file read later is given priority. By using this, it is possible to prepare a setting file for the default value and separate the setting file for the development environment and the setting file for the production environment.

project_config_index.yml


---
- :conf_path: "/path/to/config/project_config_default.yml"
- :conf_path: "/path/to/config/project_config_product.yml"

project_config_default.yml


---
- :key_path: "/home/vagrant/.crypt_config/project_key.yml"
- :animal:
    :value: |-
      ?0thjXJj2wR4kzp3EidmJ+bgWtYXs7/DZpTlcDFiLjDbsSB2YWnZEB+ovMTNC
      37kx
    :desc: ''
  :fruit:
    :value: |-
      ?wIGrSC/bf5om/rlfIlz7hAIOjaZFjIgw4ulguIKI7YpBrx4JOcmUo3cLH1vC
      eDeW
    :desc: This fruit color is yellow.

project_config_product.yml


---
- :key_path: "/home/vagrant/.crypt_config/project_key.yml"
- :fruit:
    :value: banana
    :desc:Yellow fruit
  :furniture:
    :value: chair
    :desc:Things to sit on

After reading the above, it will be as follows.

require 'dinomischus'
 
hash = Dinomischus.load_file('project_name_config_index.yml')
# => {:animal=>"owl", 
#     :fruit=>"banana",
#     :furniture=>"chair"}

: The setting value of fruit changed to `` `banana``` depending on the file read later.

Creating / writing a configuration file

Especially for the first time, there are various rules, so Use it by calling a menu from a shell command.

Creating a template file

(It is assumed that the installation described later has been completed.)

$ cd /path/to/Project settings directory(config)Location
$ dinomischus
****** Welcome Egoistic Config ******
  1. Make Template
  2. Add or Update Crypted Value
  3. List Configs Simple
  4. List Configs Specify
  c. Clear Screen
  h. Ruby Command List
  z. End 
-----------> Select Menu [1-4,c,h,z]: 

Select 1 when the ↑ menu appears.

-----------> Select Menu [1-4,c,h,z]: 1
  
****** Make Template ******
  
Input Your Config Prefix Name : ____

If you select 1, you will be asked for the first character string for identification in the configuration file. Please enter any characters. (Here prj) (If there is a file that already exists with the identification character set once, the file creation process is skipped.)

Input Your Config Prefix Name : prj
Make File Default Value is ... 
  Key    File Place [/path/to/Home directory/.crypt_config/prj_key.yml]
  Config File Place [/path/to/Current directory/config/prj_config.yml]
  Define File Place [/path/to/Current directory/config/prj_config_index.yml]
 
Press Enter Key to Next Step... 

Check the created file and press the enter key.

Done! 
Next Step is Add Crypt Config|Plain Config to /path/to/Current directory/config/prj_config.yml .  
Add Config Value then You Select Menu No.2 .
-----------> Select Menu [1-4,c,h,z]: 

This completes the template creation. After that, copy the completed file as appropriate or change the save destination to change it to the configuration you want. If you change the directory, the path of each file is written at the beginning of the configuration file, so Please change that as well.

Addition of setting value (encryption)

Call the menu from the dinomischus command as before.

$ cd /path/to/Project settings directory(config)Location
$ dinomischus
****** Welcome Egoistic Config ******
  1. Make Template
  2. Add or Update Crypted Value
  3. List Configs Simple
  4. List Configs Specify
  c. Clear Screen
  h. Ruby Command List
  z. End 
-----------> Select Menu [1-4,c,h,z]: 

Select 2 when the ↑ menu appears.

-----------> Select Menu [1-4,c,h,z]: 2
 
****** Crypted Value Setting ******"
  Input Your Config Path : config/prj_config.yml
  Input Your Key   : email
  Input Your Value : [email protected]
  Input Your Description :Setting sample E-mail Address. It doesn't exist.
 
Done! 

After entering the configuration file path you want to edit Enter the value you want to set for each item that appears. The encryption of the setting value is over. If all goes well, you should have a configuration file like the one below.

config/prj_config_index.yml


---
- :conf_path: "/home/vagrant/project/config/prj_config.yml"

/path/to/project/config/prj_config.yml


---
- :key_path: "~/.crypt_config/prj_key.yml"
- :dummy:
    :value: ''
    :desc: ''
  :email:
    :value: |-
      ?Afo3Ccn307HZgQgBRwmyUQtCqDus3063wz1h9CIpRMIWdpRC07yfd2TG5jKa
      OrQiDDMfySjQIhWfL1Gt0UJ8tngSiUJT4gfgvjN7/+LHpdk=
    :desc:Setting sample E-mail Address. It doesn't exist.

yaml:~/.crypt_config/prj_key.yml


---
:key:
  :type: sha256
  :value: eUIZzgKKnsJCoLcRX5pXXg

Installation method

Since the installation is registered in ruby gem, it can be done by gem install.

$ gem install dinomischus

Description of each file

Relationship diagram between each file

ConfigFileRelation.png

Definition file ( * _ config_index.yml)

Defines the reading order of the configuration files.

Configuration file ( * _ config.yml)

The link to the key file (key_path) and the text of the setting value are saved.

Key file ( * _ key.yml)

It stores the password used for encryption / decryption.

Source

It is available on KurokoSin / Dinomischus (Github).

Development environment

Ubuntu 20.04LTS  ruby 2.6.3 is. I make it for ruby, not for rails. I wish I could move on rails as well. .. .. ..

Recommended Posts

Created a selfish configuration file library
[CentOS, Eclipse] Load a library file in a C project
Created a menu program
What is a jar file?
[Java] Create a temporary file