Skip to content

Commit 3347f68

Browse files
committed
Added grouping in pulled file and better access handling
1 parent c98fe52 commit 3347f68

File tree

5 files changed

+35
-26
lines changed

5 files changed

+35
-26
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nandi95/laravel-env-in-aws-ssm",
3-
"version": "0.1.1",
3+
"version": "0.1.2",
44
"description": "Manage your environment variables in AWS' SSM Parameter store",
55
"type": "library",
66
"license": "MIT",

composer.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Console/EnvPull.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use Exception;
66
use Illuminate\Console\Command;
7+
use Illuminate\Support\Collection;
8+
use Illuminate\Support\Str;
79
use Nandi95\LaravelEnvInAwsSsm\Traits\InteractsWithSSM;
810

911
class EnvPull extends Command
@@ -35,14 +37,16 @@ public function handle(): int
3537
{
3638
$this->stage = $this->argument('stage');
3739

38-
// todo group by the first part before `_` and add extra line in between in the output
39-
$resolvedEnv = $this->getEnvironmentVarsFromRemote()
40+
$resolvedEnv = '';
41+
42+
$this->getEnvironmentVarsFromRemote()
4043
->sortKeys()
41-
->reduce(function (string $resolvedGroup, string $val, string $key) {
42-
return $resolvedGroup . $key . '=' . $val . "\n";
43-
},
44-
''
45-
);
44+
->mapToGroups(function ($value, $key) {
45+
return [Str::before($key, '_') => $key . '=' . $value];
46+
})
47+
->each(static function (Collection $envs) use (&$resolvedEnv) {
48+
$resolvedEnv .= $envs->join("\n") . "\n\n";
49+
});
4650

4751
if (file_exists('.env.' . $this->stage)) {
4852
$this->backupEnvFile();

src/Console/EnvPush.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,13 @@ public function handle(): int
4848
$remoteKeysNotInLocal = $remoteEnvs->diffKeys($localEnvs);
4949

5050
// user deleted some keys, remove from remote too
51-
if ($remoteKeysNotInLocal->count()) {
51+
if ($remoteKeysNotInLocal->isNotEmpty()) {
52+
$this->info($remoteKeysNotInLocal->count() . ' variables found not present in .env.' . $this->stage . '. Deleting removed keys.');
53+
if ($localEnvs->isEmpty()) {
54+
$this->warn('There are no environment variables set locally.');
55+
$this->confirm('This will remove all variables in SSM, are you sure you want to proceed?');
56+
}
57+
5258
$qualifiedKeys = $remoteKeysNotInLocal
5359
->keys()
5460
->map(fn (string $key) => $this->qualifyKey($key))

src/Traits/InteractsWithSSM.php

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,48 +7,47 @@
77
use Exception;
88
use Illuminate\Support\Collection;
99
use Illuminate\Support\Str;
10-
use InvalidArgumentException;
1110
use Symfony\Component\Dotenv\Dotenv;
1211

1312
trait InteractsWithSSM
1413
{
1514
/**
1615
* The stage/environment of the app.
1716
*
18-
* @var string|null
17+
* @var string
1918
*/
20-
protected string|null $stage;
19+
protected string $stage;
2120

2221
/**
2322
* The name of the app.
2423
*
25-
* @var string|null
24+
* @var string
2625
*/
27-
private string|null $appName;
26+
private string $appName;
2827

2928
/**
3029
* The region we're operating in.
3130
*
32-
* @var string|null
31+
* @var string
3332
*/
34-
private string|null $region;
33+
private string $region;
3534

3635
/**
3736
* The Dotenv instance.
3837
*
39-
* @var Dotenv|null
38+
* @var Dotenv
4039
*/
41-
private Dotenv|null $dotEnv;
40+
private Dotenv $dotEnv;
4241

4342
/**
44-
* @var Credentials|null
43+
* @var Credentials
4544
*/
46-
private Credentials|null $credentials;
45+
private Credentials $credentials;
4746

4847
/**
49-
* @var SsmClient|null
48+
* @var SsmClient
5049
*/
51-
private SsmClient|null $client;
50+
private SsmClient $client;
5251

5352
/**
5453
* Get the parameter name as a qualified path
@@ -133,12 +132,12 @@ public function getRegion(): string
133132
if (file_exists('.env.' . $this->stage)) {
134133
$env = $this->getDotenv()->parse(file_get_contents('.env.' . $this->stage));
135134

136-
if ($env['AWS_DEFAULT_REGION']) {
135+
if (isset($env['AWS_DEFAULT_REGION'])) {
137136
$this->region = $env['AWS_DEFAULT_REGION'];
138137
}
139138
}
140139

141-
if (!$this->region) {
140+
if (!isset($this->region)) {
142141
$this->region = $this->ask('AWS Region');
143142
}
144143

@@ -180,7 +179,7 @@ public function getAppName(): string
180179
$appName = $this->getEnvironmentVarsFromFile()->get('APP_NAME');
181180
}
182181

183-
$this->appName = $appName ?? $this->ask('app name');
182+
$this->appName = $appName ?? $this->ask('App name');
184183

185184
return $this->appName;
186185
}

0 commit comments

Comments
 (0)