Skip to content

Commit

Permalink
Update SetEnvironmentVariable.php
Browse files Browse the repository at this point in the history
  • Loading branch information
levizoesch authored Aug 29, 2024
1 parent f122e28 commit c8f3309
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/SetEnvironmentVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@

class SetEnvironmentVariable extends Command
{
protected $signature = 'env:set {key} {value}';
protected $signature = 'env:set {key} {value} {--quotes}';
protected $description = 'Set an environment variable';

public function handle(): void
{
$key = $this->argument('key');
$value = $this->argument('value');

// Check if the --quotes flag is provided
if ($this->option('quotes')) {
$value = "\"$value\"";
}

$env[$key] = $value;
$this->setEnvValues($env);
$this->info("Environment variable [$key] set to [$value]");
Expand All @@ -33,7 +38,7 @@ private function setEnvValues(array $values): bool
if (count($values) > 0) {
foreach ($values as $envKey => $envValue) {
if ($this->isEnvKeySet($envKey, $envFileContents)) {
$envFileContents = preg_replace("/^{$envKey}=.*?[\s$]/m", "{$envKey}={$envValue}\n", $envFileContents);
$envFileContents = preg_replace("/^{$envKey}=.*?[\s$]/m", "{$envKey}={$envValue}", $envFileContents);

$this->info("Updated {$envKey} with new value in your `.env` file.");
} else {
Expand All @@ -59,4 +64,5 @@ private function isEnvKeySet(string $envKey, ?string $envFileContents = null): b

return (bool)preg_match("/^{$envKey}=.*?[\s$]/m", $envFileContents);
}

}

0 comments on commit c8f3309

Please sign in to comment.