deno.land / std@0.158.0 / examples / gist.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env -S deno run --allow-net --allow-env// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
/** * A program to post files to gist.github.com. * * Use the following to install it: * * ```shellsession * $ deno install -f --allow-env --allow-read --allow-net=api.github.com https://deno.land/std/examples/gist.ts * ``` * * @module */
import { parse } from "../flags/mod.ts";
function pathBase(p: string): string { const parts = p.split("/"); return parts[parts.length - 1];}
const token = Deno.env.get("GIST_TOKEN");if (!token) { console.error("GIST_TOKEN environmental variable not set."); console.error("Get a token here: https://github.com/settings/tokens"); Deno.exit(1);}
const parsedArgs = parse(Deno.args);
if (parsedArgs._.length === 0) { console.error( "Usage: gist.ts --allow-env --allow-net [-t|--title Example] some_file " + "[next_file]", ); Deno.exit(1);}
const files: Record<string, { content: string }> = {};for (const filename of parsedArgs._) { const base = pathBase(filename as string); const content = await Deno.readFile(filename as string); 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);}
std
Deno standard library
GitHub Repositorydenoland/deno_std
GitHub Stars
2336

Version Info

Tagged at
3 days ago