forked from labring/laf
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): impl trigger command (labring#1413)
- Loading branch information
Showing
3 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters