Files
OmniRoute/bin/cli
diegosouzapw 69a2b27a33 fix(cli): code-review — C1/C2/C3/I1/I3/I4/I5/m1/m2/m4
C1: sanitize opts.name and backupId against path traversal (replace /\\ with _)
C2: read backup files locally as base64 instead of sending local path to cloud API
C3: implement markOAuthDone/markOAuthFailed via module-level callbacks registered
    in useEffect — TUI now transitions to DONE/FAILED when polling signals completion
I1: fix matchesGlob — equality-only for non-glob patterns (removes false-positive
    startsWith), and support multi-wildcard patterns (e.g. **.json) by iterating parts
I3: replace two hardcoded English strings in tunnel.mjs with t() calls; add
    tunnel.notAvailable and tunnel.noTunnels to en.json and pt-BR.json
I4: log HTTP 4xx errors in runKeysAddCommand and return 1 instead of falling
    through silently to DB write on client errors
I5: truncate err.message to 100 chars in ProvidersTestAll.jsx and test-provider.mjs
m1: fix EvalWatch StatusBadge — completed state now uses status="ok" instead of "running"
m2: replace hardcoded ~/.omniroute/backup-schedule.json with resolveDataDir()-based path
m4: remove ...opts spread from all apiFetch calls in keys.mjs — pass only explicit options
    (method, body, retry, acceptNotOk) to avoid leaking apiKey/baseUrl/yes/etc.
2026-05-15 09:48:30 -03:00
..
2026-05-10 00:55:06 -03:00
2026-05-10 00:55:06 -03:00
2026-05-10 00:55:06 -03:00
2026-05-10 00:55:06 -03:00
2026-05-10 00:55:06 -03:00
2026-05-10 00:55:06 -03:00

bin/cli — OmniRoute CLI internals

This directory contains the CLI runtime, helpers, and commands for the omniroute binary.

Structure

bin/cli/
├── CONVENTIONS.md          ← normative design rules (read this first)
├── README.md               ← this file
├── index.mjs               ← central command router (will migrate to Commander in 1.1)
├── args.mjs                ← legacy arg parser (replaced by Commander in 1.1)
├── api.mjs                 ← apiFetch() — all HTTP calls + retry/backoff
├── runtime.mjs             ← withRuntime() — server-first / DB-fallback
├── i18n.mjs                ← t() — i18n helper + locale detection
├── output.mjs              ← emit() — table/json/jsonl/csv + printSuccess/printError
├── io.mjs                  ← ask() / askSecret() — interactive prompts
├── data-dir.mjs            ← resolveDataDir() / resolveStoragePath()
├── sqlite.mjs              ← openOmniRouteDb() — DB bootstrap
├── encryption.mjs          ← encrypt/decrypt credentials
├── provider-catalog.mjs    ← static provider catalog
├── provider-store.mjs      ← DB CRUD for provider_connections
├── provider-test.mjs       ← testProviderApiKey()
├── settings-store.mjs      ← DB CRUD for key_value settings
├── locales/
│   ├── en.json             ← English strings
│   └── pt-BR.json          ← Portuguese (Brazil) strings
└── commands/
    ├── setup.mjs
    ├── doctor.mjs
    ├── providers.mjs
    ├── config.mjs
    ├── status.mjs
    ├── logs.mjs
    └── update.mjs

Key helpers

apiFetch(path, opts)api.mjs

All HTTP calls to the OmniRoute server must go through this wrapper.

import { apiFetch } from "./api.mjs";

const res = await apiFetch("/api/health");
if (!res.ok) await res.assertOk(); // throws ApiError with mapped exit code
const data = await res.json();

Options:

  • baseUrl — override base URL (default: OMNIROUTE_BASE_URL env or localhost:20128)
  • apiKey — override API key (default: OMNIROUTE_API_KEY)
  • method, body, headers — standard fetch options
  • timeout — per-attempt ms (default: 30000)
  • retryfalse to disable (default: enabled)
  • retryMax — total attempts (default: 3)
  • verbose — log retry attempts to stderr

withRuntime(fn, opts)runtime.mjs

Provides server-first / DB-fallback transparently.

import { withRuntime } from "./runtime.mjs";

await withRuntime(async (ctx) => {
  if (ctx.kind === "http") {
    const res = await ctx.api("/v1/providers");
    return res.json();
  }
  return ctx.db.prepare("SELECT * FROM provider_connections").all();
});
  • opts.requireServer = true — throws ServerOfflineError (exit 3) if offline
  • opts.preferDb = true — always use DB (skip server check)

t(key, vars)i18n.mjs

Internationalized strings. Catalog loaded from locales/{locale}.json.

import { t } from "./i18n.mjs";

console.log(t("common.serverOffline"));
console.log(t("setup.testFailed", { error: err.message }));

Locale detection order: OMNIROUTE_LANGLC_ALLLC_MESSAGESLANGen.

emit(data, opts)output.mjs

Format-aware output. Reads opts.output to select table/json/jsonl/csv.

import { emit, printError, EXIT_CODES } from "./output.mjs";

emit(providers, { output: opts.output ?? "table" });
printError("Something went wrong");
process.exit(EXIT_CODES.SERVER_OFFLINE);

Adding a new command

  1. Create bin/cli/commands/your-command.mjs
  2. Export runYourCommand(argv, context) (pre-1.1) or registerYourCommand(program) (post-1.1)
  3. Register in bin/cli/index.mjs (pre-1.1) or bin/cli/program.mjs (post-1.1)
  4. Add strings to both locales/en.json and locales/pt-BR.json
  5. Write test in tests/unit/cli-your-command.test.ts

See CONVENTIONS.md for exit codes, flag naming, output format, and destructive-action rules.