Skip to content

Commit

Permalink
feat: allow selecting a deployment when creating a new service [fixes D…
Browse files Browse the repository at this point in the history
…XJ-776] (#914)

* feat: allow selecting a deployment when creating a new service [fixes DXJ-776]

* improve

* fix offers flag description

* Apply automatic changes

---------

Co-authored-by: shamsartem <[email protected]>
  • Loading branch information
shamsartem and shamsartem authored Apr 25, 2024
1 parent 8192990 commit 7605eae
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 70 deletions.
16 changes: 8 additions & 8 deletions docs/commands/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1302,8 +1302,8 @@ FLAGS
--no-input Don't interactively ask for any input from the user
--nox-names=<nox-1,nox-2> Comma-separated names of noxes from provider.yaml. To use all of your
noxes: --nox-names all
--offers=<offer-1,offer-2> Comma-separated list of offer names. Can't be used together with
--offer-ids. To use all of your offers: --offers all
--offers=<offer-1,offer-2> Comma-separated list of offer names. To use all of your offers: --offers
all
--priv-key=<private-key> !WARNING! for debug purposes only. Passing private keys through flags is
unsecure. On local network
0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 key
Expand Down Expand Up @@ -1596,8 +1596,8 @@ USAGE
FLAGS
--env=<dar | kras | stage | local | custom> Fluence Environment to use when running the command
--no-input Don't interactively ask for any input from the user
--offers=<offer-1,offer-2> Comma-separated list of offer names. Can't be used together with
--offer-ids. To use all of your offers: --offers all
--offers=<offer-1,offer-2> Comma-separated list of offer names. To use all of your offers: --offers
all
--priv-key=<private-key> !WARNING! for debug purposes only. Passing private keys through flags is
unsecure. On local network
0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 key
Expand Down Expand Up @@ -1626,8 +1626,8 @@ FLAGS
--no-input Don't interactively ask for any input from the user
--offer-ids=<id-1,id-2> Comma-separated list of offer ids. Can't be used together with --offers
flag
--offers=<offer-1,offer-2> Comma-separated list of offer names. Can't be used together with
--offer-ids. To use all of your offers: --offers all
--offers=<offer-1,offer-2> Comma-separated list of offer names. To use all of your offers: --offers
all
--priv-key=<private-key> !WARNING! for debug purposes only. Passing private keys through flags is
unsecure. On local network
0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 key
Expand All @@ -1653,8 +1653,8 @@ USAGE
FLAGS
--env=<dar | kras | stage | local | custom> Fluence Environment to use when running the command
--no-input Don't interactively ask for any input from the user
--offers=<offer-1,offer-2> Comma-separated list of offer names. Can't be used together with
--offer-ids. To use all of your offers: --offers all
--offers=<offer-1,offer-2> Comma-separated list of offer names. To use all of your offers: --offers
all
--priv-key=<private-key> !WARNING! for debug purposes only. Passing private keys through flags is
unsecure. On local network
0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 key
Expand Down
24 changes: 9 additions & 15 deletions src/commands/spell/new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof New> {
static override description = "Create a new spell template";
Expand Down Expand Up @@ -115,23 +115,17 @@ export default class New extends BaseCommand<typeof New> {

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<string> {
return [];
Expand All @@ -140,7 +134,7 @@ export default class New extends BaseCommand<typeof New> {

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;
Expand Down Expand Up @@ -168,7 +162,7 @@ export default class New extends BaseCommand<typeof New> {
await fluenceConfig.$commit();

commandObj.log(
`Added ${color.yellow(spellName)} to deployments: ${color.yellow(
`Added spell ${color.yellow(spellName)} to deployments: ${color.yellow(
deploymentNames.join(", "),
)}`,
);
Expand Down
141 changes: 96 additions & 45 deletions src/lib/addService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -82,7 +82,7 @@ type AddServiceArg = {
fluenceConfig: FluenceConfig;
marineCli: MarineCLI;
marineBuildArgs?: string | undefined;
interactive?: boolean;
isATemplateInitStep?: boolean;
};

export async function addService({
Expand All @@ -91,7 +91,7 @@ export async function addService({
fluenceConfig,
marineCli,
marineBuildArgs,
interactive = true,
isATemplateInitStep = false,
}: AddServiceArg): Promise<string> {
let serviceName = serviceNameFromArgs;

Expand Down Expand Up @@ -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,
Expand All @@ -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<string> {
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(", "),
)}`,
);
}
2 changes: 1 addition & 1 deletion src/lib/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: "<offer-1,offer-2>",
};

Expand Down
2 changes: 1 addition & 1 deletion src/lib/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ export async function init(options: InitArg = {}): Promise<FluenceConfig> {
fluenceConfig,
marineCli: await initMarineCli(),
absolutePathOrUrl: absoluteServicePath,
interactive: false,
isATemplateInitStep: true,
});
}

Expand Down

0 comments on commit 7605eae

Please sign in to comment.