Skip to content

Commit 887671d

Browse files
committed
feat: add cli command check run
1 parent 36c7198 commit 887671d

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

index.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const { Command } = require('commander')
22
const { getConfig } = require('./src/config')
33
const { projectCategories, dbSettings } = getConfig()
44
const { logger } = require('./src/utils')
5-
const { runAddProjectCommand, runWorkflowCommand, listWorkflowCommand, listCheckCommand } = require('./src/cli')
5+
const { runAddProjectCommand, runWorkflowCommand, listWorkflowCommand, listCheckCommand, runCheckCommand } = require('./src/cli')
66
const knex = require('knex')(dbSettings)
77

88
const program = new Command()
@@ -69,4 +69,19 @@ check
6969
}
7070
})
7171

72+
check
73+
.command('run')
74+
.description('Run a check')
75+
.option('--name <name>', 'Name of the check')
76+
.action(async (options) => {
77+
try {
78+
await runCheckCommand(knex, options)
79+
} catch (error) {
80+
logger.error('Error running check:', error.message)
81+
process.exit(1)
82+
} finally {
83+
await knex.destroy()
84+
}
85+
})
86+
7287
program.parse(process.argv)

src/cli/checks.js

+31-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
const inquirer = require('inquirer').default
12
const { initializeStore } = require('../store')
23
const { logger } = require('../utils')
4+
const checks = require('../checks')
5+
const debug = require('debug')('cli:checks')
36

47
async function listCheckCommand (knex, options = {}) {
58
const { getAllComplianceChecks } = initializeStore(knex)
@@ -13,6 +16,33 @@ async function listCheckCommand (knex, options = {}) {
1316
return implementedChecks
1417
}
1518

19+
async function runCheckCommand (knex, options = {}) {
20+
const { getAllComplianceChecks } = initializeStore(knex)
21+
const complianceChecks = await getAllComplianceChecks(knex)
22+
const implementedChecks = complianceChecks.filter(check => check.implementation_status === 'completed')
23+
const validCommandNames = implementedChecks.map((item) => item.code_name)
24+
25+
if (Object.keys(options).length && !validCommandNames.includes(options.name)) {
26+
throw new Error('Invalid check name. Please enter a valid check name.')
27+
}
28+
29+
const answers = options.name
30+
? options
31+
: await inquirer.prompt([
32+
{
33+
type: 'list',
34+
name: 'name',
35+
message: 'What is the name (code_name) of the check?',
36+
choices: validCommandNames,
37+
when: () => !options.name
38+
}
39+
])
40+
41+
debug('Running check with code_name:', answers.name)
42+
checks[answers.name](knex)
43+
}
44+
1645
module.exports = {
17-
listCheckCommand
46+
listCheckCommand,
47+
runCheckCommand
1848
}

0 commit comments

Comments
 (0)