MCPcopy Create free account
hub / github.com/SmartThingsCommunity/smartthings-cli / loadConfig

Function loadConfig

src/lib/cli-config.ts:115–177  ·  view source on GitHub ↗
(description: CLIConfigDescription, logger: Logger)

Source from the content-addressed store, hash-verified

113}
114
115export const loadConfig = async (description: CLIConfigDescription, logger: Logger): Promise<CLIConfig> => {
116 const config = await loadConfigFile(description.configFilename)
117 const managedProfiles = await loadConfigFile(description.managedConfigFilename)
118 const mergedProfiles = mergeProfiles(config, managedProfiles)
119
120 const profile = description.profileName in mergedProfiles
121 ? mergedProfiles[description.profileName]
122 : {}
123
124 function stringConfigValue(keyName: string): string
125 function stringConfigValue(keyName: string, defaultValue: string): string
126 function stringConfigValue(keyName: string, defaultValue?: string): string | undefined {
127 if (keyName in profile) {
128 const configValue = profile[keyName]
129 if (typeof configValue === 'string') {
130 return configValue
131 }
132 logger.warn(`expected string value for config key ${keyName} but got ${typeof profile[keyName]}`)
133 return defaultValue
134 }
135 logger.trace(`key ${keyName} not found in ${description.profileName} config`)
136 return defaultValue
137 }
138
139 function stringArrayConfigValue(keyName: string, defaultValue: string[] = []): string[] {
140 if (keyName in profile) {
141 const configValue = profile[keyName]
142 if (typeof configValue === 'string') {
143 return [configValue]
144 }
145 if (Array.isArray(configValue) && configValue.every(dir => typeof dir === 'string')) {
146 return configValue
147 }
148 logger.warn(`expected string or array of strings for config key ${keyName} but got ${typeof configValue}`)
149 return defaultValue
150 }
151 return defaultValue
152 }
153
154 function booleanConfigValue(keyName: string, defaultValue = false): boolean {
155 if (keyName in profile) {
156 const configValue = profile[keyName]
157 if (typeof configValue === 'boolean') {
158 return configValue
159 }
160 logger.warn(`expected boolean value for config key ${keyName} but got ${typeof profile[keyName]}`)
161 return defaultValue
162 }
163 logger.trace(`key ${keyName} not found in ${description.profileName} config`)
164 return defaultValue
165 }
166
167 return {
168 ...description,
169 profiles: config,
170 managedProfiles,
171 mergedProfiles,
172 profile,

Callers 3

smartThingsCommandFunction · 0.85
cli-config.test.tsFile · 0.85
buildConfigFunction · 0.85

Calls 2

loadConfigFileFunction · 0.85
mergeProfilesFunction · 0.85

Tested by 1

buildConfigFunction · 0.68