mirror of
https://github.com/denoland/deno.git
synced 2025-07-29 16:13:54 +00:00
Add examples/ directory (denoland/deno_std#28)
Previously https://github.com/denoland/deno_examples
Original: 14be9a0e82
This commit is contained in:
parent
ecde6a4c50
commit
3542f2de0c
6 changed files with 122 additions and 0 deletions
63
examples/gist.ts
Executable file
63
examples/gist.ts
Executable file
|
@ -0,0 +1,63 @@
|
|||
#!/usr/bin/env deno --allow-net --allow-env
|
||||
|
||||
import { args, env, exit, readFile } from "deno";
|
||||
import parseArgs from "https://deno.land/x/parseargs/index.ts";
|
||||
|
||||
function pathBase(p: string): string {
|
||||
const parts = p.split("/");
|
||||
return parts[parts.length - 1];
|
||||
}
|
||||
|
||||
async function main() {
|
||||
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 = parseArgs(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) {
|
||||
let resObj = await res.json();
|
||||
console.log("Success");
|
||||
console.log(resObj["html_url"]);
|
||||
} else {
|
||||
let err = await res.text();
|
||||
console.error("Failure to POST", err);
|
||||
}
|
||||
}
|
||||
|
||||
main();
|
Loading…
Add table
Add a link
Reference in a new issue