-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdoctor_command.dart
More file actions
389 lines (345 loc) · 10.8 KB
/
doctor_command.dart
File metadata and controls
389 lines (345 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
import 'dart:io';
import 'package:args/command_runner.dart';
import 'package:devals/src/utils/env.dart';
import 'package:devals/src/utils/expand_home_dir.dart';
import 'package:howdy/howdy.dart';
/// The result status of a single doctor check.
enum CheckStatus { ok, warning, error }
/// The result of a single prerequisite check.
class CheckResult {
const CheckResult({
required this.status,
this.version,
this.message,
this.fix,
});
final CheckStatus status;
final String? version;
final String? message;
final String? fix;
}
/// A single prerequisite check to run.
class DoctorCheck {
const DoctorCheck({
required this.name,
required this.component,
required this.check,
this.isRequired = false,
});
final String name;
final String component;
final Future<CheckResult> Function() check;
final bool isRequired;
}
/// Typedef for a function that runs a process, enabling test injection.
typedef ProcessRunner =
Future<ProcessResult> Function(
String executable,
List<String> arguments,
);
/// Command that checks whether prerequisites are installed.
///
/// Similar to `flutter doctor`, this verifies the tools needed
/// for the CLI, eval_runner, and eval_explorer.
class DoctorCommand extends Command<int> {
DoctorCommand({ProcessRunner? processRunner})
: _runProcess = processRunner ?? Process.run;
final ProcessRunner _runProcess;
@override
String get name => 'doctor';
@override
String get description =>
'Check that all prerequisites are installed for '
'the CLI, eval_runner, and eval_explorer.';
@override
Future<int> run() async {
terminal.scrollClear();
terminal.writeln();
final checks = buildChecks(processRunner: _runProcess);
Text.body('devals doctor');
Text.body('Checking prerequisites...\n');
final results = <(DoctorCheck, CheckResult)>[];
for (final check in checks) {
final result = await check.check();
results.add((check, result));
_printResult(check, result);
}
terminal.writeln();
// Collect issues.
final issues = results.where((r) => r.$2.status != CheckStatus.ok).toList();
if (issues.isEmpty) {
Text.success('No issues found!\n');
return 0;
}
Text.warning('Issues found:\n');
for (final (check, result) in issues) {
final (icon, style) = switch (result.status) {
CheckStatus.error => (
'${Icon.error} ',
Theme.current.focused.errorMessage,
),
CheckStatus.warning => (
'${Icon.warning} ',
Theme.current.focused.warningMessage,
),
_ => ('', const TextStyle()),
};
terminal.writeln(' ${'$icon${check.name}'.style(style)}');
if (result.message != null) {
terminal.writeln(' ${result.message}');
}
if (result.fix != null) {
terminal.writeln(' Fix: ${result.fix}');
}
}
final hasErrors = issues.any((r) => r.$2.status == CheckStatus.error);
return hasErrors ? 1 : 0;
}
void _printResult(DoctorCheck check, CheckResult result) {
final (icon, style) = switch (result.status) {
CheckStatus.ok => (Icon.check, Theme.current.focused.successMessage),
CheckStatus.warning => (
Icon.warning,
Theme.current.focused.warningMessage,
),
CheckStatus.error => (Icon.error, Theme.current.focused.errorMessage),
};
final versionSuffix = result.version != null ? ' (${result.version})' : '';
final messageSuffix = result.message != null ? ' — ${result.message}' : '';
terminal.writeln(
' ${'$icon ${check.name}$versionSuffix$messageSuffix'.style(style)}',
);
}
}
// ---------------------------------------------------------------------------
// Check definitions
// ---------------------------------------------------------------------------
/// Builds the list of all doctor checks.
///
/// [processRunner] is injectable for testing.
List<DoctorCheck> buildChecks({ProcessRunner? processRunner}) {
final run = processRunner ?? Process.run;
return [
DoctorCheck(
name: 'Dart SDK',
component: 'CLI, eval_explorer',
isRequired: true,
check: () => _checkDart(run),
),
DoctorCheck(
name: 'Python',
component: 'eval_runner',
isRequired: true,
check: () => _checkPython(run),
),
DoctorCheck(
name: 'eval_runner installed',
component: 'eval_runner',
isRequired: true,
check: () => _checkEvalRunner(run),
),
DoctorCheck(
name: 'Podman',
component: 'eval_runner',
check: () => _checkPodman(run),
),
DoctorCheck(
name: 'Flutter SDK',
component: 'eval_explorer',
isRequired: true,
check: () => _checkFlutter(run),
),
DoctorCheck(
name: 'Serverpod CLI',
component: 'eval_explorer',
check: () => _checkServerpod(run),
),
DoctorCheck(
name: 'API keys',
component: 'eval_runner',
isRequired: true,
check: () => _checkApiKeys(),
),
DoctorCheck(
name: 'Publish config',
component: 'CLI (devals publish)',
check: () => _checkPublishConfig(),
),
];
}
/// Runs a command and returns the stdout, or `null` if it fails.
Future<String?> _tryRun(
ProcessRunner run,
String executable,
List<String> args,
) async {
try {
final result = await run(executable, args);
if (result.exitCode == 0) {
return (result.stdout as String).trim();
}
return null;
} on ProcessException {
return null;
}
}
/// Extracts a version number pattern (e.g. "3.10.1") from [text].
String? _extractVersion(String text) {
final match = RegExp(r'(\d+\.\d+[\.\d]*)').firstMatch(text);
return match?.group(1);
}
// -- Individual check implementations ----------------------------------------
Future<CheckResult> _checkDart(ProcessRunner run) async {
final output = await _tryRun(run, 'dart', ['--version']);
if (output == null) {
return const CheckResult(
status: CheckStatus.error,
message: 'not found',
fix: 'Install the Dart SDK: https://dart.dev/get-dart',
);
}
return CheckResult(status: CheckStatus.ok, version: _extractVersion(output));
}
Future<CheckResult> _checkPython(ProcessRunner run) async {
final output = await _tryRun(run, 'python3', ['--version']);
if (output == null) {
return const CheckResult(
status: CheckStatus.error,
message: 'not found',
fix: 'Install Python 3.13+: https://www.python.org/downloads/',
);
}
final version = _extractVersion(output);
if (version != null) {
final parts = version.split('.');
final major = int.tryParse(parts[0]) ?? 0;
final minor = parts.length > 1 ? (int.tryParse(parts[1]) ?? 0) : 0;
if (major < 3 || (major == 3 && minor < 13)) {
return CheckResult(
status: CheckStatus.error,
version: version,
message: 'Python 3.13+ required, found $version',
fix: 'Upgrade Python: https://www.python.org/downloads/',
);
}
}
return CheckResult(status: CheckStatus.ok, version: version);
}
Future<CheckResult> _checkEvalRunner(ProcessRunner run) async {
final output = await _tryRun(run, 'run-evals', ['--help']);
if (output == null) {
return const CheckResult(
status: CheckStatus.error,
message: 'not found',
fix: 'cd path/to/eval_runner && pip install -e .',
);
}
return const CheckResult(status: CheckStatus.ok);
}
Future<CheckResult> _checkPodman(ProcessRunner run) async {
final output = await _tryRun(run, 'podman', ['--version']);
if (output == null) {
return const CheckResult(
status: CheckStatus.warning,
message: 'not found (optional, needed for sandbox tasks)',
fix: 'Install Podman: https://podman.io/getting-started/installation',
);
}
return CheckResult(status: CheckStatus.ok, version: _extractVersion(output));
}
Future<CheckResult> _checkFlutter(ProcessRunner run) async {
final output = await _tryRun(run, 'flutter', ['--version']);
if (output == null) {
return const CheckResult(
status: CheckStatus.error,
message: 'not found',
fix: 'Install the Flutter SDK: https://flutter.dev/flow',
);
}
return CheckResult(status: CheckStatus.ok, version: _extractVersion(output));
}
Future<CheckResult> _checkServerpod(ProcessRunner run) async {
final output = await _tryRun(run, 'serverpod', ['version']);
if (output == null) {
return const CheckResult(
status: CheckStatus.error,
message: 'not found',
fix: 'dart pub global activate serverpod_cli',
);
}
return CheckResult(status: CheckStatus.ok, version: _extractVersion(output));
}
Future<CheckResult> _checkApiKeys() async {
const keys = ['GEMINI_API_KEY', 'ANTHROPIC_API_KEY', 'OPENAI_API_KEY'];
final env = loadEnv();
final present = keys.where((k) => env.containsKey(k));
final missing = keys.where((k) => !env.containsKey(k));
if (present.isEmpty) {
return const CheckResult(
status: CheckStatus.error,
message: 'no API keys found',
fix:
'Set at least one of: GEMINI_API_KEY, ANTHROPIC_API_KEY, OPENAI_API_KEY\n'
' Tip: add them to your .env file (see .env.example)',
);
}
if (missing.isNotEmpty) {
return CheckResult(
status: CheckStatus.warning,
message: '${present.join(', ')} set; ${missing.join(', ')} missing',
);
}
return CheckResult(
status: CheckStatus.ok,
message: 'all keys set',
);
}
Future<CheckResult> _checkPublishConfig() async {
final env = loadEnv();
final requiredKeys = [
EnvKeys.gcsBucket,
EnvKeys.gcpProjectId,
EnvKeys.googleApplicationCredentials,
];
final present = <String>[];
final missing = <String>[];
for (final key in requiredKeys) {
final value = env[key];
if (value != null && value.isNotEmpty) {
present.add(key);
} else {
missing.add(key);
}
}
if (present.isEmpty) {
return const CheckResult(
status: CheckStatus.warning,
message: 'not configured',
fix:
'cp .env.example .env and fill in GCS_BUCKET, '
'GCP_PROJECT_ID, GOOGLE_APPLICATION_CREDENTIALS',
);
}
// Check that credentials file actually exists
final credPath = env[EnvKeys.googleApplicationCredentials];
if (credPath != null && credPath.isNotEmpty) {
var resolvedPath = expandHomeDir(credPath);
if (!File(resolvedPath).existsSync()) {
return CheckResult(
status: CheckStatus.error,
message: 'credentials file not found: $credPath',
);
}
}
if (missing.isNotEmpty) {
return CheckResult(
status: CheckStatus.warning,
message: '${present.join(', ')} set; ${missing.join(', ')} missing',
fix: 'Set missing values in .env (see .env.example)',
);
}
return const CheckResult(
status: CheckStatus.ok,
message: 'all configured',
);
}