Command: add JSON printing support

This commit is contained in:
李通洲
2023-08-30 14:13:39 +08:00
parent acd353b8b3
commit 01aa9b9516
2 changed files with 31 additions and 1 deletions
+30 -1
View File
@@ -36,7 +36,7 @@ void ffPrintCommand(FFCommandOptions* options)
void ffInitCommandOptions(FFCommandOptions* options)
{
ffOptionInitModuleBaseInfo(&options->moduleInfo, FF_COMMAND_MODULE_NAME, ffParseCommandCommandOptions, ffParseCommandJsonObject, ffPrintCommand, NULL);
ffOptionInitModuleBaseInfo(&options->moduleInfo, FF_COMMAND_MODULE_NAME, ffParseCommandCommandOptions, ffParseCommandJsonObject, ffPrintCommand, ffGenerateCommandJson);
ffOptionInitModuleArg(&options->moduleArgs);
ffStrbufInitStatic(&options->shell,
@@ -107,3 +107,32 @@ void ffParseCommandJsonObject(FFCommandOptions* options, yyjson_val* module)
ffPrintError(FF_COMMAND_MODULE_NAME, 0, &options->moduleArgs, "Unknown JSON key %s", key);
}
}
void ffGenerateCommandJson(FF_MAYBE_UNUSED FFCommandOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
{
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);
}
+1
View File
@@ -9,3 +9,4 @@ void ffInitCommandOptions(FFCommandOptions* options);
bool ffParseCommandCommandOptions(FFCommandOptions* options, const char* key, const char* value);
void ffDestroyCommandOptions(FFCommandOptions* options);
void ffParseCommandJsonObject(FFCommandOptions* options, yyjson_val* module);
void ffGenerateCommandJson(FFCommandOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module);