-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgolinkctl.ts
More file actions
178 lines (157 loc) · 4.8 KB
/
golinkctl.ts
File metadata and controls
178 lines (157 loc) · 4.8 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env -S deno run -A
import { Command } from "@cliffy/command";
import { exit } from "jsr:@cliffy/internal@1.0.0-rc.7/runtime/exit";
// types go here
const headers: Record<string, string> = {
"Sec-Golink": "1",
"Authorization": ""
}
type GoLinkData = {
Short: string,
Long: string,
Created: string,
LastEdit: string,
Owner: string,
Clicks?: number
}
type GlobalCliOptions = {
// globals
apiKey?: string | undefined,
url: string | "http://go",
}
type CliExportCommandOpts = GlobalCliOptions & {
file?: string
}
// utility function go here
function generateSlug(length: number) {
let result = "";
const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
const charactersLength = characters.length;
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
async function getBaseGolink(url: string) {
if (url == "http://go") {
const chaos = await fetch(url)
return chaos.url
} else {
return url
}
}
// the main entrypoint
const command = await new Command()
.name("golinkctl")
.env(
"URL=<url:string>",
"base URL of the golink server, used if you manage another tailnet's golink server using shared machines or building your own over the internet",
{ global: true, required: false, prefix: "GOLINK_", value: () => "http://go" }
)
.env(
"API_KEY=<apiKey:string>",
"Golink API key, used by custom implementations of golink server (see https://github.com/andreijiroh-dev/golinks for context)",
{ global: true, required: false, prefix: "GOLINK_", value: () => "" }
)
.description("manage your golinks for https://github.com/tailscale-dev/golink compat API servers")
.globalOption("-u, --url [url:string]", "the base URL of your golink server", {
default: "http://go"
})
.globalOption("-k, --api-key [apiKey:string]", "API key for some custom golink server implementation")
.action(() => {
console.log("I don't usually run without subcommands! See help for hints")
exit(1)
})
command.command("set", "create a new golink (or update a existing one)")
.alias("new").alias("update")
.arguments("<target:string [golink:string]")
.action(async(options: GlobalCliOptions, ...args) => {
const long = args[0] as string
const short = args[1] as string || generateSlug(8)
if (options.apiKey) {
headers.Authorization = `bearer ${options.apiKey}`
}
headers["Content-Type"] = "application/x-www-form-urlencoded"
try {
const data = await fetch(await getBaseGolink(`${options.url}`), {
method: "POST",
body: new URLSearchParams({
long,
short
}),
headers,
redirect: "follow"
})
if (data.ok == true) {
const json: GoLinkData = await data.json()
const log =`\
Short: ${json.Short} (${await getBaseGolink(options.url)}${json.Short})
Long: ${json.Long}
Created on: ${json.Created}
Owner: ${json.Owner}
Last edited: ${json.LastEdit}`
console.log(log)
}
} catch (error) {
console.error(error)
exit(1)
}
})
// info
command.command("info", "show details about a golink")
.alias("show")
.arguments("<golink:string>")
// @ts-ignore: I know the risks of being not typed here
.action(async (options: GlobalCliOptions, args) => {
if (options.apiKey) {
headers.Authorization = `bearer ${options.apiKey}`
}
try {
const data = await fetch(`${options.url}/${args}+`, {
headers,
redirect: "follow"
})
if (data.status === 404) {
console.error(`error: golink ${args} does not exist`)
Deno.exit(1)
}
const json: GoLinkData = await data.json()
// print the hell out
const log = `\
Short: ${json.Short} (${await getBaseGolink(options.url)}${json.Short})
Long: ${json.Long}
Created on: ${json.Created}
Owner: ${json.Owner}
Last edited: ${json.LastEdit}
Clicks/opens: ${json.Clicks || 0}`
console.log(log)
} catch (error) {
console.error(error)
exit(1)
}
})
// export
command.command("export", "export your golinks in JSON Lines format")
.option("-f, --file <file:file>", "path to output file for exports instead of via stdout")
.action(async (options: CliExportCommandOpts) => {
if (options.apiKey) {
headers.Authorization = `bearer ${options.apiKey}`
}
try {
const data = await fetch(`${options.url}/.export`, {
headers,
redirect: "follow"
})
const text = await data.text()
if (options.file == null) {
console.log(text)
} else {
await Deno.writeTextFile(options.file, text)
console.log("Successfully written to "+options.file)
}
} catch (error) {
console.error(error)
exit(1)
}
})
command.parse(Deno.args)