Skip to content

Commit

Permalink
fix(fig): better generate spec for fig mount commands (#165)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
miguelmig and autofix-ci[bot] authored Nov 10, 2024
1 parent 8134a12 commit d9851f8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
39 changes: 30 additions & 9 deletions cli/assets/fig/generators.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,52 @@
const usageGenerateSpec = (cmds: string[]) => {
return async (
tokens: string[],
context: string[],
executeCommand: Fig.ExecuteCommandFunction,
): Promise<Fig.Spec> => {
const promises = cmds.map(async (cmd) => {
const promises = cmds.map(async (cmd): Promise<Fig.Subcommand[]> => {
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;
};
};

Expand Down
3 changes: 3 additions & 0 deletions cli/src/cli/generate/fig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ struct FigCommand {

#[serde(skip_serializing_if = "Option::is_none")]
generate_spec: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
cache: Option<bool>,
}

impl FigGenerator {
Expand Down Expand Up @@ -299,6 +301,7 @@ impl FigCommand {
.join(",");
format!("${calls}$")
}),
cache: (!cmd.mounts.is_empty()).then_some(false),
})
}
}
Expand Down

0 comments on commit d9851f8

Please sign in to comment.