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 updateService = locator<UpdateService>();
  final progress = logger.progress('Checking for updates...');

  final latestVersion = await updateService.getLatestVersion();

  if (latestVersion == null) {
    progress.fail(
        'Failed to check for updates. Please check your internet connection.');
    return 1;
  }

  if (!updateService.isNewerVersion(packageVersion, latestVersion)) {
    progress
        .complete('flo_cli is already up to date (version $packageVersion).');
    return 0;
  }

  progress.update('Updating flo_cli to version $latestVersion...');

  try {
    final result =
        await Process.run('dart', ['pub', 'global', 'activate', 'flo_cli']);

    if (result.exitCode == 0) {
      progress.complete(
          'Successfully updated flo_cli to version $latestVersion!');
      return 0;
    } else {
      progress.fail('Failed to update flo_cli.');
      logger.error(result.stderr as String);
      return result.exitCode;
    }
  } catch (e) {
    progress.fail('Failed to execute update command.');
    logger.error(e.toString());
    return 1;
  }
}