feat(cli): Tarball pull (#867)

* Add tarball pull

* Move command & prompt on conflict

* Remove old file

Co-authored-by: Ruben Fiszel <ruben@rubenfiszel.com>
This commit is contained in:
Kai Jellinghaus
2022-11-07 16:02:49 +01:00
committed by GitHub
co-authored by Ruben Fiszel
parent b6fd957271
commit d375836989
3 changed files with 89 additions and 0 deletions
+2
View File
@@ -13,6 +13,7 @@ export type Context = {
workspace: string;
baseUrl: string;
urlStore: string;
token: string;
};
export async function getContext({
@@ -45,5 +46,6 @@ export async function getContext({
workspace: workspaceId,
baseUrl: baseUrl,
urlStore: urlStore,
token: token,
};
}
+2
View File
@@ -13,6 +13,7 @@ import user from "./user.ts";
import setup from "./setup.ts";
import variable from "./variable.ts";
import push from "./push.ts";
import pull from "./pull.ts";
const VERSION = "v1.45.0";
@@ -71,6 +72,7 @@ await new Command()
.command("setup", setup)
.command("variable", variable)
.command("push", push)
.command("pull", pull)
.command(
"upgrade",
new UpgradeCommand({
+85
View File
@@ -0,0 +1,85 @@
import { Untar } from "https://deno.land/std@0.162.0/archive/tar.ts";
import { Command } from "https://deno.land/x/cliffy@v0.25.4/command/command.ts";
import {
copy,
readerFromStreamReader,
} from "https://deno.land/std@0.162.0/streams/mod.ts";
import { getContext } from "./context.ts";
import { GlobalOptions } from "./types.ts";
import * as path from "https://deno.land/std@0.162.0/path/mod.ts";
import { colors } from "https://deno.land/x/cliffy@v0.25.4/ansi/colors.ts";
import { ensureDir } from "https://deno.land/std@0.162.0/fs/ensure_dir.ts";
import { Confirm } from "https://deno.land/x/cliffy@v0.25.4/prompt/confirm.ts";
async function pull(opts: GlobalOptions & { override: boolean }, dir: string) {
const { workspace, baseUrl, token } = await getContext(opts);
const requestHeaders: HeadersInit = new Headers();
requestHeaders.set("Authorization", "Bearer " + token);
requestHeaders.set("Content-Type", "application/octet-stream");
const tarResponse = await fetch(
baseUrl + "/api/w/" + workspace + "/workspaces/tarball",
{
headers: requestHeaders,
method: "GET",
}
);
if (!tarResponse.ok) {
console.log(colors.red("Failed to request tarball from API"));
console.log(colors.red(await tarResponse.text()));
return;
}
const streamReader = tarResponse.body?.getReader();
if (!streamReader) {
console.log(colors.red("Failed to read tar request body"));
return;
}
console.log(colors.yellow("Streaming tarball to disk..."));
const denoReader = readerFromStreamReader(streamReader);
const untar = new Untar(denoReader);
for await (const entry of untar) {
console.log(entry.fileName);
const filePath = path.resolve(dir, entry.fileName);
if (entry.type === "directory") {
await ensureDir(filePath);
continue;
}
await ensureDir(path.dirname(filePath));
if (!opts.override) {
let exists = false;
try {
const _stat = await Deno.stat(filePath);
exists = true;
} catch {
exists = false;
}
if (exists) {
if (
!(await Confirm.prompt(
"Conflict at " +
filePath +
" do you want to override the local version?"
))
) {
continue;
}
}
}
const file = await Deno.open(filePath, { write: true, create: true });
await copy(entry, file);
file.close();
}
console.log(colors.green("Done. Wrote all files to disk."));
}
const command = new Command()
.description(
"Pull all definitions in the current workspace from the API and write them to disk."
)
.arguments("<dir:string>")
.action(pull as any);
export default command;