From 863cea1c24bf1bc20d2ca4ccf151588570bb6f59 Mon Sep 17 00:00:00 2001 From: gkc Date: Tue, 7 May 2024 12:32:12 +0100 Subject: [PATCH 1/3] feat: at_cli_commons 1.1.0 : CLIBase: add maxConnectAttempts parameter --- packages/at_cli_commons/CHANGELOG.md | 18 ++++++ packages/at_cli_commons/lib/src/cli_base.dart | 55 +++++++++++-------- packages/at_cli_commons/pubspec.yaml | 2 +- 3 files changed, 52 insertions(+), 23 deletions(-) diff --git a/packages/at_cli_commons/CHANGELOG.md b/packages/at_cli_commons/CHANGELOG.md index 8dd70c76..df317f4b 100644 --- a/packages/at_cli_commons/CHANGELOG.md +++ b/packages/at_cli_commons/CHANGELOG.md @@ -1,16 +1,34 @@ +## 1.1.0 + +- feat: Add `maxConnectAttempts` parameter to CLIBase. The default is 100, + which means 20 attempts to connect, with a 3-second delay between attempts. + In scripted situations, this is important as the previous behaviour (retry + forever) is usually not what is required. + ## 1.0.5 + - fix: Make CLIBase write progress messages to stderr, not stdout + ## 1.0.4 + - fix: handle malformed atsigns (no leading `@`) in CLIBase constructor - build: updated dependencies + ## 1.0.3 + - Added `example/` package, moved code samples from `bin/` to `example/` + ## 1.0.2 + - docs: Added some code samples in bin/ directory - docs: Added some class and method documentation to CLIBase - docs: Updated README - feat: Added static `fromCommandLineArgs` factory method to CLIBase + ## 1.0.1 + - Small edits to README + ## 1.0.0 + - Initial version. diff --git a/packages/at_cli_commons/lib/src/cli_base.dart b/packages/at_cli_commons/lib/src/cli_base.dart index 399cacd5..f13af35b 100644 --- a/packages/at_cli_commons/lib/src/cli_base.dart +++ b/packages/at_cli_commons/lib/src/cli_base.dart @@ -35,7 +35,11 @@ class CLIBase { help: 'Root Domain', defaultsTo: 'root.atsign.org') ..addFlag('verbose', abbr: 'v', negatable: false, help: 'More logging') - ..addFlag('never-sync', negatable: false, help: 'Do not run sync'); + ..addFlag('never-sync', negatable: false, help: 'Do not run sync') + ..addOption('max-connect-attempts', + help: 'Number of times to attempt to initially connect to atServer.' + ' Note: there is a 3-second delay between connection attempts.', + defaultsTo: "100"); /// Constructs a CLIBase from a list of command-line arguments /// and calls [init] on it. @@ -61,15 +65,17 @@ class CLIBase { } CLIBase cliBase = CLIBase( - atSign: parsedArgs['atsign'], - atKeysFilePath: parsedArgs['key-file'], - nameSpace: parsedArgs['namespace'], - rootDomain: parsedArgs['root-domain'], - homeDir: getHomeDirectory(), - storageDir: parsedArgs['storage-dir'], - verbose: parsedArgs['verbose'], - cramSecret: parsedArgs['cram-secret'], - syncDisabled: parsedArgs['never-sync']); + atSign: parsedArgs['atsign'], + atKeysFilePath: parsedArgs['key-file'], + nameSpace: parsedArgs['namespace'], + rootDomain: parsedArgs['root-domain'], + homeDir: getHomeDirectory(), + storageDir: parsedArgs['storage-dir'], + verbose: parsedArgs['verbose'], + cramSecret: parsedArgs['cram-secret'], + syncDisabled: parsedArgs['never-sync'], + maxConnectAttempts: int.parse(parsedArgs['max-connect-attempts']), + ); await cliBase.init(); @@ -85,6 +91,7 @@ class CLIBase { final String? downloadDir; final String? cramSecret; final bool syncDisabled; + final int maxConnectAttempts; late final String atKeysFilePathToUse; late final String localStoragePathToUse; @@ -111,17 +118,19 @@ class CLIBase { /// cliBase.logger.logger.level = Level.FINEST; /// ``` /// Throws an [IllegalArgumentException] if the parameters fail validation. - CLIBase( - {required String atSign, - required this.nameSpace, - required this.rootDomain, - this.homeDir, - this.verbose = false, - this.atKeysFilePath, - this.storageDir, - this.downloadDir, - this.cramSecret, - this.syncDisabled = false}) { + CLIBase({ + required String atSign, + required this.nameSpace, + required this.rootDomain, + this.homeDir, + this.verbose = false, + this.atKeysFilePath, + this.storageDir, + this.downloadDir, + this.cramSecret, + this.syncDisabled = false, + this.maxConnectAttempts = 5, + }) { this.atSign = AtUtils.fixAtSign(atSign); if (homeDir == null) { if (atKeysFilePath == null) { @@ -188,9 +197,11 @@ class CLIBase { bool authenticated = false; Duration retryDuration = Duration(seconds: 3); - while (!authenticated) { + int attempts = 0; + while (!authenticated && attempts < maxConnectAttempts) { try { stderr.write(chalk.brightBlue('\r\x1b[KConnecting ... ')); + attempts++; await Future.delayed(Duration( milliseconds: 1000)); // Pause just long enough for the retry to be visible diff --git a/packages/at_cli_commons/pubspec.yaml b/packages/at_cli_commons/pubspec.yaml index c9fa598d..6fdc7faa 100644 --- a/packages/at_cli_commons/pubspec.yaml +++ b/packages/at_cli_commons/pubspec.yaml @@ -1,6 +1,6 @@ name: at_cli_commons description: Library of useful stuff when building cli programs which use the AtClient SDK -version: 1.0.5 +version: 1.1.0 repository: https://github.com/atsign-foundation/at_libraries/tree/trunk/packages/at_cli_commons homepage: https://docs.atsign.com/ From 6dc9807d75c3d83344d7bc8f7a193687aa46190d Mon Sep 17 00:00:00 2001 From: gkc Date: Tue, 7 May 2024 12:52:53 +0100 Subject: [PATCH 2/3] fix: cli_base maxConnectAttempts --- packages/at_cli_commons/lib/src/cli_base.dart | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/at_cli_commons/lib/src/cli_base.dart b/packages/at_cli_commons/lib/src/cli_base.dart index f13af35b..f98e950b 100644 --- a/packages/at_cli_commons/lib/src/cli_base.dart +++ b/packages/at_cli_commons/lib/src/cli_base.dart @@ -214,6 +214,12 @@ class CLIBase { await Future.delayed(retryDuration); } } + if (!authenticated) { + stderr.writeln(); + var msg = 'Failed to connect after $attempts attempts'; + stderr.writeln(chalk.brightRed(msg)); + throw SecondaryServerConnectivityException(msg); + } stderr.writeln(chalk.brightGreen('Connected')); // Get the AtClient which the onboardingService just authenticated From 686fae42b91c3fe30323336025062b78e1251f4a Mon Sep 17 00:00:00 2001 From: gkc Date: Tue, 7 May 2024 13:23:07 +0100 Subject: [PATCH 3/3] feat: tweak defaults --- packages/at_cli_commons/CHANGELOG.md | 6 +++--- packages/at_cli_commons/lib/src/cli_base.dart | 5 +++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/at_cli_commons/CHANGELOG.md b/packages/at_cli_commons/CHANGELOG.md index df317f4b..00db432b 100644 --- a/packages/at_cli_commons/CHANGELOG.md +++ b/packages/at_cli_commons/CHANGELOG.md @@ -1,8 +1,8 @@ ## 1.1.0 -- feat: Add `maxConnectAttempts` parameter to CLIBase. The default is 100, - which means 20 attempts to connect, with a 3-second delay between attempts. - In scripted situations, this is important as the previous behaviour (retry +- feat: Add `maxConnectAttempts` parameter to CLIBase. The default is 20, + i.e. 20 attempts to connect, with a 3-second delay between attempts. When + used in scripts this is important, as the previous behaviour (retry forever) is usually not what is required. ## 1.0.5 diff --git a/packages/at_cli_commons/lib/src/cli_base.dart b/packages/at_cli_commons/lib/src/cli_base.dart index f98e950b..12313333 100644 --- a/packages/at_cli_commons/lib/src/cli_base.dart +++ b/packages/at_cli_commons/lib/src/cli_base.dart @@ -11,6 +11,7 @@ import 'package:logging/logging.dart'; import 'package:version/version.dart'; class CLIBase { + static const defaultMaxConnectAttempts = 20; /// An ArgParser which has all of the options and flags required by [CLIBase] /// Used by [fromCommandLineArgs] if the `parser` parameter isn't supplied. static final ArgParser argsParser = ArgParser() @@ -39,7 +40,7 @@ class CLIBase { ..addOption('max-connect-attempts', help: 'Number of times to attempt to initially connect to atServer.' ' Note: there is a 3-second delay between connection attempts.', - defaultsTo: "100"); + defaultsTo: defaultMaxConnectAttempts.toString()); /// Constructs a CLIBase from a list of command-line arguments /// and calls [init] on it. @@ -129,7 +130,7 @@ class CLIBase { this.downloadDir, this.cramSecret, this.syncDisabled = false, - this.maxConnectAttempts = 5, + this.maxConnectAttempts = defaultMaxConnectAttempts, }) { this.atSign = AtUtils.fixAtSign(atSign); if (homeDir == null) {