-
Notifications
You must be signed in to change notification settings - Fork 0
/
blob.ts
133 lines (117 loc) · 3.24 KB
/
blob.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
import { Command, Table, toText } from "./deps.ts";
import { editText, fetchValTown } from "./lib.ts";
export const blobCmd = new Command()
.name("val")
.description("Manage Blobs")
.action(() => {
blobCmd.showHelp();
});
blobCmd
.command("list")
.alias("ls")
.description("List blobs.")
.option("-p, --prefix <prefix:string>", "Prefix to filter by.")
.action(async (options) => {
const { data: blobs, error } = await fetchValTown<
{ key: string; size: number; lastModified: string }[]
>(
options.prefix
? `/v1/blob?prefix=${encodeURIComponent(options.prefix)}`
: "/v1/blob"
);
if (error) {
console.error(error);
Deno.exit(1);
}
if (!Deno.stdout.isTerminal()) {
for (const blob of blobs) {
console.log(`${blob.key}\t${blob.size}\t${blob.lastModified}`);
}
return;
}
const rows = blobs.map((blob) => [blob.key, blob.size, blob.lastModified]);
const table = new Table(...rows).header(["key", "size", "lastModified"]);
table.render();
});
blobCmd
.command("download")
.description("Download a blob.")
.arguments("<key:string> <path:string>")
.action(async (_, key, path) => {
const { data: blob } = await fetchValTown(
`/v1/blob/${encodeURIComponent(key)}`
);
if (path == "-") {
Deno.stdout.writeSync(new Uint8Array(await blob.arrayBuffer()));
} else {
Deno.writeFileSync(path, new Uint8Array(await blob.arrayBuffer()));
}
});
blobCmd
.command("view")
.alias("cat")
.description("Print a blob to stdout.")
.arguments("<key:string>")
.action(async (_, key) => {
const { data: content } = await fetchValTown(
`/v1/blob/${encodeURIComponent(key)}`
);
await Deno.stdout.write(new Uint8Array(content));
});
blobCmd
.command("edit")
.description("Edit a blob.")
.arguments("<key:string>")
.action(async (_, key) => {
let { data: content, error } = await fetchValTown<string>(
`/v1/blob/${encodeURIComponent(key)}`
);
if (error) {
console.error(error);
Deno.exit(1);
}
if (Deno.stdin.isTerminal()) {
const extension = key.split(".").pop() || "txt";
content = await editText(content, extension);
} else {
content = await toText(Deno.stdin.readable);
}
await fetchValTown(`/v1/blob/${encodeURIComponent(key)}`, {
method: "POST",
body: content,
});
console.log(`Updated blob ${key}`);
});
blobCmd
.command("upload")
.description("Upload a blob.")
.arguments("<path:string> <key:string>")
.action(async (_, path, key) => {
let body: ReadableStream;
if (path === "-") {
body = Deno.stdin.readable;
} else {
const file = await Deno.open(path);
body = file.readable;
}
await fetchValTown(`/v1/blob/${encodeURIComponent(key)}`, {
method: "POST",
body,
});
console.log("Uploaded!");
});
blobCmd
.command("delete")
.description("Delete a blob.")
.alias("rm")
.arguments("<key:string>")
.action(async (_, key) => {
const res = await fetchValTown(`/v1/blob/${encodeURIComponent(key)}`, {
method: "DELETE",
});
if (res.error) {
console.error(res.error);
Deno.exit(1);
}
console.log("Deleted!");
});