Skip to content

Commit

Permalink
feat(cli): impl trigger command (labring#1413)
Browse files Browse the repository at this point in the history
  • Loading branch information
skyoct authored Jul 26, 2023
1 parent 4ad0d53 commit 2ec2929
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
58 changes: 58 additions & 0 deletions cli/src/action/trigger/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { CreateTriggerDto } from "../../api/v1/data-contracts";
import { triggerControllerCreate, triggerControllerFindAll, triggerControllerRemove } from "../../api/v1/trigger";
import { AppSchema } from "../../schema/app";
import * as Table from 'cli-table3'
import { getEmoji } from "../../util/print";
import { formatDate } from "../../util/format";

export async function list(): Promise<void> {
const appSchema = AppSchema.read()
const triggers = await triggerControllerFindAll(appSchema.appid)
const table = new Table({
head: ['id', 'name', 'target', 'cron', 'createdAt'],
})

for (let trigger of triggers) {
table.push([
trigger._id,
trigger.desc,
trigger.target,
trigger.cron,
formatDate(trigger.createdAt),
])
}
console.log(table.toString())
}

export async function create(name: string, target: string, cron: string): Promise<void> {
const appSchema = AppSchema.read()

const createDto: CreateTriggerDto = {
desc: name,
target,
cron,
}
await triggerControllerCreate(appSchema.appid, createDto)
console.log(`${getEmoji('✅')} trigger "${name}" created`)
}


export async function del(options: {
id?: string
name?: string
}): Promise<void> {
const appSchema = AppSchema.read()
if (options.id) {
await triggerControllerRemove(options.id, appSchema.appid)
console.log(`${getEmoji('✅')} trigger "${options.id}" deleted`)
} else if (options.name) {
const triggers = await triggerControllerFindAll(appSchema.appid)
const trigger = triggers.find((t) => t.desc === options.name)
if (trigger) {
await triggerControllerRemove(trigger._id, appSchema.appid)
console.log(`${getEmoji('✅')} trigger "${options.name}" deleted`)
} else {
console.log(`${getEmoji('❌')} trigger "${options.name}" not found`)
}
}
}
42 changes: 42 additions & 0 deletions cli/src/command/trigger/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Command, program } from "commander"
import { checkApplication } from "../../common/hook"
import { create, del, list } from "../../action/trigger"

export function command(): Command {
const cmd = program.command('trigger').hook('preAction', () => {
checkApplication()
})

cmd
.command('list')
.description('trigger list')
.action(() => {
list()
})

cmd
.command('create <name> <target> <cron>')
.description('create a trigger')
.action((name, target, cron) => {
create(name, target, cron)
})

cmd
.command('del [id]')
.description('delete a trigger')
.option('-n, --name <name>', 'trigger name')
.action((id, options) => {
if (!id && !options.name) {
console.log('please enter an id or name to delete')
return
}
del({
id,
name: options.name
})
})



return cmd
}
2 changes: 2 additions & 0 deletions cli/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { command as storageCommand } from './command/storage'
import { command as policyCommand } from './command/policy'
import { command as websiteCommand } from './command/website'
import { command as deployCommand } from './command/deploy'
import { command as triggerCommand } from './command/trigger'

const program = new Command()
program.option('-v, --version', 'output version').action((options) => {
Expand All @@ -30,5 +31,6 @@ program.addCommand(dependencyCommand())
program.addCommand(policyCommand())
program.addCommand(websiteCommand())
program.addCommand(deployCommand())
program.addCommand(triggerCommand())

program.parse(process.argv)

0 comments on commit 2ec2929

Please sign in to comment.