From d9851f85d9b7eeee6c03b03d4192f29a43cfae21 Mon Sep 17 00:00:00 2001 From: Miguel Oliveira Date: Sun, 10 Nov 2024 18:36:00 +0000 Subject: [PATCH] fix(fig): better generate spec for fig mount commands (#165) * bugfix: better generate spec for fig mount commands * [autofix.ci] apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> --- cli/assets/fig/generators.ts | 39 +++++++++++++++++++++++++++--------- cli/src/cli/generate/fig.rs | 3 +++ 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/cli/assets/fig/generators.ts b/cli/assets/fig/generators.ts index ed02773..d86e938 100644 --- a/cli/assets/fig/generators.ts +++ b/cli/assets/fig/generators.ts @@ -1,31 +1,52 @@ const usageGenerateSpec = (cmds: string[]) => { return async ( - tokens: string[], + context: string[], executeCommand: Fig.ExecuteCommandFunction, ): Promise => { - const promises = cmds.map(async (cmd) => { + const promises = cmds.map(async (cmd): Promise => { try { - const { stdout } = await executeCommand({ - command: "sh", - args: ["-c", cmd], + const args = cmd.split(" "); + const { + stdout, + stderr: cmdStderr, + status: cmdStatus, + } = await executeCommand({ + command: args[0], + args: args.splice(1), }); - const { stdout: figSpecOut } = await executeCommand({ + if (cmdStatus !== 0) { + return [{ name: "error", description: cmdStderr }]; + } + const { + stdout: figSpecOut, + stderr: figSpecStderr, + status: usageFigStatus, + } = await executeCommand({ command: "usage", args: ["g", "fig", "--spec", stdout], }); + if (usageFigStatus !== 0) { + return [{ name: "error", description: figSpecStderr }]; + } + const start_of_json = figSpecOut.indexOf("{"); const j = figSpecOut.slice(start_of_json); return JSON.parse(j).subcommands as Fig.Subcommand[]; } catch (e) { - throw e; + return [{ name: "error", description: e }] as Fig.Subcommand[]; } }); - const subcommands = (await Promise.allSettled(promises)) + // eslint-disable-next-line compat/compat + const results = await Promise.allSettled(promises); + const subcommands = results .filter((p) => p.status === "fulfilled") .map((p) => p.value); + const failed = results + .filter((p) => p.status === "rejected") + .map((p) => ({ name: "error", description: p.reason })); - return { subcommands: subcommands.flat() } as Fig.Spec; + return { subcommands: [...subcommands.flat(), ...failed] } as Fig.Spec; }; }; diff --git a/cli/src/cli/generate/fig.rs b/cli/src/cli/generate/fig.rs index 238f8d1..df8bef5 100644 --- a/cli/src/cli/generate/fig.rs +++ b/cli/src/cli/generate/fig.rs @@ -89,6 +89,8 @@ struct FigCommand { #[serde(skip_serializing_if = "Option::is_none")] generate_spec: Option, + #[serde(skip_serializing_if = "Option::is_none")] + cache: Option, } impl FigGenerator { @@ -299,6 +301,7 @@ impl FigCommand { .join(","); format!("${calls}$") }), + cache: (!cmd.mounts.is_empty()).then_some(false), }) } }