Skip to content

Commit 120b5a5

Browse files
committed
feat(cli): Add apps command to interact with applications.
1 parent 493b386 commit 120b5a5

4 files changed

Lines changed: 91 additions & 1 deletion

File tree

.changeset/thirty-trees-win.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@cartesi/cli": patch
3+
---
4+
5+
Add new apps command to interact with cartesi application. First option is to enable and disable an application.

apps/cli/src/commands/apps.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { Command, Option } from "@commander-js/extra-typings";
2+
import chalk from "chalk";
3+
import { getProjectName } from "../base";
4+
import { getDeployments, setApplicationStatus } from "../exec/rollups";
5+
6+
const choices = ["enabled", "disabled"] as const;
7+
8+
export const createAppsCommand = () => {
9+
return new Command("apps")
10+
.description("Interact with applications.")
11+
.showHelpAfterError(
12+
chalk.cyan("use cartesi apps --help for additional information"),
13+
)
14+
.addOption(
15+
new Option(
16+
"--project-name <name>",
17+
`name of project (used by docker compose and cartesi-rollups-node). ${chalk.cyan("(Default: Current working directory)")}`,
18+
),
19+
)
20+
.addOption(
21+
new Option("--set <status>", "Change the application status")
22+
.choices(choices)
23+
.conflicts(["list"]),
24+
)
25+
.action(async (options) => {
26+
const projectName = getProjectName(options);
27+
if (options.set) {
28+
const result = await setApplicationStatus({
29+
application: projectName,
30+
projectName: projectName,
31+
force: true,
32+
status: options.set,
33+
});
34+
35+
if (result.ok) {
36+
console.log(chalk.green(result.message));
37+
} else {
38+
console.log(chalk.red(result.message));
39+
}
40+
} else {
41+
const deployments = await getDeployments({
42+
projectName: projectName,
43+
});
44+
45+
console.log(chalk.cyan(JSON.stringify(deployments, null, " ")));
46+
}
47+
});
48+
};

apps/cli/src/exec/rollups.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,41 @@ const parseDeployment = (
6262
templateHash: deployment.template_hash as Hex,
6363
});
6464

65+
type SetApplicationStatusReturn = { ok: boolean; message: string };
66+
export const setApplicationStatus = async (options: {
67+
application: string | Address;
68+
force: boolean;
69+
projectName: string;
70+
status: "disabled" | "enabled";
71+
}): Promise<SetApplicationStatusReturn> => {
72+
try {
73+
const args = [options.application, options.status];
74+
75+
if (options.force) {
76+
args.push("--yes");
77+
}
78+
79+
const { stdout: confirmation } = await execa("docker", [
80+
"compose",
81+
"--project-name",
82+
options.projectName,
83+
"exec",
84+
"rollups_node",
85+
"cartesi-rollups-cli",
86+
"app",
87+
"status",
88+
...args,
89+
]);
90+
91+
return { ok: true, message: confirmation };
92+
} catch {
93+
return {
94+
ok: false,
95+
message: `Failed to change ${options.application} status to ${options.status}`,
96+
};
97+
}
98+
};
99+
65100
export const getDeployments = async (
66101
options: ComposeParams,
67102
): Promise<RollupsDeployment[]> => {
@@ -549,7 +584,7 @@ export const removeApplication = async (options: {
549584
const removeArgs = [application];
550585

551586
if (force) {
552-
removeArgs.push("--force");
587+
removeArgs.push("--yes");
553588
}
554589

555590
return execa("docker", [

apps/cli/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Command } from "@commander-js/extra-typings";
22
import pkg from "../package.json" with { type: "json" };
33
import { createAddressBookCommand } from "./commands/address-book.js";
4+
import { createAppsCommand } from "./commands/apps.js";
45
import { createBuildCommand } from "./commands/build.js";
56
import { createCleanCommand } from "./commands/clean.js";
67
import { createCreateCommand } from "./commands/create.js";
@@ -29,6 +30,7 @@ const program = new Command()
2930
.version(pkg.version)
3031
.addHelpText("before", splash)
3132
.addCommand(createAddressBookCommand())
33+
.addCommand(createAppsCommand())
3234
.addCommand(createBuildCommand())
3335
.addCommand(createCleanCommand())
3436
.addCommand(createCreateCommand())

0 commit comments

Comments
 (0)