Skip to content

Commit

Permalink
[ Edit ] applied some changes on the supported langs command
Browse files Browse the repository at this point in the history
  • Loading branch information
anasfik committed Sep 27, 2023
1 parent cbf257f commit 40cbd1c
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 68 deletions.
Binary file modified bin/langsync.exe
Binary file not shown.
7 changes: 7 additions & 0 deletions lib/src/commands/account_command/account_command.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:args/command_runner.dart';
import 'package:langsync/src/commands/account_command/sub_commands/auth_command.dart';
import 'package:langsync/src/commands/account_command/sub_commands/info_command.dart';
Expand All @@ -20,4 +22,9 @@ class AccountCommand extends Command<int> {
String get name => 'account';

final Logger _logger;

@override
FutureOr<int>? run() {
return super.run();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,10 @@ class SupportedLangsCommand extends Command<int> {
SupportedLangsCommand({
required this.logger,
}) {
argParser
..addOption(
'lang',
help: 'Check for a single language support.',
)
..addOption(
'langs',
help: 'Check for multiple languages support.',
);
argParser.addOption(
'langs',
help: 'Check for multiple languages support.',
);
}

final Logger logger;
Expand All @@ -32,20 +27,9 @@ class SupportedLangsCommand extends Command<int> {

@override
FutureOr<int>? run() async {
final langOption = argResults?['lang'] as String?;
final langsOption = argResults?['langs'] as String?;

if (langOption != null && langsOption != null) {
logger.err(
"Can't use both --lang and --langs at the same time, try again with only one of them.");
return ExitCode.usage.code;
}

if (langOption != null) {
final lang = langOption.toLowerCase();

return await _handleLangSupport(lang, _progressFor(lang));
} else if (langsOption != null) {
if (langsOption != null) {
final langs = langsOption.split(',').map((e) => e.trim()).toList();

return await _checkLangsSupport(langs);
Expand All @@ -63,63 +47,67 @@ class SupportedLangsCommand extends Command<int> {
return ExitCode.usage.code;
} else {
final langs = langPrompt.split(',').map((e) => e.trim()).toList();
if (langs.length == 1) {
return await _handleLangSupport(
langs.first,
_progressFor(langs.first),
);
} else {
return await _checkLangsSupport(langs);
}

return await _checkLangsSupport(langs);
}
}
}

Future<int> _handleLangSupport(String lang, Progress prog) async {
if (lang.split(',').length > 1) {
logger.err('Only one language is allowed with the --lang flag');
// Future<int> _handleLangSupport(String lang, Progress prog) async {
// if (lang.split(',').length > 1) {
// logger.err('Only one language is allowed with the --lang flag');

return ExitCode.usage.code;
}
// return ExitCode.usage.code;
// }

// try {
// prog.update('Checking language $lang support');

// final isSupported = await NetClient.instance.supportsLang(lang);
// if (isSupported) {
// prog.complete('Language $lang is supported.');
// } else {
// prog.complete('Language $lang is not supported.');
// }

// return ExitCode.success.code;
// } catch (e) {
// logger.customErr(
// error: e,
// progress: prog,
// update:
// 'Something went wrong while checking language $lang support, please try again.',
// );

// return ExitCode.software.code;
// }
// }

Future<int> _checkLangsSupport(List<String> langs) async {
final checkProgress =
logger.progress('Checking languages support for: $langs');

try {
prog.update('Checking language $lang support');
final langsCheckResponse = await NetClient.instance.supportsLang(langs);
checkProgress.complete('Done checking languages support.');

final isSupported = await NetClient.instance.supportsLang(lang);
if (isSupported) {
prog.complete('Language $lang is supported.');
} else {
prog.complete('Language $lang is not supported.');
for (final element in langs) {
if (langsCheckResponse[element] ?? false) {
logger.success('The language $element is supported.');
} else {
logger.err('The language $element is not supported.');
}
}

return ExitCode.success.code;
} catch (e) {
logger.customErr(
progress: checkProgress,
update: 'Something went wrong..',
error: e,
progress: prog,
update:
'Something went wrong while checking language $lang support, please try again.',
);

return ExitCode.software.code;
}
}

Future<int> _checkLangsSupport(List<String> langs) async {
final langsStatusCode = <int>{};

for (final lang in langs) {
langsStatusCode.add(
await _handleLangSupport(lang, _progressFor(lang)),
);
}

return langsStatusCode.contains(ExitCode.software.code)
? ExitCode.software.code
: ExitCode.success.code;
}

Progress _progressFor(String lang) {
return logger.customProgress('Checking language $lang support');
}
}
25 changes: 21 additions & 4 deletions lib/src/etc/networking/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,20 @@ class NetClient {
});
}

Future<bool> supportsLang(String lang) {
return _makeRes<bool>('langs/$lang', 'GET', {}, {}, (res) {
return res['isSupported'] as bool;
Future<Map<String, bool>> supportsLang(List<String> lang) {
return _makeRes<Map<String, bool>>('/langs-support', 'POST', {}, {
'langs': lang,
}, (res) {
final map = <String, bool>{};
print(res);

final list = (res['checkResultList'] as List<dynamic>).cast<String>();

for (final element in list) {
map[element] = true;
}

return map;
});
}

Expand All @@ -56,7 +67,13 @@ class NetClient {
final res = await request.send();
final asBytes = await res.stream.bytesToString();

return onSuccess(jsonDecode(asBytes) as Map<String, dynamic>);
final decoded = jsonDecode(asBytes) as Map<String, dynamic>;

if (res.statusCode >= 200 && res.statusCode < 300) {
return onSuccess(decoded);
} else {
throw Exception(decoded);
}
}

Future<T> _makeMultiPartRes<T>(
Expand Down
3 changes: 1 addition & 2 deletions lib/src/version.dart
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
// Generated code. Do not modify.
const packageVersion = '0.0.1';
const packageVersion = '0.0.3';
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: langsync
description: A Very Good Project created by Very Good CLI.
version: 0.0.1
version: 0.0.3
publish_to: none

environment:
Expand Down

0 comments on commit 40cbd1c

Please sign in to comment.