From fc308faa5bd3f57151ee975fb79872cf4e8ed88f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Thu, 25 Jan 2024 18:09:26 +0100 Subject: [PATCH] Fix old variables from project was getting prefixed with app for no reason --- app/Lib/CdEnvVariable.php | 51 +++++++++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 13 deletions(-) diff --git a/app/Lib/CdEnvVariable.php b/app/Lib/CdEnvVariable.php index 9857262..1bfd662 100644 --- a/app/Lib/CdEnvVariable.php +++ b/app/Lib/CdEnvVariable.php @@ -43,6 +43,13 @@ class CdEnvVariable implements \Stringable */ private bool $isDefault = false; + /** + * The variable was prefiexed with $k8sPrefix + * + * @var boolean + */ + private bool $wasPrefixed = false; + /** * Properties that can be accessed from the object. * @@ -88,8 +95,13 @@ class CdEnvVariable implements \Stringable public static function make( array $params ): self { + $wasPrefixed = Str::startsWith(Arr::get($params, 'key'), self::$k8sPrefix); self::removeVarNamePrefixes($params); - return new self(...$params); + $var = new self(...$params); + if ($wasPrefixed) { + $var->wasPrefixed(); + } + return $var; } /** @@ -108,7 +120,7 @@ class CdEnvVariable implements \Stringable } /** - * Return is default. + * Set is default. * * @return self */ @@ -118,6 +130,17 @@ class CdEnvVariable implements \Stringable return $this; } + /** + * Set was prefixed. + * + * @return self + */ + public function wasPrefixed(): self + { + $this->wasPrefixed = true; + return $this; + } + /** * Normalize var name * @@ -125,20 +148,23 @@ class CdEnvVariable implements \Stringable */ public function getFormatedGitlabKey(): string { + $key = $this->key; // * Format with wanted prefixes if ($this->isDefault) { - return sprintf( - '%s_%s_%s', - self::$k8sPrefix, + $key = sprintf( + '%s_%s', self::$prefix, - Str::upper($this->key) + $key ); } - return sprintf( - '%s_%s', - self::$k8sPrefix, - str::upper($this->key) - ); + if ($this->wasPrefixed) { + $key = sprintf( + '%s_%s', + self::$k8sPrefix, + $key + ); + } + return Str::upper($key); } // * PRIVATE @@ -177,8 +203,7 @@ class CdEnvVariable implements \Stringable $printStr = $printStr->append(\str_pad('value', $titlePad, pad_type: \STR_PAD_LEFT)) ->append(' => '); - $value = $this->masked ? - Str::mask($this->value, '*', 0, intval(ceil(strlen($this->value) * 0.65))) : $this->value; + $value = $this->masked ? '*********' : $this->value; $printStr = $printStr->append("`{$value}`")->newLine(); $printStr = $printStr->append(\str_pad('environment_scope', $titlePad, pad_type: \STR_PAD_LEFT))