Skip to content

Commit

Permalink
feat(cli): impl bucket curd cmd (#683)
Browse files Browse the repository at this point in the history
  • Loading branch information
skyoct authored Jan 24, 2023
1 parent 9e95bcb commit fd5ca45
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
68 changes: 68 additions & 0 deletions cli/src/action/stroage/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { bucketControllerCreate, bucketControllerFindAll, bucketControllerRemove, bucketControllerUpdate } from "../../api/v1/storage";
import { readApplicationConfig } from "../../config/application";
import * as Table from 'cli-table3';
import * as prompts from 'prompts';
import { CreateBucketDto, UpdateBucketDto } from "../../api/v1/data-contracts";
import { getEmoji } from "../../util/print";


export async function list() {
const appConfig = readApplicationConfig()
const buckets = await bucketControllerFindAll(appConfig.appid)
const table = new Table({
head: ['name', 'shortName', 'policy', 'updatedAt'],
})
for (let item of buckets) {
table.push([item.name, item.shortName, item.policy, item.updatedAt])
}
console.log(table.toString());
}


const policySelect = {
type: 'select',
name: 'policy',
message: 'please select policy',
choices: [
{ title: 'private', value: 'private' },
{ title: 'readonly', value: 'readonly' },
{ title: 'readwrite', value: 'readwrite' }
],
};


export async function create(bucketName, options) {
if (options) {

}
const appConfig = readApplicationConfig()
console.log('please select bucket storage policy')
const policyResult = await prompts(policySelect);
const bucketDto: CreateBucketDto = {
shortName: bucketName,
policy: policyResult.policy,
}
await bucketControllerCreate(appConfig.appid, bucketDto)
console.log(`${getEmoji('✅')} bucket ${bucketName} created`)
}

export async function update(bucketName, options) {
if (options) { }
const appConfig = readApplicationConfig()
console.log('please select the storage policy to be replaced')
const policyResult = await prompts(policySelect);
const bucketDto: UpdateBucketDto = {
policy: policyResult.policy,
}
await bucketControllerUpdate(appConfig.appid, bucketName, bucketDto)
console.log(`${getEmoji('✅')} bucket ${bucketName} updated`)
}

export async function del(bucketName, options) {
if (options) {

}
const appConfig = readApplicationConfig()
await bucketControllerRemove(appConfig.appid, bucketName)
console.log(`${getEmoji('✅')} bucket ${bucketName} deleted`)
}
38 changes: 38 additions & 0 deletions cli/src/command/stroage/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Command, program } from "commander"
import { create, del, list, update } from "../../action/stroage"
import { checkApplication } from "../../common/hook"

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

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

cmd.command('create <bucketName>')
.description('create a bucket')
.action((bucketName, options) => {
create(bucketName, options)
})

cmd.command('update <bucketName>')
.description('update bucket')
.action((bucketName, options) => {
update(bucketName, options)
})

cmd.command('del <bucketName>')
.description('delete bucket')
.action((bucketName, options) => {
del(bucketName, options)
})


return cmd
}

2 changes: 2 additions & 0 deletions cli/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { command as functionCommand } from './command/function/'
import { command as dependencyCommand } from './command/dependency/'

import { loginCommand, logoutCommand } from './command/auth'
import { bucketCommand } from './command/stroage'


const program = new Command()
Expand All @@ -18,6 +19,7 @@ program.addCommand(loginCommand())
program.addCommand(logoutCommand())
program.addCommand(applicationCommand())
program.addCommand(functionCommand())
program.addCommand(bucketCommand())
program.addCommand(dependencyCommand())

program.parse(process.argv)
Expand Down

0 comments on commit fd5ca45

Please sign in to comment.