run method

  1. @override
Future<int> run()
override

Runs this command.

The return value is wrapped in a Future if necessary and returned by CommandRunner.runCommand.

Implementation

@override
Future<int> run() async {
  final hubUrl = argResults!.option('hub') ?? environment['OMNYSTORE_HUB'];
  if (hubUrl == null) {
    throw const CliException(
      'Provide the hub endpoint: --hub wss://store.example.com/_node',
      exitCode: 64,
    );
  }
  final nodeId = argResults!.option('id');
  if (nodeId == null) {
    throw const CliException(
      'Provide this node\'s id: --id node-eu',
      exitCode: 64,
    );
  }
  final organizations = argResults!.multiOption('org').toSet();
  if (organizations.isEmpty) {
    throw const CliException(
      'A node must serve at least one organization: --org acme',
      exitCode: 64,
    );
  }

  final dataDir =
      globals.option('data') ??
      environment['OMNYSTORE_DATA'] ??
      '.omnystore-node';
  final logger = CliContext.loggerFor(verbose: !globals.flag('quiet'));

  final store = await CliContext.openLocalStore(
    dataDir,
    logger: logger,
    providerId: nodeId,
  );

  final node = OmnyStoreNode(
    hubUri: Uri.parse(hubUrl),
    nodeId: nodeId,
    store: store,
    organizations: organizations,
    publicBaseUrl: argResults!.option('public-url'),
    labels: _labels(argResults!.multiOption('label')),
    priority: int.tryParse(argResults!.option('priority') ?? '0') ?? 0,
    capacityBytes: int.tryParse(argResults!.option('capacity') ?? ''),
    logger: logger,
    authToken:
        argResults!.option('token') ?? environment['OMNYSTORE_NODE_TOKEN'],
  );

  await node.start();
  final quiet = globals.flag('quiet');
  if (!quiet) {
    out.writeln('OmnyStore node $omnyStoreVersion');
    out.writeln('  id:            $nodeId');
    out.writeln('  hub:           $hubUrl');
    out.writeln('  organizations: ${organizations.join(', ')}');
    out.writeln('  data:          $dataDir');
    out.writeln('Press Ctrl-C to stop.');
  }

  await _awaitInterrupt();
  // Draining first lets the hub stop placing new artifacts here while
  // downloads already in flight finish.
  if (!quiet) out.writeln('\nDraining and shutting down…');
  await node.drain();
  await node.close();
  return 0;
}