From 14ba9fd6a4672b016b5c80e647f9b58f4e17c353 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 13 Oct 2024 12:25:19 +0200 Subject: [PATCH] Create command to return the value of an env var for current env --- .../src/Command/Config/ReadEnvVarCommand.php | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 module/CLI/src/Command/Config/ReadEnvVarCommand.php diff --git a/module/CLI/src/Command/Config/ReadEnvVarCommand.php b/module/CLI/src/Command/Config/ReadEnvVarCommand.php new file mode 100644 index 00000000..4eac16e4 --- /dev/null +++ b/module/CLI/src/Command/Config/ReadEnvVarCommand.php @@ -0,0 +1,58 @@ +setName(self::NAME) + ->setHidden() + ->setDescription('Display current value for an env var') + ->addArgument('envVar', InputArgument::REQUIRED, 'The env var to read'); + } + + protected function interact(InputInterface $input, OutputInterface $output): void + { + $io = new SymfonyStyle($input, $output); + $envVar = $input->getArgument('envVar'); + $validEnvVars = enumValues(EnvVars::class); + + if ($envVar === null) { + $envVar = $io->choice('Select the env var to read', $validEnvVars); + } + + if (! contains($envVar, $validEnvVars)) { + throw new InvalidArgumentException(sprintf('%s is not a valid Shlink environment variable', $envVar)); + } + + $input->setArgument('envVar', $envVar); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $envVar = $input->getArgument('envVar'); + $output->writeln(formatEnvVarValue(EnvVars::from($envVar)->loadFromEnv())); + + return ExitCode::EXIT_SUCCESS; + } +}