forked from NoeFabris/opencode-antigravity-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.ts
More file actions
140 lines (120 loc) · 3.83 KB
/
Copy pathcli.ts
File metadata and controls
140 lines (120 loc) · 3.83 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
import { createInterface } from "node:readline/promises";
import { stdin as input, stdout as output } from "node:process";
import {
showAuthMenu,
showAccountDetails,
isTTY,
type AccountInfo,
type AccountStatus,
} from "./ui/auth-menu";
export async function promptProjectId(): Promise<string> {
const rl = createInterface({ input, output });
try {
const answer = await rl.question("Project ID (leave blank to use your default project): ");
return answer.trim();
} finally {
rl.close();
}
}
export async function promptAddAnotherAccount(currentCount: number): Promise<boolean> {
const rl = createInterface({ input, output });
try {
const answer = await rl.question(`Add another account? (${currentCount} added) (y/n): `);
const normalized = answer.trim().toLowerCase();
return normalized === "y" || normalized === "yes";
} finally {
rl.close();
}
}
export type LoginMode = "add" | "fresh" | "manage" | "check" | "cancel";
export interface ExistingAccountInfo {
email?: string;
index: number;
addedAt?: number;
lastUsed?: number;
status?: AccountStatus;
isCurrentAccount?: boolean;
enabled?: boolean;
}
export interface LoginMenuResult {
mode: LoginMode;
deleteAccountIndex?: number;
refreshAccountIndex?: number;
toggleAccountIndex?: number;
deleteAll?: boolean;
}
async function promptLoginModeFallback(existingAccounts: ExistingAccountInfo[]): Promise<LoginMenuResult> {
const rl = createInterface({ input, output });
try {
console.log(`\n${existingAccounts.length} account(s) saved:`);
for (const acc of existingAccounts) {
const label = acc.email || `Account ${acc.index + 1}`;
console.log(` ${acc.index + 1}. ${label}`);
}
console.log("");
while (true) {
const answer = await rl.question("(a)dd new, (f)resh start, (m)anage, (c)heck quotas? [a/f/m/c]: ");
const normalized = answer.trim().toLowerCase();
if (normalized === "a" || normalized === "add") {
return { mode: "add" };
}
if (normalized === "f" || normalized === "fresh") {
return { mode: "fresh" };
}
if (normalized === "m" || normalized === "manage") {
return { mode: "manage" };
}
if (normalized === "c" || normalized === "check") {
return { mode: "check" };
}
console.log("Please enter 'a', 'f', 'm', or 'c'.");
}
} finally {
rl.close();
}
}
export async function promptLoginMode(existingAccounts: ExistingAccountInfo[]): Promise<LoginMenuResult> {
if (!isTTY()) {
return promptLoginModeFallback(existingAccounts);
}
const accounts: AccountInfo[] = existingAccounts.map(acc => ({
email: acc.email,
index: acc.index,
addedAt: acc.addedAt,
lastUsed: acc.lastUsed,
status: acc.status,
isCurrentAccount: acc.isCurrentAccount,
enabled: acc.enabled,
}));
console.log("");
while (true) {
const action = await showAuthMenu(accounts);
switch (action.type) {
case "add":
return { mode: "add" };
case "check":
return { mode: "check" };
case "manage":
return { mode: "manage" };
case "select-account": {
const accountAction = await showAccountDetails(action.account);
if (accountAction === "delete") {
return { mode: "add", deleteAccountIndex: action.account.index };
}
if (accountAction === "refresh") {
return { mode: "add", refreshAccountIndex: action.account.index };
}
if (accountAction === "toggle") {
return { mode: "manage", toggleAccountIndex: action.account.index };
}
continue;
}
case "delete-all":
return { mode: "fresh", deleteAll: true };
case "cancel":
return { mode: "cancel" };
}
}
}
export { isTTY } from "./ui/auth-menu";
export type { AccountStatus } from "./ui/auth-menu";