Skip to content

Commit 3415ff8

Browse files
committed
feat(cli): allow address-book to print a single contract's address when given its name
1 parent e0f8e81 commit 3415ff8

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

.changeset/great-ride-unrxja.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@cartesi/cli": minor
3+
---
4+
5+
allow `address-book` to print a single contract's address when given its name (useful for shell scripting instead of `--json` + `jq`)

apps/cli/src/commands/address-book.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,39 @@ export const createAddressBookCommand = () => {
77
.description(
88
"Prints the addresses of all smart contracts deployed to the runtime environment of the application.",
99
)
10+
.argument(
11+
"[contract]",
12+
"name of a contract; when provided, prints only its address (useful for scripting)",
13+
)
1014
.option("--json", "Format output as json.")
1115
.option(
1216
"--project-name <string>",
1317
"name of project (used by docker compose and cartesi-rollups-node)",
1418
)
15-
.action(async (options) => {
19+
.action(async (contract, options, command) => {
1620
const { json } = options;
1721
const projectName = getProjectName(options);
1822
const addressBook = await getAddressBook({ projectName });
23+
24+
if (contract !== undefined) {
25+
// look up a single contract by name (case-insensitive)
26+
const entry = Object.entries(addressBook).find(
27+
([name]) => name.toLowerCase() === contract.toLowerCase(),
28+
);
29+
if (!entry) {
30+
command.error(`Contract not found: ${contract}`);
31+
return;
32+
}
33+
const [, address] = entry;
34+
if (!json) {
35+
// print only the address, for easy shell integration
36+
console.log(address);
37+
} else {
38+
process.stdout.write(JSON.stringify(address));
39+
}
40+
return;
41+
}
42+
1943
if (!json) {
2044
// print as a table
2145
const table = new Table({

0 commit comments

Comments
 (0)