Skip to content

Commit

Permalink
feat(cli): impl website command (#854)
Browse files Browse the repository at this point in the history
* feat: impl website command
  • Loading branch information
skyoct authored Mar 6, 2023
1 parent d491ddd commit 409312a
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 7 deletions.
85 changes: 85 additions & 0 deletions cli/src/action/website/index.ts
Original file line number Diff line number Diff line change
@@ -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}`)
}
2 changes: 1 addition & 1 deletion cli/src/command/policy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
Expand Down
2 changes: 1 addition & 1 deletion cli/src/command/storage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
Expand Down
41 changes: 41 additions & 0 deletions cli/src/command/website/index.ts
Original file line number Diff line number Diff line change
@@ -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 <bucketName>')
.description('create a website')
.action((bucketName, options) => {
create(bucketName, options)
})


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

cmd
.command('custom <bucketName> <domain>')
.description('custom website domain')
.action((bucketName, domain, options) => {
custom(bucketName, domain, options)
})

return cmd
}
9 changes: 5 additions & 4 deletions cli/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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)
5 changes: 4 additions & 1 deletion cli/src/util/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 409312a

Please sign in to comment.