|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace Bref\Cli\Commands; |
| 4 | + |
| 5 | +use Bref\Cli\BrefCloudClient; |
| 6 | +use Bref\Cli\Cli\IO; |
| 7 | +use Exception; |
| 8 | +use Symfony\Component\Console\Input\InputArgument; |
| 9 | +use Symfony\Component\Console\Input\InputInterface; |
| 10 | +use Symfony\Component\Console\Input\InputOption; |
| 11 | +use Symfony\Component\Console\Output\OutputInterface; |
| 12 | +use Symfony\Component\Console\Question\ChoiceQuestion; |
| 13 | +use Symfony\Component\Console\Question\Question; |
| 14 | +use Symfony\Contracts\HttpClient\Exception\HttpExceptionInterface; |
| 15 | + |
| 16 | +class SecretCreate extends ApplicationCommand |
| 17 | +{ |
| 18 | + protected function configure(): void |
| 19 | + { |
| 20 | + $this |
| 21 | + ->setName('secret:create') |
| 22 | + ->setDescription('Create a secret for an environment') |
| 23 | + ->addArgument('name', InputArgument::OPTIONAL, 'The secret name') |
| 24 | + ->addArgument('value', InputArgument::OPTIONAL, 'The secret value') |
| 25 | + ->addOption('app', null, InputOption::VALUE_REQUIRED, 'The app name (if no config file exists)'); |
| 26 | + |
| 27 | + parent::configure(); |
| 28 | + } |
| 29 | + |
| 30 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 31 | + { |
| 32 | + // If --app and --team are provided, we can skip loading the config file |
| 33 | + if ($input->getOption('app') && $input->getOption('team')) { |
| 34 | + $appName = $input->getOption('app'); |
| 35 | + $team = $input->getOption('team'); |
| 36 | + $environment = $input->getOption('env'); |
| 37 | + } else { |
| 38 | + [ |
| 39 | + 'appName' => $appName, |
| 40 | + 'environmentName' => $environment, |
| 41 | + 'team' => $team, |
| 42 | + ] = $this->parseStandardOptions($input); |
| 43 | + |
| 44 | + // Override app name if --app option provided |
| 45 | + if ($input->getOption('app')) { |
| 46 | + $appName = $input->getOption('app'); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + // Get secret name (from argument or prompt) |
| 51 | + $name = $input->getArgument('name'); |
| 52 | + if (empty($name)) { |
| 53 | + $question = new Question('Secret name: '); |
| 54 | + $question->setValidator(function (?string $value): string { |
| 55 | + if (empty($value)) { |
| 56 | + throw new Exception('Secret name cannot be empty'); |
| 57 | + } |
| 58 | + return $value; |
| 59 | + }); |
| 60 | + $name = IO::ask($question); |
| 61 | + } |
| 62 | + |
| 63 | + // Get secret value (from argument or prompt) |
| 64 | + $value = $input->getArgument('value'); |
| 65 | + if ($value === null) { |
| 66 | + $question = new Question('Secret value: '); |
| 67 | + $question->setHidden(true)->setHiddenFallback(false); |
| 68 | + $value = IO::ask($question); |
| 69 | + } |
| 70 | + |
| 71 | + $brefCloud = new BrefCloudClient; |
| 72 | + |
| 73 | + // Resolve team slug to team ID |
| 74 | + $teams = $brefCloud->listTeams(); |
| 75 | + $teamId = null; |
| 76 | + foreach ($teams as $t) { |
| 77 | + if ($t['slug'] === $team) { |
| 78 | + $teamId = $t['id']; |
| 79 | + break; |
| 80 | + } |
| 81 | + } |
| 82 | + if ($teamId === null) { |
| 83 | + throw new Exception("Team '$team' not found"); |
| 84 | + } |
| 85 | + |
| 86 | + // Check if the environment exists and is deployed |
| 87 | + $awsAccountId = null; |
| 88 | + $region = null; |
| 89 | + try { |
| 90 | + $existingEnv = $brefCloud->findEnvironment($team, $appName, $environment); |
| 91 | + if (! empty($existingEnv['region'])) { |
| 92 | + // Environment is deployed, use its aws_account_id and region |
| 93 | + $awsAccountId = $existingEnv['aws_account_id']; |
| 94 | + $region = $existingEnv['region']; |
| 95 | + } |
| 96 | + } catch (HttpExceptionInterface $e) { |
| 97 | + if ($e->getResponse()->getStatusCode() !== 404) { |
| 98 | + throw $e; |
| 99 | + } |
| 100 | + IO::verbose('Environment not found: ' . $e->getMessage()); |
| 101 | + // Environment doesn't exist |
| 102 | + } |
| 103 | + |
| 104 | + if ($awsAccountId === null) { |
| 105 | + // Environment doesn't exist or isn't deployed, need to select AWS account and region |
| 106 | + $awsAccounts = $brefCloud->listAwsAccounts(); |
| 107 | + if (empty($awsAccounts)) { |
| 108 | + throw new Exception('No AWS accounts found. Please connect an AWS account first using "bref connect".'); |
| 109 | + } |
| 110 | + $awsAccountsByName = []; |
| 111 | + foreach ($awsAccounts as $account) { |
| 112 | + $awsAccountsByName[$account['name']] = $account['id']; |
| 113 | + } |
| 114 | + $awsAccountName = IO::ask(new ChoiceQuestion( |
| 115 | + 'Select the AWS account:', |
| 116 | + array_keys($awsAccountsByName), |
| 117 | + )); |
| 118 | + if (! is_string($awsAccountName)) { |
| 119 | + throw new Exception('No AWS account selected'); |
| 120 | + } |
| 121 | + $awsAccountId = $awsAccountsByName[$awsAccountName]; |
| 122 | + |
| 123 | + $region = IO::ask(new ChoiceQuestion( |
| 124 | + 'Select the AWS region:', |
| 125 | + BrefCloudClient::AWS_REGIONS, |
| 126 | + )); |
| 127 | + if (! is_string($region)) { |
| 128 | + throw new Exception('No region selected'); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + IO::spin('Creating secret'); |
| 133 | + $brefCloud->createSecret($teamId, $appName, $environment, $name, $value, $awsAccountId, $region); |
| 134 | + IO::spinSuccess('Secret created'); |
| 135 | + |
| 136 | + return 0; |
| 137 | + } |
| 138 | +} |
0 commit comments