55 lines
1.6 KiB
Dart
55 lines
1.6 KiB
Dart
import 'dart:io';
|
|
|
|
import 'agent_models.dart';
|
|
import 'agent_service.dart';
|
|
|
|
class AgentRepository {
|
|
const AgentRepository(this.api);
|
|
final AgentApi api;
|
|
String get connectionKey => api.connectionKey;
|
|
String redact(String text) => api.redact(text);
|
|
Future<List<ManagedAgent>> list({
|
|
required bool local,
|
|
required String config,
|
|
}) async {
|
|
final values = local
|
|
? [
|
|
<String, Object?>{
|
|
'id': '',
|
|
'online': true,
|
|
'status': await api.local(config, {'op': 'status'}),
|
|
},
|
|
]
|
|
: await api.list();
|
|
if (local) {
|
|
final status = Map<String, Object?>.from(
|
|
values[0]['status']! as Map<String, Object?>,
|
|
);
|
|
values[0]['id'] = status['id'];
|
|
}
|
|
return values.map(ManagedAgent.fromMap).toList(growable: false);
|
|
}
|
|
|
|
Future<Object?> call(
|
|
bool local,
|
|
String config,
|
|
String agent,
|
|
JsonMap operation,
|
|
) => local ? api.local(config, operation) : api.request(agent, operation);
|
|
Future<void> importAdmin(String path) => api.importAdmin(path);
|
|
Future<String> enrollment(String path) async {
|
|
final file = File(path);
|
|
if (await file.length() > 65536) {
|
|
throw const FormatException('凭据文件过大');
|
|
}
|
|
final line = (await file.readAsLines())
|
|
.where((v) => v.startsWith('ADS_AGENT_TOKEN='))
|
|
.firstOrNull;
|
|
final value = line?.substring(16).trim() ?? '';
|
|
if (value.length < 32) {
|
|
throw const FormatException('请选择包含 ADS_AGENT_TOKEN 的 agent.env');
|
|
}
|
|
return value;
|
|
}
|
|
}
|