@@ -4,7 +4,7 @@ import { TaskTemplateService } from '@core/services/task-template.service';
44import { DatabaseMaintenanceService } from '@core/services/database-maintenance.service' ;
55import { closeDb } from '@core/db' ;
66import { parseDuration } from '@core/duration' ;
7- import type { TaskStatus , ScheduleType } from '@core/db/schema' ;
7+ import type { ScheduleType } from '@core/db/schema' ;
88import {
99 getPackageVersion ,
1010 restartGatewayAfterMaintenance ,
@@ -15,6 +15,11 @@ import {
1515 renderDatabaseResult ,
1616 type GatewayMaintenanceReport ,
1717} from './database-output' ;
18+ import {
19+ parseBoundedInteger ,
20+ parsePositiveInteger ,
21+ parseTaskStatus ,
22+ } from './validation' ;
1823
1924async function withDb < T > (
2025 fn : ( ) => Promise < T > ,
@@ -113,12 +118,12 @@ program
113118 prompt : options . prompt ,
114119 model : options . model ,
115120 category : options . category ,
116- importance : parseInt ( options . importance ) ,
117- urgency : parseInt ( options . urgency ) ,
121+ importance : parseBoundedInteger ( options . importance , 'importance' , 1 , 5 ) ,
122+ urgency : parseBoundedInteger ( options . urgency , 'urgency' , 1 , 5 ) ,
118123 batchId : options . batch ,
119- dependsOn : options . depends ? parseInt ( options . depends ) : undefined ,
124+ dependsOn : options . depends ? parsePositiveInteger ( options . depends , 'depends' ) : undefined ,
120125 cwd : submitCwd ,
121- maxRetries : parseInt ( options . maxRetries ) ,
126+ maxRetries : parseBoundedInteger ( options . maxRetries , 'max-retries' , 0 , 1000 ) ,
122127 retryBackoffMs,
123128 timeoutMs,
124129 } ) ;
@@ -152,7 +157,7 @@ program
152157 . description ( '开始执行任务(标记为 running)' )
153158 . requiredOption ( '--id <id>' , '任务 ID' )
154159 . action ( async ( options ) => withDb ( async ( ) => {
155- const task = await TaskService . start ( parseInt ( options . id ) , { cwd : process . cwd ( ) } ) ;
160+ const task = await TaskService . start ( parsePositiveInteger ( options . id , 'id' ) , { cwd : process . cwd ( ) } ) ;
156161 if ( task ) {
157162 console . log ( JSON . stringify ( { id : task . id , status : task . status } ) ) ;
158163 } else {
@@ -167,7 +172,7 @@ program
167172 . requiredOption ( '--id <id>' , '任务 ID' )
168173 . option ( '-l, --log <log>' , '结果日志' )
169174 . action ( async ( options ) => withDb ( async ( ) => {
170- const task = await TaskService . done ( parseInt ( options . id ) , options . log , { cwd : process . cwd ( ) } ) ;
175+ const task = await TaskService . done ( parsePositiveInteger ( options . id , 'id' ) , options . log , { cwd : process . cwd ( ) } ) ;
171176 if ( task ) {
172177 console . log ( JSON . stringify ( { id : task . id , status : task . status } ) ) ;
173178 } else {
@@ -182,7 +187,7 @@ program
182187 . requiredOption ( '--id <id>' , '任务 ID' )
183188 . option ( '-l, --log <log>' , '错误日志' )
184189 . action ( async ( options ) => withDb ( async ( ) => {
185- const task = await TaskService . fail ( parseInt ( options . id ) , options . log , { cwd : process . cwd ( ) } ) ;
190+ const task = await TaskService . fail ( parsePositiveInteger ( options . id , 'id' ) , options . log , { cwd : process . cwd ( ) } ) ;
186191 if ( task ) {
187192 console . log ( JSON . stringify ( {
188193 id : task . id ,
@@ -200,7 +205,7 @@ program
200205 . description ( '取消任务' )
201206 . requiredOption ( '--id <id>' , '任务 ID' )
202207 . action ( async ( options ) => withDb ( async ( ) => {
203- const task = await TaskService . cancel ( parseInt ( options . id ) , { cwd : process . cwd ( ) } ) ;
208+ const task = await TaskService . cancel ( parsePositiveInteger ( options . id , 'id' ) , { cwd : process . cwd ( ) } ) ;
204209 if ( task ) {
205210 console . log ( JSON . stringify ( { id : task . id , status : task . status } ) ) ;
206211 } else {
@@ -216,7 +221,7 @@ program
216221 . option ( '-b, --batch <batchId>' , '批次 ID(批量重试)' )
217222 . action ( async ( options ) => withDb ( async ( ) => {
218223 if ( options . id ) {
219- const task = await TaskService . retry ( parseInt ( options . id ) , { cwd : process . cwd ( ) } ) ;
224+ const task = await TaskService . retry ( parsePositiveInteger ( options . id , 'id' ) , { cwd : process . cwd ( ) } ) ;
220225 if ( task ) {
221226 console . log ( JSON . stringify ( { id : task . id , status : task . status } ) ) ;
222227 } else {
@@ -250,11 +255,11 @@ program
250255 . option ( '-l, --limit <number>' , '限制数量' , '20' )
251256 . action ( async ( options ) => withDb ( async ( ) => {
252257 const tasks = await TaskService . list ( {
253- status : options . status as TaskStatus ,
258+ status : parseTaskStatus ( options . status ) ,
254259 batchId : options . batch ,
255260 category : options . category ,
256261 cwd : process . cwd ( ) ,
257- limit : parseInt ( options . limit ) ,
262+ limit : parsePositiveInteger ( options . limit , 'limit' ) ,
258263 } ) ;
259264 console . log ( JSON . stringify ( tasks , null , 2 ) ) ;
260265 } ) ) ;
@@ -264,7 +269,7 @@ program
264269 . description ( '获取单个任务详情' )
265270 . requiredOption ( '--id <id>' , '任务 ID' )
266271 . action ( async ( options ) => withDb ( async ( ) => {
267- const task = await TaskService . getById ( parseInt ( options . id ) , { cwd : process . cwd ( ) } ) ;
272+ const task = await TaskService . getById ( parsePositiveInteger ( options . id , 'id' ) , { cwd : process . cwd ( ) } ) ;
268273 if ( task ) {
269274 console . log ( JSON . stringify ( task , null , 2 ) ) ;
270275 } else {
@@ -278,8 +283,9 @@ program
278283 . description ( '删除任务' )
279284 . requiredOption ( '--id <id>' , '任务 ID' )
280285 . action ( async ( options ) => withDb ( async ( ) => {
281- const deleted = await TaskService . delete ( parseInt ( options . id ) , { cwd : process . cwd ( ) } ) ;
282- console . log ( JSON . stringify ( { deleted, id : parseInt ( options . id ) } ) ) ;
286+ const id = parsePositiveInteger ( options . id , 'id' ) ;
287+ const deleted = await TaskService . delete ( id , { cwd : process . cwd ( ) } ) ;
288+ console . log ( JSON . stringify ( { deleted, id } ) ) ;
283289 } ) ) ;
284290
285291program
@@ -336,16 +342,16 @@ program
336342 prompt : options . prompt ,
337343 model : options . model ,
338344 category : options . category ,
339- importance : parseInt ( options . importance ) ,
340- urgency : parseInt ( options . urgency ) ,
345+ importance : parseBoundedInteger ( options . importance , 'importance' , 1 , 5 ) ,
346+ urgency : parseBoundedInteger ( options . urgency , 'urgency' , 1 , 5 ) ,
341347 cwd : process . cwd ( ) ,
342348 batchId : options . batch ,
343349 scheduleType : options . type as ScheduleType ,
344350 cronExpr : options . cron ,
345351 intervalMs,
346352 runAt,
347- maxInstances : parseInt ( options . maxInstances ) ,
348- maxRetries : parseInt ( options . maxRetries ) ,
353+ maxInstances : parseBoundedInteger ( options . maxInstances , 'max-instances' , 1 , 1000 ) ,
354+ maxRetries : parseBoundedInteger ( options . maxRetries , 'max-retries' , 0 , 1000 ) ,
349355 retryBackoffMs,
350356 timeoutMs,
351357 } ) ;
@@ -365,7 +371,7 @@ program
365371 . description ( '启用模板' )
366372 . requiredOption ( '--id <id>' , '模板 ID' )
367373 . action ( async ( options ) => withDb ( async ( ) => {
368- const tmpl = await TaskTemplateService . enable ( parseInt ( options . id ) ) ;
374+ const tmpl = await TaskTemplateService . enable ( parsePositiveInteger ( options . id , 'id' ) ) ;
369375 if ( tmpl ) {
370376 console . log ( JSON . stringify ( { id : tmpl . id , enabled : true } ) ) ;
371377 } else {
@@ -379,7 +385,7 @@ program
379385 . description ( '禁用模板' )
380386 . requiredOption ( '--id <id>' , '模板 ID' )
381387 . action ( async ( options ) => withDb ( async ( ) => {
382- const tmpl = await TaskTemplateService . disable ( parseInt ( options . id ) ) ;
388+ const tmpl = await TaskTemplateService . disable ( parsePositiveInteger ( options . id , 'id' ) ) ;
383389 if ( tmpl ) {
384390 console . log ( JSON . stringify ( { id : tmpl . id , enabled : false } ) ) ;
385391 } else {
@@ -393,8 +399,9 @@ program
393399 . description ( '删除模板' )
394400 . requiredOption ( '--id <id>' , '模板 ID' )
395401 . action ( async ( options ) => withDb ( async ( ) => {
396- const deleted = await TaskTemplateService . delete ( parseInt ( options . id ) ) ;
397- console . log ( JSON . stringify ( { deleted, id : parseInt ( options . id ) } ) ) ;
402+ const id = parsePositiveInteger ( options . id , 'id' ) ;
403+ const deleted = await TaskTemplateService . delete ( id ) ;
404+ console . log ( JSON . stringify ( { deleted, id } ) ) ;
398405 } ) ) ,
399406 ) ;
400407
@@ -408,6 +415,7 @@ databaseCommand
408415 . action ( async ( options : { json ?: boolean } ) => withDb ( async ( ) => {
409416 const result = DatabaseMaintenanceService . check ( ) ;
410417 console . log ( renderDatabaseResult ( 'check' , result , { forceJson : options . json } ) ) ;
418+ if ( ! result . ok ) process . exitCode = 1 ;
411419 } , ( error ) => renderDatabaseError ( error , { forceJson : options . json } ) ) ) ;
412420
413421databaseCommand
0 commit comments