mirror of
https://github.com/beilunyang/moemail.git
synced 2026-09-04 23:06:37 +08:00
feat(cli): add config module and config command
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -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 <key> <value>")
|
||||||
|
.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)"}`);
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -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<string, string> = {};
|
||||||
|
if (existsSync(CONFIG_FILE)) {
|
||||||
|
try {
|
||||||
|
config = JSON.parse(readFileSync(CONFIG_FILE, "utf-8"));
|
||||||
|
} catch {}
|
||||||
|
}
|
||||||
|
|
||||||
|
const keyMap: Record<string, string> = {
|
||||||
|
"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));
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
import { Command } from "commander";
|
import { Command } from "commander";
|
||||||
|
import { registerConfigCommand } from "./commands/config.js";
|
||||||
|
|
||||||
const program = new Command();
|
const program = new Command();
|
||||||
|
|
||||||
@@ -9,4 +10,6 @@ program
|
|||||||
.version("0.1.0")
|
.version("0.1.0")
|
||||||
.option("--json", "output as JSON");
|
.option("--json", "output as JSON");
|
||||||
|
|
||||||
|
registerConfigCommand(program);
|
||||||
|
|
||||||
program.parse();
|
program.parse();
|
||||||
|
|||||||
Reference in New Issue
Block a user