protected function interact(InputInterface $input, OutputInterface $output)
{
$instance = $input->getArgument('instance');
if (empty($instance)) {
// find instances based on tag(s)
$tags = $input->getOption('tag');
$tags = $this->convertTags($tags);
$repository = new Repository();
$instanceCollection = $repository->findEc2InstancesByTags($tags);
$count = count($instanceCollection);
if ($count == 0) {
throw new \Exception('No instance found matching the given tags');
} elseif ($count == 1) {
$instanceObj = $instanceCollection->getFirst();
/* @var $instanceObj Instance */
$input->setArgument('instance', $instanceObj->getInstanceId());
} else {
$mapping = [];
// dynamically add current tags
foreach (array_keys($tags) as $tagName) {
$mapping[$tagName] = 'Tags[?Key==`' . $tagName . '`].Value | [0]';
}
foreach ($input->getOption('column') as $tagName) {
$mapping[$tagName] = 'Tags[?Key==`' . $tagName . '`].Value | [0]';
}
$labels = [];
foreach ($instanceCollection as $instanceObj) {
/* @var $instanceObj Instance */
$instanceLabel = $instanceObj->getInstanceId();
$tmp = [];
foreach ($instanceObj->extractData($mapping) as $field => $value) {
if (!empty($value)) {
$tmp[] = "{$field}: {$value}";
}
}
if (count($tmp)) {
$labels[] = $instanceLabel . ' (' . implode('; ', $tmp) . ')';
} else {
$labels[] = $instanceLabel;
}
}
$helper = $this->getHelper('question');
$question = new ChoiceQuestion('Please select an instance', $labels);
$question->setErrorMessage('Instance %s is invalid.');
$instance = $helper->ask($input, $output, $question);
$output->writeln('Selected Instance: ' . $instance);
list($instance) = explode(' ', $instance);
$input->setArgument('instance', $instance);
}
}
if (!$input->getOption('force')) {
$helper = $this->getHelper('question');
$question = new ConfirmationQuestion("Are you sure you want to terminate following instance? {$instance} [y/N] ", false);
if (!$helper->ask($input, $output, $question)) {
throw new \Exception('Operation aborted');
}
$input->setOption('force', true);
}
}