-
Notifications
You must be signed in to change notification settings - Fork 28
/
dump-guts
executable file
·673 lines (617 loc) · 25.3 KB
/
dump-guts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
#!/usr/bin/env escript
%% -*- erlang -*-
-mode(compile).
maybe_add_flag_arg(undefined, Flags) ->
Flags;
maybe_add_flag_arg(FlagArg, Flags) ->
[{FlagArg, true} | Flags].
parse_options_loop([], _BoolFlagP, Flags, NonFlags, LastFlag) ->
{lists:reverse(maybe_add_flag_arg(LastFlag, Flags)), lists:reverse(NonFlags)};
parse_options_loop(["--" | RestArgs], BoolFlagP, Flags, NonFlags, LastFlag) ->
parse_options_loop([], BoolFlagP, Flags, lists:reverse(RestArgs, NonFlags), LastFlag);
parse_options_loop(["--" ++ Arg | RestArgs], BoolFlagP, Flags0, NonFlags, LastFlag) ->
Flags = maybe_add_flag_arg(LastFlag, Flags0),
case lists:splitwith(fun (Chr) -> Chr =/= $= end, Arg) of
{_, ""} ->
case BoolFlagP(Arg) of
true ->
parse_options_loop(RestArgs, BoolFlagP, [{Arg, true} | Flags], NonFlags, undefined);
false ->
parse_options_loop(RestArgs, BoolFlagP, Flags, NonFlags, Arg)
end;
{RealArg, "=" ++ Value} ->
parse_options_loop(RestArgs, BoolFlagP, [{RealArg, Value} | Flags], NonFlags, undefined)
end;
parse_options_loop([Arg | RestArgs], BoolFlagP, Flags, NonFlags, undefined = _LastFlag) ->
parse_options_loop(RestArgs, BoolFlagP, Flags, [Arg | NonFlags], undefined);
parse_options_loop([ArgValue | RestArgs], BoolFlagP, Flags, NonFlags, LastFlag) ->
true = (LastFlag =/= undefined),
parse_options_loop(RestArgs, BoolFlagP, [{LastFlag, ArgValue} | Flags], NonFlags, undefined).
filter_bad_flags(Flags, BoolFlagP, AccBool, AccNonBool) ->
case Flags of
[] ->
{AccBool, AccNonBool};
[{Flag, Value} | RestFlags] ->
NeedBool = BoolFlagP(Flag),
GotBool = (Value =:= true),
case NeedBool =:= GotBool of
false ->
case NeedBool of
true -> filter_bad_flags(RestFlags, BoolFlagP, [Flag | AccBool], AccNonBool);
_ -> filter_bad_flags(RestFlags, BoolFlagP, AccBool, [Flag, AccNonBool])
end;
_ ->
filter_bad_flags(RestFlags, BoolFlagP, AccBool, AccNonBool)
end
end.
parse_options(Args, BoolFlagP) ->
{Flags, NonFlags} = parse_options_loop(Args, BoolFlagP, [], [], undefined),
case filter_bad_flags(Flags, BoolFlagP, [], []) of
{[], []} ->
{ok, Flags, NonFlags};
{BadBool, BadNonBool} ->
{bad_flags, BadBool, BadNonBool, Flags, NonFlags}
end.
complain(Format, Args) ->
io:format(standard_error, Format, Args).
maybe_report_unknown_flags(Flags) ->
case [Flag || {Flag, _} <- Flags,
not valid_option(Flag)] of
[] ->
true;
BadFlags ->
[complain("Got unknown flag: --~s~n", [Flag]) || Flag <- BadFlags],
false
end.
options_descriptions() ->
[{"static-config", "<path-to-config>"},
{"node-file", "<path-to-node-file>"},
{"node-name", undefined},
{"couch-inis", "<paths to etc/couchdb/*.ini separated by ;>"},
{"initargs-path", "<path to var/lib/couchbase/initargs>"},
{"guess-prefix", bool},
{"help", bool}].
valid_option(Name) ->
case lists:keyfind(Name, 1, options_descriptions()) of
false -> false;
_ -> true
end.
bool_option_p(Name) ->
case lists:keyfind(Name, 1, options_descriptions()) of
{_, bool} -> true;
_ -> false
end.
parse_app_flags(Args) ->
case parse_options(Args, fun (X) -> bool_option_p(X) end) of
{ok, Flags, NonFlags} ->
case maybe_report_unknown_flags(Flags) of
true ->
{Flags, NonFlags};
_ ->
complain("\n", []),
usage(),
init:stop(1)
end;
{bad_flags, BadBools, BadNonBools, Flags, _} ->
[complain("Value flag ~s (i.e. --~s=<value>) is used without value~n", [Flag, Flag])
|| Flag <- BadNonBools,
valid_option(Flag)],
[complain("Bool flag --~s used as value flag (--~s=<value>)~n", [Flag, Flag])
|| Flag <- BadBools,
valid_option(Flag)],
maybe_report_unknown_flags(Flags),
complain("\n", []),
usage(),
init:stop(1)
end.
main(Args) ->
try
case parse_app_flags(Args) of
{Flags, NonFlags} ->
case proplists:get_value("help", Flags) of
true ->
usage(),
init:stop(0);
_ ->
main_main(Flags, NonFlags)
end;
X ->
X
end
catch T:E ->
Stack = erlang:get_stacktrace(),
complain("Got exception: ~p~n~p~n~n", [{T, E}, Stack]),
timer:sleep(100),
erlang:raise(T, E, Stack)
end.
-record(config, {static_config,
node_file,
node_name,
couch_inis,
initargs_path,
guess_prefix}).
flag_to_config_field("static-config") -> #config.static_config;
flag_to_config_field("node-file") -> #config.node_file;
flag_to_config_field("node-name") -> #config.node_name;
flag_to_config_field("couch-inis") -> #config.couch_inis;
flag_to_config_field("initargs-path") -> #config.initargs_path;
flag_to_config_field("guess-prefix") -> #config.guess_prefix.
confget(Field) ->
Config = erlang:get(config),
erlang:element(Field, Config).
do_get_initargs(Path) ->
case file:read_file(Path) of
{ok, Binary} ->
case (catch {ok, binary_to_term(Binary)}) of
{ok, _Term} = RV ->
RV;
Err ->
{error, {binary_to_term_failed, Err}}
end;
Err ->
Err
end.
get_initargs(#config{initargs_path = Path}) ->
Key = {get_initargs, Path},
case erlang:get(Key) of
undefined ->
RV = do_get_initargs(Path),
case RV of
{ok, _} ->
%% complain("Got initargs:~n~p~n", [RV]),
ok;
_ ->
complain("Failed to read initargs:~n~p~n", [RV])
end,
erlang:put(Key, RV),
RV;
RV ->
RV
end.
get_app_init_arg(InitArgs, App, Flag) ->
RV = do_get_app_init_arg(InitArgs, App, atom_to_list(Flag)),
try make_term(RV) of
V -> {ok, V}
catch T:E ->
{make_term_error, Flag, RV, T, E, erlang:get_stacktrace()}
end.
do_get_app_init_arg([], _App, _Flag) ->
undefined;
do_get_app_init_arg([{App, Args} | Rest], App, Flag) ->
case do_get_app_init_arg(Rest, App, Flag) of
undefined ->
get_app_init_arg_scan_args(Args, Flag);
Value ->
Value
end;
do_get_app_init_arg([_ | Rest], App, Flag) ->
do_get_app_init_arg(Rest, App, Flag).
get_app_init_arg_scan_args([], _Flag) ->
undefined;
get_app_init_arg_scan_args([Flag, Value | Rest], Flag) ->
case get_app_init_arg_scan_args(Rest, Flag) of
undefined ->
Value;
Value ->
Value
end;
get_app_init_arg_scan_args([_, _ | Rest], Flag) ->
get_app_init_arg_scan_args(Rest, Flag);
get_app_init_arg_scan_args(_, _Flag) ->
undefined.
%% (alk) NOTE: the following is stolen from application_controller in
%% erlang src
%%
% Fix some day: eliminate the duplicated code here
make_term(Str) ->
case erl_scan:string(Str) of
{ok, Tokens, _} ->
case erl_parse:parse_term(Tokens ++ [{dot, 1}]) of
{ok, Term} ->
Term;
{error, {_,_M,_Reason}} ->
%% error_logger:format("application_controller: ~s: ~s~n",
%% [M:format_error(Reason), Str]),
throw({error, {bad_environment_value, Str}})
end;
{error, {_,_M,_Reason}, _} ->
%% error_logger:format("application_controller: ~s: ~s~n",
%% [M:format_error(Reason), Str]),
throw({error, {bad_environment_value, Str}})
end.
massage_config_with_not_undefined(Config, Field) ->
Config1 = setelement(Field, Config, not_undefined),
Config2 = massage_config(Config1),
setelement(Field, Config2, undefined).
massage_config(#config{initargs_path = undefined,
guess_prefix = true} = Config) ->
Path = filename:absname(
filename:join([filename:dirname(escript:script_name()),
"..",
"var",
"lib"
"couchbase",
"initargs"])),
massage_config(Config#config{initargs_path = Path});
massage_config(#config{static_config = undefined,
initargs_path = InitArgsPath} = Config) when InitArgsPath =/= undefined ->
case get_initargs(Config) of
{ok, InitArgs} ->
case get_app_init_arg(InitArgs, ns_server, config_path) of
{ok, ConfigPath0} ->
ConfigPath = filename:absname(ConfigPath0, proplists:get_value(cwd, InitArgs, "/")),
massage_config(Config#config{static_config = ConfigPath});
Err ->
complain("Failed to extract config_path from initargs: ~p~n", [Err]),
massage_config_with_not_undefined(Config, #config.static_config)
end;
_Err ->
massage_config_with_not_undefined(Config, #config.static_config)
end;
massage_config(#config{couch_inis = undefined,
initargs_path = InitArgsPath} = Config) when InitArgsPath =/= undefined ->
case get_initargs(Config) of
{ok, InitArgs} ->
case proplists:get_value(couch_ini, InitArgs) of
undefined ->
complain("Failed to get couch_ini: ~p~n", [InitArgs]),
massage_config_with_not_undefined(Config, #config.couch_inis);
Values0 ->
CWD = proplists:get_value(cwd, InitArgs, "/"),
Values = [filename:absname(V, CWD) || V <- Values0],
Config#config{couch_inis = string:join(Values, ";")}
end;
_Err ->
massage_config_with_not_undefined(Config, #config.couch_inis)
end;
massage_config(Config) ->
Config.
main_main(_Flags, [_|_]) ->
complain("Got non-option arguments~n", []),
usage(),
%% {ok, Bin} = file:read_file("etc/capi.ini.in"),
%% complain("Couch stuff:~n~p~n", [parse_couch_ini_file(Bin)]),
init:stop(1);
main_main(Flags, []) ->
Config0 = lists:foldl(fun ({Flag, Value}, Acc) ->
Field = flag_to_config_field(Flag),
erlang:setelement(Field, Acc, Value)
end, #config{}, Flags),
Config = massage_config(Config0),
erlang:put(config, Config),
StaticPath = Config#config.static_config,
case StaticPath of
undefined ->
case Config of
#config{initargs_path = undefined} ->
complain("--static-config= or --initargs-path= option is required\n", []),
usage();
_ ->
ok
end,
init:stop(1);
_ ->
case file:consult(StaticPath) of
{ok, Terms} when is_list(Terms) ->
Terms2 = case get_initargs(Config) of
{ok, InitArgs} ->
maybe_override_static_config_with_initargs(Terms, InitArgs);
_ ->
Terms
end,
main_with_static_config(Terms2);
Err ->
io:format(standard_error, "Failed to read static config ~s (~p):~n~p~n", [filename:absname(StaticPath), file:get_cwd(), Err]),
init:stop(1)
end
end.
maybe_override_static_config_with_initargs(Terms, InitArgs) ->
%% complain("InitArgs:~n~p", [InitArgs]),
Prepends0 =
[case get_app_init_arg(InitArgs, ns_server, K) of
{ok, Value} ->
%% complain("overriding: ~p with ~p~n", [K, Value]),
[{K, Value}];
_Err ->
%% complain("Not overriding ~p: ~p~n", [K, Err]),
[]
end || {K, _} <- Terms],
lists:append(Prepends0) ++ Terms.
read_config(DataDir) ->
DynamicCfgPath = filename:join([DataDir, "config", "config.dat"]),
%% complain("Will read config.dat here: ~s~n", [DynamicCfgPath]),
case file:read_file(DynamicCfgPath) of
{ok, Data} ->
[Config|_] = erlang:binary_to_term(Data),
{ok, Config};
Err ->
Err
end.
trim_from_left(String) ->
lists:dropwhile(fun (C) ->
case C of
$\s -> true;
$\r -> true;
$\t -> true;
$\n -> true;
_ -> false
end
end, String).
trim(String) ->
String2 = trim_from_left(String),
lists:reverse(trim_from_left(lists:reverse(String2))).
do_read_node_file(Path) ->
case file:read_file(Path) of
{ok, B} ->
{ok, list_to_atom(trim(binary_to_list(B)))};
Err ->
complain("Failed to read node-file (~s): ~p~n", [Path, Err]),
failed
end.
do_read_node(StaticConfig) ->
case confget(#config.node_name) of
undefined ->
case confget(#config.node_file) of
undefined ->
do_read_node_file(proplists:get_value(nodefile, StaticConfig));
Path ->
do_read_node_file(Path)
end;
NodeString ->
{ok, list_to_atom(NodeString)}
end.
read_couch_ini_file(Path) ->
%% complain("Reading couch ini: ~p~n", [Path]),
case file:read_file(Path) of
{ok, Binary} ->
parse_couch_ini_file(Binary);
Err ->
Err
end.
read_couch_inis() ->
Paths0 = confget(#config.couch_inis),
%% usually last ini file overrides any values in previous
%% config. To get similar behavior with simple proplist:get_value
%% we reverse list of inis
Paths = lists:reverse(string:tokens(Paths0, ";")),
RVPairs = [{Name, read_couch_ini_file(Name)}
|| Name <- Paths],
Errors = [Pair || {_, RV} = Pair <- RVPairs,
case RV of
{ok, _} -> false;
_ -> true
end],
case Errors of
[] ->
{ok, lists:append([case RV of
{ok, Config} -> Config
end || {_, RV} <- RVPairs])};
_ ->
[complain("Failed to load ~s due to ~p~n", [Name, Error])
|| {Name, Error} <- Errors],
failed
end.
main_with_static_config(Terms) ->
%% io:format("Terms:~n~p~n", [Terms]),
case lists:keyfind(path_config_datadir, 1, Terms) of
{_, DataDir} ->
case (catch read_config(DataDir)) of
{ok, NSConfig} ->
main_after_ns_config(Terms, NSConfig);
Err ->
complain("Failed to read ns_config: ~p~n", [Err]),
init:stop(1)
end;
_ ->
complain("static config is missing path_config_datadir~n~p~n", [Terms]),
init:stop(1)
end.
main_after_ns_config(Terms, NSConfig) ->
case read_couch_inis() of
{ok, CouchInisPList} ->
main_after_couch_inis(Terms, NSConfig, CouchInisPList);
failed ->
init:stop(1)
end.
main_after_couch_inis(Terms, NSConfig, CouchInisPList) ->
case do_read_node(Terms) of
{ok, Atom} ->
case lists:keyfind({node, Atom, port_servers}, 1, NSConfig) of
false ->
complain("Given node name (~s) doesn't appear in ns_config.~n", [Atom]),
init:stop(1);
_ ->
main_with_everything(Terms, NSConfig, CouchInisPList, Atom)
end;
failed ->
%% NOTE: error is already reported here
init:stop(1)
end.
strip_config_meta([{'_vclock', _} | Value]) ->
Value;
strip_config_meta(Value) ->
Value.
search_config(NSConfig, Key, Default) ->
case lists:keyfind(Key, 1, NSConfig) of
false ->
Default;
{_, Value} ->
strip_config_meta(Value)
end.
extract_rest_creds(NSConfig) ->
CredsTop = search_config(NSConfig, rest_creds, []),
Creds = search_config(CredsTop, creds, []),
case Creds of
[{User, [{password, Pwd}]}] ->
{User, Pwd};
[] ->
{"", ""};
_Crap ->
complain("Warning: some garbage in creds:~p~n", [Creds]),
{"", ""}
end.
search_config_for_node(NSConfig, Node, Key, Default) ->
None = make_ref(),
case search_config(NSConfig, {node, Node, Key}, None) of
None ->
search_config(NSConfig, Key, Default);
'_use_global_value' ->
search_config(NSConfig, Key, Default);
Value ->
Value
end.
extract_rest_port(NSConfig, Node) ->
None = make_ref(),
case search_config_for_node(NSConfig, Node, rest, None) of
None ->
dig_rest_port(search_config(NSConfig, rest, []));
KV ->
case lists:keyfind(port_meta, 1, KV) of
{port_meta, global} ->
dig_rest_port(search_config(NSConfig, rest, []));
_ ->
dig_rest_port(KV)
end
end.
dig_rest_port(KV) ->
search_config(KV, port, "").
main_with_everything(StaticTerms, NSConfig, CouchInisPList, Node) ->
Cookie = search_config(search_config(NSConfig, otp, []),
cookie, []),
DBDir = proplists:get_value({"couchdb", "database_dir"}, CouchInisPList, ""),
IdxDir = proplists:get_value({"couchdb", "view_index_dir"}, CouchInisPList, ""),
{RestUser, RestPass} = extract_rest_creds(NSConfig),
RestPort = extract_rest_port(NSConfig, Node),
MCDConfig = search_config_for_node(NSConfig, Node, memcached, []),
MCDPort = search_config(MCDConfig, port, ""),
MCDAdmin = search_config(MCDConfig, admin_user, ""),
MCDPass = search_config(MCDConfig, admin_pass, ""),
MCDLogs = search_config(MCDConfig, log_path, ""),
BucketConfigs = search_config(search_config(NSConfig, buckets, []),
configs, []),
BucketTypePairs = [{Name, search_config(Props, type, undefined)} || {Name, Props} <- BucketConfigs],
BucketNames = [Name || {Name, _} <- BucketTypePairs],
MCDBucketNames = [Name || {Name, memcached} <- BucketTypePairs],
NSLogPath = search_config(search_config_for_node(NSConfig, Node, ns_log, []),
filename,
""),
Pairs = [{ns_config, iolist_to_binary(io_lib:format("~p", [NSConfig]))},
{static_config, iolist_to_binary(io_lib:format("~p", [StaticTerms]))},
{couch_inis, confget(#config.couch_inis)},
{node, Node},
{cookie, Cookie},
{db_dir, DBDir},
{idx_dir, IdxDir},
{rest_user, RestUser},
{rest_pass, RestPass},
{rest_port, RestPort},
{memcached_port, MCDPort},
{buckets, string:join(BucketNames, ",")},
{memcached_buckets, string:join(MCDBucketNames, ",")},
{ns_log_path, NSLogPath},
{log_path, search_config(StaticTerms, error_logger_mf_dir, "")},
{memcached_admin, MCDAdmin},
{memcached_pass, MCDPass},
{memcached_logs_path, MCDLogs}],
output([E || {K, V} <- Pairs,
E <- [K, V]]),
_ = io:rows(user),
init:stop(0),
%% not sure why but output to tty is sometimes truncated otherwise
timer:sleep(100).
binarify_output_item(Binary) when is_binary(Binary) -> Binary;
binarify_output_item(Atom) when is_atom(Atom) -> atom_to_binary(Atom, latin1);
binarify_output_item(Int) when is_integer(Int) -> integer_to_list(Int);
binarify_output_item(String) when is_list(String) -> String.
output(Items) ->
io:fwrite("~s", [[[binarify_output_item(Item) | <<0:8>>] || Item <- Items]]).
usage() ->
Descs = options_descriptions(),
Options =
[case BoolText of
bool ->
[" --", Name, "\n"];
_ ->
[" --", Name, "=", case BoolText of
undefined ->
["<", Name, ">"];
_ ->
BoolText
end, "\n"]
end || {Name, BoolText} <- Descs],
io:format(standard_error, "Usage: ~s [OPTION]\n\nOptions:~n~s", [escript:script_name(), Options]).
couchutil_implode(List, Sep) ->
couchutil_implode(List, Sep, []).
couchutil_implode([], _Sep, Acc) ->
lists:flatten(lists:reverse(Acc));
couchutil_implode([H], Sep, Acc) ->
couchutil_implode([], Sep, [H|Acc]);
couchutil_implode([H|T], Sep, Acc) ->
couchutil_implode(T, Sep, [Sep,H|Acc]).
%% this is stolen from couch_config.erl and massaged a bit
parse_couch_ini_file(IniBin) ->
Lines = re:split(IniBin, "\r\n|\n|\r|\032", [{return, list}]),
{_, ParsedIniValues} =
lists:foldl(
fun(Line, {AccSectionName, AccValues}) ->
case string:strip(Line) of
"[" ++ Rest ->
case re:split(Rest, "\\]", [{return, list}]) of
[NewSectionName, ""] ->
{NewSectionName, AccValues};
_Else -> % end bracket not at end, ignore this line
{AccSectionName, AccValues}
end;
";" ++ _Comment ->
{AccSectionName, AccValues};
Line2 ->
case re:split(Line2, "\s?=\s?", [{return, list}]) of
[Value] ->
MultiLineValuePart = case re:run(Line, "^ \\S", []) of
{match, _} ->
true;
_ ->
false
end,
case {MultiLineValuePart, AccValues} of
{true, [{{_, ValueName}, PrevValue} | AccValuesRest]} ->
% remove comment
case re:split(Value, " ;|\t;", [{return, list}]) of
[[]] ->
% empty line
{AccSectionName, AccValues};
[LineValue | _Rest] ->
E = {{AccSectionName, ValueName},
PrevValue ++ " " ++ LineValue},
{AccSectionName, [E | AccValuesRest]}
end;
_ ->
{AccSectionName, AccValues}
end;
[""|_LineValues] -> % line begins with "=", ignore
{AccSectionName, AccValues};
[ValueName|LineValues] -> % yeehaw, got a line!
RemainingLine = couchutil_implode(LineValues, "="),
%% removes comments
case re:split(RemainingLine, " ;|\t;", [{return, list}]) of
[[]] ->
%% ALK: clearly this
%% stateful code works for
%% cases when later file
%% overrides earlier
%% file. But given we're
%% going to read just one
%% file we're fine without
%% it.
%%
%% empty line means delete this key
%% ets:delete(?MODULE, {AccSectionName, ValueName}),
{AccSectionName, AccValues};
[LineValue | _Rest] ->
{AccSectionName,
[{{AccSectionName, ValueName}, LineValue} | AccValues]}
end
end
end
end, {"", []}, Lines),
{ok, ParsedIniValues}.