-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.ts
More file actions
144 lines (129 loc) · 3.69 KB
/
Copy pathauth.ts
File metadata and controls
144 lines (129 loc) · 3.69 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
import fsp from "node:fs/promises";
import path from "node:path";
import { stdin as input, stdout as output } from "node:process";
import readline from "node:readline/promises";
import {
DEFAULT_SERVER_ADDR,
getConfigPath,
loadConfigFile,
normalizeConfig,
} from "@/lib/config";
import { printStatusMessage } from "@/lib/output";
import { Command } from "@commander-js/extra-typings";
async function pathExists(filePath: string) {
try {
await fsp.access(filePath);
return true;
} catch {
return false;
}
}
async function loadExistingConfig(opts: AuthInitOptions) {
try {
return loadConfigFile();
} catch (error) {
if (opts.force) {
return {};
}
throw error;
}
}
async function promptForValue(
rl: readline.Interface,
prompt: string,
existing?: string,
) {
if (!input.isTTY) {
return existing;
}
const suffix = existing ? ` [${existing}]` : "";
const value = (await rl.question(`${prompt}${suffix}: `)).trim();
return value || existing;
}
interface AuthInitOptions {
serverAddr?: string;
apiKey?: string;
force?: boolean;
}
export const authCmd = new Command()
.name("auth")
.description("authentication commands");
authCmd
.command("init")
.description("setup CLI authentication config")
.option("--server-addr <addr>", "the address of the server to connect to")
.option("--api-key <key>", "the API key to interact with the API")
.option("-f, --force", "overwrite an existing config without confirmation")
.action(async (rawOpts, command) => {
const rootOpts = command.parent?.parent?.opts() as AuthInitOptions;
const opts = {
serverAddr: rootOpts.serverAddr,
apiKey: rootOpts.apiKey,
force: Boolean(rawOpts.force),
};
const configPath = getConfigPath();
try {
const existingConfig = await loadExistingConfig(opts);
const existingAuth = normalizeConfig(existingConfig);
if ((await pathExists(configPath)) && !opts.force) {
if (!input.isTTY) {
throw new Error(
`Config file already exists at ${configPath}. Re-run with --force to overwrite it.`,
);
}
const rl = readline.createInterface({ input, output });
const answer = (
await rl.question(
`Config file already exists at ${configPath}. Update it? (yes/no): `,
)
)
.trim()
.toLowerCase();
rl.close();
if (answer !== "y" && answer !== "yes") {
printStatusMessage(false, "Auth init aborted by user");
return;
}
}
const rl = readline.createInterface({ input, output });
const serverAddr =
opts.serverAddr ??
(await promptForValue(
rl,
"Marka server address",
existingAuth.serverAddr ?? DEFAULT_SERVER_ADDR,
));
const apiKey =
opts.apiKey ??
(await promptForValue(rl, "Marka API key", existingAuth.apiKey));
rl.close();
if (!serverAddr || !apiKey) {
throw new Error("Both server address and API key are required");
}
await fsp.mkdir(path.dirname(configPath), { recursive: true });
await fsp.writeFile(
configPath,
JSON.stringify(
{
...existingConfig,
serverAddr,
apiKey,
},
null,
2,
) + "\n",
{
encoding: "utf-8",
mode: 0o600,
},
);
await fsp.chmod(configPath, 0o600);
printStatusMessage(true, `Wrote auth config to ${configPath}`);
} catch (error) {
printStatusMessage(
false,
error instanceof Error ? error.message : `${error}`,
);
process.exitCode = 1;
}
});