Skip to content

Commit dc48a3a

Browse files
committed
recognize --key in help
1 parent 43ae49c commit dc48a3a

2 files changed

Lines changed: 48 additions & 56 deletions

File tree

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ async function main(): Promise<void> {
3535
if (parsed.category) {
3636
showCategoryHelp(parsed.category);
3737
} else {
38-
await showGlobalHelp();
38+
await showGlobalHelp(parsed.globalFlags.key);
3939
}
4040
return;
4141
}

src/router.ts

Lines changed: 47 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -166,30 +166,56 @@ export function showVersion(): void {
166166
console.log(`${COMMAND_NAME} ${PACKAGE_VERSION}`);
167167
}
168168

169-
interface KeyInfo {
170-
status: "env" | "keymanager" | "inactive" | "none";
171-
name?: string | undefined;
172-
endpoint?: string | undefined;
173-
}
174-
175-
async function getKeyInfo(): Promise<KeyInfo> {
176-
if (process.env.ITERABLE_API_KEY) return { status: "env" };
177-
if (isTestEnv()) return { status: "none" };
169+
async function getKeyStatusLine(keyOverride?: string): Promise<string> {
170+
if (process.env.ITERABLE_API_KEY && !keyOverride) {
171+
return theme.muted(
172+
" Using API key from ITERABLE_API_KEY environment variable"
173+
);
174+
}
175+
if (isTestEnv()) {
176+
return (
177+
theme.key(" No API key configured.") +
178+
theme.muted(
179+
` Run ${theme.accent(`${COMMAND_NAME} keys add`)} to add one.`
180+
)
181+
);
182+
}
178183
try {
179184
const { getKeyManager } = await import("./key-manager.js");
180185
const km = getKeyManager();
181-
await km.initialize();
182-
if (!(await km.hasKeys())) return { status: "none" };
183-
if (!(await km.hasActiveKey())) return { status: "inactive" };
184-
const meta = await km.getActiveKeyMetadata();
185-
return {
186-
status: "keymanager",
187-
name: meta?.name,
188-
endpoint: meta?.baseUrl.replace("https://", ""),
189-
};
186+
const keys = await km.listKeys();
187+
188+
const meta = keyOverride
189+
? keys.find((k) => k.name === keyOverride || k.id === keyOverride)
190+
: keys.find((k) => k.isActive);
191+
192+
if (meta) {
193+
const endpoint = meta.baseUrl.replace("https://", "");
194+
return theme.muted(` Using key "${meta.name}" (${endpoint})`);
195+
}
196+
if (keyOverride) {
197+
return (
198+
theme.key(` Key "${keyOverride}" not found.`) +
199+
theme.muted(
200+
` Run ${theme.accent(`${COMMAND_NAME} keys list`)} to see available keys.`
201+
)
202+
);
203+
}
204+
if (keys.length > 0) {
205+
return (
206+
theme.key(" No active API key.") +
207+
theme.muted(
208+
` Run ${theme.accent(`${COMMAND_NAME} keys activate <name>`)} to activate one.`
209+
)
210+
);
211+
}
190212
} catch {
191-
return { status: "none" };
213+
// Fall through to "none"
192214
}
215+
return (
216+
theme.key(" No API key configured.") +
217+
theme.muted(` Run ${theme.accent(`${COMMAND_NAME} keys add`)} to add one.`)
218+
);
193219
}
194220

195221
function flagLabel(def: FlagDef): string {
@@ -289,51 +315,17 @@ function getGlobalOptionsLines(): string[] {
289315
return [theme.bold("OPTIONS"), ...formatOptionLines(options)];
290316
}
291317

292-
export async function showGlobalHelp(): Promise<void> {
318+
export async function showGlobalHelp(keyOverride?: string): Promise<void> {
293319
const version = PACKAGE_VERSION;
294320
const categories = getCategories();
295-
const keyInfo = await getKeyInfo();
296321

297322
const lines = [
298323
"",
299324
theme.bold(`${COMMAND_NAME} v${version}`) +
300325
" - Command-line interface for the Iterable API",
326+
await getKeyStatusLine(keyOverride),
301327
];
302328

303-
switch (keyInfo.status) {
304-
case "env":
305-
lines.push(
306-
theme.muted(
307-
" Using API key from ITERABLE_API_KEY environment variable"
308-
)
309-
);
310-
break;
311-
case "keymanager":
312-
lines.push(
313-
theme.muted(
314-
` Using key "${keyInfo.name}"` +
315-
(keyInfo.endpoint ? ` (${keyInfo.endpoint})` : "")
316-
)
317-
);
318-
break;
319-
case "inactive":
320-
lines.push(
321-
theme.key(" No active API key.") +
322-
theme.muted(
323-
` Run ${theme.accent(`${COMMAND_NAME} keys activate <name>`)} to activate one.`
324-
)
325-
);
326-
break;
327-
case "none":
328-
lines.push(
329-
theme.key(" No API key configured.") +
330-
theme.muted(
331-
` Run ${theme.accent(`${COMMAND_NAME} keys add`)} to add one.`
332-
)
333-
);
334-
break;
335-
}
336-
337329
lines.push(
338330
"",
339331
theme.bold("USAGE"),

0 commit comments

Comments
 (0)