From 3244b82f5754076da78d5f21ac6eab62736b32a3 Mon Sep 17 00:00:00 2001 From: Mohammad Niknab Date: Thu, 9 Feb 2023 13:12:43 +0000 Subject: [PATCH] added configFile option to all commands --- CHANGELOG.md | 4 ++++ src/Command/AbstractCommand.php | 12 ++++++++++++ src/Command/Field.php | 3 +++ src/Settings/Settings.php | 26 ++++++++++++++++++++------ 4 files changed, 39 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 01aaf0c..398fc32 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ CHANGELOG ========= +6.29.3 +----- +* Added configFile option to all commands. + 6.29.2 ----- * Fixed getEntries in certain conditions on mailLists. diff --git a/src/Command/AbstractCommand.php b/src/Command/AbstractCommand.php index 61af297..fdb8dd5 100644 --- a/src/Command/AbstractCommand.php +++ b/src/Command/AbstractCommand.php @@ -43,10 +43,22 @@ public function __construct(string $name = null) Field::FORMAT__DESC, 'json' ); + + // adds --configFile option to all commands + $this->addOption( + Field::CONFIG_FILE, + null, + InputOption::VALUE_OPTIONAL, + Field::CONFIG_FILE__DESC, + Settings::getDefaultConfigFilePath() + ); } protected function initialize(InputInterface $input, OutputInterface $output): void { + $configFile = $input->getOption(Field::CONFIG_FILE); + Settings::setConfigFilePath($configFile); + if (!($this instanceof Setup)) { $isDemoMode = $input->getOption('demo'); diff --git a/src/Command/Field.php b/src/Command/Field.php index 7b1c3fc..91de2ea 100644 --- a/src/Command/Field.php +++ b/src/Command/Field.php @@ -358,4 +358,7 @@ class Field public const INCLUDE = 'include'; public const INCLUDE__DESC = 'Data to include in the response'; + + public const CONFIG_FILE = 'configFile'; + public const CONFIG_FILE__DESC = 'The config file path'; } diff --git a/src/Settings/Settings.php b/src/Settings/Settings.php index aa559c5..fdf13ab 100644 --- a/src/Settings/Settings.php +++ b/src/Settings/Settings.php @@ -12,12 +12,12 @@ class Settings { - private const CONFIG_DIR_NAME = '.config/transip-api'; - private const CONFIG_FILE_NAME = 'cli-config.json'; + private const DEFAULT_CONFIG_DIR_NAME = '.config/transip-api'; + private const DEFAULT_CONFIG_FILE_NAME = 'cli-config.json'; public const TRANSIP_API_ENDPOINT = 'https://api.transip.nl/v6'; - public const TRANSIP_CLI_VERSION = '6.29.2'; + public const TRANSIP_CLI_VERSION = '6.29.3'; /** * @var string @@ -49,6 +49,11 @@ class Settings */ private static $instance; + /** + * @var string + */ + private static $configFilePath; + private function initialise(): void { $configFilePath = self::getConfigFilePath(); @@ -122,13 +127,12 @@ public function getShowConfigFilePermissionWarning(): string public static function getConfigDir(): string { - $homeDirectory = Path::getHomeDirectory(); - return Path::join($homeDirectory, self::CONFIG_DIR_NAME); + return Path::getDirectory(self::getConfigFilePath()); } public static function getConfigFilePath(): string { - return Path::join(self::getConfigDir(), self::CONFIG_FILE_NAME); + return self::$configFilePath; } public function ensureConfigFileIsReadOnly(FormatterHelper $formatter, OutputInterface $output): void @@ -163,4 +167,14 @@ public function ensureConfigFileIsReadOnly(FormatterHelper $formatter, OutputInt $output->writeln($warning); $output->writeln(''); } + + public static function setConfigFilePath(string $configFile): void + { + self::$configFilePath = $configFile; + } + + public static function getDefaultConfigFilePath(): string + { + return Path::join(Path::getHomeDirectory(), self::DEFAULT_CONFIG_DIR_NAME, self::DEFAULT_CONFIG_FILE_NAME); + } }