import { CliLeaf, CliStringInput } from '@alwaysai/alwayscli';
import { yesCliInput } from '../../cli-inputs';
import { appReleasePullComponent } from '../../components/release';
import { echo } from '../../util';

export const pullAppReleaseCliLeaf = CliLeaf({
  name: 'pull',
  description: 'Retrieve a release of your application',
  namedInputs: {
    yes: yesCliInput,
    project: CliStringInput({
      description: 'Project ID',
      required: true
    }),
    releaseHash: CliStringInput({
      description: 'Hash of a particular cloud app release'
    })
  },
  async action(_, opts) {
    const { yes, project, releaseHash } = opts;
    const retrievedReleaseHash = await appReleasePullComponent({
      yes,
      project,
      releaseHash
    });
    echo(
      `Downloaded ${
        retrievedReleaseHash
          ? `released application ${retrievedReleaseHash}`
          : 'most recently released application'
      } for project ${project} to ${process.cwd()}`
    );
  }
});
