Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 185 lines (156 sloc) 4.584 kb
2a82982 @dreverri Basic implementation of Erlang template helpers
dreverri authored
1 # ErlangTemplateHelper
2
4573ff1 @dreverri Add argument file handler
dreverri authored
3 This library allows one to specify Erlang config and args files in JSON. This is
4 useful when deploying an Erlang application with Chef. For example, here is a
5 snippet of a Riak config file specified in JSON:
3091926 @dreverri Include snippet of Riak JSON config file in README
dreverri authored
6
7 ```json
8 {
9 "riak_kv": {
10 "storage_backend": "riak_kv_multi_backend",
11 "multi_backend_default": "first_backend",
12 "multi_backend": [
09d52de @dreverri Fix invalid JSON
dreverri authored
13 ["__tuple", "first_backend", "riak_kv_bitcask_backend", {
3091926 @dreverri Include snippet of Riak JSON config file in README
dreverri authored
14 "data_root": "__string_/var/lib/riak/bitcask"}],
09d52de @dreverri Fix invalid JSON
dreverri authored
15 ["__tuple", "second_backend", "riak_kv_leveldb_backend", {
3091926 @dreverri Include snippet of Riak JSON config file in README
dreverri authored
16 "data_root": "__string_/var/lib/riak/leveldb"}]
17 ]
18 }
19 }
20 ```
2a82982 @dreverri Basic implementation of Erlang template helpers
dreverri authored
21
22 ## Usage
23
4573ff1 @dreverri Add argument file handler
dreverri authored
24 ### Config
25
2a82982 @dreverri Basic implementation of Erlang template helpers
dreverri authored
26 * JSON strings are Erlang atoms unless prefixed with `__binary_`, or
27 `__string_`. The prefix `__atom_` is also recognized.
28 * `"ok"` becomes `ok`
29 * `"__binary_0b:"` becomes `<<"0b:">>`
30 * `"__string_127.0.0.1"` becomes `"127.0.0.1"`
c769a42 @dreverri Make note of atom handling in README
dreverri authored
31 * `"__atom_ok"` becomes `ok`
32 * Atoms will be single quoted when necessary
33 * `"riak@127.0.0.1"` becomes `'riak@127.0.0.1'`
2a82982 @dreverri Basic implementation of Erlang template helpers
dreverri authored
34 * JSON arrays are Erlang lists unless prefixed with `__tuple`. The prefix
35 `__list` is also recognized.
36 * `[1, 2, 3]` becomes `[1, 2, 3]`
37 * `["__tuple", 1, 2, 3]` becomes `{1, 2, 3}`
38 * `["__list", 1, 2, 3]` becomes `[1, 2, 3]`
39 * JSON objects are Erlang proplists.
40 * `{"storage_backend":"bitcask"}` becomes `[{storage_backend, bitcask}]`
41
42 ```ruby
43 > require "erlang_template_helper"
44 => true
45 > config = Eth::Config.new({"riak_kv" => {"storage_backend" => "bitcask"}})
46 => [{riak_kv, [{storage_backend, bitcask}]}]
47 > puts config.pp
48 [
49 {riak_kv, [
50 {storage_backend, bitcask}
51 ]}
52 ]
53 => nil
54 ```
55
4de1776 @dreverri Update README; add rake console task
dreverri authored
56 #### String, Array Helpers
57
58 Helper methods are provided for the String and Array classes as an alternate to
59 using prefixes in Ruby code. They need to be included into the appropriate
60 class before using.
61
62 Including the modules:
63
64 ```ruby
65 class String
66 include Eth::Erlang::String
67 end
68
69 class Array
70 include Eth::Erlang::Array
71 end
72 ```
73
74 Using `to_erl_string`:
75
76 ```ruby
77 > class String; include Eth::Erlang::String; end
78 => String
79 > Eth::Config.new({"data_root" => "/var/lib/riak/leveldb".to_erl_string})
80 => [{data_root, "/var/lib/riak/leveldb"}].
81 ```
82
83 Using `to_erl_binary`:
84
85 ```ruby
86 > class String; include Eth::Erlang::String; end
87 => String
88 > Eth::Config.new({"0b:".to_erl_binary => "be_blocks"})
89 => [{<<"0b:">>, be_blocks}].
90 ```
91
92 Using `to_erl_tuple`:
93
94 ```ruby
95 class Array; include Eth::Erlang::Array; end
96 > Eth::Config.new(["be_default", "riak_kv_eleveldb_backend", {"max_open_files" => 20}].to_erl_tuple)
97 => {be_default, riak_kv_eleveldb_backend, [{max_open_files, 20}]}.
98 ```
99
4573ff1 @dreverri Add argument file handler
dreverri authored
100 ### Args
101
21f1a2d @dreverri Handle nested objects
dreverri authored
102 Arguments can be specified as a simple JSON object. Nested objects are flattened
21000e0 @dreverri Fix typo in README
dreverri authored
103 by joining the keys and final value by a single space. For example:
4573ff1 @dreverri Add argument file handler
dreverri authored
104
105 ```ruby
106 > require "erlang_template_helper"
107 => true
21f1a2d @dreverri Handle nested objects
dreverri authored
108 > args = Eth::Args.new({"-name" => "riak@127.0.0.1", "-env" => {"ERL_MAX_PORTS" => 4096}})
4573ff1 @dreverri Add argument file handler
dreverri authored
109 => -name riak@127.0.0.1 -env ERL_MAX_PORTS 4096
110 > puts args.pp
111 -name riak@127.0.0.1
112 -env ERL_MAX_PORTS 4096
113 => nil
114 ```
115
9d55552 @dreverri Update README with information about scripts in the bin directory
dreverri authored
116 ## Command line applications
117
118 Two command line applications are provided in `bin`. These are:
119
120 * `config_to_json`
121 * `json_to_config`
122
123 These applications allow one to convert between Erlang config format and the
124 JSON specification used by ErlangTemplateHelper. They work as follows:
125
126 ```bash
127 $ ./bin/config_to_json test/examples/multi_backend.config -p
128 {
129 "riak_kv": {
130 "multi_backend_prefix_list": {
131 "__binary_0b:": "be_blocks"
132 },
133 "multi_backend": [
134 [
135 "__tuple",
136 "be_default",
137 "riak_kv_eleveldb_backend",
138 {
139 "cache_size": 47721858,
140 "data_root": "__string_/var/lib/riak/leveldb",
141 "max_open_files": 50
142 }
143 ],
144 [
145 "__tuple",
146 "be_blocks",
147 "riak_kv_bitcask_backend",
148 {
149 "data_root": "__string_/var/lib/riak/bitcask"
150 }
151 ]
152 ]
153 }
154 }
155 ```
156
157 ```bash
158 $ ./bin/json_to_config test/examples/multi_backend.json -p
159 [
160 {riak_kv, [
161 {multi_backend_prefix_list, [
162 {<<"0b:">>, be_blocks}
163 ]},
164 {multi_backend, [
165 {be_default, riak_kv_eleveldb_backend, [
166 {cache_size, 47721858},
167 {data_root, "/var/lib/riak/leveldb"},
168 {max_open_files, 50}
169 ]},
170 {be_blocks, riak_kv_bitcask_backend, [
171 {data_root, "/var/lib/riak/bitcask"}
172 ]}
173 ]}
174 ]}
175 ].
176 ```
177
2a82982 @dreverri Basic implementation of Erlang template helpers
dreverri authored
178 ## Contributing
179
180 1. Fork it
181 2. Create your feature branch (`git checkout -b my-new-feature`)
182 3. Commit your changes (`git commit -am 'Added some feature'`)
183 4. Push to the branch (`git push origin my-new-feature`)
184 5. Create new Pull Request
Something went wrong with that request. Please try again.