Skip to content

Commit 7b61bdb

Browse files
committed
add list contracts
1 parent ca9d32b commit 7b61bdb

4 files changed

Lines changed: 41 additions & 2 deletions

File tree

src/helpers/errors.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@ export function logAndQuit(message: string) {
22
console.error(message);
33
process.exit(1);
44
}
5+
6+
export function logLoginMessageAndQuit() {
7+
logAndQuit("You need to login first.\n\n\t$ sf login\n");
8+
}

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { registerSSH } from "./lib/ssh";
77
import { registerUpgrade } from "./lib/upgrade";
88
import { version } from "../package.json";
99
import { registerBuy } from "./lib/buy";
10+
import { registerContracts } from "./lib/contracts";
1011

1112
const program = new Command();
1213

@@ -20,6 +21,7 @@ registerLogin(program);
2021
registerSSH(program);
2122
registerUpgrade(program);
2223
registerBuy(program);
24+
registerContracts(program);
2325

2426
// (only development commands)
2527
registerDev(program);

src/lib/buy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Command } from "commander";
22
import * as chrono from "chrono-node";
33
import parseDuration from "parse-duration";
4-
import { logAndQuit } from "../helpers/errors";
4+
import { logAndQuit, logLoginMessageAndQuit } from "../helpers/errors";
55
import { loadConfig } from "../helpers/config";
66
import c from "chalk";
77
import dayjs from "dayjs";
@@ -138,7 +138,7 @@ async function placeBuyOrder(props: PlaceBuyOrderArguments) {
138138
const { type, duration, price, quantity, start } = props;
139139
const config = await loadConfig();
140140
if (!config.token) {
141-
return logAndQuit("You need to login first.\n\n\t$ sf login\n");
141+
return logLoginMessageAndQuit();
142142
}
143143

144144
const orderQuantity = quantity ?? 1;

src/lib/contracts.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { Command } from "commander";
2+
import { getApiUrl } from "../helpers/urls";
3+
import { loadConfig } from "../helpers/config";
4+
import { logLoginMessageAndQuit } from "../helpers/errors";
5+
6+
export function registerContracts(program: Command) {
7+
program
8+
.command("contracts")
9+
.description("Manage contracts")
10+
.addCommand(
11+
new Command("list").description("List all contracts").action(async () => {
12+
await listContracts();
13+
}),
14+
);
15+
}
16+
17+
async function listContracts() {
18+
const config = await loadConfig();
19+
if (!config.token) {
20+
return logLoginMessageAndQuit();
21+
}
22+
23+
const response = await fetch(await getApiUrl("contracts_list"), {
24+
method: "GET",
25+
headers: {
26+
"Content-Type": "application/json",
27+
Authorization: `Bearer ${config.token}`,
28+
},
29+
});
30+
31+
const data = await response.json();
32+
console.log(data);
33+
}

0 commit comments

Comments
 (0)