@@ -26,11 +26,20 @@ int main(int argc, char **argv)
26
26
fmt::print (" Build on {} {} {}\n " , CompilerHelper::getInstance ().BuildMachineInfo , CompilerHelper::getInstance ().BuildDate , CompilerHelper::getInstance ().BuildTime );
27
27
fmt::print (fg (fmt::color::green), " \r {:=^{}}\n " , " =" , PlatformHelper::getInstance ().getTerminalWidth ());
28
28
29
- if (! access (configPath, R_OK))
29
+ switch ( access (configPath, R_OK))
30
30
{
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 ;
34
43
}
35
44
36
45
fmt::print (" Load config {}\n " , configPath);
@@ -132,7 +141,6 @@ int main(int argc, char **argv)
132
141
{
133
142
spdlog::info (" Trigger hook `{}`" , name);
134
143
135
- auto command_output_future = PlatformHelper::getInstance ().executeCommandAsync (command);
136
144
kainjow::mustache::data context;
137
145
kainjow::mustache::data request;
138
146
kainjow::mustache::data response;
@@ -153,6 +161,18 @@ int main(int argc, char **argv)
153
161
154
162
response.set (" content_length" , std::to_string (req.body .size ()));
155
163
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);
156
176
response.set (" command_output" , kainjow::mustache::lambda_t {[&command_output_future, command_timeout](const std::string &)
157
177
{
158
178
if (command_timeout > 0 )
@@ -175,27 +195,9 @@ int main(int argc, char **argv)
175
195
return command_output_future.get ();
176
196
}
177
197
}});
198
+ context.set (" response" , response); // re-assign
178
199
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
- }});
197
200
kainjow::mustache::mustache content_tmpl{content};
198
-
199
201
auto result = content_tmpl.render (context);
200
202
if (!content_tmpl.is_valid ())
201
203
{
0 commit comments