diff --git a/cli/src/action/website/index.ts b/cli/src/action/website/index.ts new file mode 100644 index 0000000000..57eeb08059 --- /dev/null +++ b/cli/src/action/website/index.ts @@ -0,0 +1,85 @@ +import { websiteControllerBindDomain, websiteControllerCreate, websiteControllerFindAll, websiteControllerRemove } from "../../api/v1/websitehosting" +import { readApplicationConfig } from "../../config/application" +import * as Table from 'cli-table3' +import { formatDate } from "../../util/format" +import { BindCustomDomainDto, CreateWebsiteDto } from "../../api/v1/data-contracts" +import { getEmoji } from "../../util/print" + + +export async function list() { + const appConfig = readApplicationConfig() + const websites = await websiteControllerFindAll(appConfig.appid) + const table = new Table({ + head: ['bucketName', 'domain', 'state', 'updatedAt'], + }) + for (let item of websites) { + table.push([item.bucketName, item.domain, item.state, formatDate(item.updatedAt),]) + } + console.log(table.toString()) +} + +export async function create(bucketName: string, options: any) { + const appConfig = readApplicationConfig() + + if (!bucketName.startsWith(appConfig.appid + '-')) { + bucketName = appConfig.appid + '-' + bucketName + } + + const createDto: CreateWebsiteDto = { + bucketName, + state: 'Active', + } + const website = await websiteControllerCreate(appConfig.appid, createDto) + + if (options) { } + + console.log(`${getEmoji('✅')} create website success!`) + console.log(`You can access through this domain: ${website.domain}`) +} + +export async function del(bucketName: string, options: any) { + const appConfig = readApplicationConfig() + const websites = await websiteControllerFindAll(appConfig.appid) + + if (options) { + } + + if (!bucketName.startsWith(appConfig.appid + '-')) { + bucketName = appConfig.appid + '-' + bucketName + } + + const targetId = websites.find((item) => item.bucketName === bucketName)?.id + if (!targetId) { + console.log(`${getEmoji('❌')} website ${bucketName} not found`) + return + } + await websiteControllerRemove(appConfig.appid, targetId) + + console.log(`${getEmoji('✅')} delete website success!`) +} + +export async function custom(bucketName: string, domain: string, options: any) { + const appConfig = readApplicationConfig() + const websites = await websiteControllerFindAll(appConfig.appid) + + if (options) { + } + + if (!bucketName.startsWith(appConfig.appid + '-')) { + bucketName = appConfig.appid + '-' + bucketName + } + + const targetId = websites.find((item) => item.bucketName === bucketName)?.id + if (!targetId) { + console.log(`${getEmoji('❌')} website ${bucketName} not found`) + return + } + + const patchDto: BindCustomDomainDto = { + domain, + } + const website = await websiteControllerBindDomain(appConfig.appid, targetId, patchDto) + + console.log(`${getEmoji('✅')} bind custom success!`) + console.log(`You can access through this domain: ${website.domain}`) +} \ No newline at end of file diff --git a/cli/src/command/policy/index.ts b/cli/src/command/policy/index.ts index 4cd9acfd39..3fa75856ad 100644 --- a/cli/src/command/policy/index.ts +++ b/cli/src/command/policy/index.ts @@ -2,7 +2,7 @@ import { Command, program } from 'commander' import { list, pullOne, pushOne, pullAll, pushAll } from '../../action/policy' import { checkApplication } from '../../common/hook' -export function policyCommand(): Command { +export function command(): Command { const cmd = program.command('policy').hook('preAction', () => { checkApplication() }) diff --git a/cli/src/command/storage/index.ts b/cli/src/command/storage/index.ts index 3031e0d1be..d24b716041 100644 --- a/cli/src/command/storage/index.ts +++ b/cli/src/command/storage/index.ts @@ -2,7 +2,7 @@ import { Command, program } from 'commander' import { create, del, list, pull, push, update } from '../../action/storage' import { checkApplication, checkStorageToken } from '../../common/hook' -export function bucketCommand(): Command { +export function command(): Command { const cmd = program.command('storage').hook('preAction', () => { checkApplication() }) diff --git a/cli/src/command/website/index.ts b/cli/src/command/website/index.ts new file mode 100644 index 0000000000..9f7e12b83f --- /dev/null +++ b/cli/src/command/website/index.ts @@ -0,0 +1,41 @@ +import { Command, program } from 'commander' +import { create, custom, del, list } from '../../action/website' +import { checkApplication } from '../../common/hook' + + +export function command(): Command { + const cmd = program.command('website').hook('preAction', () => { + checkApplication() + }) + + cmd + .command('list') + .description('website list') + .action(() => { + list() + }) + + cmd + .command('create ') + .description('create a website') + .action((bucketName, options) => { + create(bucketName, options) + }) + + + cmd + .command('del ') + .description('del website') + .action((bucketName, options) => { + del(bucketName, options) + }) + + cmd + .command('custom ') + .description('custom website domain') + .action((bucketName, domain, options) => { + custom(bucketName, domain, options) + }) + + return cmd +} \ No newline at end of file diff --git a/cli/src/main.ts b/cli/src/main.ts index 76e95b9853..ce7ff5448c 100644 --- a/cli/src/main.ts +++ b/cli/src/main.ts @@ -4,10 +4,10 @@ import { Command } from 'commander' import { command as applicationCommand } from './command/application/' import { command as functionCommand } from './command/function/' import { command as dependencyCommand } from './command/dependency/' - import { loginCommand, logoutCommand } from './command/auth' -import { bucketCommand } from './command/storage' -import { policyCommand } from './command/policy' +import { command as storageCommand } from './command/storage' +import { command as policyCommand } from './command/policy' +import { command as websiteCommand } from './command/website' const program = new Command() program.option('-v, --version', 'output version').action((options) => { @@ -23,8 +23,9 @@ program.addCommand(loginCommand()) program.addCommand(logoutCommand()) program.addCommand(applicationCommand()) program.addCommand(functionCommand()) -program.addCommand(bucketCommand()) +program.addCommand(storageCommand()) program.addCommand(dependencyCommand()) program.addCommand(policyCommand()) +program.addCommand(websiteCommand()) program.parse(process.argv) diff --git a/cli/src/util/request.ts b/cli/src/util/request.ts index d8fe400bf0..efe79be950 100644 --- a/cli/src/util/request.ts +++ b/cli/src/util/request.ts @@ -71,7 +71,10 @@ request.interceptors.response.use( } else if (statusCode == 403) { console.log('Forbidden resource!') process.exit(1) - } else if (statusCode === 503) { + } else if (statusCode === 500) { + console.log('Internal server error!') + process.exit(1) + }else if (statusCode === 503) { console.log('The server is abnormal, please contact the administrator!') process.exit(1) }