Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions pm-stack/opencode-usage-export/opencode-usage-export.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -362,12 +362,21 @@ function parsePathFlag(flag, raw, args) {

function parseArgs(argv) {
const args = { db: null, outDir: null, windowHours: DEFAULT_WINDOW_HOURS, freshHours: DEFAULT_FRESH_HOURS };
const seen = new Set();
for (let i = 0; i < argv.length; i += 1) {
const a = argv[i];
if (a === "--db") args.db = parsePathFlag("--db", argv[++i], args);
else if (a === "--out-dir") args.outDir = parsePathFlag("--out-dir", argv[++i], args);
else if (a === "--window-hours") args.windowHours = parseHourFlag("--window-hours", argv[++i], args);
else if (a === "--fresh-hours") args.freshHours = parseHourFlag("--fresh-hours", argv[++i], args);
if (["--db", "--out-dir", "--window-hours", "--fresh-hours"].includes(a)) {
if (seen.has(a)) {
const err = new ExportError("invalid-argument", 2, `opencode usage export: duplicate option ${a}`);
err.suppressFailureDocument = true;
throw err;
}
seen.add(a);
if (a === "--db") args.db = parsePathFlag(a, argv[++i], args);
else if (a === "--out-dir") args.outDir = parsePathFlag(a, argv[++i], args);
else if (a === "--window-hours") args.windowHours = parseHourFlag(a, argv[++i], args);
else args.freshHours = parseHourFlag(a, argv[++i], args);
}
else throw new Error(`unknown argument: ${a}`);
}
return args;
Expand All @@ -394,10 +403,12 @@ export async function run({
? err
: new ExportError("invalid-argument", 2, `opencode usage export: ${err?.message ?? err}`);
const doc = failureDocument(e.status, e.message, attemptedAt);
try {
await writeDocument(err?.args?.outDir ?? join(home, "atlas/opencode-usage-export"), doc);
} catch {
/* nowhere to write the failure either; exit code carries it */
if (!e.suppressFailureDocument) {
try {
await writeDocument(err?.args?.outDir ?? join(home, "atlas/opencode-usage-export"), doc);
} catch {
/* nowhere to write the failure either; exit code carries it */
}
}
return { exitCode: e.exitCode, doc };
}
Expand Down
21 changes: 21 additions & 0 deletions pm-stack/opencode-usage-export/opencode-usage-export.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,27 @@ test("trailing --out-dir: invalid-argument failure document, exit 2 (#6934)", as
assert.equal(latest.status, "invalid-argument");
});

test("duplicate scalar value options fail before probing or writing", async () => {
const { home } = await fakeHome();
const first = join(home, "first-output");
const second = join(home, "second-output");
const cases = [
["--db", "first.db", "--db", "second.db"],
["--out-dir", first, "--out-dir", second],
["--window-hours", "6", "--window-hours", "12"],
["--fresh-hours", "6", "--fresh-hours", "12"],
];
for (const argv of cases) {
const { exitCode, doc } = await run({ home, now: NOW, argv });
assert.equal(exitCode, 2);
assert.equal(doc.status, "invalid-argument");
assert.match(doc.detail, new RegExp(`duplicate option ${argv[0]}`));
}
assert.equal(fs.existsSync(first), false);
assert.equal(fs.existsSync(second), false);
assert.equal(fs.existsSync(join(home, "atlas/opencode-usage-export")), false);
});

test("valid --db / --out-dir behave as before (#6934 control)", async () => {
const { home, outDir } = await fakeHome();
const dbPath = join(home, "explicit.db");
Expand Down
Loading