Skip to content

Commit ff97581

Browse files
authored
chore: migrate to select for machine commands (#43)
1 parent e2ce254 commit ff97581

6 files changed

Lines changed: 134 additions & 50 deletions

File tree

commands/machine/delete/mod.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { asserts } from "../../../lib/asserts.ts";
22
import { loading } from "../../../lib/loading.ts";
3-
import { input } from "../../../prompts/input.ts";
43
import { args, command, flags, z } from "../../../zcli.ts";
54
import { dataTable } from "../../../lib/data-table.ts";
65
import { fields } from "../../../flags.ts";
76
import { pickJson } from "../../../lib/pick-json.ts";
87
import { machines } from "../../../api/machines.ts";
8+
import { select } from "../../../prompts/select.ts";
99

1010
/**
1111
* This variable is automatically generated by `zcli add`. Do not remove this
@@ -36,10 +36,24 @@ export const del = command("delete", {
3636
let [id] = args;
3737

3838
if (!id) {
39-
id = await input("ID:", {
40-
filter: (v) => !!v.sequence.match(/[a-zA-Z0-9_-]/),
41-
});
42-
asserts(id, "A machine ID is required");
39+
const existingMachines = await loading(machines.list({ limit: 50 }));
40+
asserts(existingMachines.ok, existingMachines);
41+
42+
const selected = await select(
43+
"Select a machine:",
44+
existingMachines.data.items,
45+
{
46+
filter(input, option) {
47+
return option.name.toLowerCase().startsWith(input);
48+
},
49+
renderOption(option, isSelected) {
50+
return `${isSelected ? ">" : " "} ${option.name}`;
51+
},
52+
},
53+
);
54+
55+
asserts(selected, "No machine selected.");
56+
id = selected.id;
4357
}
4458

4559
const response = await loading(

commands/machine/get/mod.ts

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
import { machines } from "../../../api/machines.ts";
2+
import { fields } from "../../../flags.ts";
13
import { asserts } from "../../../lib/asserts.ts";
2-
import { loading } from "../../../lib/loading.ts";
3-
import { input } from "../../../prompts/input.ts";
4-
import { args, command, flags, z } from "../../../zcli.ts";
54
import { dataTable } from "../../../lib/data-table.ts";
6-
import { fields } from "../../../flags.ts";
5+
import { loading } from "../../../lib/loading.ts";
76
import { pickJson } from "../../../lib/pick-json.ts";
8-
import { machines } from "../../../api/machines.ts";
7+
import { select } from "../../../prompts/select.ts";
8+
import { args, command, flags, z } from "../../../zcli.ts";
99

1010
/**
1111
* This variable is automatically generated by `zcli add`. Do not remove this
@@ -36,10 +36,24 @@ export const get = command("get", {
3636
let [id] = args;
3737

3838
if (!id) {
39-
id = await input("ID:", {
40-
filter: (v) => !!v.sequence.match(/[a-zA-Z0-9_-]/),
41-
});
42-
asserts(id, "A machine ID is required");
39+
const existingMachines = await loading(machines.list({ limit: 50 }));
40+
asserts(existingMachines.ok, existingMachines);
41+
42+
const selected = await select(
43+
"Select a machine:",
44+
existingMachines.data.items,
45+
{
46+
filter(input, option) {
47+
return option.name.toLowerCase().startsWith(input);
48+
},
49+
renderOption(option, isSelected) {
50+
return `${isSelected ? ">" : " "} ${option.name}`;
51+
},
52+
},
53+
);
54+
55+
asserts(selected, "No machine selected.");
56+
id = selected.id;
4357
}
4458

4559
const response = await loading(

commands/machine/restart/mod.ts

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
import { machines } from "../../../api/machines.ts";
2+
import { fields } from "../../../flags.ts";
13
import { asserts } from "../../../lib/asserts.ts";
2-
import { loading } from "../../../lib/loading.ts";
3-
import { input } from "../../../prompts/input.ts";
4-
import { args, command, flags, z } from "../../../zcli.ts";
54
import { dataTable } from "../../../lib/data-table.ts";
6-
import { fields } from "../../../flags.ts";
5+
import { loading } from "../../../lib/loading.ts";
76
import { pickJson } from "../../../lib/pick-json.ts";
8-
import { machines } from "../../../api/machines.ts";
7+
import { select } from "../../../prompts/select.ts";
8+
import { args, command, flags, z } from "../../../zcli.ts";
99

1010
/**
1111
* This variable is automatically generated by `zcli add`. Do not remove this
@@ -36,10 +36,24 @@ export const restart = command("restart", {
3636
let [id] = args;
3737

3838
if (!id) {
39-
id = await input("ID:", {
40-
filter: (v) => !!v.sequence.match(/[a-zA-Z0-9_-]/),
41-
});
42-
asserts(id, "A machine ID is required");
39+
const existingMachines = await loading(machines.list({ limit: 50 }));
40+
asserts(existingMachines.ok, existingMachines);
41+
42+
const selected = await select(
43+
"Select a machine:",
44+
existingMachines.data.items,
45+
{
46+
filter(input, option) {
47+
return option.name.toLowerCase().startsWith(input);
48+
},
49+
renderOption(option, isSelected) {
50+
return `${isSelected ? ">" : " "} ${option.name}`;
51+
},
52+
},
53+
);
54+
55+
asserts(selected, "No machine selected.");
56+
id = selected.id;
4357
}
4458

4559
const response = await loading(

commands/machine/start/mod.ts

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
import { machines } from "../../../api/machines.ts";
2+
import { fields } from "../../../flags.ts";
13
import { asserts } from "../../../lib/asserts.ts";
2-
import { loading } from "../../../lib/loading.ts";
3-
import { input } from "../../../prompts/input.ts";
4-
import { args, command, flags, z } from "../../../zcli.ts";
54
import { dataTable } from "../../../lib/data-table.ts";
6-
import { fields } from "../../../flags.ts";
5+
import { loading } from "../../../lib/loading.ts";
76
import { pickJson } from "../../../lib/pick-json.ts";
8-
import { machines } from "../../../api/machines.ts";
7+
import { select } from "../../../prompts/select.ts";
8+
import { args, command, flags, z } from "../../../zcli.ts";
99

1010
/**
1111
* This variable is automatically generated by `zcli add`. Do not remove this
@@ -36,10 +36,24 @@ export const start = command("start", {
3636
let [id] = args;
3737

3838
if (!id) {
39-
id = await input("ID:", {
40-
filter: (v) => !!v.sequence.match(/[a-zA-Z0-9_-]/),
41-
});
42-
asserts(id, "A machine ID is required");
39+
const existingMachines = await loading(machines.list({ limit: 50 }));
40+
asserts(existingMachines.ok, existingMachines);
41+
42+
const selected = await select(
43+
"Select a machine:",
44+
existingMachines.data.items,
45+
{
46+
filter(input, option) {
47+
return option.name.toLowerCase().startsWith(input);
48+
},
49+
renderOption(option, isSelected) {
50+
return `${isSelected ? ">" : " "} ${option.name}`;
51+
},
52+
},
53+
);
54+
55+
asserts(selected, "No machine selected.");
56+
id = selected.id;
4357
}
4458

4559
const response = await loading(

commands/machine/stop/mod.ts

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
import { machines } from "../../../api/machines.ts";
2+
import { fields } from "../../../flags.ts";
13
import { asserts } from "../../../lib/asserts.ts";
2-
import { loading } from "../../../lib/loading.ts";
3-
import { input } from "../../../prompts/input.ts";
4-
import { args, command, flags, z } from "../../../zcli.ts";
54
import { dataTable } from "../../../lib/data-table.ts";
6-
import { fields } from "../../../flags.ts";
5+
import { loading } from "../../../lib/loading.ts";
76
import { pickJson } from "../../../lib/pick-json.ts";
8-
import { machines } from "../../../api/machines.ts";
7+
import { select } from "../../../prompts/select.ts";
8+
import { args, command, flags, z } from "../../../zcli.ts";
99

1010
/**
1111
* This variable is automatically generated by `zcli add`. Do not remove this
@@ -36,10 +36,24 @@ export const stop = command("stop", {
3636
let [id] = args;
3737

3838
if (!id) {
39-
id = await input("ID:", {
40-
filter: (v) => !!v.sequence.match(/[a-zA-Z0-9_-]/),
41-
});
42-
asserts(id, "A machine ID is required");
39+
const existingMachines = await loading(machines.list({ limit: 50 }));
40+
asserts(existingMachines.ok, existingMachines);
41+
42+
const selected = await select(
43+
"Select a machine:",
44+
existingMachines.data.items,
45+
{
46+
filter(input, option) {
47+
return option.name.toLowerCase().startsWith(input);
48+
},
49+
renderOption(option, isSelected) {
50+
return `${isSelected ? ">" : " "} ${option.name}`;
51+
},
52+
},
53+
);
54+
55+
asserts(selected, "No machine selected.");
56+
id = selected.id;
4357
}
4458

4559
const response = await loading(

commands/machine/update/mod.ts

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1+
import { machines } from "../../../api/machines.ts";
2+
import { fields } from "../../../flags.ts";
13
import { asserts } from "../../../lib/asserts.ts";
2-
import { loading } from "../../../lib/loading.ts";
3-
import { args, command, flag, flags, z } from "../../../zcli.ts";
44
import { dataTable } from "../../../lib/data-table.ts";
5-
import { fields } from "../../../flags.ts";
5+
import { loading } from "../../../lib/loading.ts";
66
import { pickJson } from "../../../lib/pick-json.ts";
7-
import { machines } from "../../../api/machines.ts";
7+
import { select } from "../../../prompts/select.ts";
8+
import { args, command, flag, flags, z } from "../../../zcli.ts";
89
import {
910
MachineAutoSnapshotFrequencySchema,
1011
MachinePublicIpTypeSchema,
1112
MachineRestorePointFrequencySchema,
1213
} from "../schemas.ts";
13-
import { input } from "../../../prompts/input.ts";
1414

1515
/**
1616
* This variable is automatically generated by `zcli add`. Do not remove this
@@ -84,10 +84,24 @@ export const update = command("update", {
8484
async function* ({ args, flags }) {
8585
let [id] = args;
8686
if (!id) {
87-
id = await input("ID:", {
88-
filter: (v) => !!v.sequence.match(/[a-zA-Z0-9_-]/),
89-
});
90-
asserts(id, "A machine ID is required");
87+
const existingMachines = await loading(machines.list({ limit: 50 }));
88+
asserts(existingMachines.ok, existingMachines);
89+
90+
const selected = await select(
91+
"Select a machine:",
92+
existingMachines.data.items,
93+
{
94+
filter(input, option) {
95+
return option.name.toLowerCase().startsWith(input);
96+
},
97+
renderOption(option, isSelected) {
98+
return `${isSelected ? ">" : " "} ${option.name}`;
99+
},
100+
},
101+
);
102+
103+
asserts(selected, "No machine selected.");
104+
id = selected.id;
91105
}
92106
const parsedPublicIpType = MachinePublicIpTypeSchema.safeParse(
93107
flags["public-ip-type"],

0 commit comments

Comments
 (0)