Skip to content

Commit 553b794

Browse files
committed
chore(scripts): add create actor script
1 parent 222dae8 commit 553b794

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

scripts/api/actors/create.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/usr/bin/env tsx
2+
3+
import * as readline from "readline/promises";
4+
5+
const rl = readline.createInterface({
6+
input: process.stdin,
7+
output: process.stdout,
8+
});
9+
10+
const rivetToken =
11+
process.env.RIVET_TOKEN ||
12+
(await rl.question("Rivet Token (default: dev): ")).trim() ||
13+
"dev";
14+
15+
const endpoint =
16+
process.env.RIVET_ENDPOINT ||
17+
(await rl.question("Rivet Endpoint (default: http://localhost:6420): ")) ||
18+
"http://localhost:6420";
19+
20+
const namespace =
21+
process.env.RIVET_NAMESPACE ||
22+
(await rl.question("Namespace (default: default): ")) || "default";
23+
24+
const name = (await rl.question("Actor Name: ")).trim();
25+
if (!name) {
26+
console.error("Error: Actor Name is required");
27+
process.exit(1);
28+
}
29+
30+
const runnerNameSelector =
31+
(await rl.question("Runner Name Selector (default: default): ")).trim() ||
32+
"default";
33+
34+
const crashPolicy =
35+
(await rl.question("Crash Policy (default: restart): ")).trim() || "restart";
36+
37+
const datacenter = (await rl.question("Datacenter (optional): ")).trim() || undefined;
38+
39+
const key = (await rl.question("Key (optional): ")).trim() || undefined;
40+
41+
const input = (await rl.question("Input JSON (optional): ")).trim() || undefined;
42+
43+
rl.close();
44+
45+
const body = {
46+
name,
47+
runner_name_selector: runnerNameSelector,
48+
crash_policy: crashPolicy,
49+
...(datacenter && { datacenter }),
50+
...(key && { key }),
51+
...(input && { input }),
52+
};
53+
54+
const response = await fetch(
55+
`${endpoint}/actors?namespace=${namespace}`,
56+
{
57+
method: "POST",
58+
headers: {
59+
Authorization: `Bearer ${rivetToken}`,
60+
"Content-Type": "application/json",
61+
},
62+
body: JSON.stringify(body),
63+
},
64+
);
65+
66+
if (!response.ok) {
67+
console.error(`Error: ${response.status} ${response.statusText}`);
68+
console.error(await response.text());
69+
process.exit(1);
70+
}
71+
72+
const data = await response.json();
73+
74+
console.log(JSON.stringify(data, null, 2));

0 commit comments

Comments
 (0)