Skip to content

Commit c08c450

Browse files
committed
Write to env file if prompted for configuration
1 parent 760bd43 commit c08c450

File tree

3 files changed

+84
-3
lines changed

3 files changed

+84
-3
lines changed

config/cachet.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,5 +116,5 @@
116116
| Support using an alternative database connection, defaults to default connecton of application
117117
|
118118
*/
119-
'database_connection' => env('CACHET_DB_CONNECTION'),
119+
'database_connection' => env('CACHET_DB_CONNECTION', 'default'),
120120
];

src/Commands/InstallCommand.php

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
use Cachet\Settings\AppSettings;
77
use Cachet\Settings\Attributes\Description;
88
use Illuminate\Console\Command;
9+
use Illuminate\Support\Facades\File;
910
use Illuminate\Support\Sleep;
11+
use Illuminate\Support\Str;
1012
use ReflectionClass;
1113
use ReflectionProperty;
1214
use function Laravel\Prompts\confirm;
@@ -48,7 +50,67 @@ public function handle(AppSettings $settings)
4850

4951
protected function configureEnvironmentSettings(): void
5052
{
51-
//@todo configure environment variables inside cachet.php
53+
$path = text(
54+
'Which path do you want Cachet to be accessible from?',
55+
default: config('cachet.path')
56+
);
57+
58+
$title = text(
59+
'What will the title of your status page be?',
60+
default: config('cachet.title')
61+
);
62+
63+
$connection = text(
64+
'Which database connection do you wish to use for Cachet?',
65+
default: config('cachet.database_connection')
66+
);
67+
68+
$beacon = confirm(
69+
'Do you wish to send anonymous data to cachet to help us understand how Cachet is used?',
70+
default: config('cachet.beacon')
71+
);
72+
73+
$this->writeEnv([
74+
'CACHET_PATH' => $path,
75+
'CACHET_TITLE' => $title,
76+
'CACHET_DB_CONNECTION' => $connection,
77+
'CACHET_BEACON' => $beacon,
78+
]);
79+
}
80+
81+
protected function writeEnv(array $values): void
82+
{
83+
$environmentFile = app()->environmentFile();
84+
$environmentPath = app()->environmentPath();
85+
$fullPath = $environmentPath . '/' . $environmentFile;
86+
87+
$envFileContents = File::lines($fullPath)->collect();
88+
89+
foreach ($values as $key => $value) {
90+
$existingKey = $envFileContents->search(function ($line) use ($key) {
91+
return stripos($line, $key) !== false;
92+
});
93+
94+
if (Str::contains($value, ' ')) {
95+
$value = Str::wrap($value,'"');
96+
}
97+
98+
if ($value === true) {
99+
$value = 'true';
100+
}
101+
102+
if ($value === false) {
103+
$value = 'false';
104+
}
105+
106+
if ($existingKey === false) {
107+
$envFileContents->push($key . '=' . $value);
108+
} else {
109+
$envFileContents->put($existingKey, $key . '=' . $value);
110+
}
111+
}
112+
113+
File::put($fullPath, $envFileContents->implode("\n"));
52114
}
53115

54116
protected function configureDatabaseSettings(AppSettings $settings): AppSettings

tests/Unit/Commands/InstallCommandTest.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Tests\Unit\Commands;
44

55
use Cachet\Settings\AppSettings;
6+
use Illuminate\Support\Facades\File;
67

78
it('runs install command successfully without configuration', function () {
89
$this->artisan('cachet:install')
@@ -13,11 +14,17 @@
1314
->assertSuccessful();
1415
});
1516

16-
it('updates app settings when configuration is passed', function () {
17+
it('updates app settings and config filewhen configuration is passed', function () {
18+
File::copy(base_path('.env.example'), base_path('.env'));
19+
1720
$this->artisan('cachet:install')
1821
->expectsOutputToContain('Welcome to the Cachet installer!')
1922
->expectsConfirmation('Do you want to configure Cachet before installing?', 'yes')
2023
->expectsOutputToContain('Configuring Cachet...')
24+
->expectsQuestion('Which path do you want Cachet to be accessible from?', '/status')
25+
->expectsQuestion('What will the title of your status page be?', 'Laravel Envoyer')
26+
->expectsQuestion('Which database connection do you wish to use for Cachet?', 'default')
27+
->expectsQuestion('Do you wish to send anonymous data to cachet to help us understand how Cachet is used?', true)
2128
->expectsQuestion('What is the name of your application?', 'Laravel Envoyer')
2229
->expectsQuestion('What is your application about?', 'Zero downtime deployment tool.')
2330
->expectsConfirmation('Do you want to show support for Cachet?', 'yes')
@@ -32,6 +39,14 @@
3239
->expectsOutputToContain('Cachet is installed ⚡')
3340
->assertSuccessful();
3441

42+
$envContent = file_get_contents(base_path('.env'));
43+
44+
expect($envContent)
45+
->toContain('CACHET_PATH=/status')
46+
->toContain('CACHET_TITLE="Laravel Envoyer"')
47+
->toContain('CACHET_DB_CONNECTION=default')
48+
->toContain('CACHET_BEACON=true');
49+
3550
$settings = app(AppSettings::class);
3651
expect($settings->name)->toBe('Laravel Envoyer')
3752
->and($settings->about)->toBe('Zero downtime deployment tool.')
@@ -43,4 +58,8 @@
4358
->and($settings->refresh_rate)->toBe(10)
4459
->and($settings->dashboard_login_link)->toBeFalse()
4560
->and($settings->major_outage_threshold)->toBe(50);
61+
});
62+
63+
afterAll(function() {
64+
File::delete(base_path('.env'));
4665
});

0 commit comments

Comments
 (0)