Skip to content

Commit a3bfecd

Browse files
authored
feat(templates): split template into os-template and custom-template (#72)
1 parent abe6efa commit a3bfecd

11 files changed

Lines changed: 1235 additions & 1085 deletions

File tree

api/openapi.ts

Lines changed: 1085 additions & 1044 deletions
Large diffs are not rendered by default.

api/templates.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import { client } from "./client.ts";
22

3-
export const templates = {
4-
create: client("/templates").post,
5-
get: client("/templates/{id}").get,
6-
list: client("/templates").get,
7-
delete: client("/templates/{id}").delete,
3+
export const customTemplates = {
4+
create: client("/custom-templates").post,
5+
get: client("/custom-templates/{id}").get,
6+
list: client("/custom-templates").get,
7+
delete: client("/custom-templates/{id}").delete,
8+
};
9+
10+
export const osTemplates = {
11+
list: client("/os-templates").get,
812
};
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { command, flag, flags } from "../../../zcli.ts";
44
import { dataTable } from "../../../lib/data-table.ts";
55
import { fields } from "../../../flags.ts";
66
import { pickJson } from "../../../lib/pick-json.ts";
7-
import { templates } from "../../../api/templates.ts";
7+
import { customTemplates } from "../../../api/templates.ts";
88
import { defaultFields } from "../mod.ts";
99

1010
/**
@@ -14,21 +14,21 @@ import { defaultFields } from "../mod.ts";
1414
const subCommands: ReturnType<typeof command>[] = [];
1515

1616
export const create = command("create", {
17-
short: "Create a template",
17+
short: "Create a custom template",
1818
long: `
19-
Create a template from a machine.
19+
Create a custom template from a machine.
2020
`,
2121
commands: subCommands,
2222
flags: flags({
2323
fields,
2424
}).merge(flags({
2525
"name": flag({
2626
aliases: ["n"],
27-
short: "The name of the template",
27+
short: "The name of the custom template",
2828
}).string(),
2929
"machine-id": flag({
3030
aliases: ["m"],
31-
short: "The ID of the machine to create a template from",
31+
short: "The ID of the machine to create a custom template from",
3232
}).string(),
3333
})),
3434
// We use command metadata in the `persistentPreRun` function to check if a
@@ -41,7 +41,7 @@ export const create = command("create", {
4141
}).run(
4242
async function* ({ flags }) {
4343
const response = await loading(
44-
templates.create({
44+
customTemplates.create({
4545
name: flags.name,
4646
machineId: flags["machine-id"],
4747
}),
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { args, command, flags, z } from "../../../zcli.ts";
55
import { dataTable } from "../../../lib/data-table.ts";
66
import { fields } from "../../../flags.ts";
77
import { pickJson } from "../../../lib/pick-json.ts";
8-
import { templates } from "../../../api/templates.ts";
8+
import { customTemplates } from "../../../api/templates.ts";
99
import { defaultFields } from "../mod.ts";
1010

1111
/**
@@ -15,13 +15,13 @@ import { defaultFields } from "../mod.ts";
1515
const subCommands: ReturnType<typeof command>[] = [];
1616

1717
export const del = command("delete", {
18-
short: "Delete a template",
18+
short: "Delete a custom template",
1919
long: `
20-
Delete a template from a team.
20+
Delete a custom template from a team.
2121
`,
2222
commands: subCommands,
2323
args: args().tuple([
24-
z.string().describe("The ID of the template to delete"),
24+
z.string().describe("The ID of the custom template to delete"),
2525
]).optional(),
2626
flags: flags({
2727
fields,
@@ -40,11 +40,11 @@ export const del = command("delete", {
4040
id = await input("ID:", {
4141
filter: (v) => !!v.sequence.match(/[a-zA-Z0-9_-]/),
4242
});
43-
asserts(id, "A template ID is required");
43+
asserts(id, "A custom template ID is required");
4444
}
4545

4646
const response = await loading(
47-
templates.delete({ id }),
47+
customTemplates.delete({ id }),
4848
);
4949

5050
asserts(response.ok, response);
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { args, command, flags, z } from "../../../zcli.ts";
55
import { dataTable } from "../../../lib/data-table.ts";
66
import { fields } from "../../../flags.ts";
77
import { pickJson } from "../../../lib/pick-json.ts";
8-
import { templates } from "../../../api/templates.ts";
8+
import { customTemplates } from "../../../api/templates.ts";
99
import { defaultFields } from "../mod.ts";
1010

1111
/**
@@ -15,13 +15,13 @@ import { defaultFields } from "../mod.ts";
1515
const subCommands: ReturnType<typeof command>[] = [];
1616

1717
export const get = command("get", {
18-
short: "Get a template",
18+
short: "Get a custom template",
1919
long: `
20-
Get a template from a team.
20+
Get a custom template from a team.
2121
`,
2222
commands: subCommands,
2323
args: args().tuple([
24-
z.string().describe("The ID of the template to get"),
24+
z.string().describe("The ID of the custom template to get"),
2525
]).optional(),
2626
flags: flags({
2727
fields,
@@ -40,11 +40,11 @@ export const get = command("get", {
4040
id = await input("ID:", {
4141
filter: (v) => !!v.sequence.match(/[a-zA-Z0-9_-]/),
4242
});
43-
asserts(id, "A template ID is required");
43+
asserts(id, "A custom template ID is required");
4444
}
4545

4646
const response = await loading(
47-
templates.get({ id }),
47+
customTemplates.get({ id }),
4848
);
4949

5050
asserts(response.ok, response);
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { loading } from "../../../lib/loading.ts";
55
import * as psFlags from "../../../flags.ts";
66
import { pickJson } from "../../../lib/pick-json.ts";
77
import { config } from "../../../config.ts";
8-
import { templates } from "../../../api/templates.ts";
8+
import { customTemplates } from "../../../api/templates.ts";
99
import { defaultFields } from "../mod.ts";
1010

1111
/**
@@ -15,13 +15,13 @@ import { defaultFields } from "../mod.ts";
1515
const subCommands: ReturnType<typeof command>[] = [];
1616

1717
export const list = command("list", {
18-
short: "List templates.",
18+
short: "List custom templates.",
1919
long: ({ root }) => `
20-
List templates in your team.
20+
List custom templates in your team.
2121
2222
Pick a subset of fields to display:
2323
\`\`\`
24-
${root.name} template list -F name -F dtModified
24+
${root.name} custom-template list -F name -F dtModified
2525
\`\`\`
2626
`,
2727
commands: subCommands,
@@ -48,9 +48,9 @@ export const list = command("list", {
4848
}).run(
4949
async function* ({ flags }) {
5050
const team = await config.get("team");
51-
asserts(team, "You must be in a team to list templates.");
51+
asserts(team, "You must be in a team to list custom templates.");
5252
const result = await loading(
53-
templates.list({
53+
customTemplates.list({
5454
limit: flags.limit,
5555
after: flags.after,
5656
orderBy: "dtCreated",
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ const subCommands: ReturnType<typeof command>[] = [
2424
del,
2525
];
2626

27-
export const template = command("template", {
28-
short: "Manage your templates",
27+
export const customTemplate = command("custom-template", {
28+
short: "Manage your custom templates",
2929
long: `
30-
Manage your templates. Templates are a backup of your machine's disk.
31-
They can be used to create additional machines. You can use them to
32-
prepopulate a machine with your desired software stack.
30+
Manage your custom templates. Custom templates are a backup of your
31+
machine's disk. They can be used to create additional machines. You
32+
can use them to prepopulate a machine with your desired software stack.
3333
3434
For more information, see ${new URL(
3535
"/compute/custom-templates",
@@ -38,7 +38,7 @@ export const template = command("template", {
3838
`,
3939
commands: subCommands,
4040
}).run(function* ({ ctx }) {
41-
for (const line of template.help(ctx)) {
41+
for (const line of customTemplate.help(ctx)) {
4242
yield line;
4343
}
4444
});

commands/mod.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ import { signup } from "./signup/mod.ts";
2929
import { snapshot } from "./snapshot/mod.ts";
3030
import { startupScript } from "./startup-script/mod.ts";
3131
import { storageProvider } from "./storage-provider/mod.ts";
32-
import { template } from "./template/mod.ts";
32+
import { customTemplate } from "./custom-template/mod.ts";
3333
import { up } from "./up/mod.ts";
3434
import { getLatestVersion, upgrade } from "./upgrade/mod.ts";
35+
import { osTemplate } from "./os-template/mod.ts";
3536

3637
/**
3738
* This variable is automatically generated by `zcli add`. Do not remove this
@@ -41,6 +42,7 @@ export const commands = [
4142
autoscalingGroup,
4243
config,
4344
console,
45+
customTemplate,
4446
dataset,
4547
deployment,
4648
docs,
@@ -49,6 +51,7 @@ export const commands = [
4951
logout,
5052
machine,
5153
machineEvent,
54+
osTemplate,
5255
privateNetwork,
5356
project,
5457
publicIp,
@@ -58,7 +61,6 @@ export const commands = [
5861
sharedDrive,
5962
startupScript,
6063
storageProvider,
61-
template,
6264
up,
6365
upgrade,
6466
model,

commands/os-template/list/mod.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { command, flag, flags } from "../../../zcli.ts";
2+
import { asserts } from "../../../lib/asserts.ts";
3+
import { dataTable } from "../../../lib/data-table.ts";
4+
import { loading } from "../../../lib/loading.ts";
5+
import * as psFlags from "../../../flags.ts";
6+
import { pickJson } from "../../../lib/pick-json.ts";
7+
import { osTemplates } from "../../../api/templates.ts";
8+
import { defaultFields } from "../mod.ts";
9+
10+
/**
11+
* This variable is automatically generated by `zcli add`. Do not remove this
12+
* or change its name unless you're no longer using `zcli add`.
13+
*/
14+
const subCommands: ReturnType<typeof command>[] = [];
15+
16+
export const list = command("list", {
17+
short: "List OS templates.",
18+
long: ({ root }) => `
19+
List OS templates.
20+
21+
Pick a subset of fields to display:
22+
\`\`\`
23+
${root.name} os-template list -F name
24+
\`\`\`
25+
`,
26+
commands: subCommands,
27+
flags: psFlags.paginator.merge(flags({
28+
"name": flag({
29+
aliases: ["n"],
30+
short: "Filter by name.",
31+
}).ostring(),
32+
})),
33+
// We use command metadata in the `persistentPreRun` function to check if a
34+
// command requires an API key. If it does, we'll check to see if one is
35+
// set. If not, we'll throw an error.
36+
meta: {
37+
requireApiKey: true,
38+
},
39+
}).run(
40+
async function* ({ flags }) {
41+
const result = await loading(
42+
osTemplates.list({
43+
limit: flags.limit,
44+
after: flags.after,
45+
orderBy: "name",
46+
order: flags.asc ? "asc" : undefined,
47+
name: flags.name,
48+
}),
49+
{ enabled: !flags.json },
50+
);
51+
52+
asserts(result.ok, result);
53+
54+
if (!flags.json) {
55+
for await (
56+
const line of dataTable(
57+
result.data.items,
58+
flags.fields ?? defaultFields,
59+
)
60+
) {
61+
yield line;
62+
}
63+
} else {
64+
yield pickJson(result.data, flags.fields);
65+
}
66+
},
67+
);

commands/os-template/mod.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { command } from "../../zcli.ts";
2+
import { list } from "./list/mod.ts";
3+
import { env } from "../../env.ts";
4+
5+
export const defaultFields = [
6+
"id",
7+
"name",
8+
"operatingSystemLabel",
9+
"defaultSizeGb",
10+
];
11+
12+
/**
13+
* This variable is automatically generated by `zcli add`. Do not remove this
14+
* or change its name unless you're no longer using `zcli add`.
15+
*/
16+
const subCommands: ReturnType<typeof command>[] = [
17+
list,
18+
];
19+
20+
export const osTemplate = command("os-template", {
21+
short: "List OS templates",
22+
long: `
23+
List OS templates. OS templates are pre-configured virtual machines that
24+
you can use to create a new machine.
25+
26+
For more information, see ${new URL(
27+
"/compute/os-templates",
28+
env.get("PAPERSPACE_DOCS_URL"),
29+
)}.
30+
`,
31+
commands: subCommands,
32+
}).run(function* ({ ctx }) {
33+
for (const line of osTemplate.help(ctx)) {
34+
yield line;
35+
}
36+
});

0 commit comments

Comments
 (0)