Skip to content

Commit

Permalink
Merge pull request #1 from Snapp-Embedded/feat/delete-interactive
Browse files Browse the repository at this point in the history
Feat/delete interactive
  • Loading branch information
payam-zahedi authored Nov 8, 2023
2 parents d6e1ba2 + e54850b commit c8b46ff
Show file tree
Hide file tree
Showing 2 changed files with 45 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
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ dependencies:
file: ^6.1.4
async: ^2.11.0
collection: ^1.18.0
interact: ^2.2.0

dev_dependencies:
lints: ^2.0.0
Expand Down

0 comments on commit c8b46ff

Please sign in to comment.