-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.ts
More file actions
134 lines (121 loc) · 4.01 KB
/
cli.ts
File metadata and controls
134 lines (121 loc) · 4.01 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
/*
202402*pike
Fairpost cli handler
*/
import fs from "fs";
import os from "os";
import path from "path";
import { spawnSync } from "child_process";
import "./bootstrap.ts";
import Fairpost from "./services/Fairpost.ts";
import { JSONReplacer, parsePayload } from "./utilities.ts";
import { PlatformId } from "./platforms/index.ts";
import { SourceStage, PostStatus } from "./types/index.ts";
import Operator from "./models/Operator.ts";
import User from "./models/User.ts";
// arguments
const USER = process.argv[2]?.includes("@")
? process.argv[2].replace("@", "")
: "";
const COMMAND = process.argv[2]?.includes("@")
? (process.argv[3] ?? "help")
: (process.argv[2] ?? "help");
// options
const DRY_RUN = !!getOption("dry-run");
const OPERATOR = (getOption("operator") as string) ?? "admin";
const PASSWORD = (getOption("password") as string) ?? undefined;
const MODEL = (getOption("model") as string) ?? undefined;
const PLATFORMS =
((getOption("platforms") as string)?.split(",") as PlatformId[]) ?? undefined;
const SOURCES = (getOption("sources") as string)?.split(",") ?? undefined;
const DATE = (getOption("date") as string) ?? undefined;
const STATUS = (getOption("status") as PostStatus) ?? undefined;
const STAGE = (getOption("stage") as SourceStage) ?? undefined;
let PLATFORM = (getOption("platform") as string as PlatformId) ?? undefined;
let SOURCE = (getOption("source") as string) ?? undefined;
const POST = (getOption("post") as string) ?? undefined;
if (POST) {
[SOURCE, PLATFORM] = POST.split(":") as [string, PlatformId];
}
// payload
const chunks: Buffer[] = [];
if (!process.stdin.isTTY) {
for await (const chunk of process.stdin) {
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
}
}
let PAYLOAD = chunks.length
? await parsePayload(Buffer.concat(chunks))
: undefined;
// utilities
function getOption(key: string): boolean | string | null {
if (process.argv.includes(`--${key}`)) return true;
const value = process.argv.find((element) => element.startsWith(`--${key}=`));
if (!value) return null;
return value.replace(`--${key}=`, "");
}
async function editPayload(getCommand: string, putCommand: string) {
const tmpFile = path.join(os.tmpdir(), "fairpost.tmp");
fs.writeFileSync(tmpFile, await execute(getCommand), "utf8");
const edit = spawnSync(`${process.env.EDITOR || "nano"} "${tmpFile}"`, {
stdio: "inherit",
shell: true,
});
if (edit.error) {
console.error("Failed to launch editor:", edit.error);
process.exit(1);
}
if (edit.status !== 0) {
console.warn(
`Editor exited with code ${edit.status} — assuming user cancelled.`,
);
process.exit(1);
}
PAYLOAD = await parsePayload(fs.readFileSync(tmpFile));
return await execute(putCommand);
}
// main
async function execute(command: string): Promise<string> {
const operator = new Operator(OPERATOR, ["admin"], "cli", true);
const user =
USER && COMMAND !== "create-user" ? await User.getUser(USER) : undefined;
try {
const output = await Fairpost.execute(operator, user, command, {
dryrun: DRY_RUN,
user: USER,
password: PASSWORD,
model: MODEL,
platforms: PLATFORMS,
platform: PLATFORM,
sources: SOURCES,
source: SOURCE,
date: DATE ? new Date(DATE) : undefined,
status: STATUS,
stage: STAGE,
payload: PAYLOAD,
});
return JSON.stringify(output, JSONReplacer, "\t");
} catch (e) {
console.error((e as Error).message ?? e);
throw e;
}
}
switch (COMMAND) {
case "edit-platform":
console.info(await editPayload("get-platform", "put-platform"));
break;
case "edit-user":
console.info(await editPayload("get-user", "put-user"));
break;
case "edit-feed":
console.info(await editPayload("get-feed", "put-feed"));
break;
case "edit-source":
console.info(await editPayload("get-source", "put-source"));
break;
case "edit-post":
console.info(await editPayload("get-post", "put-post"));
break;
default:
console.info(await execute(COMMAND));
}