Skip to content

Commit 2ec2929

Browse files
authored
feat(cli): impl trigger command (#1413)
1 parent 4ad0d53 commit 2ec2929

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed

cli/src/action/trigger/index.ts

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { CreateTriggerDto } from "../../api/v1/data-contracts";
2+
import { triggerControllerCreate, triggerControllerFindAll, triggerControllerRemove } from "../../api/v1/trigger";
3+
import { AppSchema } from "../../schema/app";
4+
import * as Table from 'cli-table3'
5+
import { getEmoji } from "../../util/print";
6+
import { formatDate } from "../../util/format";
7+
8+
export async function list(): Promise<void> {
9+
const appSchema = AppSchema.read()
10+
const triggers = await triggerControllerFindAll(appSchema.appid)
11+
const table = new Table({
12+
head: ['id', 'name', 'target', 'cron', 'createdAt'],
13+
})
14+
15+
for (let trigger of triggers) {
16+
table.push([
17+
trigger._id,
18+
trigger.desc,
19+
trigger.target,
20+
trigger.cron,
21+
formatDate(trigger.createdAt),
22+
])
23+
}
24+
console.log(table.toString())
25+
}
26+
27+
export async function create(name: string, target: string, cron: string): Promise<void> {
28+
const appSchema = AppSchema.read()
29+
30+
const createDto: CreateTriggerDto = {
31+
desc: name,
32+
target,
33+
cron,
34+
}
35+
await triggerControllerCreate(appSchema.appid, createDto)
36+
console.log(`${getEmoji('✅')} trigger "${name}" created`)
37+
}
38+
39+
40+
export async function del(options: {
41+
id?: string
42+
name?: string
43+
}): Promise<void> {
44+
const appSchema = AppSchema.read()
45+
if (options.id) {
46+
await triggerControllerRemove(options.id, appSchema.appid)
47+
console.log(`${getEmoji('✅')} trigger "${options.id}" deleted`)
48+
} else if (options.name) {
49+
const triggers = await triggerControllerFindAll(appSchema.appid)
50+
const trigger = triggers.find((t) => t.desc === options.name)
51+
if (trigger) {
52+
await triggerControllerRemove(trigger._id, appSchema.appid)
53+
console.log(`${getEmoji('✅')} trigger "${options.name}" deleted`)
54+
} else {
55+
console.log(`${getEmoji('❌')} trigger "${options.name}" not found`)
56+
}
57+
}
58+
}

cli/src/command/trigger/index.ts

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { Command, program } from "commander"
2+
import { checkApplication } from "../../common/hook"
3+
import { create, del, list } from "../../action/trigger"
4+
5+
export function command(): Command {
6+
const cmd = program.command('trigger').hook('preAction', () => {
7+
checkApplication()
8+
})
9+
10+
cmd
11+
.command('list')
12+
.description('trigger list')
13+
.action(() => {
14+
list()
15+
})
16+
17+
cmd
18+
.command('create <name> <target> <cron>')
19+
.description('create a trigger')
20+
.action((name, target, cron) => {
21+
create(name, target, cron)
22+
})
23+
24+
cmd
25+
.command('del [id]')
26+
.description('delete a trigger')
27+
.option('-n, --name <name>', 'trigger name')
28+
.action((id, options) => {
29+
if (!id && !options.name) {
30+
console.log('please enter an id or name to delete')
31+
return
32+
}
33+
del({
34+
id,
35+
name: options.name
36+
})
37+
})
38+
39+
40+
41+
return cmd
42+
}

cli/src/main.ts

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { command as storageCommand } from './command/storage'
99
import { command as policyCommand } from './command/policy'
1010
import { command as websiteCommand } from './command/website'
1111
import { command as deployCommand } from './command/deploy'
12+
import { command as triggerCommand } from './command/trigger'
1213

1314
const program = new Command()
1415
program.option('-v, --version', 'output version').action((options) => {
@@ -30,5 +31,6 @@ program.addCommand(dependencyCommand())
3031
program.addCommand(policyCommand())
3132
program.addCommand(websiteCommand())
3233
program.addCommand(deployCommand())
34+
program.addCommand(triggerCommand())
3335

3436
program.parse(process.argv)

0 commit comments

Comments
 (0)