From 49095c04bf386e4853835c1688fcbeb27eea3ebb Mon Sep 17 00:00:00 2001 From: Bottlenose Creative <30525951+bottlenosecreative@users.noreply.github.com> Date: Wed, 20 May 2020 13:12:24 +0800 Subject: [PATCH 1/8] Update composer.json --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 2bff4a1..1b258e0 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { - "name": "imliam/laravel-env-set-command", - "description": "Set a .env file variable from the command line", + "name": "bottlenosecreative/laravel-env-set-command", + "description": "Set a .env file variable from the command line - forked from imliam", "keywords": [ "imliam", "laravel-env-set-command", From bd09b9b20d68401e1820e29cd0d522d96c0cc3a8 Mon Sep 17 00:00:00 2001 From: Michael Dolphin Date: Wed, 20 May 2020 15:29:36 +0800 Subject: [PATCH 2/8] Comment out existing key rather than outright replacing it. Insert new value on new line. Include timecode when it was changed. --- composer.json | 3 ++- src/EnvironmentSetCommand.php | 9 +++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 1b258e0..cdef98a 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,8 @@ } ], "require": { - "php": "^7.1" + "php": "^7.1", + "nesbot/carbon": "^2.0" }, "autoload": { "psr-4": { diff --git a/src/EnvironmentSetCommand.php b/src/EnvironmentSetCommand.php index 1ae04dd..171fe31 100644 --- a/src/EnvironmentSetCommand.php +++ b/src/EnvironmentSetCommand.php @@ -49,7 +49,13 @@ public function handle() $contents = file_get_contents($envFilePath); if ($oldValue = $this->getOldValue($contents, $key)) { - $contents = str_replace("{$key}={$oldValue}", "{$key}={$value}", $contents); + // Comment out previous value and insert new line underneath + $date = \Carbon\Carbon::now()->toDateTimeString(); + $contents = str_replace("{$key}={$oldValue}", "#{$key}={$oldValue} # Edited on: {$date}\n{$key}={$value}\n", $contents); + // Fix double comments + $contents = str_replace("##{$key}", "#{$key}", $contents); + + // Store changes $this->writeFile($envFilePath, $contents); return $this->info("Environment variable with key '{$key}' has been changed from '{$oldValue}' to '{$value}'"); @@ -87,7 +93,6 @@ protected function getOldValue(string $envFile, string $key): string { // Match the given key at the beginning of a line preg_match("/^{$key}=[^\r\n]*/m", $envFile, $matches); - if (count($matches)) { return substr($matches[0], strlen($key) + 1); } From 9e4f796ffd868434afb3a604eaa02a5a4e6cc49a Mon Sep 17 00:00:00 2001 From: Michael Dolphin Date: Wed, 20 May 2020 16:03:20 +0800 Subject: [PATCH 3/8] Use regex to replace existing key/value to prevent editing previously commented out keys with a matching value. This is important so we don't destroy the datetimes in the previously commented values and also it fixes a bug with random line breaks in the env file. --- src/EnvironmentSetCommand.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/EnvironmentSetCommand.php b/src/EnvironmentSetCommand.php index 171fe31..e0df45c 100644 --- a/src/EnvironmentSetCommand.php +++ b/src/EnvironmentSetCommand.php @@ -49,11 +49,10 @@ public function handle() $contents = file_get_contents($envFilePath); if ($oldValue = $this->getOldValue($contents, $key)) { - // Comment out previous value and insert new line underneath + // Comment out previous value and insert new line underneath - preg_replace to get the "active" key not commented keys $date = \Carbon\Carbon::now()->toDateTimeString(); - $contents = str_replace("{$key}={$oldValue}", "#{$key}={$oldValue} # Edited on: {$date}\n{$key}={$value}\n", $contents); - // Fix double comments - $contents = str_replace("##{$key}", "#{$key}", $contents); + $replace = "#{$key}={$oldValue} # Edited: {$date}\n{$key}={$value}"; + $contents = preg_replace("/^{$key}={$oldValue}[^\r\n]*/m", $replace, $contents); // Store changes $this->writeFile($envFilePath, $contents); From 1a1ea534ffdccd0f98a35e6c0adaaa942691c1c9 Mon Sep 17 00:00:00 2001 From: bottlenosecreative Date: Wed, 20 May 2020 17:23:03 +0800 Subject: [PATCH 4/8] Implemented env variable to enable history. Removed Carbon dependancy --- composer.json | 3 +-- src/EnvironmentSetCommand.php | 7 ++++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/composer.json b/composer.json index cdef98a..1b258e0 100644 --- a/composer.json +++ b/composer.json @@ -20,8 +20,7 @@ } ], "require": { - "php": "^7.1", - "nesbot/carbon": "^2.0" + "php": "^7.1" }, "autoload": { "psr-4": { diff --git a/src/EnvironmentSetCommand.php b/src/EnvironmentSetCommand.php index e0df45c..7abe509 100644 --- a/src/EnvironmentSetCommand.php +++ b/src/EnvironmentSetCommand.php @@ -49,9 +49,10 @@ public function handle() $contents = file_get_contents($envFilePath); if ($oldValue = $this->getOldValue($contents, $key)) { - // Comment out previous value and insert new line underneath - preg_replace to get the "active" key not commented keys - $date = \Carbon\Carbon::now()->toDateTimeString(); - $replace = "#{$key}={$oldValue} # Edited: {$date}\n{$key}={$value}"; + + // Check if history is enabled, if so Comment out previous value and insert new line underneath + $date = new Date('y-m-d H:i:s'); + $replace = env('ENVSET_HISTORY', false) ? "#{$key}={$oldValue} # Edited: {$date}\n{$key}={$value}" : "{$key}={$value}"; $contents = preg_replace("/^{$key}={$oldValue}[^\r\n]*/m", $replace, $contents); // Store changes From 53774f1a42c2acea23c614258a5e5f42bfef7b1f Mon Sep 17 00:00:00 2001 From: Liam Hammett Date: Sat, 23 May 2020 14:05:07 +0100 Subject: [PATCH 5/8] Update history to work with rewrite --- src/EnvironmentSetCommand.php | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/src/EnvironmentSetCommand.php b/src/EnvironmentSetCommand.php index 8bc86c2..b3538b8 100644 --- a/src/EnvironmentSetCommand.php +++ b/src/EnvironmentSetCommand.php @@ -2,6 +2,7 @@ namespace ImLiam\EnvironmentSetCommand; +use Carbon\Carbon; use Illuminate\Console\Command; use Illuminate\Support\Facades\App; use Illuminate\Support\Str; @@ -76,10 +77,15 @@ public function handle(): void */ public function setEnvVariable(string $envFileContent, string $key, string $value): array { - // For existed key. + // For existing key. $oldKeyValuePair = $this->readKeyValuePair($envFileContent, $key); + if ($oldKeyValuePair !== null) { - return [str_replace($oldKeyValuePair, $key . '=' . $value, $envFileContent), false]; + $historyString = $this->getHistoryString($oldKeyValuePair); + + $updatedContent = preg_replace("/^{$oldKeyValuePair}[^\r\n]*/m", $historyString . $key . '=' . $value, $envFileContent); + + return [$updatedContent, false]; } // For a new key. @@ -98,14 +104,8 @@ public function setEnvVariable(string $envFileContent, string $key, string $valu public function readKeyValuePair(string $envFileContent, string $key): ?string { // Match the given key at the beginning of a line -<<<<<<< HEAD - preg_match("/^{$key}=[^\r\n]*/m", $envFile, $matches); - if (count($matches)) { - return substr($matches[0], strlen($key) + 1); -======= if (preg_match("#^ *{$key} *= *[^\r\n]*$#imu", $envFileContent, $matches)) { return $matches[0]; ->>>>>>> master } return null; @@ -188,4 +188,22 @@ protected function writeFile(string $path, string $contents): bool { return (bool)file_put_contents($path, $contents, LOCK_EX); } + + /** + * Add history to a file. + * + * @param string $keyValuePair + * + * @return string + */ + protected function getHistoryString(string $oldKeyValuePair = null): string + { + if (!env('ENVSET_HISTORY', false) || $oldKeyValuePair === null) { + // return ''; + } + + $date = Carbon::now()->format('Y-m-d H:i:s'); + + return "# {$oldKeyValuePair} # Edited: {$date}\n"; + } } From 29adfa20b4ca345787497465f67c2f770cfb8f0a Mon Sep 17 00:00:00 2001 From: Liam Hammett Date: Sat, 23 May 2020 14:16:12 +0100 Subject: [PATCH 6/8] Remove commented code --- src/EnvironmentSetCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/EnvironmentSetCommand.php b/src/EnvironmentSetCommand.php index b3538b8..be47477 100644 --- a/src/EnvironmentSetCommand.php +++ b/src/EnvironmentSetCommand.php @@ -199,7 +199,7 @@ protected function writeFile(string $path, string $contents): bool protected function getHistoryString(string $oldKeyValuePair = null): string { if (!env('ENVSET_HISTORY', false) || $oldKeyValuePair === null) { - // return ''; + return ''; } $date = Carbon::now()->format('Y-m-d H:i:s'); From 6b695cabbfc6be7585f79c5f6614d1ec4bb3e1d5 Mon Sep 17 00:00:00 2001 From: Liam Hammett Date: Sat, 23 May 2020 14:48:35 +0100 Subject: [PATCH 7/8] Escape special characters in the old pair --- src/EnvironmentSetCommand.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/EnvironmentSetCommand.php b/src/EnvironmentSetCommand.php index be47477..e9a32c9 100644 --- a/src/EnvironmentSetCommand.php +++ b/src/EnvironmentSetCommand.php @@ -83,6 +83,7 @@ public function setEnvVariable(string $envFileContent, string $key, string $valu if ($oldKeyValuePair !== null) { $historyString = $this->getHistoryString($oldKeyValuePair); + $oldKeyValuePair = preg_quote($oldKeyValuePair); $updatedContent = preg_replace("/^{$oldKeyValuePair}[^\r\n]*/m", $historyString . $key . '=' . $value, $envFileContent); return [$updatedContent, false]; From 41c4124f3edf69bfa4c154fa127dfa5005ad75b9 Mon Sep 17 00:00:00 2001 From: Liam Hammett Date: Sat, 23 May 2020 15:01:56 +0100 Subject: [PATCH 8/8] Add test for command history --- src/EnvironmentSetCommand.php | 2 +- tests/Unit/EnvironmentSetCommandTest.php | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/EnvironmentSetCommand.php b/src/EnvironmentSetCommand.php index e9a32c9..a6128f6 100644 --- a/src/EnvironmentSetCommand.php +++ b/src/EnvironmentSetCommand.php @@ -197,7 +197,7 @@ protected function writeFile(string $path, string $contents): bool * * @return string */ - protected function getHistoryString(string $oldKeyValuePair = null): string + public function getHistoryString(string $oldKeyValuePair = null): string { if (!env('ENVSET_HISTORY', false) || $oldKeyValuePair === null) { return ''; diff --git a/tests/Unit/EnvironmentSetCommandTest.php b/tests/Unit/EnvironmentSetCommandTest.php index 1b25eb5..42ad38c 100644 --- a/tests/Unit/EnvironmentSetCommandTest.php +++ b/tests/Unit/EnvironmentSetCommandTest.php @@ -2,6 +2,7 @@ namespace Tests\Unit; +use Carbon\Carbon; use ImLiam\EnvironmentSetCommand\EnvironmentSetCommand; use InvalidArgumentException; use PHPUnit\Framework\TestCase; @@ -85,6 +86,25 @@ public function testAssertKeyIsValid(string $key, bool $isGood): void $this->assertTrue($this->command->assertKeyIsValid($key)); } + /** + * @covers EnvironmentSetCommand::getHistoryString + */ + public function testHistoryString(): void + { + $this->assertEquals('', $this->command->getHistoryString('old_key=old_value')); + + putenv('ENVSET_HISTORY=true'); + + Carbon::setTestNow('2020-01-01 12:00:00'); + + $this->assertEquals( + "# old_key=old_value # Edited: 2020-01-01 12:00:00\n", + $this->command->getHistoryString('old_key=old_value') + ); + + putenv('ENVSET_HISTORY'); + } + /** * @return array * @see EnvironmentSetCommandTest::testSetEnvVariable