Skip to content

Commit 5a1af5e

Browse files
feat: add command polling (#13)
1 parent b720cec commit 5a1af5e

File tree

3 files changed

+113
-80
lines changed

3 files changed

+113
-80
lines changed

deno.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/cli.ts

Lines changed: 55 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
ViewerDocument,
3232
} from "./paperspace-graphql.ts";
3333
import * as project from "./projects/index.ts";
34+
import { pollCmd } from "./poll.ts";
3435

3536
const DOCS_ENDPOINT = "https://docs.paperspace.com";
3637

@@ -144,47 +145,6 @@ cli
144145
this.showHelp();
145146
});
146147

147-
/**
148-
* Notebooks
149-
*/
150-
cli
151-
.command(
152-
"notebook, nb",
153-
new Command()
154-
.command("init")
155-
.action(() => {
156-
console.log("init");
157-
})
158-
.command("create")
159-
.action(act.ifLoggedIn(() => {
160-
console.log("create");
161-
return { value: "" };
162-
}))
163-
.command("update")
164-
.action(act.ifLoggedIn(() => {
165-
console.log("update");
166-
return { value: "" };
167-
}))
168-
.command("delete")
169-
.action(act.ifLoggedIn(() => {
170-
console.log("delete");
171-
return { value: "" };
172-
}))
173-
.command("list")
174-
.action(act.ifLoggedIn(() => {
175-
console.log("list");
176-
return { value: "" };
177-
}))
178-
.command("get")
179-
.action(act.ifLoggedIn(() => {
180-
console.log("get");
181-
return { value: "" };
182-
})),
183-
)
184-
.action(function () {
185-
this.showHelp();
186-
});
187-
188148
/**
189149
* Machines
190150
*/
@@ -333,28 +293,43 @@ cli
333293
.option("--fields <fields:string[]>", "Return only these fields.", {
334294
collect: true,
335295
})
336-
.action(act.ifLoggedIn(async (opt, id) => {
337-
const fields = opt.fields?.flat().map((field) => field.trim()) as any;
296+
.option(
297+
"--poll [interval:string]",
298+
`Poll the server for updates. Defaults to "3s".`,
299+
)
300+
.action(
301+
act.ifLoggedIn(async (opt, id) => {
302+
const fields = opt.fields?.flat().map((field) => field.trim()) as any;
338303

339-
if (!id) {
340-
const suggestions = await gqlFetch(ListProjectsDocument, {
341-
first: 100,
342-
});
343-
344-
id = await select({
345-
message: "Select a project",
346-
options: suggestions.projects.nodes.map((p) => ({
347-
name: `${p.name} (${p.id})`,
348-
value: p.id,
349-
})),
350-
search: true,
351-
searchLabel: "| 🔍",
352-
info: true,
353-
});
354-
}
304+
if (!id) {
305+
const suggestions = await gqlFetch(ListProjectsDocument, {
306+
first: 100,
307+
});
355308

356-
return await project.get({ id, fields });
357-
})),
309+
id = await select({
310+
message: "Select a project",
311+
options: suggestions.projects.nodes.map((p) => ({
312+
name: `${p.name} (${p.id})`,
313+
value: p.id,
314+
})),
315+
search: true,
316+
searchLabel: "| 🔍",
317+
info: true,
318+
});
319+
}
320+
321+
if (opt.poll) {
322+
return {
323+
value: await pollCmd(
324+
async () => await project.get({ id: id!, fields }),
325+
opt,
326+
),
327+
};
328+
} else {
329+
return await project.get({ id, fields });
330+
}
331+
}),
332+
),
358333
)
359334
.description(`Manage your Paperspace Gradient projects.`)
360335
.action(function () {
@@ -641,23 +616,23 @@ cli
641616

642617
/**
643618
* Adds a command to upgrade the CLI
644-
645-
cli.command(
646-
"upgrade",
647-
new UpgradeCommand({ provider: ["choco", "brew", "curl", "scoop"] })
648-
);
649-
650-
async function checkVersion() {
651-
const mainCommand = cli.getMainCommand();
652-
const upgradeCommand = mainCommand.getCommand("upgrade");
653-
const latestVersion = await upgradeCommand.getLatestVersion();
654-
const currentVersion = mainCommand.getVersion();
655-
656-
if (currentVersion === latestVersion) {
657-
return;
658-
}
659-
660-
const versionHelpText = `(New version available: ${latestVersion}. Run '${mainCommand.getName()} upgrade' to upgrade to the latest version!)`;
661-
console.log(warn(versionHelpText));
662-
}
663619
*/
620+
// cli.command(
621+
// "upgrade",
622+
// new UpgradeCommand({ provider: ["choco", "brew", "curl", "scoop"] }),
623+
// );
624+
625+
// async function checkVersion() {
626+
// const mainCommand = cli.getMainCommand();
627+
// const upgradeCommand = mainCommand.getCommand("upgrade");
628+
// const latestVersion = await upgradeCommand.getLatestVersion();
629+
// const currentVersion = mainCommand.getVersion();
630+
631+
// if (currentVersion === latestVersion) {
632+
// return;
633+
// }
634+
635+
// const versionHelpText =
636+
// `(New version available: ${latestVersion}. Run '${mainCommand.getName()} upgrade' to upgrade to the latest version!)`;
637+
// console.log(warn(versionHelpText));
638+
// }

lib/poll.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { Formats } from "./act.ts";
2+
import { ms } from "https://deno.land/x/[email protected]/ms.ts";
3+
import { bold, cursorUp, eraseLines } from "./ansi.ts";
4+
5+
/**
6+
* Poll a function infinitely or until a condition is met.
7+
*/
8+
export async function poll(
9+
fn: () => Promise<boolean>,
10+
interval: string | number = 1000,
11+
) {
12+
await fn();
13+
const pollInterval = Number(ms(interval + ""));
14+
15+
return await new Promise<void>((resolve) => {
16+
const id = setInterval(async () => {
17+
if (await fn()) {
18+
clearInterval(id);
19+
resolve();
20+
}
21+
}, pollInterval);
22+
});
23+
}
24+
25+
export async function pollCmd(fn: () => Promise<Formats>, opt: any) {
26+
let n = 0;
27+
let output = "";
28+
29+
return await poll(async () => {
30+
const response = await fn();
31+
32+
if (opt.json && "json" in response) {
33+
output = JSON.stringify(
34+
response.json,
35+
null,
36+
2,
37+
);
38+
} else {
39+
output = "human" in response
40+
? String(response.human)
41+
: String(response.value);
42+
}
43+
44+
output += `\n\nPress ${bold("Ctrl+C")} to exit.`;
45+
46+
if (n > 0) {
47+
console.log(
48+
eraseLines(output.split("\n").length + 1),
49+
cursorUp(1),
50+
);
51+
}
52+
53+
n++;
54+
console.log(output);
55+
return false;
56+
}, opt.poll === true ? "3s" : opt.poll);
57+
}

0 commit comments

Comments
 (0)