mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-10 17:24:58 +00:00
CLI: dedupe config validate errors and expose allowed values
This commit is contained in:
32
src/logging/logger.settings.test.ts
Normal file
32
src/logging/logger.settings.test.ts
Normal 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);
|
||||
});
|
||||
});
|
||||
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user