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-10-07 13:40:40 +08:00
|
|
|
#define FF_COMMAND_NUM_FORMAT_ARGS 1
|
|
|
|
|
|
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();
|
2023-03-07 19:27:49 +08:00
|
|
|
const char* error = ffProcessAppendStdOut(&result, (char* const[]){
|
|
|
|
|
options->shell.chars,
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
"/c",
|
|
|
|
|
#else
|
|
|
|
|
"-c",
|
|
|
|
|
#endif
|
|
|
|
|
options->text.chars,
|
|
|
|
|
NULL
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if(error)
|
|
|
|
|
{
|
2023-06-24 10:59:53 +08:00
|
|
|
ffPrintError(FF_COMMAND_MODULE_NAME, 0, &options->moduleArgs, "%s", error);
|
2023-03-07 19:27:49 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(!result.length)
|
|
|
|
|
{
|
2023-06-24 10:59:53 +08:00
|
|
|
ffPrintError(FF_COMMAND_MODULE_NAME, 0, &options->moduleArgs, "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
|
|
|
|
|
{
|
|
|
|
|
ffPrintFormat(FF_COMMAND_MODULE_NAME, 0, &options->moduleArgs, FF_COMMAND_NUM_FORMAT_ARGS, (FFformatarg[]){
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &result}
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-03-07 19:27:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ffInitCommandOptions(FFCommandOptions* options)
|
|
|
|
|
{
|
2023-10-23 10:14:22 +08:00
|
|
|
ffOptionInitModuleBaseInfo(&options->moduleInfo, FF_COMMAND_MODULE_NAME, ffParseCommandCommandOptions, ffParseCommandJsonObject, ffPrintCommand, ffGenerateCommandJsonResult, ffPrintCommandHelpFormat);
|
2023-03-07 19:27:49 +08:00
|
|
|
ffOptionInitModuleArg(&options->moduleArgs);
|
|
|
|
|
|
2023-08-01 10:23:36 +08:00
|
|
|
ffStrbufInitStatic(&options->shell,
|
2023-03-07 19:27:49 +08:00
|
|
|
#ifdef _WIN32
|
2023-08-01 10:23:36 +08:00
|
|
|
"cmd.exe"
|
2023-03-07 19:27:49 +08:00
|
|
|
#else
|
2023-08-01 10:23:36 +08:00
|
|
|
"/bin/sh"
|
2023-03-07 19:27:49 +08:00
|
|
|
#endif
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
ffStrbufInit(&options->text);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ffParseCommandCommandOptions(FFCommandOptions* options, const char* key, const char* value)
|
|
|
|
|
{
|
|
|
|
|
const char* subKey = ffOptionTestPrefix(key, FF_COMMAND_MODULE_NAME);
|
|
|
|
|
if (!subKey) return false;
|
|
|
|
|
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
|
|
|
|
|
return true;
|
|
|
|
|
|
2023-06-19 15:21:49 +08:00
|
|
|
if(ffStrEqualsIgnCase(subKey, "shell"))
|
2023-03-07 19:27:49 +08:00
|
|
|
{
|
|
|
|
|
ffOptionParseString(key, value, &options->shell);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-19 15:21:49 +08:00
|
|
|
if(ffStrEqualsIgnCase(subKey, "text"))
|
2023-03-07 19:27:49 +08:00
|
|
|
{
|
|
|
|
|
ffOptionParseString(key, value, &options->text);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ffDestroyCommandOptions(FFCommandOptions* options)
|
|
|
|
|
{
|
|
|
|
|
ffOptionDestroyModuleArg(&options->moduleArgs);
|
|
|
|
|
ffStrbufDestroy(&options->shell);
|
|
|
|
|
ffStrbufDestroy(&options->text);
|
|
|
|
|
}
|
2023-03-09 16:10:46 +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
|
|
|
{
|
2023-08-17 16:09:41 +08:00
|
|
|
yyjson_val *key_, *val;
|
|
|
|
|
size_t idx, max;
|
|
|
|
|
yyjson_obj_foreach(module, idx, max, key_, val)
|
2023-03-09 16:10:46 +08:00
|
|
|
{
|
2023-08-17 16:09:41 +08:00
|
|
|
const char* key = yyjson_get_str(key_);
|
|
|
|
|
if(ffStrEqualsIgnCase(key, "type"))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (ffStrEqualsIgnCase(key, "shell"))
|
|
|
|
|
{
|
|
|
|
|
ffStrbufSetS(&options->shell, yyjson_get_str(val));
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ffStrEqualsIgnCase(key, "text"))
|
2023-03-15 15:24:40 +08:00
|
|
|
{
|
2023-08-17 16:09:41 +08:00
|
|
|
ffStrbufSetS(&options->text, yyjson_get_str(val));
|
|
|
|
|
continue;
|
2023-03-15 15:24:40 +08:00
|
|
|
}
|
2023-08-17 16:09:41 +08:00
|
|
|
|
|
|
|
|
ffPrintError(FF_COMMAND_MODULE_NAME, 0, &options->moduleArgs, "Unknown JSON key %s", key);
|
2023-03-09 16:10:46 +08:00
|
|
|
}
|
|
|
|
|
}
|
2023-08-30 14:13:39 +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();
|
|
|
|
|
const char* error = ffProcessAppendStdOut(&result, (char* const[]){
|
|
|
|
|
options->shell.chars,
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
"/c",
|
|
|
|
|
#else
|
|
|
|
|
"-c",
|
|
|
|
|
#endif
|
|
|
|
|
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
|
|
|
|
|
|
|
|
void ffPrintCommandHelpFormat(void)
|
|
|
|
|
{
|
|
|
|
|
ffPrintModuleFormatHelp(FF_COMMAND_MODULE_NAME, "{1}", FF_COMMAND_NUM_FORMAT_ARGS, (const char* []) {
|
|
|
|
|
"Command result"
|
|
|
|
|
});
|
|
|
|
|
}
|