run method
Runs this command.
The return value is wrapped in a Future if necessary and returned by
CommandRunner.runCommand.
Implementation
@override
Future<void> run() async {
final args = argResults!;
final rest = args.rest;
if (rest.isEmpty) {
throw CliError(
'usage: preset apply <preset.json|preset-id> [<node>] '
'[--label env=prod | --all]',
);
}
// A path if it exists on disk, otherwise the id of a preset saved on the Hub.
// The saved form is the one worth using: a file is whatever copy *this*
// caller happens to have, while an id is the one everybody agrees on.
final source = rest.first;
final file = File(source);
final body = <String, Object?>{
if (file.existsSync())
'preset': jsonDecode(file.readAsStringSync())
else
'presetId': source,
};
final client = _apiClientFrom(args);
try {
final nodes = await _selectNodes(
client,
args,
positional: rest.skip(1).toList(),
);
final async = args['async'] as bool;
await _fanOut(nodes, (node) async {
final reply =
await client.post('/presets/apply', {
'nodeId': node,
...body,
if (async) 'async': true,
})
as Map;
if (async) return 'dispatched — ops show ${reply['id']}';
final results = (reply['results'] as List).cast<Map>();
final failed = results.where((r) => r['success'] != true).length;
final changed = results.where((r) => r['changed'] == true).length;
return failed == 0
? 'applied ${results.length} steps ($changed changed)'
: 'FAILED $failed/${results.length} steps';
});
} finally {
client.close();
}
}