Skip to content

Commit

Permalink
add interactive delete option
Browse files Browse the repository at this point in the history
  • Loading branch information
payam-zahedi committed Nov 8, 2023
1 parent 440655b commit e54850b
Showing 1 changed file with 44 additions and 3 deletions.
47 changes: 44 additions & 3 deletions lib/commands/delete_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import 'dart:async';

import 'package:flutter_tools/src/base/common.dart';
import 'package:interact/interact.dart';
import 'package:snapp_debugger/command_runner/command_runner.dart';
import 'package:snapp_debugger/commands/base_command.dart';

Expand All @@ -21,12 +22,20 @@ class DeleteCommand extends BaseDebuggerCommand {

@override
FutureOr<int>? run() {
if (!globalResults!.wasParsed(deviceIdOption)) {
missingRequiredOption();
/// check if the user has provided a device id with the -d option
if (globalResults!.wasParsed(deviceIdOption)) {
final deviceId = globalResults!.stringArg(deviceIdOption)!;

return _deleteDeviceWithId(deviceId);
}

final deviceId = globalResults!.stringArg(deviceIdOption)!;
/// if the user didn't provide a device id, then we will show an interactive
/// prompt to select a device to delete
return _interactiveDeleteDevice();
}

/// Delete device with id [deviceId] from the Flutter SDK
int _deleteDeviceWithId(String deviceId) {
if (!customDevicesConfig.contains(deviceId)) {
throwToolExit(
'Couldn\'t find device with id "$deviceId" in config at "${customDevicesConfig.configPath}"');
Expand All @@ -39,6 +48,38 @@ class DeleteCommand extends BaseDebuggerCommand {
return 0;
}

int _interactiveDeleteDevice() {
if (customDevicesConfig.devices.isEmpty) {
throwToolExit(
'''
No devices found in config at "${customDevicesConfig.configPath}"
Before you can delete a device, you need to add one first.
''',
);
}

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

final selectedDevice = Select(
prompt: 'Select a device to delete',
options: devices.keys.toList(),
).interact();

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

final deviceId = devices[deviceKey];

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

return _deleteDeviceWithId(deviceId);
}

void missingRequiredOption() {
usageException(
'''
Expand Down

0 comments on commit e54850b

Please sign in to comment.