diff --git a/packages/cli/src/commands/config.ts b/packages/cli/src/commands/config.ts new file mode 100644 index 0000000..ed1cb70 --- /dev/null +++ b/packages/cli/src/commands/config.ts @@ -0,0 +1,28 @@ +import { Command } from "commander"; +import { loadConfig, saveConfig } from "../config.js"; + +export function registerConfigCommand(program: Command) { + const cmd = program.command("config").description("Configure API endpoint and API Key"); + + cmd + .command("set ") + .description("Set a config value (api-url or api-key)") + .action((key: string, value: string) => { + try { + saveConfig(key, value); + console.error(`Set ${key} successfully.`); + } catch (e: any) { + console.error(e.message); + process.exit(1); + } + }); + + cmd + .command("list") + .description("Show current configuration") + .action(() => { + const config = loadConfig(); + console.log(`api-url: ${config.apiUrl || "(not set)"}`); + console.log(`api-key: ${config.apiKey ? config.apiKey.slice(0, 6) + "..." : "(not set)"}`); + }); +} diff --git a/packages/cli/src/config.ts b/packages/cli/src/config.ts new file mode 100644 index 0000000..3d5ce21 --- /dev/null +++ b/packages/cli/src/config.ts @@ -0,0 +1,56 @@ +import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs"; +import { homedir } from "os"; +import { join } from "path"; + +export interface CliConfig { + apiUrl: string; + apiKey: string; +} + +const CONFIG_DIR = join(homedir(), ".moemail"); +const CONFIG_FILE = join(CONFIG_DIR, "config.json"); + +export function loadConfig(): CliConfig { + const config: CliConfig = { apiUrl: "", apiKey: "" }; + + // File config + if (existsSync(CONFIG_FILE)) { + try { + const raw = JSON.parse(readFileSync(CONFIG_FILE, "utf-8")); + if (raw.apiUrl) config.apiUrl = raw.apiUrl; + if (raw.apiKey) config.apiKey = raw.apiKey; + } catch {} + } + + // Env overrides (higher priority) + if (process.env.MOEMAIL_API_URL) config.apiUrl = process.env.MOEMAIL_API_URL; + if (process.env.MOEMAIL_API_KEY) config.apiKey = process.env.MOEMAIL_API_KEY; + + return config; +} + +export function saveConfig(key: string, value: string): void { + if (!existsSync(CONFIG_DIR)) { + mkdirSync(CONFIG_DIR, { recursive: true }); + } + + let config: Record = {}; + if (existsSync(CONFIG_FILE)) { + try { + config = JSON.parse(readFileSync(CONFIG_FILE, "utf-8")); + } catch {} + } + + const keyMap: Record = { + "api-url": "apiUrl", + "api-key": "apiKey", + }; + + const configKey = keyMap[key]; + if (!configKey) { + throw new Error(`Unknown config key: ${key}. Valid keys: api-url, api-key`); + } + + config[configKey] = value; + writeFileSync(CONFIG_FILE, JSON.stringify(config, null, 2)); +} diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index d8f23d6..6c960e8 100644 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -1,5 +1,6 @@ #!/usr/bin/env node import { Command } from "commander"; +import { registerConfigCommand } from "./commands/config.js"; const program = new Command(); @@ -9,4 +10,6 @@ program .version("0.1.0") .option("--json", "output as JSON"); +registerConfigCommand(program); + program.parse();