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({ required bool local, required String config, }) async { final values = local ? [ { 'id': '', 'online': true, 'status': await api.local(config, {'op': 'status'}), }, ] : await api.list(); if (local) { final status = Map.from( values[0]['status']! as Map, ); values[0]['id'] = status['id']; } return values.map(ManagedAgent.fromMap).toList(growable: false); } Future call( bool local, String config, String agent, JsonMap operation, ) => local ? api.local(config, operation) : api.request(agent, operation); Future importAdmin(String path) => api.importAdmin(path); Future 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; } }