OmniRoute/bin/cli/contexts.mjs
diegosouzapw ed19c824c0 feat(cli): fase 8.4 — profiles/contexts (omniroute config contexts)
- contexts.mjs: loadContexts/saveContexts/resolveActiveContext (~/.omniroute/config.json)
- commands/contexts.mjs: CRUD completo (add/use/list/show/remove/rename/export/import)
- config.mjs: subgroup contexts registrado sob config contexts
- program.mjs: flag global --context <name> com env OMNIROUTE_CONTEXT
- en.json/pt-BR.json: chaves program.context e config.contexts adicionadas
- import usa validação explícita de campos (sem Object.assign cru)
2026-05-15 03:36:51 -03:00

43 lines
1.2 KiB
JavaScript

import { existsSync, readFileSync, writeFileSync, mkdirSync, chmodSync } from "node:fs";
import { join, dirname } from "node:path";
import { resolveDataDir } from "./data-dir.mjs";
const CONFIG_VERSION = 1;
export function configPath() {
return join(resolveDataDir(), "config.json");
}
function defaultConfig() {
return {
version: CONFIG_VERSION,
currentContext: "default",
contexts: {
default: { baseUrl: `http://localhost:${process.env.PORT || "20128"}`, apiKey: null },
},
};
}
export function loadContexts() {
try {
if (!existsSync(configPath())) return defaultConfig();
return JSON.parse(readFileSync(configPath(), "utf8"));
} catch {
return defaultConfig();
}
}
export function saveContexts(cfg) {
const path = configPath();
mkdirSync(dirname(path), { recursive: true });
writeFileSync(path, JSON.stringify(cfg, null, 2));
try {
chmodSync(path, 0o600);
} catch {}
}
export function resolveActiveContext(overrideName) {
const cfg = loadContexts();
const name = overrideName || cfg.currentContext || "default";
return cfg.contexts?.[name] || cfg.contexts?.default || { baseUrl: "http://localhost:20128" };
}