-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathset.ts
More file actions
120 lines (105 loc) · 3.54 KB
/
set.ts
File metadata and controls
120 lines (105 loc) · 3.54 KB
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
111
112
113
114
115
116
117
118
119
120
import fs from 'fs'
import path from 'path'
import chalk from 'chalk'
import { fileUtils } from '../utils'
import Command from './base'
import { CloudGraphConfig } from '../types'
import { getProviderQuestion } from '../utils/questions'
import { DEFAULT_CG_CONFIG } from '../utils/constants'
export default class SetField extends Command {
static description = 'Configure cloud provider service properties'
static examples = ['$ cg set', '$ cg set aws [Initialize AWS provider]']
static flags = {
...Command.flags,
}
static hidden = false
static strict = false
static args = Command.args
async getProvider(): Promise<string> {
const { provider } = await this.interface.prompt(getProviderQuestion)
this.logger.debug(provider)
return provider
}
/**
* Ensures that the configuration path exists and saves the CloudGraph json config file in it
*/
saveCloudGraphConfigFile(configResult: CloudGraphConfig): void {
const { configDir } = this.config
const previousConfig = this.getCGConfig()
const newConfig = configResult
if (previousConfig) {
for (const key of Object.keys(previousConfig)) {
if (!configResult[key]) {
newConfig[key] = previousConfig[key]
}
}
} else {
fileUtils.makeDirIfNotExists(configDir)
}
fs.writeFileSync(
path.join(configDir, '.cloud-graphrc.json'),
JSON.stringify(newConfig, null, 2)
)
}
async run(): Promise<void> {
const { configDir } = this.config
const { argv } = await this.parse(SetField)
const config = this.getCGConfig() ?? DEFAULT_CG_CONFIG
// First determine the provider if one has not been passed in args
// if no provider is passed, they can select from a list of offically supported providers
let allProviders: string[] = argv
if (allProviders.length === 0) {
allProviders = [await this.getProvider()]
}
const configResult: { [key: string]: Record<string, any> } = { ...config }
for (const provider of allProviders) {
/**
* First install and require the provider plugin
*/
const { services, serviceProperties } = await this.getProviderClient(
provider
)
if (!serviceProperties) {
this.logger.warn(
"Provider's Properties should be defined to configure services"
)
continue // eslint-disable-line no-continue
}
const { resources: resourcesAnswer } = await this.interface.prompt([
{
type: 'checkbox',
message: 'Select services to configure',
loop: false,
name: 'resources',
choices: Object.values(services as { [key: string]: string }).map(
(service: string) => ({
name: service,
})
),
},
])
const properties: { [field: string]: any } = {}
for (const resource of resourcesAnswer) {
const resourceProperties = serviceProperties[resource].map(
({ field, defaultValue }) => ({
type: 'input',
message: `set ${resource} ${field}`,
name: field,
default: defaultValue ?? undefined,
})
)
properties[resource] = await this.interface.prompt(resourceProperties)
}
configResult[provider] = {
...configResult[provider],
properties,
}
}
this.saveCloudGraphConfigFile(configResult)
this.logger.success(
`Your config has been successfully stored at ${chalk.italic.green(
path.join(configDir, '.cloud-graphrc.json')
)}`
)
}
}