diff options
author | Ryan Dahl <ry@tinyclouds.org> | 2019-10-10 05:31:23 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-10-10 05:31:23 -0400 |
commit | e7562eed8c816cd0d97aab6b818d7c8453dbaa2b (patch) | |
tree | c5a9f536e79d2c8d2d02897511a9138acaf35394 /std/examples/gist.ts | |
parent | 3882c9d19a641e0c919f1350d87c6d7ee280cf78 (diff) | |
parent | 93f7f00c956c14620ef031626f124b57397ca867 (diff) |
Merge deno_std in main repo (#3091)
The history of deno_std is persevered but rewritten to update links to issues and PRs
Fixes denoland/deno_std#603
Diffstat (limited to 'std/examples/gist.ts')
m--------- | std | 0 | ||||
-rwxr-xr-x | std/examples/gist.ts | 65 |
2 files changed, 65 insertions, 0 deletions
diff --git a/std b/std deleted file mode 160000 -Subproject 43aafbf33285753e7b42230f0eb7969b300f71c 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(); |