Skip to content

Commit

Permalink
fix possibility to add duplicated device ID
Browse files Browse the repository at this point in the history
  • Loading branch information
payam-zahedi committed Nov 8, 2023
1 parent 4df32a0 commit a484a52
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions lib/commands/add_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import 'package:snapp_debugger/utils/common.dart';
import 'package:snapp_debugger/utils/flutter_sdk.dart';

/// Add a new raspberry device to the Flutter SDK custom devices
///
///
// TODO: add get platform for example: x64 or arm64
class AddCommand extends BaseDebuggerCommand {
AddCommand({
required this.flutterSdkManager,
Expand Down Expand Up @@ -55,12 +58,15 @@ class AddCommand extends BaseDebuggerCommand {
prompt:
'Please enter the id you want to device to have. Must contain only alphanumeric or underscore characters. (example: pi)',
validator: (s) {
if (RegExp(r'^\w+$').hasMatch(s)) {
return true;
if (!RegExp(r'^\w+$').hasMatch(s.trim())) {
throw ValidationError('Invalid input. Please try again.');
} else if (_isDuplicatedDeviceId(s.trim())) {
throw ValidationError('Device with this id already exists.');
}
throw ValidationError('Invalid input. Please try again.');

return true;
},
).interact();
).interact().trim();

printSpaces();

Expand Down Expand Up @@ -88,8 +94,6 @@ class AddCommand extends BaseDebuggerCommand {
},
).interact();

// TODO: add get platform for example x64 or arm64

final InternetAddress? targetIp = InternetAddress.tryParse(targetStr);
final bool useIp = targetIp != null;
final bool ipv6 = useIp && targetIp.type == InternetAddressType.IPv6;
Expand Down Expand Up @@ -248,4 +252,8 @@ class AddCommand extends BaseDebuggerCommand {
logger.printStatus(' ');
}
}

bool _isDuplicatedDeviceId(String s) {
return customDevicesConfig.devices.any((element) => element.id == s);
}
}

0 comments on commit a484a52

Please sign in to comment.