Skip to content

feat(cli): impl bucket curd cmd #683

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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