101 lines
2.8 KiB
Dart
101 lines
2.8 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 a kube YAML file path.');
|
|
_printUsage();
|
|
exitCode = 64;
|
|
return;
|
|
}
|
|
|
|
final yamlPath = positional.first;
|
|
final yamlFile = File(yamlPath);
|
|
if (!await yamlFile.exists()) {
|
|
stderr.writeln('YAML file not found: $yamlPath');
|
|
exitCode = 66;
|
|
return;
|
|
}
|
|
|
|
final yaml = await yamlFile.readAsString();
|
|
final down = flags.contains('--down');
|
|
final force = flags.contains('--force');
|
|
|
|
final replace = flags.contains('--replace');
|
|
final serviceContainer = !flags.contains('--no-service-container');
|
|
final networkValues = _readMultiOptions(flags, 'network');
|
|
|
|
final client = PodmanClient();
|
|
try {
|
|
final report = down
|
|
? await client.playKubeDown(yaml, force: force)
|
|
: await client.playKube(
|
|
yaml,
|
|
options: PlayKubeOptions(
|
|
replace: replace,
|
|
serviceContainer: serviceContainer,
|
|
networks: networkValues,
|
|
),
|
|
);
|
|
|
|
print('Operation: ${down ? 'down' : 'up'}');
|
|
print('Pods: ${report.pods.isEmpty ? '(none)' : report.pods}');
|
|
print('Volumes: ${report.volumes.isEmpty ? '(none)' : report.volumes}');
|
|
print('Secrets: ${report.secrets.isEmpty ? '(none)' : report.secrets}');
|
|
print(
|
|
'Service container: ${report.serviceContainerId.isEmpty ? '(none)' : report.serviceContainerId}',
|
|
);
|
|
} finally {
|
|
await client.close();
|
|
}
|
|
}
|
|
|
|
List<String> _readMultiOptions(List<String> args, String name) {
|
|
final prefix = '--$name=';
|
|
final values = <String>[];
|
|
|
|
for (final arg in args) {
|
|
if (arg.startsWith(prefix)) {
|
|
final value = arg.substring(prefix.length);
|
|
if (value.isNotEmpty) {
|
|
values.add(value);
|
|
}
|
|
}
|
|
}
|
|
|
|
return values;
|
|
}
|
|
|
|
void _printUsage() {
|
|
print('''
|
|
Play kube example
|
|
|
|
Usage:
|
|
dart run example/play_kube_file_example.dart <kube-yaml-path> [options]
|
|
|
|
Examples:
|
|
dart run example/play_kube_file_example.dart ./stack.yaml --replace --network=groupware
|
|
dart run example/play_kube_file_example.dart ./stack.yaml --down --force
|
|
|
|
Options:
|
|
--down Tear down resources instead of creating them
|
|
--force Force delete on down operation
|
|
--replace Replace existing resources on up operation
|
|
--network=<name> Attach created pods to network (repeatable)
|
|
--no-service-container Disable service container on up operation
|
|
--help, -h Show this help
|
|
''');
|
|
}
|