This repository was archived by the owner on May 2, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroot.ts
More file actions
155 lines (133 loc) · 4.21 KB
/
Copy pathroot.ts
File metadata and controls
155 lines (133 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import { Command } from "@cliffy/command";
import { CompletionsCommand } from "@cliffy/command/completions";
import { UpgradeCommand } from "@cliffy/command/upgrade";
import { JsrProvider } from "@cliffy/command/upgrade/provider/jsr";
import manifest from "./deno.json" with { type: "json" };
import open from "open";
import { toText } from "@std/streams";
import { Table } from "@cliffy/table";
import { valCmd } from "./val.ts";
import { fetchValTown, printJson, printYaml } from "./lib.ts";
import { blobCmd } from "./blob.ts";
import { tableCmd } from "./table.ts";
const cmd: Command = new Command()
.name("vt")
.help({ colors: Deno.stdout.isTerminal() })
.version(manifest.version)
.action(
() => {
cmd.showHelp();
},
);
cmd.command("val", valCmd);
cmd.command("blob", blobCmd);
cmd.command("table", tableCmd);
cmd
.command("api")
.description("Make an API request.")
.example("Get your user info", "vt api /v1/me")
.arguments("<url-or-path:string>")
.option("-X, --method <method:string>", "HTTP method.", { default: "GET" })
.option("-d, --data <data:string>", "Request Body")
.option("-H, --header <header:string>", "Request Header", { collect: true })
.action(async ({ method, data, header }, url) => {
const headers: Record<string, string> = {};
for (const h of header || []) {
const [key, value] = h.split(":", 2);
headers[key.trim()] = value.trim();
}
let body: string | undefined;
if (data == "@-") {
body = await toText(Deno.stdin.readable);
} else if (data) {
body = data;
}
const resp = await fetchValTown(url, {
method,
headers,
body,
});
if (!resp.ok) {
console.error(await resp.text());
Deno.exit(1);
}
if (resp.headers.get("Content-Type")?.includes("application/json")) {
return printJson(await resp.json());
}
console.log(await resp.text());
});
cmd
.command("openapi")
.description("View openapi specs")
.hidden()
.option("--web, -w", "Open in browser")
.action(async ({ web }) => {
if (web) {
open("https://www.val.town/docs/openapi.html");
Deno.exit(0);
}
const resp = await fetch("https://www.val.town/docs/openapi.yaml");
if (resp.status != 200) {
console.error(resp.statusText);
Deno.exit(1);
}
if (!resp.ok) {
console.error(await resp.text());
Deno.exit(1);
}
printYaml(await resp.text());
});
cmd.command("completions", new CompletionsCommand());
cmd
.command("email")
.description("Send an email.")
.option("-t, --to <to:string>", "To")
.option("-s, --subject <subject:string>", "Subject")
.option("-b, --body <body:string>", "Body")
.action(async (options) => {
const resp = await fetchValTown("/v1/email", {
method: "POST",
body: JSON.stringify({
from: "pomdtr.vt@valtown.email",
to: options.to,
subject: options.subject,
text: options.body,
}),
});
if (!resp.ok) {
console.error(await resp.text());
Deno.exit(1);
}
console.log("Email sent.");
});
cmd
.command("query")
.description("Execute a query.")
.arguments("<query:string>")
.action(async (_, query) => {
const resp = await fetchValTown("/v1/sqlite/execute", {
method: "POST",
body: JSON.stringify({ statement: query }),
});
if (!resp.ok) {
console.error(await resp.text());
Deno.exit(1);
}
const res = await resp.json();
if (!Deno.stdout.isTerminal()) {
console.log(JSON.stringify(res));
return;
}
const table = new Table(...res.rows).header(res.columns);
table.render();
});
cmd.command(
"upgrade",
new UpgradeCommand({
args: ["--allow-all"],
provider: new JsrProvider({
package: "@pomdtr/vt",
}),
}),
);
export { cmd };