Skip to content

Instantly share code, notes, and snippets.

@aschepis
Created August 24, 2011 17:37
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aschepis/1168630 to your computer and use it in GitHub Desktop.
Save aschepis/1168630 to your computer and use it in GitHub Desktop.
Updating properties in a riak config file
#!/usr/bin/env escript
-export([main/1]).
main([]) ->
{ok,[AppConfig]} = file:consult("app.config"),
[Http] = get_config_value(AppConfig, riak_core, http),
io:format("HTTP Address: ~p~n", [Http]),
Updated = update_config_value(AppConfig, riak_core, http, [{"0.0.0.0", 8080}]),
io:format("Original Values:~n~p~n", [AppConfig]),
io:format("Updated Values:~n~p~n", [Updated]).
update_config_value([], _, _, _) ->
[];
update_config_value([Section | Rest], SectionName, Name, NewValue) ->
case Section of
{SectionName, Values} ->
[{SectionName, update_config_value_in_section(Values, Name, NewValue)} | update_config_value(Rest, SectionName, Name, NewValue)];
Other ->
[Other | update_config_value(Rest, SectionName, Name, NewValue)]
end.
update_config_value_in_section([], _, _) ->
[];
update_config_value_in_section([Value | Rest], Name, NewValue) ->
case Value of
{Name, V} ->
[{Name, NewValue} | update_config_value_in_section(Rest, Name, NewValue)];
Other ->
[Other | update_config_value_in_section(Rest, Name, NewValue)]
end.
get_config_value([], _, _) ->
not_found;
get_config_value([Section | Rest], SectionName, Name) ->
case Section of
{SectionName, Values} ->
get_config_value_in_section(Values, Name);
_ ->
get_config_value(Rest, SectionName, Name)
end.
get_config_value_in_section([], _) ->
not_found;
get_config_value_in_section([Value | Rest], Name) ->
case Value of
{Name, V} ->
V;
_ ->
get_config_value_in_section(Rest, Name)
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment