/
update.js
110 lines (94 loc) · 3.96 KB
/
update.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const { Flags, Args } = require('@oclif/core')
const { readFileSync } = require('fs-extra')
const { getDomain, postDomain } = require('../../requests/domain')
const { getDomainIdentifierArg, splitPathParams } = require('../../support/command/parse-input')
const { getVersion, parseDefinition } = require('../../utils/definitions')
const BaseCommand = require('../../support/command/base-command')
const UpdateCommand = require('../../support/command/update-command')
class UpdateDomainCommand extends UpdateCommand {
async updateDomain(owner, name, version, flags) {
const queryParams = { version }
if (flags.visibility) {
queryParams['isPrivate'] = flags.visibility !== 'public'
this.logCommandSuccess = this.setSuccessMessage('DomainUpdateVisibility')
}
const updateDomainObj = {
pathParams: [owner, name],
queryParams,
body: readFileSync(flags.file)
}
return await this.executeHttp({
execute: () => postDomain(updateDomainObj),
onResolve: this.logCommandSuccess({ owner, name, version, visibility: flags.visibility }),
options: { resolveStatus: [403] }
})
}
async run() {
const { args, flags } = await this.parse(UpdateDomainCommand)
if (!Object.keys(flags).length) {
return this.error('No updates specified', { exit: 1 })
}
const definition = flags.file ? parseDefinition(flags.file) : null
const domainPath = getDomainIdentifierArg(args)
const [owner, name, version] = splitPathParams(domainPath)
const defaultVersion = definition ? getVersion(definition) : await this.getDefaultDomainVersion([owner, name])
const domainVersion = version ? version : defaultVersion
const type = 'domains'
if (flags.file) {
await this.handleUpdate(owner, name, domainVersion, flags)
} else if (flags.visibility) {
await this.updateVisibility(type, owner, name, domainVersion, flags.visibility !== 'public')
}
if (flags.published) await this.updatePublish(type, owner, name, domainVersion, flags.published === 'publish')
if (flags.setdefault) await this.updateDefault(type, owner, name, domainVersion)
}
async handleUpdate(owner, name, version, flags) {
await this.executeHttp({
execute: () => getDomain([owner, name, version]),
onResolve: () => this.updateDomain(owner, name, version, flags),
options: { resolveStatus: [403] }
})
}
}
UpdateDomainCommand.description = `update a domain
The domain version from the file will be used unless the version is specified in the command argument.
When no file is specified then the default domain version will be updated.
The domain visibility can be changed by using visibility flag.
`
UpdateDomainCommand.examples = [
'swaggerhub domain:update organization/domain --file domain.yaml',
'swaggerhub domain:update organization/domain/1.0.0 --file domain.json',
'swaggerhub domain:update organization/domain/1.0.0 --published=publish --file domain.json',
'swaggerhub domain:update organization/domain/1.0.0 --setdefault --file domain.json',
'swaggerhub domain:update organization/domain/1.0.0 --published=unpublish --setdefault --file domain.json',
'swaggerhub domain:update organization/domain/1.0.0 --visibility=private',
]
UpdateDomainCommand.args = {
'OWNER/DOMAIN_NAME/[VERSION]': Args.string({
required: true,
description: 'Domain to update on SwaggerHub'
})
}
UpdateDomainCommand.flags = {
file: Flags.string({
char: 'f',
description: 'file location of domain to update',
required: false,
multiple: false
}),
visibility: Flags.string({
description: 'visibility of domain in SwaggerHub',
options: ['public', 'private']
}),
published: Flags.string({
description: 'sets the lifecycle setting of the domain version',
options: ['publish', 'unpublish'],
required: false
}),
setdefault: Flags.boolean({
description: 'sets domain version to be the default',
required: false
}),
...BaseCommand.flags
}
module.exports = UpdateDomainCommand