MCPcopy Index your code
hub / github.com/SmartThingsCommunity/smartthings-cli / selectFromList

Function selectFromList

src/lib/command/select.ts:141–194  ·  view source on GitHub ↗
(
		command: SmartThingsCommand<SelectFromListFlags>,
		config: SelectFromListConfig<L>,
		options: SelectOptions<L, ID>,
)

Source from the content-addressed store, hash-verified

139 * @returns Selected id if one was chosen. Logs message if no items are found and exits.
140 */
141export async function selectFromList<L extends object, ID = string>(
142 command: SmartThingsCommand<SelectFromListFlags>,
143 config: SelectFromListConfig<L>,
144 options: SelectOptions<L, ID>,
145): Promise<ID> {
146 if (options.preselectedId) {
147 return options.preselectedId
148 }
149
150 if (options.defaultValue) {
151 const configuredDefault = command.cliConfig.profile[options.defaultValue.configKey] as ID
152 if (configuredDefault) {
153 try {
154 const item = await options.defaultValue.getItem(configuredDefault)
155 if (item) {
156 console.log(options.defaultValue.userMessage(item))
157 return configuredDefault
158 }
159 } catch (error) {
160 if (!error.response || error.response.status !== 404 && error.response.status !== 403) {
161 throw error
162 }
163 }
164
165 await resetManagedConfigKey(command.cliConfig, options.defaultValue.configKey)
166 command.logger.debug(`removed ${options.defaultValue.configKey} from ${command.cliConfig.profileName}`)
167 }
168 }
169
170 const userSelected = await promptUser(command, config, options)
171
172 const neverAgainKey = `${options.defaultValue?.configKey ?? ''}::neverAskForSaveAgain`
173 if (options.defaultValue && !command.cliConfig.booleanConfigValue(neverAgainKey)) {
174 const answer = await select({
175 message: 'Do you want to save this as the default?',
176 choices: [
177 { name: 'Yes', value: 'yes' },
178 { name: 'No', value: 'no' },
179 { name: 'No, and do not ask again', value: 'never' },
180 ],
181 })
182
183 const resetInfo = 'You can reset these settings using the config:reset command.'
184 if (answer === 'yes') {
185 await setConfigKey(command.cliConfig, options.defaultValue.configKey, userSelected)
186 console.log(`${userSelected} is now the default.\n${resetInfo}`)
187 } else if (answer === 'never') {
188 await setConfigKey(command.cliConfig, neverAgainKey, true)
189 console.log(`You will not be asked again.\n${resetInfo}`)
190 }
191 }
192
193 return userSelected
194}

Callers 15

handlerFunction · 0.85
handlerFunction · 0.85
handlerFunction · 0.85
chooseCapabilityFunction · 0.85
chooseCapabilityFilteredFunction · 0.85
chooseDevicePrototypeFunction · 0.85
chooseCapabilityFunction · 0.85
chooseAttributeFunction · 0.85
chooseUnitFunction · 0.85
chooseValueFunction · 0.85

Calls 3

resetManagedConfigKeyFunction · 0.85
promptUserFunction · 0.85
setConfigKeyFunction · 0.85

Tested by

no test coverage detected