run method

  1. @override
int run()
override

Runs this command.

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

Implementation

@override
int run() {
  final rest = argResults!.rest;
  if (rest.length != 1) {
    return _error(
      ExitReporter.usageError,
      'MISSING_PLAN_ID',
      'Expected exactly one plan ID.',
    );
  }
  final plan = planStore.loadPlan(rest.single);
  if (plan == null) {
    return _error(
      ExitReporter.planNotFoundOrStateForbids,
      'PLAN_NOT_FOUND',
      'Change Plan "${rest.single}" was not found.',
    );
  }
  if (!PlanState.canTransition(plan.state, PlanState.discarded)) {
    return _error(
      ExitReporter.planNotFoundOrStateForbids,
      'PLAN_STATE_FORBIDS_DISCARD',
      'Plan state ${plan.state.name} forbids discard.',
    );
  }

  final discarded = ChangePlan.fromJson({
    ...plan.toJson(),
    'state': PlanState.discarded.name,
  });
  planStore.savePlan(discarded);
  final location = planStore.loadPlanLocation(plan.planId);
  if (location != null) releaseStagingCallback(location.stagingRoot);

  if (_json) {
    out(
      renderEnvelope(
        command: 'plan discard',
        ok: true,
        exitCode: ExitReporter.success,
        result: {
          'planId': plan.planId,
          'state': PlanState.discarded.name,
          'stagingReleased': location != null,
        },
      ),
    );
  } else {
    out('Discarded ${plan.planId}; staging released; destination untouched.');
  }
  return ExitReporter.success;
}