129 lines
3.5 KiB
Dart
129 lines
3.5 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:podman/podman.dart';
|
|
|
|
Future<void> main(List<String> args) async {
|
|
if (args.isEmpty || args.contains('--help') || args.contains('-h')) {
|
|
_printUsage();
|
|
return;
|
|
}
|
|
|
|
final flags = args
|
|
.where((arg) => arg.startsWith('--'))
|
|
.toList(growable: false);
|
|
final positional = args
|
|
.where((arg) => !arg.startsWith('--'))
|
|
.toList(growable: false);
|
|
|
|
if (positional.isEmpty) {
|
|
stderr.writeln('Provide an OCI artifact reference to pull.');
|
|
_printUsage();
|
|
exitCode = 64;
|
|
return;
|
|
}
|
|
|
|
final artifactRef = positional.first;
|
|
final runPush = flags.contains('--push');
|
|
final cleanupMode = _readOption(flags, 'cleanup');
|
|
|
|
final retry = int.tryParse(_readOption(flags, 'retry') ?? '');
|
|
final retryDelay = _readOption(flags, 'retry-delay');
|
|
final tlsVerify = _parseOptionalBool(_readOption(flags, 'tls-verify'));
|
|
|
|
final client = PodmanClient();
|
|
try {
|
|
final pulled = await client.pullArtifact(
|
|
artifactRef,
|
|
options: ArtifactPullOptions(
|
|
retry: retry,
|
|
retryDelay: retryDelay,
|
|
tlsVerify: tlsVerify,
|
|
),
|
|
);
|
|
|
|
print('Pulled artifact: ${pulled.artifactDigest}');
|
|
|
|
final details = await client.inspectArtifact(artifactRef);
|
|
print('Name: ${details.name}');
|
|
print('Digest: ${details.digest}');
|
|
print('Manifest keys: ${details.manifest.keys.toList()..sort()}');
|
|
|
|
final allArtifacts = await client.listArtifacts();
|
|
print('Local artifact count: ${allArtifacts.length}');
|
|
|
|
if (runPush) {
|
|
final pushed = await client.pushArtifact(
|
|
artifactRef,
|
|
options: ArtifactPushOptions(
|
|
retry: retry,
|
|
retryDelay: retryDelay,
|
|
tlsVerify: tlsVerify,
|
|
),
|
|
);
|
|
print('Pushed artifact digest: ${pushed.artifactDigest}');
|
|
}
|
|
|
|
if (cleanupMode == 'single') {
|
|
final removed = await client.removeArtifact(
|
|
artifactRef,
|
|
ignoreMissing: true,
|
|
);
|
|
print('Removed (single): ${removed.artifactDigests}');
|
|
}
|
|
|
|
if (cleanupMode == 'batch') {
|
|
final removed = await client.removeArtifacts(
|
|
ArtifactRemoveOptions(artifacts: <String>[artifactRef], ignore: true),
|
|
);
|
|
print('Removed (batch): ${removed.artifactDigests}');
|
|
}
|
|
} finally {
|
|
await client.close();
|
|
}
|
|
}
|
|
|
|
String? _readOption(List<String> args, String name) {
|
|
final prefix = '--$name=';
|
|
for (final arg in args) {
|
|
if (arg.startsWith(prefix)) {
|
|
return arg.substring(prefix.length);
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
bool? _parseOptionalBool(String? input) {
|
|
if (input == null || input.isEmpty) {
|
|
return null;
|
|
}
|
|
|
|
final normalized = input.toLowerCase();
|
|
if (normalized == 'true' || normalized == '1' || normalized == 'yes') {
|
|
return true;
|
|
}
|
|
if (normalized == 'false' || normalized == '0' || normalized == 'no') {
|
|
return false;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
void _printUsage() {
|
|
print('''
|
|
Artifact workflow example
|
|
|
|
Usage:
|
|
dart run example/artifact_workflow_example.dart <artifact-ref> [options]
|
|
|
|
Examples:
|
|
dart run example/artifact_workflow_example.dart quay.io/groupware/policy:latest
|
|
dart run example/artifact_workflow_example.dart quay.io/groupware/policy:latest --cleanup=batch
|
|
|
|
Options:
|
|
--retry=<count> Retry count for pull/push
|
|
--retry-delay=<duration> Retry delay (for example: 2s)
|
|
--tls-verify=<bool> TLS verification for registry operations
|
|
--push Push artifact using its current reference
|
|
--cleanup=single|batch Remove pulled artifact after example run
|
|
--help, -h Show this help
|
|
''');
|
|
}
|