Files
openclaw/src/cli/profile.ts
Dallin Romney e8db9c3bc0 test(qa): add qa run --profile and unified output summary/evidence (#91587)
* test(qa): add mapped qa run profiles

* test(qa): document mapped profile runner

* test(qa): validate run profiles from mapping

* test(qa): preserve root profile parsing

* test(qa): simplify taxonomy profile dispatch

* test(qa): align tool coverage CLI expectation

* test(qa): fix profile dispatch fixture type

* test(qa): share profile runner option types

* test(qa): split shared cli runner options

* test(qa): unify profile suite artifacts

* fix(qa): filter profile scenarios by provider lane

* test(qa): drop native scenario subreports

* fix(qa): keep native log refs repo-relative

* fix(cli): preserve qa run root profile parsing

* fix(qa): avoid qa profile flag collision

* fix(qa): reject profile flags without qa profile
2026-06-14 18:08:42 -07:00

111 lines
3.6 KiB
TypeScript

// Root --profile/--dev parsing and environment projection for profile-specific state.
import os from "node:os";
import path from "node:path";
import {
normalizeLowercaseStringOrEmpty,
normalizeOptionalString,
} from "@openclaw/normalization-core/string-coerce";
import { resolveRequiredHomeDir } from "../infra/home-dir.js";
import { resolveCliArgvInvocation } from "./argv-invocation.js";
import { isValidProfileName } from "./profile-utils.js";
import { scanCliRootOptions } from "./root-option-scan.js";
import { takeCliRootOptionValue } from "./root-option-value.js";
type CliProfileParseResult =
| { ok: true; profile: string | null; argv: string[] }
| { ok: false; error: string };
export function parseCliProfileArgs(argv: string[]): CliProfileParseResult {
// Root profile flags are stripped before Commander sees argv, except command-local cases.
let profile: string | null = null;
let sawDev = false;
const scanned = scanCliRootOptions(argv, ({ arg, args, index, out }) => {
if (arg === "--dev") {
if (resolveCliArgvInvocation(out).primary === "gateway") {
out.push(arg);
return { kind: "handled" };
}
if (profile && profile !== "dev") {
return { kind: "error", error: "Cannot combine --dev with --profile" };
}
sawDev = true;
profile = "dev";
return { kind: "handled" };
}
if (arg === "--profile" || arg.startsWith("--profile=")) {
const next = args[index + 1];
const { value, consumedNext } = takeCliRootOptionValue(arg, next);
const [primary, secondary] = resolveCliArgvInvocation(out).commandPath;
if (primary === "qa" && secondary === "matrix") {
out.push(arg);
if (consumedNext) {
out.push(next);
}
return { kind: "handled", consumedNext };
}
if (sawDev) {
return { kind: "error", error: "Cannot combine --dev with --profile" };
}
if (!value) {
return { kind: "error", error: "--profile requires a value" };
}
if (!isValidProfileName(value)) {
return {
kind: "error",
error: 'Invalid --profile (use letters, numbers, "_", "-" only)',
};
}
profile = value;
return { kind: "handled", consumedNext };
}
return { kind: "pass" };
});
if (!scanned.ok) {
return scanned;
}
return { ok: true, profile, argv: scanned.argv };
}
function resolveProfileStateDir(
profile: string,
env: Record<string, string | undefined>,
homedir: () => string,
): string {
const suffix = normalizeLowercaseStringOrEmpty(profile) === "default" ? "" : `-${profile}`;
return path.join(resolveRequiredHomeDir(env as NodeJS.ProcessEnv, homedir), `.openclaw${suffix}`);
}
export function applyCliProfileEnv(params: {
profile: string;
env?: Record<string, string | undefined>;
homedir?: () => string;
}) {
const env = params.env ?? (process.env as Record<string, string | undefined>);
const homedir = params.homedir ?? os.homedir;
const profile = params.profile.trim();
if (!profile) {
return;
}
// Convenience only: fill defaults, never override explicit env values.
env.OPENCLAW_PROFILE = profile;
const existingStateDir = normalizeOptionalString(env.OPENCLAW_STATE_DIR);
const stateDir = existingStateDir || resolveProfileStateDir(profile, env, homedir);
if (!existingStateDir) {
env.OPENCLAW_STATE_DIR = stateDir;
}
if (!normalizeOptionalString(env.OPENCLAW_CONFIG_PATH)) {
env.OPENCLAW_CONFIG_PATH = path.join(stateDir, "openclaw.json");
}
if (profile === "dev" && !env.OPENCLAW_GATEWAY_PORT?.trim()) {
env.OPENCLAW_GATEWAY_PORT = "19001";
}
}