resolve static method

Future<CliContext> resolve(
  1. ArgResults options,
  2. Map<String, String> environment, {
  3. StringSink? out,
  4. StringSink? err,
})

Builds a context from parsed global options and the environment.

Throws CliException with an exit code of 64 (EX_USAGE) when neither a server nor a data directory is available — a usage error, not a runtime one, and worth distinguishing so CI can tell a misconfiguration from a failed publish.

Implementation

static Future<CliContext> resolve(
  ArgResults options,
  Map<String, String> environment, {
  StringSink? out,
  StringSink? err,
}) async {
  final serverUrl = options.option('server') ?? environment['OMNYSTORE_URL'];
  final dataDir = options.option('data') ?? environment['OMNYSTORE_DATA'];
  final token = options.option('token') ?? environment['OMNYSTORE_TOKEN'];
  final jsonOutput = options.flag('json');
  final quiet = options.flag('quiet');

  if (serverUrl != null && dataDir != null) {
    throw const CliException(
      'Pass either --server or --data, not both: they select different '
      'registries and there is no sensible way to combine them.',
      exitCode: 64,
    );
  }

  if (dataDir != null) {
    final store = await openLocalStore(dataDir);
    return CliContext(
      store: store,
      jsonOutput: jsonOutput,
      quiet: quiet,
      out: out,
      err: err,
      onClose: store.close,
    );
  }

  if (serverUrl == null) {
    throw const CliException(
      'No registry selected. Pass --server <url> to use a remote registry, '
      'or --data <dir> to use a local one; or set OMNYSTORE_URL / '
      'OMNYSTORE_DATA.',
      exitCode: 64,
    );
  }

  final client = OmnyStoreClient(
    baseUrl: serverUrl,
    auth: token == null
        ? const AnonymousAuthProvider()
        : TokenAuthProvider(token),
    userAgent: 'omnystore-cli/$omnyStoreVersion',
  );
  return CliContext(
    store: client,
    jsonOutput: jsonOutput,
    quiet: quiet,
    out: out,
    err: err,
    onClose: client.close,
  );
}