From 9d8cae04ac90866b7bc0d21f09d58a5409eacdc4 Mon Sep 17 00:00:00 2001 From: Mike Wu Date: Tue, 23 Jan 2024 13:04:42 +0900 Subject: [PATCH] feat: add [Back] option --- cli.ts | 19 +++++++++++++++++++ lib/interact-for-command-selection.ts | 6 ++++++ lib/interact-for-open-api-object.ts | 12 ++++++++++++ 3 files changed, 37 insertions(+) diff --git a/cli.ts b/cli.ts index 8cf9bfb..028db68 100644 --- a/cli.ts +++ b/cli.ts @@ -157,10 +157,29 @@ async function cli(args: ParsedArgs) { commandParams.accepted_providers.split(",") } + // Hit 'back' on a top-level command path, so we start again + const lastCommandPath = selectedCommand.slice(-1)[0] + if (lastCommandPath === "[Back]") { + return await cli({ + ...args, + _: [], + }) + } + const params = await interactForCommandParams( { command: selectedCommand, params: commandParams }, ctx ) + + if (params === "[Back]") { + const previousCommands = [...selectedCommand] + previousCommands.pop() + return await cli({ + ...args, + _: previousCommands, + }) + } + const seam = await getSeam() const apiPath = `/${selectedCommand.join("/").replace(/-/g, "_")}` diff --git a/lib/interact-for-command-selection.ts b/lib/interact-for-command-selection.ts index c1bcccc..e67a085 100644 --- a/lib/interact-for-command-selection.ts +++ b/lib/interact-for-command-selection.ts @@ -30,6 +30,12 @@ export async function interactForCommandSelection( ["select", "server"], ]) + // Add dynamic 'back' command for sub-commands to allow returning + // to previous level. + if (commandPath.length > 0) { + commands.push([...commandPath, "[Back]"]) + } + const possibleCommands = uniqBy( commandPath.length === 0 ? commands diff --git a/lib/interact-for-open-api-object.ts b/lib/interact-for-open-api-object.ts index 1cae5ee..ecf6ab4 100644 --- a/lib/interact-for-open-api-object.ts +++ b/lib/interact-for-open-api-object.ts @@ -105,6 +105,10 @@ export const interactForOpenApiObject = async ( }, ] : []), + { + title: `[Back]`, + value: "back", + }, ], }) @@ -117,6 +121,14 @@ export const interactForOpenApiObject = async ( return args.params } + if (paramToEdit === "back") { + if (args.subPropertyPath) { + return args.params + } + + return "[Back]" + } + const prop = properties[paramToEdit] if (paramToEdit === "device_id") {