CLI: dedupe config validate errors and expose allowed values

This commit is contained in:
Gustavo Madeira Santana
2026-03-02 20:05:12 -05:00
parent a44843507f
commit f26853f14c
41 changed files with 1393 additions and 134 deletions

View File

@@ -0,0 +1,32 @@
import { describe, expect, it } from "vitest";
import { __test__ } from "./logger.js";
describe("shouldSkipLoadConfigFallback", () => {
it("matches config validate invocations", () => {
expect(__test__.shouldSkipLoadConfigFallback(["node", "openclaw", "config", "validate"])).toBe(
true,
);
});
it("handles root flags before config validate", () => {
expect(
__test__.shouldSkipLoadConfigFallback([
"node",
"openclaw",
"--profile",
"work",
"--no-color",
"config",
"validate",
"--json",
]),
).toBe(true);
});
it("does not match other commands", () => {
expect(
__test__.shouldSkipLoadConfigFallback(["node", "openclaw", "config", "get", "foo"]),
).toBe(false);
expect(__test__.shouldSkipLoadConfigFallback(["node", "openclaw", "status"])).toBe(false);
});
});

View File

@@ -1,6 +1,7 @@
import fs from "node:fs";
import path from "node:path";
import { Logger as TsLogger } from "tslog";
import { getCommandPathWithRootOptions } from "../cli/argv.js";
import type { OpenClawConfig } from "../config/types.js";
import { resolvePreferredOpenClawTmpDir } from "../infra/tmp-openclaw-dir.js";
import { readLoggingConfig } from "./config.js";
@@ -42,6 +43,11 @@ export type LogTransport = (logObj: LogTransportRecord) => void;
const externalTransports = new Set<LogTransport>();
function shouldSkipLoadConfigFallback(argv: string[] = process.argv): boolean {
const [primary, secondary] = getCommandPathWithRootOptions(argv, 2);
return primary === "config" && secondary === "validate";
}
function attachExternalTransport(logger: TsLogger<LogObj>, transport: LogTransport): void {
logger.attachTransport((logObj: LogObj) => {
if (!externalTransports.has(transport)) {
@@ -78,7 +84,7 @@ function resolveSettings(): ResolvedSettings {
let cfg: OpenClawConfig["logging"] | undefined =
(loggingState.overrideSettings as LoggerSettings | null) ?? readLoggingConfig();
if (!cfg) {
if (!cfg && !shouldSkipLoadConfigFallback()) {
try {
const loaded = requireConfig?.("../config/config.js") as
| {
@@ -289,6 +295,10 @@ export function registerLogTransport(transport: LogTransport): () => void {
};
}
export const __test__ = {
shouldSkipLoadConfigFallback,
};
function formatLocalDate(date: Date): string {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, "0");