2020-05-27 15:24:57 -04:00
|
|
|
#!/usr/bin/env -S deno run --allow-net --allow-env
|
2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-05-02 18:35:25 -04:00
|
|
|
// A program to post files to gist.github.com. Use the following to install it:
|
|
|
|
// deno install -f --allow-env --allow-read --allow-net=api.github.com https://deno.land/std/examples/gist.ts
|
2020-04-28 12:40:43 -04:00
|
|
|
import { parse } from "../flags/mod.ts";
|
2018-12-19 13:50:48 -05:00
|
|
|
|
|
|
|
function pathBase(p: string): string {
|
|
|
|
const parts = p.split("/");
|
|
|
|
return parts[parts.length - 1];
|
|
|
|
}
|
|
|
|
|
2020-04-29 14:48:19 -04:00
|
|
|
const token = Deno.env.get("GIST_TOKEN");
|
2019-10-28 15:58:35 -04:00
|
|
|
if (!token) {
|
|
|
|
console.error("GIST_TOKEN environmental variable not set.");
|
|
|
|
console.error("Get a token here: https://github.com/settings/tokens");
|
|
|
|
Deno.exit(1);
|
|
|
|
}
|
2018-12-19 13:50:48 -05:00
|
|
|
|
2020-01-09 13:37:01 -05:00
|
|
|
const parsedArgs = parse(Deno.args);
|
2018-12-19 13:50:48 -05:00
|
|
|
|
2019-10-28 15:58:35 -04:00
|
|
|
if (parsedArgs._.length === 0) {
|
|
|
|
console.error(
|
|
|
|
"Usage: gist.ts --allow-env --allow-net [-t|--title Example] some_file " +
|
|
|
|
"[next_file]"
|
|
|
|
);
|
|
|
|
Deno.exit(1);
|
|
|
|
}
|
2018-12-19 13:50:48 -05:00
|
|
|
|
2020-02-19 15:36:18 -05:00
|
|
|
const files: Record<string, { content: string }> = {};
|
2019-10-28 15:58:35 -04:00
|
|
|
for (const filename of parsedArgs._) {
|
2020-03-08 08:09:22 -04:00
|
|
|
const base = pathBase(filename as string);
|
|
|
|
const content = await Deno.readFile(filename as string);
|
2019-10-28 15:58:35 -04:00
|
|
|
const contentStr = new TextDecoder().decode(content);
|
|
|
|
files[base] = { content: contentStr };
|
2018-12-19 13:50:48 -05:00
|
|
|
}
|
|
|
|
|
2019-10-28 15:58:35 -04:00
|
|
|
const content = {
|
|
|
|
description: parsedArgs.title || parsedArgs.t || "Example",
|
|
|
|
public: false,
|
2020-03-28 13:03:49 -04:00
|
|
|
files: files,
|
2019-10-28 15:58:35 -04:00
|
|
|
};
|
|
|
|
const body = JSON.stringify(content);
|
|
|
|
|
|
|
|
const res = await fetch("https://api.github.com/gists", {
|
|
|
|
method: "POST",
|
|
|
|
headers: [
|
|
|
|
["Content-Type", "application/json"],
|
|
|
|
["User-Agent", "Deno-Gist"],
|
2020-03-28 13:03:49 -04:00
|
|
|
["Authorization", `token ${token}`],
|
2019-10-28 15:58:35 -04:00
|
|
|
],
|
2020-03-28 13:03:49 -04:00
|
|
|
body,
|
2019-10-28 15:58:35 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
if (res.ok) {
|
|
|
|
const resObj = await res.json();
|
|
|
|
console.log("Success");
|
|
|
|
console.log(resObj["html_url"]);
|
|
|
|
} else {
|
|
|
|
const err = await res.text();
|
|
|
|
console.error("Failure to POST", err);
|
|
|
|
}
|