Overview
Over the past few weeks, this blog has been on building configuration file checking into the application. The strategy has been to make a tool that checks the configuration file matches a grammar and to build a way to automate this process with make. The past two posts have been on matching the grammar, and building the script respectively.
This post builds a simple example which shows this system in use. A helloworld type program which reads name from a configuration file.
The test
The configuration file hello.conf contains the entry:
{name,"Tony"}.
The checker will confirm that this matches the datadef stored in hello_conf.def which contains:
{property_list,[{reqd,name,{list,{builtin,is_integer}}}]}.
Although it is true that the definition is longer than the data it describes, the definition of a configuration file is an application development activity, not an operational matter. It is conceivable that definitions will be provided in a machine readable (rather than human readable) form. Note that in Erlang a string is a list of integers.
The program that reads this configuration file, hello.erl is:
-module (hello).
-export([main/1]).
main(_) ->
Name = get_name(),
io:format("Hello ~s~n",[Name]).
get_name() ->
{ok,Bin} = file:read_file("hello.conf.etf"),
PL = binary_to_term(Bin),
proplists:get_value(name,PL).
So this program produces the string "Hello Tony". This program reads the compiled configuration file hello.conf.etf which is produced by the config_check program we built last blog post.
Finally the compilation and test is run by make. Here is the Makefile:
test: hello.conf.etf ebin/hello.beam
escript ebin/hello.beam
hello.conf.etf:hello_conf.def hello.conf
./config_check hello.conf hello_conf.def
ebin/hello.beam: src/hello.erl
erlc -o ebin src/hello.erl
clean:
rm hello.conf.etf ebin/hello.beam
To run the test type make from the terminal prompt. To run with a different name, simply edit hello.conf and type make again.
No comments:
Post a Comment
Your comments are welcome.