<commit_before>#!/usr/bin/env escript
%% -*- erlang -*-
%%
%% @author Couchbase <<EMAIL>>
%% @copyright 2011 Couchbase, Inc.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%      http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% ------------------------------------------------------------------------
%%
%% @doc Dump a config.dat file to stdout.
%%
%% Examples:
%%  linux:
%%    ./bin/mbdumpconfig.escript var/lib/membase/config/config.dat
%%  windows:
%%    bin\erlang\escript bin\mbdumpconfig.escript var\lib\membase\config\config.dat
%%
%% To just get a list of bucket names of a particular type:
%%    mbdumpconfig.escript path/to/config.dat buckets BucketType
%%  Example:
%%    mbdumpconfig.escript path/to/config.dat buckets membase

read(Path) ->
    {ok, Data} = file:read_file(Path),
    [Config|_] = erlang:binary_to_term(Data),
    Config.

main([Path]) ->
    Config = read(Path),
    io:fwrite("~p~n", [Config]);

main([Path, "buckets", Type]) ->
    Config = read(Path),
    emit(buckets(Config, list_to_atom(Type))).

emit([]) -> ok;
emit([X | Rest]) ->
    io:fwrite("~s~n", [X]),
    emit(Rest).

buckets(Config, Type) ->
    keys(Type, proplists:get_value(configs, proplists:get_value(buckets, Config)), []).

keys(_Type, [], Acc) ->
    Acc;
keys(Type, [{Key, Val} | Rest], Acc) ->
    case proplists:get_value(type, Val) of
        Type -> keys(Type, Rest, [Key | Acc]);
        _    -> keys(Type, Rest, Acc)
    end.<commit_msg>mbdumpconfig.escript config.dat [node <Node>]<commit_after>#!/usr/bin/env escript
%% -*- erlang -*-
%%
%% @author Couchbase <<EMAIL>>
%% @copyright 2011 Couchbase, Inc.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%      http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% ------------------------------------------------------------------------
%%
%% @doc Dump a config.dat file to stdout.
%%
%%   mbdumpconfig.escript [options]
%%
%% The options can be used to filter the output.  These include:
%%
%%   node <Node>
%%   buckets <BucketType>
%%
%% Examples:
%%   linux:
%%     ./bin/mbdumpconfig.escript var/lib/membase/config/config.dat
%%   windows:
%%     bin\erlang\escript bin\mbdumpconfig.escript var\lib\membase\config\config.dat
%%
%% Example of dumping info for a particular node...
%%   mbdumpconfig.escript config.dat node ns_1@127.0.0.1
%%
%% Example of dumping buckets names of type membase...
%%   mbdumpconfig.escript config.dat buckets membase

main([Path]) ->
    Config = read(Path),
    io:fwrite("~p~n", [Config]);

main([Path, "node", Node]) ->
    Config = read(Path),
    emit("~p.~n", node_only(Config, list_to_atom(Node), []));

main([Path, "buckets", Type]) ->
    Config = read(Path),
    emit("~s~n", buckets_only(Config, list_to_atom(Type))).

%% ----------------------------------------

read(Path) ->
    {ok, Data} = file:read_file(Path),
    [Config|_] = erlang:binary_to_term(Data),
    Config.

emit(_Fmt, []) -> ok;
emit(Fmt, [X | Rest]) ->
    io:fwrite(Fmt, [X]),
    emit(Fmt, Rest).

node_only([], _Node, Acc) -> Acc;
node_only([{{node, Node, _Key}, _Val} = KeyVal | Rest], Node, Acc) ->
    node_only(Rest, Node, [KeyVal | Acc]);
node_only([_NonMatchingKeyVal | Rest], Node, Acc) ->
    node_only(Rest, Node, Acc).

buckets_only(Config, Type) ->
    keys(Type, proplists:get_value(configs, proplists:get_value(buckets, Config)), []).

keys(_Type, [], Acc) ->
    Acc;
keys(Type, [{Key, Val} | Rest], Acc) ->
    case proplists:get_value(type, Val) of
        Type -> keys(Type, Rest, [Key | Acc]);
        _    -> keys(Type, Rest, Acc)
    end.

{'fraction_non_alphanumeric': 0.20085182694463125, 'fraction_numerical': 0.0051557946648733465, 'mean_word_length': 3.10865561694291, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '333548', 'n_tokens_mistral': 1465, 'n_tokens_neox': 1426, 'n_words': 410}