Swadhin Biswas feat(cli): expand command coverage and config support · 3/6/2026
7e372c1
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/**
* CLI Configuration
* Store and retrieve CLI settings
*/
import Conf from "conf";
import { execFileSync } from "node:child_process";
export interface OchConfig {
serverUrl: string;
token?: string;
username?: string;
defaultBranch?: string;
editor?: string;
pager?: string;
caFile?: string;
insecure?: boolean;
windowsEncryptedTokens?: Record<string, string>;
}
export function normalizeServerUrl(rawUrl: string): string {
const trimmed = rawUrl.trim();
if (!trimmed) return "";
let parsed: URL;
try {
parsed = new URL(trimmed);
} catch {
throw new Error("Invalid server URL. Use a full URL like https://git.example.com");
}
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
throw new Error("Invalid server URL protocol. Only http:// or https:// are supported");
}
parsed.hash = "";
parsed.search = "";
parsed.pathname = parsed.pathname.replace(/\/+$/, "");
return parsed.toString().replace(/\/$/, "");
}
const config = new Conf<OchConfig>({
projectName: "opencodehub-cli",
defaults: {
serverUrl: "",
defaultBranch: "main",
editor: "",
pager: "",
caFile: "",
insecure: false,
},
});
function sanitizeAccountName(input: string) {
return input.replace(/[^a-zA-Z0-9._:@/-]/g, "_");
}
function keychainAccount(serverUrl?: string) {
return sanitizeAccountName(serverUrl?.trim() || "default");
}
function hasCommand(command: string) {
if (process.env.OCH_DISABLE_KEYCHAIN === "1") {
return false;
}
try {
if (process.platform === "win32") {
execFileSync("where", [command], { stdio: "ignore", timeout: 500 });
} else {
execFileSync("sh", ["-lc", `command -v ${command} >/dev/null 2>&1`], {
stdio: "ignore",
timeout: 500,
});
}
return true;
} catch {
return false;
}
}
function getWindowsEncryptedTokens(): Record<string, string> {
return (config.get("windowsEncryptedTokens") || {}) as Record<string, string>;
}
function encryptWindowsToken(token: string): string | null {
if (!hasCommand("powershell")) return null;
try {
return execFileSync(
"powershell",
[
"-NoProfile",
"-NonInteractive",
"-Command",
"$ErrorActionPreference='Stop';$plain=[Console]::In.ReadToEnd();$sec=ConvertTo-SecureString $plain -AsPlainText -Force;$enc=$sec | ConvertFrom-SecureString;[Console]::Out.Write($enc)",
],
{
input: token,
encoding: "utf8",
stdio: ["pipe", "pipe", "ignore"],
timeout: 2000,
},
).trim();
} catch {
return null;
}
}
function decryptWindowsToken(encryptedValue: string): string | null {
if (!hasCommand("powershell")) return null;
try {
return execFileSync(
"powershell",
[
"-NoProfile",
"-NonInteractive",
"-Command",
"$ErrorActionPreference='Stop';$enc=[Console]::In.ReadToEnd();$sec=ConvertTo-SecureString $enc;$bstr=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($sec);try{[System.Runtime.InteropServices.Marshal]::PtrToStringBSTR($bstr)}finally{[System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($bstr)}",
],
{
input: encryptedValue,
encoding: "utf8",
stdio: ["pipe", "pipe", "ignore"],
timeout: 2000,
},
).trim();
} catch {
return null;
}
}
function getTokenFromKeychain(serverUrl?: string): string | null {
const account = keychainAccount(serverUrl);
try {
if (process.platform === "win32") {
const encrypted = getWindowsEncryptedTokens()[account];
if (!encrypted) return null;
return decryptWindowsToken(encrypted);
}
if (process.platform === "darwin" && hasCommand("security")) {
return execFileSync(
"security",
[
"find-generic-password",
"-s",
"opencodehub-cli",
"-a",
account,
"-w",
],
{ encoding: "utf8", stdio: ["ignore", "pipe", "ignore"], timeout: 1000 },
).trim();
}
if (process.platform === "linux" && hasCommand("secret-tool")) {
const value = execFileSync(
"secret-tool",
["lookup", "service", "opencodehub-cli", "account", account],
{ encoding: "utf8", stdio: ["ignore", "pipe", "ignore"], timeout: 1000 },
).trim();
return value || null;
}
} catch {
return null;
}
return null;
}
function saveTokenToKeychain(serverUrl: string | undefined, token: string): boolean {
const account = keychainAccount(serverUrl);
try {
if (process.platform === "win32") {
const encrypted = encryptWindowsToken(token);
if (!encrypted) return false;
const current = getWindowsEncryptedTokens();
current[account] = encrypted;
config.set("windowsEncryptedTokens", current);
return true;
}
if (process.platform === "darwin" && hasCommand("security")) {
execFileSync(
"security",
[
"add-generic-password",
"-U",
"-s",
"opencodehub-cli",
"-a",
account,
"-w",
token,
],
{ stdio: "ignore", timeout: 1000 },
);
return true;
}
if (process.platform === "linux" && hasCommand("secret-tool")) {
execFileSync(
"secret-tool",
[
"store",
"--label=OpenCodeHub CLI Token",
"service",
"opencodehub-cli",
"account",
account,
],
{ input: token, stdio: ["pipe", "ignore", "ignore"], timeout: 1000 },
);
return true;
}
} catch {
return false;
}
return false;
}
function removeTokenFromKeychain(serverUrl?: string): void {
const account = keychainAccount(serverUrl);
try {
if (process.platform === "win32") {
const current = getWindowsEncryptedTokens();
if (current[account]) {
delete current[account];
config.set("windowsEncryptedTokens", current);
}
return;
}
if (process.platform === "darwin" && hasCommand("security")) {
execFileSync(
"security",
["delete-generic-password", "-s", "opencodehub-cli", "-a", account],
{ stdio: "ignore", timeout: 1000 },
);
return;
}
if (process.platform === "linux" && hasCommand("secret-tool")) {
execFileSync(
"secret-tool",
["clear", "service", "opencodehub-cli", "account", account],
{ stdio: "ignore", timeout: 1000 },
);
}
} catch {
// No-op: if key is absent or provider is unavailable we still clear fallback storage.
}
}
function getToken(serverUrl?: string): string {
const envToken = process.env.OCH_TOKEN?.trim();
if (envToken) {
return envToken;
}
const keychainToken = getTokenFromKeychain(serverUrl);
if (keychainToken) {
return keychainToken;
}
return config.get("token") || "";
}
export function getConfig(): OchConfig {
return {
serverUrl: config.get("serverUrl") || "",
token: getToken(config.get("serverUrl")),
username: config.get("username"),
defaultBranch: config.get("defaultBranch") || "main",
editor: config.get("editor") || "",
pager: config.get("pager") || "",
caFile: config.get("caFile") || "",
insecure: config.get("insecure") ?? false,
};
}
export function saveConfig(updates: Partial<OchConfig>): void {
if (updates.serverUrl !== undefined) {
config.set("serverUrl", normalizeServerUrl(updates.serverUrl));
}
if (updates.token !== undefined) {
const token = updates.token.trim();
if (!token) {
removeTokenFromKeychain(updates.serverUrl || config.get("serverUrl"));
config.delete("token");
} else {
const storedInKeychain = saveTokenToKeychain(
updates.serverUrl || config.get("serverUrl"),
token,
);
if (storedInKeychain) {
config.delete("token");
} else {
config.set("token", token);
}
}
}
if (updates.username !== undefined) {
config.set("username", updates.username);
}
if (updates.defaultBranch !== undefined) {
config.set("defaultBranch", updates.defaultBranch);
}
if (updates.editor !== undefined) {
config.set("editor", updates.editor);
}
if (updates.pager !== undefined) {
config.set("pager", updates.pager);
}
if (updates.caFile !== undefined) {
config.set("caFile", updates.caFile);
}
if (updates.insecure !== undefined) {
config.set("insecure", updates.insecure);
}
}
export function clearConfig(): void {
removeTokenFromKeychain(config.get("serverUrl"));
config.clear();
}
export function unsetConfigKey(
key: "serverUrl" | "token" | "username" | "defaultBranch" | "editor" | "pager" | "caFile" | "insecure",
): void {
if (key === "token") {
removeTokenFromKeychain(config.get("serverUrl"));
}
config.delete(key);
}
export function getConfigPath(): string {
return config.path;
}
export function getTokenStorageMode(serverUrl?: string): "env" | "keychain" | "file" | "none" {
if (process.env.OCH_TOKEN?.trim()) return "env";
if (getTokenFromKeychain(serverUrl || config.get("serverUrl"))) return "keychain";
if (config.get("token")) return "file";
return "none";
}
export function supportsSecureTokenStorage(): boolean {
if (process.platform === "win32") return hasCommand("powershell");
if (process.platform === "darwin") return hasCommand("security");
if (process.platform === "linux") return hasCommand("secret-tool");
return false;
}