diff --git a/docs/commands/README.md b/docs/commands/README.md index 7c2fbc5bb..bde8a8075 100644 --- a/docs/commands/README.md +++ b/docs/commands/README.md @@ -1302,8 +1302,8 @@ FLAGS --no-input Don't interactively ask for any input from the user --nox-names= Comma-separated names of noxes from provider.yaml. To use all of your noxes: --nox-names all - --offers= Comma-separated list of offer names. Can't be used together with - --offer-ids. To use all of your offers: --offers all + --offers= Comma-separated list of offer names. To use all of your offers: --offers + all --priv-key= !WARNING! for debug purposes only. Passing private keys through flags is unsecure. On local network 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 key @@ -1596,8 +1596,8 @@ USAGE FLAGS --env= Fluence Environment to use when running the command --no-input Don't interactively ask for any input from the user - --offers= Comma-separated list of offer names. Can't be used together with - --offer-ids. To use all of your offers: --offers all + --offers= Comma-separated list of offer names. To use all of your offers: --offers + all --priv-key= !WARNING! for debug purposes only. Passing private keys through flags is unsecure. On local network 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 key @@ -1626,8 +1626,8 @@ FLAGS --no-input Don't interactively ask for any input from the user --offer-ids= Comma-separated list of offer ids. Can't be used together with --offers flag - --offers= Comma-separated list of offer names. Can't be used together with - --offer-ids. To use all of your offers: --offers all + --offers= Comma-separated list of offer names. To use all of your offers: --offers + all --priv-key= !WARNING! for debug purposes only. Passing private keys through flags is unsecure. On local network 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 key @@ -1653,8 +1653,8 @@ USAGE FLAGS --env= Fluence Environment to use when running the command --no-input Don't interactively ask for any input from the user - --offers= Comma-separated list of offer names. Can't be used together with - --offer-ids. To use all of your offers: --offers all + --offers= Comma-separated list of offer names. To use all of your offers: --offers + all --priv-key= !WARNING! for debug purposes only. Passing private keys through flags is unsecure. On local network 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 key diff --git a/src/commands/spell/new.ts b/src/commands/spell/new.ts index 46028db44..14b137fba 100644 --- a/src/commands/spell/new.ts +++ b/src/commands/spell/new.ts @@ -31,7 +31,7 @@ import { } from "../../lib/const.js"; import { initCli } from "../../lib/lifeCycle.js"; import { ensureSpellsDir, projectRootDir } from "../../lib/paths.js"; -import { checkboxes, confirm, input } from "../../lib/prompt.js"; +import { checkboxes, input } from "../../lib/prompt.js"; export default class New extends BaseCommand { static override description = "Create a new spell template"; @@ -115,23 +115,17 @@ export default class New extends BaseCommand { await fluenceConfig.$commit(); - if ( - !isInteractive || - fluenceConfig.deployments === undefined || - Object.values(fluenceConfig.deployments).length === 0 || - !(await confirm({ - message: `Do you want to add spell ${color.yellow(spellName)} to some of your deployments`, - default: true, - })) - ) { + const deployments = Object.keys(fluenceConfig.deployments ?? {}); + + if (!isInteractive || deployments.length === 0) { return; } const deploymentNames = await checkboxes({ - message: "Select deployments to add spell to", - options: Object.keys(fluenceConfig.deployments), + message: `If you want to add spell ${color.yellow(spellName)} to some of the deployments - please select them or press enter to continue`, + options: deployments, oneChoiceMessage(deploymentName) { - return `Do you want to select deployment ${color.yellow(deploymentName)}`; + return `Do you want to add spell ${color.yellow(spellName)} to deployment ${color.yellow(deploymentName)}`; }, onNoChoices(): Array { return []; @@ -140,7 +134,7 @@ export default class New extends BaseCommand { if (deploymentNames.length === 0) { commandObj.logToStderr( - `No deployments selected. You can add it manually later to ${fluenceConfig.$getPath()}`, + `No deployments selected. You can add it manually later at ${fluenceConfig.$getPath()}`, ); return; @@ -168,7 +162,7 @@ export default class New extends BaseCommand { await fluenceConfig.$commit(); commandObj.log( - `Added ${color.yellow(spellName)} to deployments: ${color.yellow( + `Added spell ${color.yellow(spellName)} to deployments: ${color.yellow( deploymentNames.join(", "), )}`, ); diff --git a/src/lib/addService.ts b/src/lib/addService.ts index 148131c7f..0c05c2db2 100644 --- a/src/lib/addService.ts +++ b/src/lib/addService.ts @@ -36,7 +36,7 @@ import { import { updateAquaServiceInterfaceFile } from "./helpers/generateServiceInterface.js"; import type { MarineCLI } from "./marineCli.js"; import { projectRootDir } from "./paths.js"; -import { confirm, input } from "./prompt.js"; +import { input, checkboxes } from "./prompt.js"; export async function ensureValidServiceName( fluenceConfig: FluenceConfig | null, @@ -82,7 +82,7 @@ type AddServiceArg = { fluenceConfig: FluenceConfig; marineCli: MarineCLI; marineBuildArgs?: string | undefined; - interactive?: boolean; + isATemplateInitStep?: boolean; }; export async function addService({ @@ -91,7 +91,7 @@ export async function addService({ fluenceConfig, marineCli, marineBuildArgs, - interactive = true, + isATemplateInitStep = false, }: AddServiceArg): Promise { let serviceName = serviceNameFromArgs; @@ -155,42 +155,19 @@ export async function addService({ await fluenceConfig.$commit(); - if (interactive) { - commandObj.log( + if (!isATemplateInitStep) { + commandObj.logToStderr( `Added ${color.yellow(serviceName)} to ${color.yellow( fluenceConfig.$getPath(), )}`, ); } - if ( - hasDefaultDeployment(fluenceConfig) && - (!interactive || - (isInteractive && - (await confirm({ - message: `Do you want to add service ${color.yellow( - serviceName, - )} to a default deployment ${color.yellow(DEFAULT_DEPLOYMENT_NAME)}`, - })))) - ) { - const defaultDeployemnt = - fluenceConfig.deployments[DEFAULT_DEPLOYMENT_NAME]; - - fluenceConfig.deployments[DEFAULT_DEPLOYMENT_NAME] = { - ...defaultDeployemnt, - services: [...(defaultDeployemnt.services ?? []), serviceName], - }; - - await fluenceConfig.$commit(); - - if (interactive) { - commandObj.log( - `Added ${color.yellow(serviceName)} to ${color.yellow( - DEFAULT_DEPLOYMENT_NAME, - )}`, - ); - } - } + await addServiceToDeployment({ + fluenceConfig, + isATemplateInitStep, + serviceName, + }); await resolveSingleServiceModuleConfigsAndBuild({ serviceName, @@ -211,17 +188,91 @@ export async function addService({ return serviceName; } -function hasDefaultDeployment( - fluenceConfig: FluenceConfig, -): fluenceConfig is FluenceConfig & { - deployments: { - [DEFAULT_DEPLOYMENT_NAME]: NonNullable< - FluenceConfig["deployments"] - >[string]; - }; -} { - return ( - fluenceConfig.deployments !== undefined && - DEFAULT_DEPLOYMENT_NAME in fluenceConfig.deployments +type AddServiceToDeploymentArgs = { + fluenceConfig: FluenceConfig; + isATemplateInitStep: boolean; + serviceName: string; +}; + +async function addServiceToDeployment({ + fluenceConfig, + isATemplateInitStep, + serviceName, +}: AddServiceToDeploymentArgs) { + const deployments = Object.keys(fluenceConfig.deployments ?? {}); + + if (deployments.length === 0) { + return; + } + + if (isATemplateInitStep) { + if (fluenceConfig.deployments === undefined) { + return; + } + + const defaultDeployment = + fluenceConfig.deployments[DEFAULT_DEPLOYMENT_NAME]; + + if (defaultDeployment === undefined) { + return; + } + + fluenceConfig.deployments[DEFAULT_DEPLOYMENT_NAME] = { + ...defaultDeployment, + services: [...(defaultDeployment.services ?? []), serviceName], + }; + + await fluenceConfig.$commit(); + return; + } + + if (!isInteractive) { + return; + } + + const deploymentNames = await checkboxes({ + message: `If you want to add service ${color.yellow(serviceName)} to some of the deployments - please select them or press enter to continue`, + options: deployments, + oneChoiceMessage(deploymentName) { + return `Do you want to add service ${color.yellow(serviceName)} to deployment ${color.yellow(deploymentName)}`; + }, + onNoChoices(): Array { + return []; + }, + }); + + if (deploymentNames.length === 0) { + commandObj.logToStderr( + `No deployments selected. You can add it manually later at ${fluenceConfig.$getPath()}`, + ); + + return; + } + + deploymentNames.forEach((deploymentName) => { + assert( + fluenceConfig.deployments !== undefined, + "Unreachable. It's checked above that fluenceConfig.deployments is not undefined", + ); + + const deployment = fluenceConfig.deployments[deploymentName]; + + assert( + deployment !== undefined, + "Unreachable. deploymentName is guaranteed to exist in fluenceConfig.deployments", + ); + + fluenceConfig.deployments[deploymentName] = { + ...deployment, + services: [...(deployment.services ?? []), serviceName], + }; + }); + + await fluenceConfig.$commit(); + + commandObj.log( + `Added service ${color.yellow(serviceName)} to deployments: ${color.yellow( + deploymentNames.join(", "), + )}`, ); } diff --git a/src/lib/const.ts b/src/lib/const.ts index 390932816..60f123b2b 100644 --- a/src/lib/const.ts +++ b/src/lib/const.ts @@ -409,7 +409,7 @@ export const OFFER_FLAG_NAME = "offers"; export const OFFER_IDS_FLAG_NAME = "offer-ids"; const OFFER_FLAG_OBJECT = { - description: `Comma-separated list of offer names. Can't be used together with --${OFFER_IDS_FLAG_NAME}. To use all of your offers: --${OFFER_FLAG_NAME} ${ALL_FLAG_VALUE}`, + description: `Comma-separated list of offer names. To use all of your offers: --${OFFER_FLAG_NAME} ${ALL_FLAG_VALUE}`, helpValue: "", }; diff --git a/src/lib/init.ts b/src/lib/init.ts index 63cf52504..dc54df6b5 100644 --- a/src/lib/init.ts +++ b/src/lib/init.ts @@ -259,7 +259,7 @@ export async function init(options: InitArg = {}): Promise { fluenceConfig, marineCli: await initMarineCli(), absolutePathOrUrl: absoluteServicePath, - interactive: false, + isATemplateInitStep: true, }); }