summaryrefslogtreecommitdiff
path: root/std/examples/gist.ts
diff options
context:
space:
mode:
authorRyan Dahl <ry@tinyclouds.org>2019-10-09 17:10:09 -0400
committerRyan Dahl <ry@tinyclouds.org>2019-10-09 17:10:09 -0400
commit151ce0266eb4de2c8fc600c81c192a5f791b6169 (patch)
tree7cb04016a1c7ee88adde83814548d7a9409dcde3 /std/examples/gist.ts
parenta355f7c807686918734416d91b79c26c21effba9 (diff)
Move everything into std subdir
Diffstat (limited to 'std/examples/gist.ts')
-rwxr-xr-xstd/examples/gist.ts65
1 files changed, 65 insertions, 0 deletions
diff --git a/std/examples/gist.ts b/std/examples/gist.ts
new file mode 100755
index 000000000..890d85099
--- /dev/null
+++ b/std/examples/gist.ts
@@ -0,0 +1,65 @@
+#!/usr/bin/env -S deno --allow-net --allow-env
+// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
+
+const { args, env, exit, readFile } = Deno;
+import { parse } from "https://deno.land/std/flags/mod.ts";
+
+function pathBase(p: string): string {
+ const parts = p.split("/");
+ return parts[parts.length - 1];
+}
+
+async function main(): Promise<void> {
+ const token = env()["GIST_TOKEN"];
+ if (!token) {
+ console.error("GIST_TOKEN environmental variable not set.");
+ console.error("Get a token here: https://github.com/settings/tokens");
+ exit(1);
+ }
+
+ const parsedArgs = parse(args.slice(1));
+
+ if (parsedArgs._.length === 0) {
+ console.error(
+ "Usage: gist.ts --allow-env --allow-net [-t|--title Example] some_file " +
+ "[next_file]"
+ );
+ exit(1);
+ }
+
+ const files = {};
+ for (const filename of parsedArgs._) {
+ const base = pathBase(filename);
+ const content = await readFile(filename);
+ const contentStr = new TextDecoder().decode(content);
+ files[base] = { content: contentStr };
+ }
+
+ const content = {
+ description: parsedArgs.title || parsedArgs.t || "Example",
+ public: false,
+ files: files
+ };
+ 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"],
+ ["Authorization", `token ${token}`]
+ ],
+ body
+ });
+
+ 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);
+ }
+}
+
+main();