Skip to content

Commit

Permalink
chore: migrate to select for machine commands (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
ps-kwang authored Jun 27, 2023
1 parent e2ce254 commit ff97581
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 50 deletions.
24 changes: 19 additions & 5 deletions commands/machine/delete/mod.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { asserts } from "../../../lib/asserts.ts";
import { loading } from "../../../lib/loading.ts";
import { input } from "../../../prompts/input.ts";
import { args, command, flags, z } from "../../../zcli.ts";
import { dataTable } from "../../../lib/data-table.ts";
import { fields } from "../../../flags.ts";
import { pickJson } from "../../../lib/pick-json.ts";
import { machines } from "../../../api/machines.ts";
import { select } from "../../../prompts/select.ts";

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

if (!id) {
id = await input("ID:", {
filter: (v) => !!v.sequence.match(/[a-zA-Z0-9_-]/),
});
asserts(id, "A machine ID is required");
const existingMachines = await loading(machines.list({ limit: 50 }));
asserts(existingMachines.ok, existingMachines);

const selected = await select(
"Select a machine:",
existingMachines.data.items,
{
filter(input, option) {
return option.name.toLowerCase().startsWith(input);
},
renderOption(option, isSelected) {
return `${isSelected ? ">" : " "} ${option.name}`;
},
},
);

asserts(selected, "No machine selected.");
id = selected.id;
}

const response = await loading(
Expand Down
32 changes: 23 additions & 9 deletions commands/machine/get/mod.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { machines } from "../../../api/machines.ts";
import { fields } from "../../../flags.ts";
import { asserts } from "../../../lib/asserts.ts";
import { loading } from "../../../lib/loading.ts";
import { input } from "../../../prompts/input.ts";
import { args, command, flags, z } from "../../../zcli.ts";
import { dataTable } from "../../../lib/data-table.ts";
import { fields } from "../../../flags.ts";
import { loading } from "../../../lib/loading.ts";
import { pickJson } from "../../../lib/pick-json.ts";
import { machines } from "../../../api/machines.ts";
import { select } from "../../../prompts/select.ts";
import { args, command, flags, z } from "../../../zcli.ts";

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

if (!id) {
id = await input("ID:", {
filter: (v) => !!v.sequence.match(/[a-zA-Z0-9_-]/),
});
asserts(id, "A machine ID is required");
const existingMachines = await loading(machines.list({ limit: 50 }));
asserts(existingMachines.ok, existingMachines);

const selected = await select(
"Select a machine:",
existingMachines.data.items,
{
filter(input, option) {
return option.name.toLowerCase().startsWith(input);
},
renderOption(option, isSelected) {
return `${isSelected ? ">" : " "} ${option.name}`;
},
},
);

asserts(selected, "No machine selected.");
id = selected.id;
}

const response = await loading(
Expand Down
32 changes: 23 additions & 9 deletions commands/machine/restart/mod.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { machines } from "../../../api/machines.ts";
import { fields } from "../../../flags.ts";
import { asserts } from "../../../lib/asserts.ts";
import { loading } from "../../../lib/loading.ts";
import { input } from "../../../prompts/input.ts";
import { args, command, flags, z } from "../../../zcli.ts";
import { dataTable } from "../../../lib/data-table.ts";
import { fields } from "../../../flags.ts";
import { loading } from "../../../lib/loading.ts";
import { pickJson } from "../../../lib/pick-json.ts";
import { machines } from "../../../api/machines.ts";
import { select } from "../../../prompts/select.ts";
import { args, command, flags, z } from "../../../zcli.ts";

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

if (!id) {
id = await input("ID:", {
filter: (v) => !!v.sequence.match(/[a-zA-Z0-9_-]/),
});
asserts(id, "A machine ID is required");
const existingMachines = await loading(machines.list({ limit: 50 }));
asserts(existingMachines.ok, existingMachines);

const selected = await select(
"Select a machine:",
existingMachines.data.items,
{
filter(input, option) {
return option.name.toLowerCase().startsWith(input);
},
renderOption(option, isSelected) {
return `${isSelected ? ">" : " "} ${option.name}`;
},
},
);

asserts(selected, "No machine selected.");
id = selected.id;
}

const response = await loading(
Expand Down
32 changes: 23 additions & 9 deletions commands/machine/start/mod.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { machines } from "../../../api/machines.ts";
import { fields } from "../../../flags.ts";
import { asserts } from "../../../lib/asserts.ts";
import { loading } from "../../../lib/loading.ts";
import { input } from "../../../prompts/input.ts";
import { args, command, flags, z } from "../../../zcli.ts";
import { dataTable } from "../../../lib/data-table.ts";
import { fields } from "../../../flags.ts";
import { loading } from "../../../lib/loading.ts";
import { pickJson } from "../../../lib/pick-json.ts";
import { machines } from "../../../api/machines.ts";
import { select } from "../../../prompts/select.ts";
import { args, command, flags, z } from "../../../zcli.ts";

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

if (!id) {
id = await input("ID:", {
filter: (v) => !!v.sequence.match(/[a-zA-Z0-9_-]/),
});
asserts(id, "A machine ID is required");
const existingMachines = await loading(machines.list({ limit: 50 }));
asserts(existingMachines.ok, existingMachines);

const selected = await select(
"Select a machine:",
existingMachines.data.items,
{
filter(input, option) {
return option.name.toLowerCase().startsWith(input);
},
renderOption(option, isSelected) {
return `${isSelected ? ">" : " "} ${option.name}`;
},
},
);

asserts(selected, "No machine selected.");
id = selected.id;
}

const response = await loading(
Expand Down
32 changes: 23 additions & 9 deletions commands/machine/stop/mod.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { machines } from "../../../api/machines.ts";
import { fields } from "../../../flags.ts";
import { asserts } from "../../../lib/asserts.ts";
import { loading } from "../../../lib/loading.ts";
import { input } from "../../../prompts/input.ts";
import { args, command, flags, z } from "../../../zcli.ts";
import { dataTable } from "../../../lib/data-table.ts";
import { fields } from "../../../flags.ts";
import { loading } from "../../../lib/loading.ts";
import { pickJson } from "../../../lib/pick-json.ts";
import { machines } from "../../../api/machines.ts";
import { select } from "../../../prompts/select.ts";
import { args, command, flags, z } from "../../../zcli.ts";

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

if (!id) {
id = await input("ID:", {
filter: (v) => !!v.sequence.match(/[a-zA-Z0-9_-]/),
});
asserts(id, "A machine ID is required");
const existingMachines = await loading(machines.list({ limit: 50 }));
asserts(existingMachines.ok, existingMachines);

const selected = await select(
"Select a machine:",
existingMachines.data.items,
{
filter(input, option) {
return option.name.toLowerCase().startsWith(input);
},
renderOption(option, isSelected) {
return `${isSelected ? ">" : " "} ${option.name}`;
},
},
);

asserts(selected, "No machine selected.");
id = selected.id;
}

const response = await loading(
Expand Down
32 changes: 23 additions & 9 deletions commands/machine/update/mod.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { machines } from "../../../api/machines.ts";
import { fields } from "../../../flags.ts";
import { asserts } from "../../../lib/asserts.ts";
import { loading } from "../../../lib/loading.ts";
import { args, command, flag, flags, z } from "../../../zcli.ts";
import { dataTable } from "../../../lib/data-table.ts";
import { fields } from "../../../flags.ts";
import { loading } from "../../../lib/loading.ts";
import { pickJson } from "../../../lib/pick-json.ts";
import { machines } from "../../../api/machines.ts";
import { select } from "../../../prompts/select.ts";
import { args, command, flag, flags, z } from "../../../zcli.ts";
import {
MachineAutoSnapshotFrequencySchema,
MachinePublicIpTypeSchema,
MachineRestorePointFrequencySchema,
} from "../schemas.ts";
import { input } from "../../../prompts/input.ts";

/**
* This variable is automatically generated by `zcli add`. Do not remove this
Expand Down Expand Up @@ -84,10 +84,24 @@ export const update = command("update", {
async function* ({ args, flags }) {
let [id] = args;
if (!id) {
id = await input("ID:", {
filter: (v) => !!v.sequence.match(/[a-zA-Z0-9_-]/),
});
asserts(id, "A machine ID is required");
const existingMachines = await loading(machines.list({ limit: 50 }));
asserts(existingMachines.ok, existingMachines);

const selected = await select(
"Select a machine:",
existingMachines.data.items,
{
filter(input, option) {
return option.name.toLowerCase().startsWith(input);
},
renderOption(option, isSelected) {
return `${isSelected ? ">" : " "} ${option.name}`;
},
},
);

asserts(selected, "No machine selected.");
id = selected.id;
}
const parsedPublicIpType = MachinePublicIpTypeSchema.safeParse(
flags["public-ip-type"],
Expand Down

0 comments on commit ff97581

Please sign in to comment.