Skip to content

Commit e734394

Browse files
author
Hidetaka Okamoto
committed
feat: add detach domain command
1 parent 8955995 commit e734394

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ USAGE
3030
<!-- commands -->
3131
* [`shifter-domain add`](#shifter-domain-add)
3232
* [`shifter-domain attach`](#shifter-domain-attach)
33+
* [`shifter-domain detach [FILE]`](#shifter-domain-detach-file)
3334
* [`shifter-domain get`](#shifter-domain-get)
3435
* [`shifter-domain get-verification-code`](#shifter-domain-get-verification-code)
3536
* [`shifter-domain help [COMMAND]`](#shifter-domain-help-command)
@@ -89,6 +90,22 @@ EXAMPLES
8990

9091
_See code: [src/commands/attach.ts](https://github.com/getshifter/domain-cli/blob/v0.1.2/src/commands/attach.ts)_
9192

93+
## `shifter-domain detach [FILE]`
94+
95+
describe the command here
96+
97+
```
98+
USAGE
99+
$ shifter-domain detach [FILE]
100+
101+
OPTIONS
102+
-f, --force
103+
-h, --help show CLI help
104+
-n, --name=name name to print
105+
```
106+
107+
_See code: [src/commands/detach.ts](https://github.com/getshifter/domain-cli/blob/v0.1.2/src/commands/detach.ts)_
108+
92109
## `shifter-domain get`
93110

94111
Domain get command

src/commands/detach.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import {flags} from '@oclif/command'
2+
import cli from 'cli-ux'
3+
import {APIClientService} from '../share/api/api.service'
4+
import {AbstractCommand} from '../share/abstract.command'
5+
6+
export default class Detach extends AbstractCommand {
7+
static description = 'Domain detach command'
8+
9+
static examples = [
10+
'$ shifter-domain detach --username USERNAME --password PASSWORD --site-id xxx-YOUR-SITE-ID-xxxx --domain test.example.com',
11+
]
12+
13+
static flags = {
14+
version: flags.version({char: 'v'}),
15+
help: flags.help({char: 'h'}),
16+
development: flags.boolean({
17+
description: 'Work as development mode (Only for Shifter developer team)',
18+
default: false,
19+
}),
20+
verbose: flags.boolean({
21+
description: 'Show verbose',
22+
default: false,
23+
}),
24+
username: flags.string({
25+
char: 'U',
26+
description: 'Shifter username',
27+
}),
28+
password: flags.string({
29+
hidden: true,
30+
char: 'P',
31+
description: 'Shifter password',
32+
}),
33+
domain: flags.string({
34+
char: 'D',
35+
description: 'Target domain name (eg. www.example.com)',
36+
}),
37+
'site-id': flags.string({
38+
char: 'S',
39+
description: 'Shifter site id',
40+
}),
41+
'no-shifter-cdn': flags.boolean({
42+
description: 'If you using another CDN like Netlify or own CloudFront etc... Please set the flag.',
43+
default: false,
44+
}),
45+
}
46+
47+
async run() {
48+
const {flags} = this.parse(Detach)
49+
const siteId = flags['site-id'] || await cli.prompt('Site id')
50+
const domain = flags.domain || await cli.prompt('Target domain')
51+
const development = flags.development === true
52+
const noShifterCDN = flags['no-shifter-cdn'] === true
53+
if (development) this.log('Work as development mode')
54+
try {
55+
const clientWithAuth = await this.setupApiClient(flags.username, flags.password, flags.verbose, development)
56+
const site = await clientWithAuth.get(`/latest/sites/${siteId}`)
57+
if (!site || site.project_id !== siteId) throw new Error(`No such site ${siteId}`)
58+
const domainObj = await clientWithAuth.get(`/latest/sites/${siteId}/domains/${domain}`)
59+
if (!domainObj) throw new Error(`No such domain ${domain}`)
60+
if (domainObj.status !== 'ISSUED') throw new Error('The domain has not been veritied. Please wait for a while and try again.')
61+
await clientWithAuth.post(`/latest/sites/${siteId}/domains/${domain}/detach`, {
62+
use_shifter_domain: !noShifterCDN,
63+
})
64+
this.log('Domain has been detached')
65+
} catch (error) {
66+
if (APIClientService.isAxiosError(error) && error.response) {
67+
const response = error.response
68+
// eslint-disable-next-line no-console
69+
if (development) console.log(response)
70+
this.error(`${response.status} - ${response.statusText}\n${response.data.message}`)
71+
}
72+
// eslint-disable-next-line no-console
73+
if (development) console.log(error)
74+
this.error(error)
75+
}
76+
}
77+
}

0 commit comments

Comments
 (0)