Skip to content

Commit 1607550

Browse files
authored
Merge pull request #4 from getshifter/feat/detach-delete
Feat/detach delete
2 parents 129d579 + 1ff4684 commit 1607550

File tree

3 files changed

+115
-3
lines changed

3 files changed

+115
-3
lines changed

README.md

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ 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)
33+
* [`shifter-domain delete [FILE]`](#shifter-domain-delete-file)
34+
* [`shifter-domain detach`](#shifter-domain-detach)
3435
* [`shifter-domain get`](#shifter-domain-get)
3536
* [`shifter-domain get-verification-code`](#shifter-domain-get-verification-code)
3637
* [`shifter-domain help [COMMAND]`](#shifter-domain-help-command)
@@ -90,20 +91,45 @@ EXAMPLES
9091

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

93-
## `shifter-domain detach [FILE]`
94+
## `shifter-domain delete [FILE]`
9495

9596
describe the command here
9697

9798
```
9899
USAGE
99-
$ shifter-domain detach [FILE]
100+
$ shifter-domain delete [FILE]
100101
101102
OPTIONS
102103
-f, --force
103104
-h, --help show CLI help
104105
-n, --name=name name to print
105106
```
106107

108+
_See code: [src/commands/delete.ts](https://github.com/getshifter/domain-cli/blob/v0.1.2/src/commands/delete.ts)_
109+
110+
## `shifter-domain detach`
111+
112+
Domain detach command
113+
114+
```
115+
USAGE
116+
$ shifter-domain detach
117+
118+
OPTIONS
119+
-D, --domain=domain Target domain name (eg. www.example.com)
120+
-S, --site-id=site-id Shifter site id
121+
-U, --username=username Shifter username
122+
-h, --help show CLI help
123+
-v, --version show CLI version
124+
--development Work as development mode (Only for Shifter developer team)
125+
--no-shifter-cdn If you using another CDN like Netlify or own CloudFront etc... Please set the flag.
126+
--verbose Show verbose
127+
128+
EXAMPLE
129+
$ shifter-domain detach --username USERNAME --password PASSWORD --site-id xxx-YOUR-SITE-ID-xxxx --domain
130+
test.example.com
131+
```
132+
107133
_See code: [src/commands/detach.ts](https://github.com/getshifter/domain-cli/blob/v0.1.2/src/commands/detach.ts)_
108134

109135
## `shifter-domain get`

src/commands/delete.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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 Delete extends AbstractCommand {
7+
static description = 'Domain delete command'
8+
9+
static examples = [
10+
'Simply usage',
11+
'$ shifter-domain delete --username USERNAME --password PASSWORD --site-id xxx-YOUR-SITE-ID-xxxx --domain test.example.com',
12+
]
13+
14+
static flags = {
15+
version: flags.version({char: 'v'}),
16+
help: flags.help({char: 'h'}),
17+
development: flags.boolean({
18+
description: 'Work as development mode (Only for Shifter developer team)',
19+
default: false,
20+
}),
21+
verbose: flags.boolean({
22+
description: 'Show verbose',
23+
default: false,
24+
}),
25+
username: flags.string({
26+
char: 'U',
27+
description: 'Shifter username',
28+
}),
29+
password: flags.string({
30+
hidden: true,
31+
char: 'P',
32+
description: 'Shifter password',
33+
}),
34+
domain: flags.string({
35+
char: 'D',
36+
description: 'Target domain name (eg. www.example.com)',
37+
}),
38+
'site-id': flags.string({
39+
char: 'S',
40+
description: 'Shifter site id',
41+
}),
42+
}
43+
44+
async run() {
45+
const {flags} = this.parse(Delete)
46+
const siteId = flags['site-id'] || await cli.prompt('Site id')
47+
const domain = flags.domain || await cli.prompt('Target domain')
48+
const development = flags.development === true
49+
if (development) this.log('Work as development mode')
50+
try {
51+
const clientWithAuth = await this.setupApiClient(flags.username, flags.password, flags.verbose, development)
52+
const site = await clientWithAuth.get(`/latest/sites/${siteId}`)
53+
if (!site || site.project_id !== siteId) throw new Error(`No such site ${siteId}`)
54+
const domainObj = await clientWithAuth.get(`/latest/sites/${siteId}/domains/${domain}`)
55+
if (!domainObj) throw new Error(`No such domain ${domain}`)
56+
await clientWithAuth.delete(`/latest/sites/${siteId}/domains/${domain}`)
57+
this.log('Domain has been deleted')
58+
} catch (error) {
59+
if (APIClientService.isAxiosError(error) && error.response) {
60+
const response = error.response
61+
// eslint-disable-next-line no-console
62+
if (development) console.log(response)
63+
this.error(`${response.status} - ${response.statusText}\n${response.data.message}`)
64+
}
65+
// eslint-disable-next-line no-console
66+
if (development) console.log(error)
67+
this.error(error)
68+
}
69+
}
70+
}

src/share/api/api.service.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,18 @@ export class APIClientService {
5454
return result.data
5555
}
5656

57+
public async delete(path: string, config?: AxiosRequestConfig) {
58+
return this._delete(path, config)
59+
}
60+
61+
protected async _delete(path: string, config?: AxiosRequestConfig) {
62+
const url = pathJoin(this.endpoint, path)
63+
if (this.debugMode) console.log({url, path, config})
64+
const result = await axios.delete(url, config)
65+
if (this.debugMode) console.log({result})
66+
return result.data
67+
}
68+
5769
public static isAxiosError(error: Error | AxiosError): error is AxiosError {
5870
return Object.prototype.hasOwnProperty.call(error, 'isAxiosError')
5971
}
@@ -85,4 +97,8 @@ export class APIClientWithAuthService extends APIClientService {
8597
public async get(path: string, config?: AxiosRequestConfig) {
8698
return this._get(path, this._putAccessTokenHeader(config))
8799
}
100+
101+
public async delete(path: string, config?: AxiosRequestConfig) {
102+
return this._delete(path, this._putAccessTokenHeader(config))
103+
}
88104
}

0 commit comments

Comments
 (0)