Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for local cache to at_cli #428

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion packages/at_cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,14 @@ The "plookup" verb, provides a proxied public lookups for a resolver that perhap

# Sample output
india
```
```

__sync__

Sync is not actually a verb, but allows you to pull a namespace to the
local cache to increase performance of batched requests.

```
# Requires both cache and authentication to be enabled
dart run bin/main.dart -v scan -r <namespace>
```
7 changes: 7 additions & 0 deletions packages/at_cli/bin/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,12 @@ Future<AtCliPreference> _getAtCliPreference(ArgResults? parsedArgs) async {
? parsedArgs['authKeyFile']
: ConfigUtil.getYaml()!['auth']['key_file_location']);

if (preferences.authRequired) {
preferences.cache =
parsedArgs['cache'] ?? ConfigUtil.getYaml()!['cache']['enabled'];

preferences.cacheDir =
parsedArgs['cache-dir'] ?? ConfigUtil.getYaml()!['cache']['directory'];
}
return preferences;
}
116 changes: 89 additions & 27 deletions packages/at_cli/lib/src/at_cli.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:convert';
import 'dart:io';
import 'dart:async';
import 'dart:typed_data';
import 'package:args/args.dart';
import 'package:at_cli/src/command_line_parser.dart';
Expand Down Expand Up @@ -72,9 +73,15 @@ class AtCli {
'Invalid command \n ${CommandLineParser.getUsage()}');
}
var command = builder.buildCommand();
result = await _atClientImpl!
.getRemoteSecondary()!
.executeCommand(command, auth: true);
if (atCliPreference.authRequired && atCliPreference.cache) {
result = await _atClientImpl!
.getLocalSecondary()!
.executeVerb(builder, sync: true);
} else {
result = await _atClientImpl!
.getRemoteSecondary()!
.executeCommand(command, auth: auth);
}
break;
case 'llookup':
var builder = LLookupVerbBuilder();
Expand All @@ -90,9 +97,15 @@ class AtCli {
'Invalid command \n ${CommandLineParser.getUsage()}');
}
var command = builder.buildCommand();
result = await _atClientImpl!
.getRemoteSecondary()!
.executeCommand(command, auth: true);
if (atCliPreference.authRequired && atCliPreference.cache) {
result = await _atClientImpl!
.getLocalSecondary()!
.executeVerb(builder, sync: true);
} else {
result = await _atClientImpl!
.getRemoteSecondary()!
.executeCommand(command, auth: auth);
}
break;
case 'lookup':
var builder = LookupVerbBuilder();
Expand All @@ -105,9 +118,15 @@ class AtCli {
'Invalid command \n ${CommandLineParser.getUsage()}');
}
var command = builder.buildCommand();
result = await _atClientImpl!
.getRemoteSecondary()!
.executeCommand(command, auth: true);
if (atCliPreference.authRequired && atCliPreference.cache) {
result = await _atClientImpl!
.getLocalSecondary()!
.executeVerb(builder, sync: true);
} else {
result = await _atClientImpl!
.getRemoteSecondary()!
.executeCommand(command, auth: auth);
}
break;
case 'plookup':
var builder = PLookupVerbBuilder();
Expand All @@ -120,9 +139,15 @@ class AtCli {
'Invalid command \n ${CommandLineParser.getUsage()}');
}
var command = builder.buildCommand();
result = await _atClientImpl!
.getRemoteSecondary()!
.executeCommand(command, auth: true);
if (atCliPreference.authRequired && atCliPreference.cache) {
result = await _atClientImpl!
.getLocalSecondary()!
.executeVerb(builder, sync: true);
} else {
result = await _atClientImpl!
.getRemoteSecondary()!
.executeCommand(command, auth: auth);
}
break;
case 'delete':
var builder = DeleteVerbBuilder();
Expand All @@ -137,9 +162,15 @@ class AtCli {
'Invalid command \n ${CommandLineParser.getUsage()}');
}
var command = builder.buildCommand();
result = await _atClientImpl!
.getRemoteSecondary()!
.executeCommand(command, auth: true);
if (atCliPreference.authRequired && atCliPreference.cache) {
result = await _atClientImpl!
.getLocalSecondary()!
.executeVerb(builder, sync: true);
} else {
result = await _atClientImpl!
.getRemoteSecondary()!
.executeCommand(command, auth: auth);
}
break;
case 'scan':
var builder = ScanVerbBuilder();
Expand All @@ -150,9 +181,27 @@ class AtCli {
throw Exception(
'Invalid command \n ${CommandLineParser.getUsage()}');
}
result = await _atClientImpl!
.getRemoteSecondary()!
.executeCommand(command, auth: auth);
if (atCliPreference.authRequired && atCliPreference.cache) {
result = await _atClientImpl!
.getLocalSecondary()!
.executeVerb(builder, sync: true);
} else {
result = await _atClientImpl!
.getRemoteSecondary()!
.executeCommand(command, auth: auth);
}
break;
case 'sync':
if (atCliPreference.authRequired && atCliPreference.cache) {
Completer<bool> completer = Completer();
var prefs = _atClientImpl!.getPreferences() ?? AtClientPreference();
prefs.namespace = arguments['regex'];
_atClientImpl!.setPreferences(prefs);
_atClientImpl!.syncService
.addProgressListener(ProgressListener(completer));
_atClientImpl!.syncService.sync();
result = (await completer.future).toString();
}
break;
}
return result;
Expand All @@ -170,14 +219,9 @@ class AtCli {
dynamic result;
try {
command = command + '\n';
if (isAuth) {
result = await _atClientImpl!
.getRemoteSecondary()!
.executeCommand(command, auth: true);
} else {
result =
await _atClientImpl!.getRemoteSecondary()!.executeCommand(command);
}
result = await _atClientImpl!
.getRemoteSecondary()!
.executeCommand(command, auth: isAuth);
} on Exception {
rethrow;
}
Expand Down Expand Up @@ -214,10 +258,28 @@ class AtCli {
AtClientPreference _getAtClientPreference(
String privateKey, AtCliPreference atCliPreference) {
var preference = AtClientPreference();
preference.isLocalStoreRequired = false;
preference.isLocalStoreRequired = atCliPreference.cache;
if (atCliPreference.cache) {
preference.hiveStoragePath = atCliPreference.cacheDir;
preference.commitLogPath = atCliPreference.cacheDir;
}

preference.privateKey = preference.rootDomain = atCliPreference.rootDomain;
preference.outboundConnectionTimeout = 60000;
preference.privateKey = privateKey;
return preference;
}
}

class ProgressListener extends SyncProgressListener {
final Completer<bool> completer;
ProgressListener(this.completer);

@override
void onSyncProgressEvent(SyncProgress syncProgress) {
if (syncProgress.syncStatus == SyncStatus.success ||
syncProgress.syncStatus == SyncStatus.failure) {
completer.complete(syncProgress.syncStatus == SyncStatus.success);
}
}
}
2 changes: 2 additions & 0 deletions packages/at_cli/lib/src/command_line_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class CommandLineParser {
abbr: 'w', help: 'atsign to whom key is shared');
parser.addOption('shared_by', abbr: 'b', help: 'atsign who shared the key');
parser.addOption('regex', abbr: 'r', help: 'regex for scan');
parser.addFlag('cache', help: 'enable storage cache');
parser.addOption('cache-dir', help: 'directory to store cache');

try {
if (arguments.isNotEmpty) {
Expand Down
4 changes: 4 additions & 0 deletions packages/at_cli/lib/src/config_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,9 @@ String defaultConfigYaml = YAMLWriter().write(
'key_file_location': '~/.atsign/keys/@alice.atKeys',
'at_sign': '@alice',
},
'cache': {
'enabled': false,
'directory': '~/.atsign/at_cli/storage/@alice',
}
},
);
2 changes: 2 additions & 0 deletions packages/at_cli/lib/src/preference.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ class AtCliPreference {
late String authMode;
late String authKeyFile;
String namespace = '';
bool cache = false;
String? cacheDir;
}