This library allows one to specify Erlang config and args files in JSON. This is useful when deploying an Erlang application with Chef. For example, here is a snippet of a Riak config file specified in JSON:
{
"riak_kv": {
"storage_backend": "riak_kv_multi_backend",
"multi_backend_default": "first_backend",
"multi_backend": [
["__tuple", "first_backend", "riak_kv_bitcask_backend", {
"data_root": "__string_/var/lib/riak/bitcask"}],
["__tuple", "second_backend", "riak_kv_leveldb_backend", {
"data_root": "__string_/var/lib/riak/leveldb"}]
]
}
}
- JSON strings are Erlang atoms unless prefixed with
__binary_
, or__string_
. The prefix__atom_
is also recognized."ok"
becomesok
"__binary_0b:"
becomes<<"0b:">>
"__string_127.0.0.1"
becomes"127.0.0.1"
"__atom_ok"
becomesok
- Atoms will be single quoted when necessary
"riak@127.0.0.1"
becomes'riak@127.0.0.1'
- JSON arrays are Erlang lists unless prefixed with
__tuple
. The prefix__list
is also recognized.[1, 2, 3]
becomes[1, 2, 3]
["__tuple", 1, 2, 3]
becomes{1, 2, 3}
["__list", 1, 2, 3]
becomes[1, 2, 3]
- JSON objects are Erlang proplists.
{"storage_backend":"bitcask"}
becomes[{storage_backend, bitcask}]
> require "erlang_template_helper"
=> true
> config = Eth::Config.new({"riak_kv" => {"storage_backend" => "bitcask"}})
=> [{riak_kv, [{storage_backend, bitcask}]}]
> puts config.pp
[
{riak_kv, [
{storage_backend, bitcask}
]}
]
=> nil
Helper methods are provided for the String and Array classes as an alternate to using prefixes in Ruby code. They need to be included into the appropriate class before using.
Including the modules:
class String
include Eth::Erlang::String
end
class Array
include Eth::Erlang::Array
end
Using to_erl_string
:
> class String; include Eth::Erlang::String; end
=> String
> Eth::Config.new({"data_root" => "/var/lib/riak/leveldb".to_erl_string})
=> [{data_root, "/var/lib/riak/leveldb"}].
Using to_erl_binary
:
> class String; include Eth::Erlang::String; end
=> String
> Eth::Config.new({"0b:".to_erl_binary => "be_blocks"})
=> [{<<"0b:">>, be_blocks}].
Using to_erl_tuple
:
class Array; include Eth::Erlang::Array; end
> Eth::Config.new(["be_default", "riak_kv_eleveldb_backend", {"max_open_files" => 20}].to_erl_tuple)
=> {be_default, riak_kv_eleveldb_backend, [{max_open_files, 20}]}.
Arguments can be specified as a simple JSON object. Nested objects are flattened by joining the keys and final value by a single space. For example:
> require "erlang_template_helper"
=> true
> args = Eth::Args.new({"-name" => "riak@127.0.0.1", "-env" => {"ERL_MAX_PORTS" => 4096}})
=> -name riak@127.0.0.1 -env ERL_MAX_PORTS 4096
> puts args.pp
-name riak@127.0.0.1
-env ERL_MAX_PORTS 4096
=> nil
Two command line applications are provided in bin
. These are:
config_to_json
json_to_config
These applications allow one to convert between Erlang config format and the JSON specification used by ErlangTemplateHelper. They work as follows:
$ ./bin/config_to_json test/examples/multi_backend.config -p
{
"riak_kv": {
"multi_backend_prefix_list": {
"__binary_0b:": "be_blocks"
},
"multi_backend": [
[
"__tuple",
"be_default",
"riak_kv_eleveldb_backend",
{
"cache_size": 47721858,
"data_root": "__string_/var/lib/riak/leveldb",
"max_open_files": 50
}
],
[
"__tuple",
"be_blocks",
"riak_kv_bitcask_backend",
{
"data_root": "__string_/var/lib/riak/bitcask"
}
]
]
}
}
$ ./bin/json_to_config test/examples/multi_backend.json -p
[
{riak_kv, [
{multi_backend_prefix_list, [
{<<"0b:">>, be_blocks}
]},
{multi_backend, [
{be_default, riak_kv_eleveldb_backend, [
{cache_size, 47721858},
{data_root, "/var/lib/riak/leveldb"},
{max_open_files, 50}
]},
{be_blocks, riak_kv_bitcask_backend, [
{data_root, "/var/lib/riak/bitcask"}
]}
]}
]}
].
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Added some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request