-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbase.ts
162 lines (140 loc) · 5.8 KB
/
base.ts
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/**
* Copyright (C) 2024 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the ""License"");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an ""AS IS"" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import paths from 'path'
import { MissingArgumentError } from '../core/errors.ts'
import { ShellRunner } from '../core/shell_runner.ts'
import type { ChartManager, ConfigManager, Helm, K8, DependencyManager, LeaseManager } from '../core/index.ts'
import type { CommandFlag, Opts } from '../types/index.ts'
import { type LocalConfig } from './../core/config/LocalConfig.ts'
export class BaseCommand extends ShellRunner {
protected readonly helm: Helm
protected readonly k8: K8
protected readonly chartManager: ChartManager
protected readonly configManager: ConfigManager
protected readonly depManager: DependencyManager
protected readonly leaseManager: LeaseManager
protected readonly _configMaps = new Map<string, any>()
protected readonly localConfig: LocalConfig
constructor (opts: Opts) {
if (!opts || !opts.logger) throw new Error('An instance of core/SoloLogger is required')
if (!opts || !opts.helm) throw new Error('An instance of core/Helm is required')
if (!opts || !opts.k8) throw new Error('An instance of core/K8 is required')
if (!opts || !opts.chartManager) throw new Error('An instance of core/ChartManager is required')
if (!opts || !opts.configManager) throw new Error('An instance of core/ConfigManager is required')
if (!opts || !opts.depManager) throw new Error('An instance of core/DependencyManager is required')
if (!opts || !opts.localConfig) throw new Error('An instance of core/LocalConfig is required')
super(opts.logger)
this.helm = opts.helm
this.k8 = opts.k8
this.chartManager = opts.chartManager
this.configManager = opts.configManager
this.depManager = opts.depManager
this.leaseManager = opts.leaseManager
this.localConfig = opts.localConfig
}
async prepareChartPath (chartDir: string, chartRepo: string, chartReleaseName: string) {
if (!chartRepo) throw new MissingArgumentError('chart repo name is required')
if (!chartReleaseName) throw new MissingArgumentError('chart release name is required')
if (chartDir) {
const chartPath = `${chartDir}/${chartReleaseName}`
await this.helm.dependency('update', chartPath)
return chartPath
}
return `${chartRepo}/${chartReleaseName}`
}
prepareValuesFiles (valuesFile: string) {
let valuesArg = ''
if (valuesFile) {
const valuesFiles = valuesFile.split(',')
valuesFiles.forEach(vf => {
const vfp = paths.resolve(vf)
valuesArg += ` --values ${vfp}`
})
}
return valuesArg
}
/**
* Dynamically builds a class with properties from the provided list of flags
* and extra properties, will keep track of which properties are used. Call
* getUnusedConfigs() to get an array of unused properties.
*/
getConfig (configName: string, flags: CommandFlag[], extraProperties: string[] = []): object {
const configManager = this.configManager
// build the dynamic class that will keep track of which properties are used
const NewConfigClass = class {
private usedConfigs: Map<string, number>
constructor () {
// the map to keep track of which properties are used
this.usedConfigs = new Map()
// add the flags as properties to this class
flags?.forEach(flag => {
// @ts-ignore
this[`_${flag.constName}`] = configManager.getFlag(flag)
Object.defineProperty(this, flag.constName, {
get () {
this.usedConfigs.set(flag.constName, this.usedConfigs.get(flag.constName) + 1 || 1)
return this[`_${flag.constName}`]
}
})
})
// add the extra properties as properties to this class
extraProperties?.forEach(name => {
// @ts-ignore
this[`_${name}`] = ''
Object.defineProperty(this, name, {
get () {
this.usedConfigs.set(name, this.usedConfigs.get(name) + 1 || 1)
return this[`_${name}`]
},
set (value) {
this[`_${name}`] = value
}
})
})
}
/** Get the list of unused configurations that were not accessed */
getUnusedConfigs () {
const unusedConfigs: string[] = []
// add the flag constName to the unusedConfigs array if it was not accessed
flags?.forEach(flag => {
if (!this.usedConfigs.has(flag.constName)) {
unusedConfigs.push(flag.constName)
}
})
// add the extra properties to the unusedConfigs array if it was not accessed
extraProperties?.forEach(item => {
if (!this.usedConfigs.has(item)) {
unusedConfigs.push(item)
}
})
return unusedConfigs
}
}
const newConfigInstance = new NewConfigClass()
// add the new instance to the configMaps so that it can be used to get the
// unused configurations using the configName from the BaseCommand
this._configMaps.set(configName, newConfigInstance)
return newConfigInstance
}
/**
* Get the list of unused configurations that were not accessed
* @returns an array of unused configurations
*/
getUnusedConfigs (configName: string): string[] {
return this._configMaps.get(configName).getUnusedConfigs()
}
}