Skip to content

Commit 1175037

Browse files
authored
Ohtoai/add command render (#50)
* Add command render && remove file render * Add config load process
1 parent d9747a9 commit 1175037

File tree

1 file changed

+26
-24
lines changed

1 file changed

+26
-24
lines changed

src/main.cpp

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,20 @@ int main(int argc, char **argv)
2626
fmt::print("Build on {} {} {}\n", CompilerHelper::getInstance().BuildMachineInfo, CompilerHelper::getInstance().BuildDate, CompilerHelper::getInstance().BuildTime);
2727
fmt::print(fg(fmt::color::green), "\r{:=^{}}\n", "=", PlatformHelper::getInstance().getTerminalWidth());
2828

29-
if (!access(configPath, R_OK))
29+
switch(access(configPath, R_OK))
3030
{
31-
WebhookConfigModal::generate(configPath);
32-
fmt::print("Config file not found, generate a new one.\n");
33-
return 0;
31+
case 0:
32+
break;
33+
case ENOENT:
34+
fmt::print("Config file not found, generate a new one.\n");
35+
WebhookConfigModal::generate(configPath);
36+
return 0;
37+
case EACCES:
38+
fmt::print("Permission denied.\n");
39+
return -EACCES;
40+
default:
41+
fmt::print("Unknown error.\n");
42+
return -1;
3443
}
3544

3645
fmt::print("Load config {}\n", configPath);
@@ -132,7 +141,6 @@ int main(int argc, char **argv)
132141
{
133142
spdlog::info("Trigger hook `{}`", name);
134143

135-
auto command_output_future = PlatformHelper::getInstance().executeCommandAsync(command);
136144
kainjow::mustache::data context;
137145
kainjow::mustache::data request;
138146
kainjow::mustache::data response;
@@ -153,6 +161,18 @@ int main(int argc, char **argv)
153161

154162
response.set("content_length", std::to_string(req.body.size()));
155163
response.set("content_type", content_type);
164+
165+
context.set("name", name);
166+
context.set("command", command);
167+
context.set("app", CompilerHelper::getInstance().AppName);
168+
context.set("version", VersionHelper::getInstance().Version);
169+
context.set("hash", CompilerHelper::getInstance().CommitHash);
170+
context.set("request", request);
171+
context.set("response", response);
172+
173+
auto rendered_command = kainjow::mustache::mustache{command}.render(context);
174+
auto command_output_future = PlatformHelper::getInstance().executeCommandAsync(rendered_command);
175+
context.set("rendered_command", rendered_command);
156176
response.set("command_output", kainjow::mustache::lambda_t{[&command_output_future, command_timeout](const std::string &)
157177
{
158178
if (command_timeout > 0)
@@ -175,27 +195,9 @@ int main(int argc, char **argv)
175195
return command_output_future.get();
176196
}
177197
}});
198+
context.set("response", response); // re-assign
178199

179-
context.set("name", name);
180-
context.set("command", command);
181-
context.set("app", CompilerHelper::getInstance().AppName);
182-
context.set("version", VersionHelper::getInstance().Version);
183-
context.set("hash", CompilerHelper::getInstance().CommitHash);
184-
context.set("request", request);
185-
context.set("response", response);
186-
context.set("file", kainjow::mustache::lambda_t{[](const std::string &file_path, const kainjow::mustache::renderer &render)
187-
{
188-
std::ifstream ifs(file_path);
189-
if (!ifs.is_open())
190-
{
191-
spdlog::error("File `{}` not found", file_path);
192-
return std::string{};
193-
}
194-
std::string content((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>()));
195-
return content;
196-
}});
197200
kainjow::mustache::mustache content_tmpl{content};
198-
199201
auto result = content_tmpl.render(context);
200202
if (!content_tmpl.is_valid())
201203
{

0 commit comments

Comments
 (0)