From 0c1f06faceca102b533db1eb66b949d8402f3226 Mon Sep 17 00:00:00 2001 From: Constantin Graf Date: Mon, 2 Dec 2024 14:53:45 +0100 Subject: [PATCH] Change default generate key env to single line --- .../SelfHost/SelfHostGenerateKeysCommand.php | 15 +++++++++++---- .../SelfHostGenerateKeysCommandTest.php | 19 +++++++++++++++++-- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/app/Console/Commands/SelfHost/SelfHostGenerateKeysCommand.php b/app/Console/Commands/SelfHost/SelfHostGenerateKeysCommand.php index 3ca3d5ef..2147a8fa 100644 --- a/app/Console/Commands/SelfHost/SelfHostGenerateKeysCommand.php +++ b/app/Console/Commands/SelfHost/SelfHostGenerateKeysCommand.php @@ -18,6 +18,7 @@ class SelfHostGenerateKeysCommand extends Command */ protected $signature = 'self-host:generate-keys { --length=4096 : The length of the passport private key } + { --multi-line : Whether to output the keys in multiple lines } { --format=env : The format of the output (env, yaml) }'; /** @@ -34,6 +35,7 @@ public function handle(): int { $format = $this->option('format'); $key = RSA::createKey((int) $this->option('length')); + $multiLine = (bool) $this->option('multi-line'); $publicKey = (string) $key->getPublicKey(); $privateKey = (string) $key; @@ -41,12 +43,17 @@ public function handle(): int if ($format === 'env') { $this->line('APP_KEY="'.$appKey.'"'); - $this->line('PASSPORT_PRIVATE_KEY="'.$privateKey.'"'); - $this->line('PASSPORT_PUBLIC_KEY="'.$publicKey.'"'); + if ($multiLine) { + $this->line('PASSPORT_PRIVATE_KEY="'.Str::replace("\r\n", "\n", $privateKey).'"'); + $this->line('PASSPORT_PUBLIC_KEY="'.Str::replace("\r\n", "\n", $publicKey).'"'); + } else { + $this->line('PASSPORT_PRIVATE_KEY="'.Str::replace("\r\n", '\n', $privateKey).'"'); + $this->line('PASSPORT_PUBLIC_KEY="'.Str::replace("\r\n", '\n', $publicKey).'"'); + } } elseif ($format === 'yaml') { $this->line('APP_KEY: "'.$appKey.'"'); - $this->line("PASSPORT_PRIVATE_KEY: |\n ".Str::replace("\n", "\n ", $privateKey)); - $this->line("PASSPORT_PUBLIC_KEY: |\n ".Str::replace("\n", "\n ", $publicKey)); + $this->line("PASSPORT_PRIVATE_KEY: |\n ".Str::replace("\r\n", "\n ", $privateKey)); + $this->line("PASSPORT_PUBLIC_KEY: |\n ".Str::replace("\r\n", "\n ", $publicKey)); } else { $this->error('Invalid format'); diff --git a/tests/Unit/Console/Commands/SelfHost/SelfHostGenerateKeysCommandTest.php b/tests/Unit/Console/Commands/SelfHost/SelfHostGenerateKeysCommandTest.php index 1dfbcddc..92e10446 100644 --- a/tests/Unit/Console/Commands/SelfHost/SelfHostGenerateKeysCommandTest.php +++ b/tests/Unit/Console/Commands/SelfHost/SelfHostGenerateKeysCommandTest.php @@ -26,8 +26,23 @@ public function test_generates_app_key_and_passport_keys_per_default_in_env_form $this->assertSame(Command::SUCCESS, $exitCode); $output = Artisan::output(); $this->assertStringContainsString('APP_KEY="base64:', $output); - $this->assertStringContainsString('PASSPORT_PRIVATE_KEY="-----BEGIN PRIVATE KEY-----', $output); - $this->assertStringContainsString('PASSPORT_PUBLIC_KEY="-----BEGIN PUBLIC KEY-----', $output); + $this->assertStringContainsString('PASSPORT_PRIVATE_KEY="-----BEGIN PRIVATE KEY-----\n', $output); + $this->assertStringContainsString('PASSPORT_PUBLIC_KEY="-----BEGIN PUBLIC KEY-----\n', $output); + } + + public function test_generates_app_key_and_passport_keys_in_env_format_in_multiline_if_requested(): void + { + // Arrange + + // Act + $exitCode = $this->withoutMockingConsoleOutput()->artisan('self-host:generate-keys --multi-line'); + + // Assert + $this->assertSame(Command::SUCCESS, $exitCode); + $output = Artisan::output(); + $this->assertStringContainsString('APP_KEY="base64:', $output); + $this->assertStringContainsString("PASSPORT_PRIVATE_KEY=\"-----BEGIN PRIVATE KEY-----\n", $output); + $this->assertStringContainsString("PASSPORT_PUBLIC_KEY=\"-----BEGIN PUBLIC KEY-----\n", $output); } public function test_generates_app_key_and_passport_keys_in_yaml_format_if_requested(): void