Skip to content

Commit 84cef05

Browse files
committed
feat(cli): add metrics status and reset commands
- Add --status flag to display metrics summary and exit - Add --metrics-reset flag to reset metrics with confirmation - Implement promptConfirmation() for safe destructive operations - Add metricsReset to CliOptions interface - Update help text with examples for new commands - Show current metrics before reset to prevent accidental data loss Signed-off-by: leocavalcante <leo@cavalcante.dev>
1 parent 10fface commit 84cef05

File tree

1 file changed

+57
-1
lines changed

1 file changed

+57
-1
lines changed

src/cli.ts

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
*/
44

55
import { resolve } from "node:path"
6+
import { createInterface } from "node:readline"
67
import { Command } from "commander"
78
import { loadConfig } from "./config.ts"
89
import { initializePaths } from "./fs.ts"
910
import { runLoop } from "./loop.ts"
10-
import { formatMetricsSummary, loadMetrics } from "./metrics.ts"
11+
import { formatMetricsSummary, loadMetrics, resetMetrics, saveMetrics } from "./metrics.ts"
1112
import type { CliOptions } from "./types.ts"
1213

1314
const VERSION = "1.0.0"
@@ -43,6 +44,7 @@ function createProgram(): Command {
4344
.option("--no-auto-push", "Disable automatic push after cycles")
4445
.option("-s, --signoff", "Add Signed-off-by line to commits")
4546
.option("--status", "Display metrics summary and exit")
47+
.option("--metrics-reset", "Reset metrics to default values (requires confirmation)")
4648

4749
// Add examples to help
4850
program.addHelpText(
@@ -73,6 +75,9 @@ Examples:
7375
$ opencoder --status -p ./myproject
7476
Display metrics for a specific project
7577
78+
$ opencoder --metrics-reset
79+
Reset metrics to default values (with confirmation)
80+
7681
Options:
7782
-p, --project <dir> Project directory (default: current directory)
7883
-m, --model <model> Model for both plan and build
@@ -83,6 +88,7 @@ Options:
8388
--no-auto-push Disable automatic push after cycles
8489
-s, --signoff Add Signed-off-by line to commits
8590
--status Display metrics summary and exit
91+
--metrics-reset Reset metrics to default values (requires confirmation)
8692
8793
Environment variables:
8894
OPENCODER_PLAN_MODEL Default plan model
@@ -108,6 +114,26 @@ Config file (.opencode/opencoder/config.json):
108114
return program
109115
}
110116

117+
/**
118+
* Prompt user for confirmation
119+
* @param message - The confirmation message to display
120+
* @returns Promise that resolves to true if user confirms, false otherwise
121+
*/
122+
async function promptConfirmation(message: string): Promise<boolean> {
123+
const rl = createInterface({
124+
input: process.stdin,
125+
output: process.stdout,
126+
})
127+
128+
return new Promise((resolve) => {
129+
rl.question(`${message} (yes/no): `, (answer) => {
130+
rl.close()
131+
const normalized = answer.trim().toLowerCase()
132+
resolve(normalized === "yes" || normalized === "y")
133+
})
134+
})
135+
}
136+
111137
/**
112138
* Parse CLI arguments without executing the action.
113139
* Useful for testing or when you need to inspect arguments before running.
@@ -145,6 +171,7 @@ export function parseCli(argv: string[] = process.argv): ParsedCli {
145171
autoPush: opts.autoPush as boolean | undefined,
146172
commitSignoff: opts.signoff as boolean | undefined,
147173
status: opts.status as boolean | undefined,
174+
metricsReset: opts.metricsReset as boolean | undefined,
148175
},
149176
hint: args[0],
150177
}
@@ -182,6 +209,7 @@ export async function run(): Promise<void> {
182209
autoPush: opts.autoPush as boolean | undefined,
183210
commitSignoff: opts.signoff as boolean | undefined,
184211
status: opts.status as boolean | undefined,
212+
metricsReset: opts.metricsReset as boolean | undefined,
185213
}
186214

187215
// Handle --status flag: display metrics and exit
@@ -198,6 +226,34 @@ export async function run(): Promise<void> {
198226
return
199227
}
200228

229+
// Handle --metrics-reset flag: reset metrics with confirmation
230+
if (cliOptions.metricsReset) {
231+
const projectDir = cliOptions.project ? resolve(cliOptions.project) : process.cwd()
232+
const paths = initializePaths(projectDir)
233+
234+
// Show current metrics before reset
235+
const currentMetrics = await loadMetrics(paths.metricsFile)
236+
console.log("\nCurrent Metrics:")
237+
console.log("================\n")
238+
console.log(formatMetricsSummary(currentMetrics))
239+
console.log("")
240+
241+
// Ask for confirmation
242+
const confirmed = await promptConfirmation(
243+
"Are you sure you want to reset all metrics to default values?",
244+
)
245+
246+
if (confirmed) {
247+
const freshMetrics = resetMetrics()
248+
await saveMetrics(paths.metricsFile, freshMetrics)
249+
console.log("\n✓ Metrics have been reset to default values.\n")
250+
} else {
251+
console.log("\nMetrics reset cancelled.\n")
252+
}
253+
254+
return
255+
}
256+
201257
const config = await loadConfig(cliOptions, hint)
202258
await runLoop(config)
203259
} catch (err) {

0 commit comments

Comments
 (0)