-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.cpp.tt
149 lines (110 loc) · 3.7 KB
/
main.cpp.tt
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
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <iostream>
#include <hoytech/error.h>
#include <docopt.h>
#include "golpe.h"
#include "app_git_version.h"
static const char USAGE[] =
R"(
Usage: [% golpe.appName %] [% IF golpe.features.config %][--config=<config>][% END -%] <command> [<args>...]
Options:
[% IF golpe.features.config %]--config=<config> Config file (default $ENV{[% golpe.appName FILTER upper %]_CONFIG} || "/etc/[% golpe.appName %].conf" || "./[% golpe.appName %].conf")[% END %]
-h --help Show this screen.
--version Show version.
[% IF cmds.size > 0 %]
commands:
[%- FOREACH cmd IN cmds %]
[% cmd %]
[%- END %]
[%- END %]
[%- FOREACH app IN apps %]
[% app.name %] commands:
[%- FOREACH cmd IN app.cmds %]
[% cmd %]
[%- END %]
[%- END %]
)";
[% IF golpe.features.config %]
std::string configFile;
[% END %]
[% IF golpe.features.db %]
std::string dbDir;
defaultDb::environment env;
[% END %]
[% FOREACH cmd IN cmds %]
void cmd_[% cmd %](const std::vector<std::string> &subArgs);
[% END %]
[% FOREACH app IN apps %][% FOREACH cmd IN app.cmds %]
void cmd_[% cmd %](const std::vector<std::string> &subArgs);
[% END %][% END %]
void run(int argc, char **argv) {
std::map<std::string, docopt::value> args = docopt::docopt(USAGE, { argv + 1, argv + argc }, true, "[% golpe.appName %] " APP_GIT_VERSION, true);
loguru::g_stderr_verbosity = [% golpe.verbosity || 0 %];
loguru::g_preamble_file = false;
loguru::init(argc, argv, loguru::Options{ .signal_options = loguru::SignalOptions{ .sigint = false, }, });
[% IF golpe.features.config %]
auto fileExists = [](const char *name) {
return access(name, F_OK) != -1;
};
if (args["--config"]) {
configFile = args["--config"].asString();
} else if (getenv("[% golpe.appName FILTER upper %]_CONFIG")) {
configFile = std::string(getenv("[% golpe.appName FILTER upper %]_CONFIG"));
} else if (fileExists("/etc/[% golpe.appName %].conf")) {
configFile = "/etc/[% golpe.appName %].conf";
} else if (fileExists("./[% golpe.appName %].conf")) {
configFile = "./[% golpe.appName %].conf";
} else {
throw hoytech::error("please specify config file");
}
loadConfig(configFile);
[% END %]
std::string command = args["<command>"].asString();
[% IF golpe.features.db %]
dbDir = cfg().db;
unsigned int dbFlags = 0;
if (cfg().dbParams__noReadAhead) dbFlags |= MDB_NORDAHEAD;
[% IF golpe.features.customLMDBSetup %]
env.lmdb_env.set_max_dbs(64);
env.lmdb_env.set_max_readers(cfg().dbParams__maxreaders);
env.lmdb_env.set_mapsize(cfg().dbParams__mapsize);
env.open(dbDir, false, dbFlags);
[% ELSE %]
env.open(dbDir, true, dbFlags);
[% END %]
{
auto txn = env.txn_rw();
[% IF golpe.features.onAppStartup %]
onAppStartup(txn, command);
[% END %]
txn.commit();
}
[% END %]
[% FOREACH cmd IN cmds %]
if (command == "[% cmd %]") {
cmd_[% cmd %](args["<args>"].asStringList());
return;
}
[% END %]
[% FOREACH app IN apps %][% FOREACH cmd IN app.cmds %]
if (command == "[% cmd %]") {
cmd_[% cmd %](args["<args>"].asStringList());
return;
}
[% END %][% END %]
throw hoytech::error("unknown command: ", command);
}
int main(int argc, char **argv) {
try {
run(argc, argv);
} catch (std::exception &e) {
std::cerr << "[% golpe.appName %] error: " << e.what() << std::endl;
::exit(1);
}
return 0;
}