Skip to content

Commit 6cc8dd2

Browse files
committed
fix contracts ls
1 parent f3876cb commit 6cc8dd2

2 files changed

Lines changed: 61 additions & 4 deletions

File tree

src/lib/contracts.ts

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,70 @@ import { Command } from "commander";
22
import { loadConfig } from "../helpers/config";
33
import { logLoginMessageAndQuit } from "../helpers/errors";
44
import { getApiUrl } from "../helpers/urls";
5+
import Table from "cli-table3";
6+
7+
interface Contract {
8+
object: string;
9+
status: string;
10+
id: string;
11+
created_at: string;
12+
instance_type: string;
13+
shape: {
14+
// These are date strings
15+
intervals: string[];
16+
quantities: number[];
17+
};
18+
colocate_with: string[];
19+
cluster_id: string;
20+
}
21+
22+
function printTable(data: Contract[]) {
23+
const table = new Table({
24+
head: [
25+
"ID",
26+
"Status",
27+
"Instance Type",
28+
// Found by looking at the first interval
29+
"Starts At",
30+
// Found by looking at the last interval
31+
"Ends At",
32+
],
33+
});
34+
35+
for (const contract of data) {
36+
const startsAt = contract.shape.intervals[0];
37+
const endsAt =
38+
contract.shape.intervals[contract.shape.intervals.length - 1];
39+
table.push([
40+
contract.id,
41+
contract.status,
42+
contract.instance_type,
43+
new Date(startsAt).toLocaleString(),
44+
new Date(endsAt).toLocaleString(),
45+
]);
46+
}
47+
48+
console.log(table.toString());
49+
}
550

651
export function registerContracts(program: Command) {
752
program
853
.command("contracts")
954
.description("Manage contracts")
1055
.addCommand(
11-
new Command("list").description("List all contracts").action(async () => {
12-
await listContracts();
13-
}),
56+
new Command("list")
57+
.alias("ls")
58+
.option("--json", "Output in JSON format")
59+
.description("List all contracts")
60+
.action(async (options) => {
61+
if (options.json) {
62+
console.log(await listContracts());
63+
} else {
64+
const data = await listContracts();
65+
printTable(data.data);
66+
}
67+
process.exit(0);
68+
}),
1469
);
1570
}
1671

@@ -29,5 +84,5 @@ async function listContracts() {
2984
});
3085

3186
const data = await response.json();
32-
console.log(data);
87+
return data;
3388
}

src/lib/upgrade.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,7 @@ export function registerUpgrade(program: Command) {
2121
} else {
2222
await Bun.$`bash -c "$(curl -fsSL https://www.sfcompute.com/cli/install)"`;
2323
}
24+
25+
process.exit(0);
2426
});
2527
}

0 commit comments

Comments
 (0)