Gem Version Build Status Code Climate Test Coverage Issue Count

DuperVisor™ Pro

The super-duper awesome library is your best friend if you want an easy way to convert configuration between any one of the supported formats, which at this time are: JSON, YAML, and Windows INI format.

This tool was originally created to allow storing configs of another great tool called supervisord, which is still highly applicable today, but unfortunately uses a rather archaic configuration file format of the decades old Windows INI format.

Whatever your preferences are, the truth is that some of the modern DevOps tools (such as Ansible and SaltStack) are using YAML format extensively to configure the environments. Ability to "embed" YAML configuration for a tool like supervisord means that you don't have to go to multiple places to see what is being run everywhere.

If you enjoy using this converted, please star the repo and we very much welcome all pull requests and contributions.

YAML/JSON vs INI

Consider the following example taken from the supervisord configuration documentation:

[supervisord]
nodaemon = false
minfds = 1024
minprocs = 200
umask = 022
user = chrism
identifier = supervisor
directory = /tmp
nocleanup = true
childlogdir = /tmp
strip_ansi = false
environment = PATH="/usr/bin:/usr/local/bin:/bin:/sbin",ENVIRONMENT="development"

[program:cat]
command=/bin/cat
process_name=%(program_name)s
numprocs=1
directory=/tmp
umask=022
priority=999
autostart=true
autorestart=unexpected

We think that it is much easier to read this:

supervisord:
  nodaemon: false
  minfds: 1024
  minprocs: 200
  umask: 022
  user: chrism
  identifier: supervisor
  directory: /tmp
  nocleanup: true
  childlogdir: /tmp
  strip_ansi: false
  environment: PATH="/usr/bin:/usr/local/bin:/bin:/sbin",ENVIRONMENT="development"

program:
  cat:
    command: /bin/cat
    process_name: %(program_name)s
    numprocs: 1
    directory: /tmp
    umask: 022
    priority: 999
    autostart: true
    autorestart: unexpected

Not only that, but with this structure you can leverage existing tools for merging information from the default environment to your production, and so on.

Installation

Install the gem:

gem install dupervisor

or, if you are using Bundler, you can add it to your gem file like so:

gem 'dupervisor'

Usage

To perform translations in code, you would use DuperVisor::Parser class to parse an existing format, and DuperVisor::Renderer class to convert the intermediary hash into the destination format:

# This helper extracts format from a file extension
Detector.new('myfile.json').detect  # => :json

# This is how you can parse content by specifying the format
content = Parser.new(File.read('myfile.json')).parse(:json) 

# But the gem can also guess the source format from either 
# the filename (if available) or the source content
content = Parser.new(File.read('myfile.json')).parse()
content.format # => :json
content.parse_result # => { ... } Hash  

# Finally, here we are using Renderer to convert a hash stored
# in content.parse_result into a YAML formatted string.
Renderer.new(content.parse_result).render(:yaml) # => YAML string

You can use the provided executable dupervisor to convert from a JSON or YAML file into an INI

dv [source-file] [options]

Summary: convert between several hierarchical configuration file formats, such as ini, yaml, json Automatically guesses the source format based either on the file extension, or by attempting to parse it for STDIN.

Specific Options

        --ini                        Generate an INI file
        --yaml                       Generate a YAML file
        --json                       Generate a JSON file
    -o, --output [FILE]              File to write, if not supplied write to STDOUT
    -v, --verbose                    Print extra debugging info
    -h, --help                       Show this message
        --version                    Show version

Examples

Guess input format, convert YAML format to an INI file:

$ cat config.yml | dv --ini > config.ini

Guess input format, convert INI format to a JSON file:

$ dv config.ini --json -o config.json

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/kigster/dupervisor.

Author

© 2016 Konstantin Gredeskoul, all rights reserved.

Acknowledgements

  • Shippo, Inc. for sponsoring this work financially, and their commitment to open source.
  • Wissam Jarjoul for many great ideas and a good eye for bugs.

License

This project is distributed under the MIT License.