run method

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

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 creds = Credentials.load();
  if (creds == null) {
    AppLogger.info('Not logged in — nothing to do.');
    return;
  }

  if (Credentials.fromEnv() != null) {
    // load() short-circuited to the env var, so `creds` above says
    // nothing about whether a real file also exists — check separately.
    if (Credentials.hasSavedFile()) {
      Credentials.delete();
      AppLogger.info(
        'Using APPFLIGHT_API_KEY — that key was not revoked. Removed a '
        'leftover credentials file that was not the one in use.',
      );
    } else {
      AppLogger.info(
        'Using APPFLIGHT_API_KEY — that key was not revoked and there is '
        'no saved file to remove.',
      );
    }
    return;
  }

  try {
    final dio = ApiClient.instance.forKey(creds.apiKey);
    final result = await AuthService.ins.logout(apiDio: dio, creds: creds);

    if (result.outcome == LogoutOutcome.unresolvedInEnv) {
      AppLogger.error(
        'Could not revoke this key against the "${Endpoints.env}" '
        'environment (${result.detail ?? 'key not found'}). If you logged '
        'in with a different --env, retry logout with that one. '
        'Credentials were not removed.',
      );
      exit(ExitCodes.authRejected);
    }

    Credentials.delete();
    AppLogger.success(
      result.outcome == LogoutOutcome.revoked
          ? 'Key revoked and credentials removed.'
          : 'Credentials removed. (Key was already revoked or not found.)',
    );
  } on DioException catch (e) {
    AppLogger.error('Network error: ${e.message}');
    AppLogger.error(
      'Credentials were not removed — try again once you\'re back online.',
    );
    exit(ExitCodes.usage);
  }
}