1 module tagion.tools.hirep.hirep;
2 
3 import std.array;
4 import std.file : exists;
5 import std.format;
6 import std.getopt;
7 import std.path;
8 import std.range;
9 import std.stdio;
10 import tagion.basic.Types : Buffer, FileExtension;
11 import tagion.basic.tagionexceptions;
12 import tagion.crypto.SecureNet;
13 import tagion.hibon.Document;
14 import tagion.hibon.HiBONFile : fread, fwrite;
15 import tagion.hibon.HiBONFile : HiBONRange;
16 import tagion.hibon.HiBONJSON : toPretty;
17 import tagion.hibon.HiBONregex : HiBONregex;
18 import tagion.tools.Basic;
19 import tagion.tools.boot.genesis;
20 import tagion.tools.revision;
21 import tagion.utils.Term;
22 
23 alias check = Check!TagionException;
24 
25 mixin Main!(_main);
26 
27 int _main(string[] args) {
28     immutable program = args[0];
29     bool version_switch;
30     bool standard_output;
31     bool standard_input;
32     bool not_flag;
33     string output_filename;
34     string name;
35     string record_type;
36     string[] types;
37     try {
38         auto main_args = getopt(args,
39                 std.getopt.config.caseSensitive,
40                 std.getopt.config.bundling,
41                 "version", "display the version", &version_switch, //        "invoice|i","Sets the HiBON input file name", &invoicefile,
42                 "v|verbose", "Prints more debug information", &__verbose_switch,
43                 "c|stdout", "Print to standard output", &standard_output,
44                 "n|name", "HiBON member name (name as text or regex as `regex`)", &name,
45                 "r|recordtype", "HiBON recordtype (name as text or regex as `regex`)", &record_type,
46                 "t|type", "HiBON data types", &types,
47                 "not", "Filter out match", &not_flag, //               "o|output", format("Output filename : Default %s", output_filename), &output_filename, // //        "output_filename|o", format("Sets the output file name: default : %s", output_filenamename), &output_filenamename,
48                 //                "p|nodekey", "Node channel key(Pubkey) ", &nodekeys, //         "bills|b", "Generate bills", &number_of_bills,
49                 // "value|V", format("Bill value : default: %d", value), &value,
50                 // "passphrase|P", format("Passphrase of the keypair : default: %s", passphrase), &passphrase
51                 //"initbills|b", "Testing mode", &initbills,
52                 //"nnc", "Initialize NetworkNameCard with given name", &nnc_name,
53 
54                 
55 
56         );
57 
58         if (version_switch) {
59             revision_text.writeln;
60             return 0;
61         }
62 
63         if (main_args.helpWanted) {
64             //       writeln(logo);
65             defaultGetoptPrinter(
66                     [
67                     //                format("%s version %s", program, REVNO),
68                     "Documentation: https://tagion.org/",
69                     "",
70                     "Usage:",
71                     format("%s [<option>...] <hibon-files> ...", program),
72                     "",
73                     "Where:",
74                     format("<file>           hibon outfile (Default %s)", output_filename),
75                     "",
76 
77                     "<option>:",
78 
79                     ].join("\n"),
80                     main_args.options);
81             return 0;
82         }
83 
84         if (name) {
85             writefln("%s", name);
86             writefln("%s", record_type);
87             writefln("%s", types);
88         }
89         HiBONregex hibon_regex;
90         if (name) {
91             hibon_regex.name = name;
92         }
93         if (record_type) {
94             hibon_regex.record_type = record_type;
95         }
96         if (args.length == 1) {
97             File fin;
98             fin = stdin;
99             File fout;
100             fout = stdout;
101             foreach (no, doc; HiBONRange(fin).enumerate) {
102                 if (hibon_regex.match(doc)) {
103                     verbose("%d\n%s", no, doc.toPretty);
104                     fout.rawWrite(doc.serialize);
105                 }
106             }
107         }
108     }
109     catch (Exception e) {
110         error(e);
111         return 1;
112 
113     }
114     return 0;
115 }