import 'dart:io'; import 'package:podman/podman.dart'; Future main(List args) async { if (args.contains('--help') || args.contains('-h')) { _printUsage(); return; } final runCheck = args.contains('--check'); final runPrune = args.contains('--prune'); final confirmPrune = args.contains('--yes'); final checkOptions = SystemCheckOptions( quick: args.contains('--quick'), repair: args.contains('--repair'), repairLossy: args.contains('--repair-lossy'), unreferencedLayerMaxAge: _readOption(args, 'max-age'), ); final pruneOptions = SystemPruneOptions( all: args.contains('--all'), volumes: args.contains('--volumes'), external: args.contains('--external'), build: args.contains('--build'), filters: _readFilters(args), ); final client = PodmanClient(); try { final df = await client.systemDf(); print('Disk usage snapshot'); print(' Images size: ${df.imagesSize} bytes'); print(' Images: ${df.images.length}'); print(' Containers: ${df.containers.length}'); print(' Volumes: ${df.volumes.length}'); if (runCheck) { final check = await client.systemCheck(options: checkOptions); print('System check completed'); print(' Errors detected: ${check.errors}'); print(' Layer findings: ${check.layers.length}'); print(' Image findings: ${check.images.length}'); print(' Container findings: ${check.containers.length}'); print(' Removed images: ${check.removedImages.length}'); print(' Removed containers: ${check.removedContainers.length}'); } if (runPrune && !confirmPrune) { stderr.writeln( 'Refusing to run prune without --yes. Add --yes to confirm destructive cleanup.', ); exitCode = 64; return; } if (runPrune && confirmPrune) { final prune = await client.systemPrune(options: pruneOptions); print('System prune completed'); print(' Reclaimed space: ${prune.reclaimedSpace} bytes'); print(' Pruned pods: ${prune.podIds.length}'); print(' Pruned containers: ${prune.containerIds.length}'); print(' Pruned images: ${prune.imageIds.length}'); print(' Pruned networks: ${prune.networkNames.length}'); print(' Pruned volumes: ${prune.volumeIds.length}'); } } finally { await client.close(); } } String? _readOption(List args, String name) { final prefix = '--$name='; for (final arg in args) { if (arg.startsWith(prefix)) { return arg.substring(prefix.length); } } return null; } Map> _readFilters(List args) { final prefix = '--filter='; final output = >{}; for (final arg in args) { if (!arg.startsWith(prefix)) { continue; } final raw = arg.substring(prefix.length); final index = raw.indexOf('='); if (index <= 0 || index == raw.length - 1) { continue; } final key = raw.substring(0, index); final value = raw.substring(index + 1); output.putIfAbsent(key, () => []).add(value); } return output; } void _printUsage() { print(''' System maintenance example Usage: dart run example/system_maintenance_example.dart [options] Behavior: - Always prints a system df snapshot - Optionally runs consistency check and/or prune Examples: dart run example/system_maintenance_example.dart --check --quick dart run example/system_maintenance_example.dart --prune --all --volumes --yes Options: --check Run system consistency checks --quick With --check, skip slower checks --repair With --check, remove inconsistent images --repair-lossy With --check, remove inconsistent containers/images --max-age= With --check, max age for unreferenced layers --prune Run system prune (destructive) --yes Required with --prune to confirm cleanup --all With --prune, include all unused images --volumes With --prune, include volumes --external With --prune, include external data --build With --prune, include build cache --filter= With --prune, add filter (repeatable) --help, -h Show this help '''); }