selectDevice method

CustomDeviceConfig selectDevice(
  1. CustomDevicesConfig customDevicesConfig, {
  2. String? title,
  3. String? description,
  4. String? errorDescription,
})

Implementation

CustomDeviceConfig selectDevice(
  CustomDevicesConfig customDevicesConfig, {
  String? title,
  String? description,
  String? errorDescription,
}) {
  if (customDevicesConfig.devices.isEmpty) {
    throwToolExit(
      '''
No devices found in config at "${customDevicesConfig.configPath}"

${errorDescription ?? 'Before you can select a device, you need to add one first.'}
''',
    );
  }

  if (description != null) {
    logger.info(description);
    logger.spaces();
  }

  final devices = {
    for (var e in customDevicesConfig.devices) '${e.id} : ${e.label}': e
  };

  final selectedTarget = selectIndex(
    title ?? 'Select a target device',
    options: devices.keys.toList(),
  );

  final deviceKey = devices.keys.elementAt(selectedTarget);

  final selectedDevice = devices[deviceKey];

  if (selectedDevice == null) {
    throwToolExit(
        'Couldn\'t find device with id "${selectedDevice!.id}" in config at "${customDevicesConfig.configPath}"');
  }

  return selectedDevice;
}