Skip to content

Commit

Permalink
feat(cli): add function delete (#664)
Browse files Browse the repository at this point in the history
  • Loading branch information
skyoct authored Jan 20, 2023
1 parent ac0be96 commit b4002ba
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 18 deletions.
21 changes: 17 additions & 4 deletions cli/src/action/function/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { CompileFunctionDto, CreateFunctionDto, UpdateFunctionDto } from "../../api/v1/data-contracts"
import { functionControllerCompile, functionControllerCreate, functionControllerFindAll, functionControllerFindOne, functionControllerUpdate, logControllerGetLogs } from "../../api/v1/function"
import { functionControllerCompile, functionControllerCreate, functionControllerFindAll, functionControllerFindOne, functionControllerRemove, functionControllerUpdate, logControllerGetLogs } from "../../api/v1/function"
import { readApplicationConfig } from "../../config/application"
import { FunctionConfig, readFunctionConfig, writeFunctionConfig } from "../../config/function"
import { existFunctionConfig, FunctionConfig, readFunctionConfig, removeFunctionConfig, writeFunctionConfig } from "../../config/function"
import * as path from "node:path"
import * as fs from "node:fs"
import * as Table from 'cli-table3'
import { formatDate } from "../../util/format"
import { readSecretConfig } from "../../config/secret"
import { invokeFunction } from "../../api/debug"
import { exist } from "../../util/file"
import { exist, remove } from "../../util/file"
import { getEmoji } from "../../util/print"


Expand Down Expand Up @@ -41,6 +41,19 @@ export async function list() {
console.log(table.toString())
}

export async function del(funcName: string) {
const appConfig = readApplicationConfig()
await functionControllerRemove(appConfig.appid, funcName)
if (existFunctionConfig(funcName)) {
removeFunctionConfig(funcName)
}
const funcPath = path.join(process.cwd(), 'functions', funcName + '.ts')
if (exist(funcPath)) {
remove(funcPath)
}
console.log(`${getEmoji('✅')} function ${funcName} deleted`)
}

async function pull(funcName: string) {
const appConfig = readApplicationConfig()
const func = await functionControllerFindOne(appConfig.appid, funcName)
Expand Down Expand Up @@ -101,7 +114,7 @@ export async function pushOne(funcName: string) {
console.log(`${getEmoji('✅')} function ${funcName} pushed`)
}

export async function exec(funcName: string, options: {log: string, requestId: boolean}) {
export async function exec(funcName: string, options: { log: string, requestId: boolean }) {
// compile code
const codePath = path.join(process.cwd(), 'functions', funcName + '.ts')
if (!exist(codePath)) {
Expand Down
34 changes: 20 additions & 14 deletions cli/src/command/function/index.ts
Original file line number Diff line number Diff line change
@@ -1,62 +1,68 @@
import { Command, program } from "commander"
import { create, exec, list, pullAll, pullOne, pushAll, pushOne } from "../../action/function"
import { create, del, exec, list, pullAll, pullOne, pushAll, pushOne } from "../../action/function"
import { checkApplication, checkFunctionDebugToken } from "../../common/hook"

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

cmd.command('create <funcName>')
.description('Create function')
.description('create function')
.option('-w --websocket', 'enable websocket', true)
.option('-m --methods <items...>', 'http methods', ['GET', 'POST'])
.option('-t --tags <items...>', 'tags', [])
.option('-d --description <description>', 'description', '')
.action((funcName, options) => {
create(funcName, options)
})


cmd.command('del <funcName>')
.description('del function')
.action((funcName) => {
del(funcName)
})

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

cmd.command('pull')
.argument('[funcName]', 'funcName')
.description('Pull function, if funcName does not exist, pull all')
.action((funcName) => {
if (funcName) {
pullOne(funcName)
} else {
} else {
pullAll()
}
})

cmd.command('push')
.argument('[FuncName]', 'funcName')
.description('Push function, if funcName does not exist, push all')
.description('push function, if funcName does not exist, push all')
.action((funcName) => {
if (funcName) {
pushOne(funcName)
} else {
} else {
pushAll()
}
})

cmd.command('exec <funcName>')
.description('Exec function')
.option('-l --log <count>', 'print log')
.option('-r --requestId', 'print requestId', false)
.hook('preAction', () => {
.hook('preAction', () => {
checkFunctionDebugToken()
})
.action((funcName, options) => {
exec(funcName, options)
})


return cmd
}

0 comments on commit b4002ba

Please sign in to comment.