2023-03-07 19:27:49 +08:00
|
|
|
#include "common/printing.h"
|
2023-06-10 15:01:54 +08:00
|
|
|
#include "common/jsonconfig.h"
|
2023-03-07 19:27:49 +08:00
|
|
|
#include "common/processing.h"
|
|
|
|
|
#include "modules/command/command.h"
|
2023-06-19 15:21:49 +08:00
|
|
|
#include "util/stringUtils.h"
|
2023-03-07 19:27:49 +08:00
|
|
|
|
2023-06-24 10:59:53 +08:00
|
|
|
void ffPrintCommand(FFCommandOptions* options)
|
2023-03-07 19:27:49 +08:00
|
|
|
{
|
2023-04-15 15:29:54 +08:00
|
|
|
FF_STRBUF_AUTO_DESTROY result = ffStrbufCreate();
|
2024-09-10 21:59:30 +08:00
|
|
|
const char* error = ffProcessAppendStdOut(&result, options->param.length ? (char* const[]){
|
|
|
|
|
options->shell.chars,
|
|
|
|
|
options->param.chars,
|
|
|
|
|
options->text.chars,
|
|
|
|
|
NULL
|
|
|
|
|
} : (char* const[]){
|
2023-03-07 19:27:49 +08:00
|
|
|
options->shell.chars,
|
|
|
|
|
options->text.chars,
|
|
|
|
|
NULL
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if(error)
|
|
|
|
|
{
|
2024-03-07 11:10:18 +08:00
|
|
|
ffPrintError(FF_COMMAND_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
|
2023-03-07 19:27:49 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(!result.length)
|
|
|
|
|
{
|
2024-03-07 11:10:18 +08:00
|
|
|
ffPrintError(FF_COMMAND_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No result printed");
|
2023-03-07 19:27:49 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-07 13:40:40 +08:00
|
|
|
if (options->moduleArgs.outputFormat.length == 0)
|
|
|
|
|
{
|
|
|
|
|
ffPrintLogoAndKey(FF_COMMAND_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
|
|
|
|
|
ffStrbufPutTo(&result, stdout);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-12-11 10:47:02 +08:00
|
|
|
FF_PRINT_FORMAT_CHECKED(FF_COMMAND_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]){
|
2024-09-03 14:52:40 +08:00
|
|
|
FF_FORMAT_ARG(result, "result")
|
2024-03-07 10:47:28 +08:00
|
|
|
}));
|
2023-10-07 13:40:40 +08:00
|
|
|
}
|
2023-03-07 19:27:49 +08:00
|
|
|
}
|
|
|
|
|
|
2023-08-17 10:56:54 +08:00
|
|
|
void ffParseCommandJsonObject(FFCommandOptions* options, yyjson_val* module)
|
2023-03-09 16:10:46 +08:00
|
|
|
{
|
2025-08-02 18:51:03 +08:00
|
|
|
yyjson_val *key, *val;
|
2023-08-17 16:09:41 +08:00
|
|
|
size_t idx, max;
|
2025-08-02 18:51:03 +08:00
|
|
|
yyjson_obj_foreach(module, idx, max, key, val)
|
2023-03-09 16:10:46 +08:00
|
|
|
{
|
2023-08-17 16:09:41 +08:00
|
|
|
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs))
|
|
|
|
|
continue;
|
|
|
|
|
|
2025-08-02 18:51:03 +08:00
|
|
|
if (unsafe_yyjson_equals_str(key, "shell"))
|
2023-08-17 16:09:41 +08:00
|
|
|
{
|
2025-08-02 17:41:19 +08:00
|
|
|
ffStrbufSetJsonVal(&options->shell, val);
|
2023-08-17 16:09:41 +08:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-02 18:51:03 +08:00
|
|
|
if (unsafe_yyjson_equals_str(key, "param"))
|
2024-09-10 21:59:30 +08:00
|
|
|
{
|
2025-08-02 17:41:19 +08:00
|
|
|
ffStrbufSetJsonVal(&options->param, val);
|
2024-09-10 21:59:30 +08:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-02 18:51:03 +08:00
|
|
|
if (unsafe_yyjson_equals_str(key, "text"))
|
2023-03-15 15:24:40 +08:00
|
|
|
{
|
2025-08-02 17:41:19 +08:00
|
|
|
ffStrbufSetJsonVal(&options->text, val);
|
2023-08-17 16:09:41 +08:00
|
|
|
continue;
|
2023-03-15 15:24:40 +08:00
|
|
|
}
|
2023-08-17 16:09:41 +08:00
|
|
|
|
2025-08-02 18:51:03 +08:00
|
|
|
ffPrintError(FF_COMMAND_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
|
2023-03-09 16:10:46 +08:00
|
|
|
}
|
|
|
|
|
}
|
2023-08-30 14:13:39 +08:00
|
|
|
|
2023-10-24 14:41:29 +08:00
|
|
|
void ffGenerateCommandJsonConfig(FFCommandOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
|
|
|
|
|
{
|
2025-08-04 16:46:28 +08:00
|
|
|
ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
|
2023-10-24 14:41:29 +08:00
|
|
|
|
2025-08-04 16:46:28 +08:00
|
|
|
yyjson_mut_obj_add_strbuf(doc, module, "shell", &options->shell);
|
2023-10-24 14:41:29 +08:00
|
|
|
|
2025-08-04 16:46:28 +08:00
|
|
|
yyjson_mut_obj_add_strbuf(doc, module, "param", &options->param);
|
2023-10-24 14:41:29 +08:00
|
|
|
|
2025-08-04 16:46:28 +08:00
|
|
|
yyjson_mut_obj_add_strbuf(doc, module, "text", &options->text);
|
2023-10-24 14:41:29 +08:00
|
|
|
}
|
|
|
|
|
|
2023-10-23 10:14:22 +08:00
|
|
|
void ffGenerateCommandJsonResult(FF_MAYBE_UNUSED FFCommandOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
|
2023-08-30 14:13:39 +08:00
|
|
|
{
|
|
|
|
|
FF_STRBUF_AUTO_DESTROY result = ffStrbufCreate();
|
2024-09-10 21:59:30 +08:00
|
|
|
const char* error = ffProcessAppendStdOut(&result, options->param.length ? (char* const[]){
|
|
|
|
|
options->shell.chars,
|
|
|
|
|
options->param.chars,
|
|
|
|
|
options->text.chars,
|
|
|
|
|
NULL
|
|
|
|
|
} : (char* const[]){
|
2023-08-30 14:13:39 +08:00
|
|
|
options->shell.chars,
|
|
|
|
|
options->text.chars,
|
|
|
|
|
NULL
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if(error)
|
|
|
|
|
{
|
|
|
|
|
yyjson_mut_obj_add_str(doc, module, "error", error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(!result.length)
|
|
|
|
|
{
|
|
|
|
|
yyjson_mut_obj_add_str(doc, module, "error", "No result printed");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
yyjson_mut_obj_add_strbuf(doc, module, "result", &result);
|
|
|
|
|
}
|
2023-10-07 13:40:40 +08:00
|
|
|
|
2023-10-23 13:32:18 +08:00
|
|
|
void ffInitCommandOptions(FFCommandOptions* options)
|
|
|
|
|
{
|
2024-07-23 23:02:35 +08:00
|
|
|
ffOptionInitModuleArg(&options->moduleArgs, "");
|
2023-10-23 13:32:18 +08:00
|
|
|
|
|
|
|
|
ffStrbufInitStatic(&options->shell,
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
"cmd.exe"
|
|
|
|
|
#else
|
|
|
|
|
"/bin/sh"
|
|
|
|
|
#endif
|
|
|
|
|
);
|
2024-09-10 21:59:30 +08:00
|
|
|
ffStrbufInitStatic(&options->param,
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
"/c"
|
|
|
|
|
#else
|
|
|
|
|
"-c"
|
|
|
|
|
#endif
|
|
|
|
|
);
|
2023-10-23 13:32:18 +08:00
|
|
|
ffStrbufInit(&options->text);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ffDestroyCommandOptions(FFCommandOptions* options)
|
|
|
|
|
{
|
|
|
|
|
ffOptionDestroyModuleArg(&options->moduleArgs);
|
|
|
|
|
ffStrbufDestroy(&options->shell);
|
2024-09-10 21:59:30 +08:00
|
|
|
ffStrbufDestroy(&options->param);
|
2023-10-23 13:32:18 +08:00
|
|
|
ffStrbufDestroy(&options->text);
|
|
|
|
|
}
|
2025-08-04 14:25:14 +08:00
|
|
|
|
|
|
|
|
FFModuleBaseInfo ffCommandModuleInfo = {
|
|
|
|
|
.name = FF_COMMAND_MODULE_NAME,
|
|
|
|
|
.description = "Run custom shell scripts",
|
|
|
|
|
.initOptions = (void*) ffInitCommandOptions,
|
|
|
|
|
.destroyOptions = (void*) ffDestroyCommandOptions,
|
|
|
|
|
.parseJsonObject = (void*) ffParseCommandJsonObject,
|
|
|
|
|
.printModule = (void*) ffPrintCommand,
|
|
|
|
|
.generateJsonResult = (void*) ffGenerateCommandJsonResult,
|
|
|
|
|
.generateJsonConfig = (void*) ffGenerateCommandJsonConfig,
|
|
|
|
|
.formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
|
|
|
|
|
{"Command result", "result"},
|
|
|
|
|
}))
|
|
|
|
|
};
|