-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
executable file
·167 lines (151 loc) · 4.56 KB
/
index.js
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
#!/usr/bin/env node
const { prompt } = require("enquirer");
const program = require("commander");
const fetch = require("node-fetch");
const BASE_URL = "https://api.cloudflare.com/client/v4/accounts";
const NAMESPACES_API = "storage/kv/namespaces";
const cache = {};
function getHeaders(email, authKey) {
return {
"X-Auth-Email": email,
"X-Auth-Key": authKey,
"Content-Type": "application/json"
};
}
async function getNamespaces({ accountId, authEmail, authKey }) {
if (cache.namespaces) {
return cache.namespaces;
}
const url = `${BASE_URL}/${accountId}/${NAMESPACES_API}`;
const resp = await fetch(url, { headers: getHeaders(authEmail, authKey) });
if (resp.status != 200) {
throw `Failed to get namespaces\n${await resp.text()}`;
}
const namespaces = await resp.json();
cache["namespaces"] = namespaces.result;
return namespaces.result;
}
async function getKeys(
{ accountId, authEmail, authKey },
namespace,
cursor = ""
) {
if (cache[namespace] && !cursor) {
return cache[namespace];
}
let url = `${BASE_URL}/${accountId}/${NAMESPACES_API}/${namespace}/keys`;
if (cursor) {
url += `?cursor=${cursor}`;
}
const resp = await fetch(url, { headers: getHeaders(authEmail, authKey) });
if (resp.status != 200) {
throw `Failed to get keys\n${await resp.text()}`;
}
let keys = await resp.json();
// check to see if there are more than 1000 keys
if (keys.result_info && keys.result_info.cursor) {
keys.result = keys.result.concat(
await getKeys(
{ accountId, authEmail, authKey },
namespace,
keys.result_info.cursor
)
);
if (!cursor) {
cache[namespace] = keys.result;
}
}
return keys.result;
}
async function getKey({ accountId, authEmail, authKey }, namespace, key) {
const url = `${BASE_URL}/${accountId}/${NAMESPACES_API}/${namespace}/values/${key}`;
const resp = await fetch(url, { headers: getHeaders(authEmail, authKey) });
if (resp.status != 200) {
throw `Failed to get key\n${await resp.text()}`;
}
const body = await resp.json();
return body;
}
async function pickNamespace(namespaces) {
const question = {
type: "autocomplete",
name: "namespace",
message: "Namespace?",
limit: 10,
suggest(input, choices) {
return choices.filter(choice => choice.message.includes(input));
},
choices: namespaces.map(ns => ns.title)
};
const resp = await prompt(question);
const chosenNS = namespaces.filter(ns => ns.title === resp.namespace);
return chosenNS ? chosenNS[0] : null;
}
async function pickKey(keys) {
const question = {
type: "autocomplete",
name: "key",
message: "Key?",
limit: 10,
suggest(input, choices) {
return choices.filter(choice => choice.message.includes(input));
},
choices: keys.map(key => key.name)
};
const resp = await prompt(question);
return resp.key;
}
async function run(accountVars, program) {
const namespaces = await getNamespaces(accountVars);
let ns;
if (program.namespace) {
ns = namespaces.filter(ns => ns.title === program.namespace);
ns = ns ? ns[0] : null;
}
if (!ns) {
ns = await pickNamespace(namespaces);
}
let key;
if (program.key) {
key = program.key;
} else {
const keys = await getKeys(accountVars, ns.id);
key = await pickKey(keys);
}
const val = await getKey(accountVars, ns.id, key);
console.log(JSON.stringify(val, undefined, 2));
}
async function main() {
program
.option("--account-id <id>", "Cloudflare Account ID")
.option("--account-email <email>", "Cloudflare Auth Email")
.option("--account-key <key>", "Cloudflare Auth Key")
.option("-n, --namespace <ns>", "Namespace")
.option("-k, --key <key>", "Key to get")
.option("-l, --loop", "Keep prompting for new values")
.parse(process.argv);
const accountId = program.account_id || process.env.CLOUDFLARE_ACCOUNT_ID;
if (!accountId) {
console.log("CLOUDFLARE_ACCOUNT_ID is required!");
process.exit(1);
}
const authEmail = program.accountEmail || process.env.CLOUDFLARE_AUTH_EMAIL;
if (!authEmail) {
console.log("CLOUDFLARE_AUTH_EMAIL is required!");
process.exit(1);
}
const authKey = program.accountKey || process.env.CLOUDFLARE_AUTH_KEY;
if (!authKey) {
console.log("CLOUDFLARE_AUTH_KEY is required!");
process.exit(1);
}
const accountVars = { accountId, authEmail, authKey };
if (program.loop && !(program.key && program.namespace)) {
while (true) {
await run(accountVars, program);
}
} else {
await run(accountVars, program);
}
}
main().catch(e => console.log(e));