main function

Future<void> main(
  1. List<String> argv
)

Runs the command tree over argv, mapping the exceptions a caller should read onto exit codes.

The mapping lives here rather than in each launcher, which is the other half of why this is a library. A consumer that wrote its own entry would copy these three catch clauses, pinned to the exceptions that existed the day it copied them — and the first one is the loudest error this tool has.

Call this from a process you are willing to lose, not in-process. The clauses below call exit(), which used to be unreachable because it lived in bin/; publishing it as a library puts it one import away, and an in-process caller — a wrapper running several commands, or a test that imports this — has its process killed on a ReleaseException rather than getting an exception or a code back. Inside a dart test isolate that takes down the runner.

Note the asymmetry with cux_buildnumber's lib/cli.dart, which sets exitCode and is import-safe. The import-safe shape here would be a Future<int> run(List<String>) holding the mapping, with exit only in main; it is not that today because changing how the collision path exits is a change to the loudest error on a release path, and it wants its own commit and its own mutation run rather than riding along with the move. lib/runner.dart and lib/exit_codes.dart already publish what a consumer needs to write that entry itself in the meantime.

Implementation

Future<void> main(List<String> argv) async {
  try {
    await buildRunner().run(argv);
  } on UploadCollisionException catch (e) {
    // **Caught before ReleaseException, which it extends.** One build number
    // naming two commits is the loudest error this tool has, and it is the one
    // most likely to be swallowed: a release wrapper calls the upload under
    // `|| exitCode=$?` on purpose, so that re-running a release for a build the
    // store already holds is a no-op. Without a code of its own, a collision
    // exits through that same path as the tolerable kind and the release
    // finishes green having published nothing and said so to nobody.
    stderr.writeln('cux_ship: ${e.message}');
    exit(uploadCollisionExit);
  } on ReleaseException catch (e) {
    // Something the caller should read and act on, not a bug. Printed rather
    // than thrown, because an unhandled Dart exception on a release path buries
    // the sentence that matters under a stack trace nobody needs.
    stderr.writeln('cux_ship: ${e.message}');
    exit(1);
  } on UsageException catch (e) {
    // Not a crash: a mistyped command or a missing required option. The usage
    // goes to stderr so that piping a `--print-*` subcommand's output somewhere
    // does not silently swallow the reason it produced nothing.
    stderr.writeln(e);
    exit(64); // EX_USAGE
  }
}