-
Notifications
You must be signed in to change notification settings - Fork 0
/
vt.ts
executable file
·152 lines (130 loc) · 3.67 KB
/
vt.ts
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
#!/usr/bin/env -S deno run -A
import { Command, CompletionsCommand, open, Table, toText } from "./deps.ts";
import { valCmd } from "./val.ts";
import { fetchValTown, printAsJSON, printCode, valtownToken } from "./lib.ts";
import { blobCmd } from "./blob.ts";
import { tableCmd } from "./table.ts";
const rootCmd = new Command().name("vt").action(() => {
rootCmd.showHelp();
});
rootCmd.command("val", valCmd);
rootCmd.command("blob", blobCmd);
rootCmd.command("table", tableCmd);
rootCmd
.command("eval")
.description("Eval an expression.")
.arguments("[expression:string]")
.option(
"--args <args:string>",
"Arguments to pass to the expression as JSON array."
)
.action(async (options, expression) => {
if (!expression) {
if (Deno.stdin.isTerminal()) {
console.error("Expression is required.");
Deno.exit(1);
}
expression = await toText(Deno.stdin.readable);
}
const { data } = await fetchValTown("/v1/eval", {
method: "POST",
body: JSON.stringify({
code: expression,
args: options.args ? JSON.parse(options.args) : undefined,
}),
});
console.log(data);
});
rootCmd
.command("repl")
.description("Start a REPL.")
.action((_) => {
const args = [
"repl",
"--allow-net",
"--allow-env",
"--reload=https://esm.town/v/",
];
const { success } = new Deno.Command("deno", {
args,
stdin: "inherit",
stdout: "inherit",
stderr: "inherit",
env: {
valtown: valtownToken,
DENO_AUTH_TOKENS: `${valtownToken}@esm.town`,
},
}).outputSync();
if (!success) {
Deno.exit(1);
}
});
rootCmd
.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 { data: res, error } = await fetchValTown(url, {
method,
headers,
body,
});
if (error) {
console.error(error.message);
Deno.exit(1);
}
printAsJSON(res);
});
rootCmd
.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);
}
printCode("yaml", await resp.text());
});
rootCmd.command("completions", new CompletionsCommand());
rootCmd
.command("query")
.description("Execute a query.")
.arguments("<query:string>")
.action(async (_, query) => {
const { data: res } = await fetchValTown<{
rows: string[][];
columns: string[];
}>("/v1/sqlite/execute", {
method: "POST",
body: JSON.stringify({ statement: query }),
});
if (!Deno.stdout.isTerminal()) {
console.log(res.rows.map((row) => row.join("\t")).join("\n"));
return;
}
const table = new Table(...res.rows).header(res.columns);
table.render();
});
await rootCmd.parse();