Skip to content

Commit cf37d1f

Browse files
Added the command description. Added Travis CI build status badge. Added support for using en external .env file when using "key=value" syntax. Merged with upstream/master.
1 parent 649cb5c commit cf37d1f

File tree

4 files changed

+114
-58
lines changed

4 files changed

+114
-58
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1111
### Security
1212

1313

14+
## [1.1.4] - 2020-05-13
15+
### Added
16+
- Added the command description.
17+
- Added Travis CI build status badge.
18+
- Added support for using en external `.env` file when using "key=value" syntax.
19+
### Changed
20+
- Merged with upstream/master.
21+
1422
## [1.1.3] - 2020-05-12
1523
### Security
1624
- roave/security-advisories finds laravel 5.3/5.4 not safe, so it is removed from the travis-ci config.
@@ -39,6 +47,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
3947
### Added
4048
- Initial release
4149

50+
[1.1.4]: https://github.com/imliam/laravel-env-set-command/compare/1.1.3...1.1.4
4251
[1.1.3]: https://github.com/imliam/laravel-env-set-command/compare/1.1.2...1.1.3
4352
[1.1.2]: https://github.com/imliam/laravel-env-set-command/compare/1.1.1...1.1.2
4453
[1.1.1]: https://github.com/imliam/laravel-env-set-command/compare/1.1.0...1.1.1

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
[![Latest Version on Packagist](https://img.shields.io/packagist/v/imliam/laravel-env-set-command.svg)](https://packagist.org/packages/imliam/laravel-env-set-command)
44
[![Total Downloads](https://img.shields.io/packagist/dt/imliam/laravel-env-set-command.svg)](https://packagist.org/packages/imliam/laravel-env-set-command)
55
[![License](https://img.shields.io/github/license/imliam/laravel-env-set-command.svg)](LICENSE.md)
6+
[![Build Status](https://travis-ci.com/imliam/laravel-env-set-command.svg?branch=master)](https://travis-ci.com/imliam/laravel-env-set-command)
67

78
Set a .env file variable from the command line.
89

@@ -64,6 +65,19 @@ $ php artisan env:set @pp_n@me Laravel
6465
# Invalid environment key. Only use letters and underscores
6566
```
6667

68+
You can specify the external `.env` file in the third optional argument.
69+
70+
```bash
71+
$ php artisan env:set APP_NAME TestApp /var/www/my_own_env.env
72+
# Environment variable with key 'APP_NAME' has been changed from 'Laravel' to 'TestApp'
73+
```
74+
75+
Or in the second parameter if you use key=value syntax.
76+
```bash
77+
$ php artisan env:set APP_NAME=TestApp /var/www/my_own_env.env
78+
# Environment variable with key 'APP_NAME' has been changed from 'Laravel' to 'TestApp'
79+
```
80+
6781
## Changelog
6882

6983
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

src/EnvironmentSetCommand.php

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ class EnvironmentSetCommand extends Command
2121
*/
2222
protected $signature
2323
= self::COMMAND_NAME
24-
. '{' . self::ARGUMENT_KEY . ' : Key or key=value pair}'
25-
. '{' . self::ARGUMENT_VALUE . '? : Value}'
26-
. '{' . self::ARGUMENT_ENV_FILE . '? : Optional path to the .env file}';
24+
. ' {' . self::ARGUMENT_KEY . ' : Key or "key=value" pair}'
25+
. ' {' . self::ARGUMENT_VALUE . '? : Value}'
26+
. ' {' . self::ARGUMENT_ENV_FILE . '? : Optional path to the .env file}';
2727

2828
/**
2929
* The console command description.
@@ -39,19 +39,23 @@ public function handle(): void
3939
{
4040
try {
4141
// Parse key and value arguments.
42-
[$key, $value] = $this->parseKeyValueArguments(
42+
[$key, $value, $envFilePath] = $this->parseCommandArguments(
4343
$this->argument(self::ARGUMENT_KEY),
44-
$this->argument(self::ARGUMENT_VALUE)
44+
$this->argument(self::ARGUMENT_VALUE),
45+
$this->argument(self::ARGUMENT_ENV_FILE)
4546
);
47+
48+
// Use system env file path if the argument env file path is not provided.
49+
$envFilePath = $envFilePath ?? App::environmentFilePath();
50+
$this->info("The following environment file is used: '" . $envFilePath . "'");
4651
} catch (InvalidArgumentException $e) {
4752
$this->error($e->getMessage());
4853
return;
4954
}
5055

51-
$envFilePath = realpath($this->argument(self::ARGUMENT_ENV_FILE) ?? App::environmentFilePath());
5256
$content = file_get_contents($envFilePath);
53-
5457
[$newEnvFileContent, $isNewVariableSet] = $this->setEnvVariable($content, $key, $value);
58+
5559
if ($isNewVariableSet) {
5660
$this->info("A new environment variable with key '{$key}' has been set to '{$value}'");
5761
} else {
@@ -102,25 +106,36 @@ public function readKeyValuePair(string $envFileContent, string $key): ?string
102106
}
103107

104108
/**
105-
* Determine what the supplied key and value is from the current command.
109+
* Parse key, value and path to .env-file from command line arguments.
106110
*
107-
* @param string $key
108-
* @param string|null $value
111+
* @param string $_key
112+
* @param string|null $_value
113+
* @param string|null $_envFilePath
109114
*
110-
* @return string[]
111-
* @throws InvalidArgumentException
115+
* @return string[] [string KEY, string value, ?string envFilePath].
112116
*/
113-
public function parseKeyValueArguments(string $key, ?string $value): array
117+
public function parseCommandArguments(string $_key, ?string $_value, ?string $_envFilePath): array
114118
{
119+
$key = null;
120+
$value = null;
121+
$envFilePath = null;
122+
115123
// Parse "key=value" key argument.
116-
if ($value === null) {
117-
$parts = explode('=', $key, 2);
118-
if (count($parts) !== 2) {
119-
$key = $parts[0];
120-
$value = '';
121-
} else {
122-
[$key, $value] = $parts;
124+
if (preg_match('#^([^=]+)=(.*)$#umu', $_key, $matches)) {
125+
[1 => $key, 2 => $value] = $matches;
126+
127+
// Use second argument as path to env file:
128+
if ($_value !== null) {
129+
$envFilePath = $_value;
123130
}
131+
} else {
132+
$key = $_key;
133+
$value = $_value;
134+
}
135+
136+
// If the path to env file is not set, use third argument or return null (default system path).
137+
if ($envFilePath === null) {
138+
$envFilePath = $_envFilePath;
124139
}
125140

126141
$this->assertKeyIsValid($key);
@@ -130,7 +145,7 @@ public function parseKeyValueArguments(string $key, ?string $value): array
130145
$value = '"' . $value . '"';
131146
}
132147

133-
return [strtoupper($key), $value];
148+
return [strtoupper($key), $value, ($envFilePath === null ? null : realpath($envFilePath))];
134149
}
135150

136151
/**

tests/Unit/EnvironmentSetCommandTest.php

Lines changed: 55 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,15 @@ public function testReadKeyValuePair(string $envFileContent, string $key, ?strin
5757
}
5858

5959
/**
60-
* @covers EnvironmentSetCommand::parseKeyValueArguments
60+
* @covers EnvironmentSetCommand::parseCommandArguments
6161
* @dataProvider parseKeyValueArgumentsDataProvider
6262
*
6363
* @param array $params
6464
* @param array $expectedResult
6565
*/
66-
public function testParseKeyValueArguments(array $params, array $expectedResult): void
66+
public function testParseCommandArguments(array $params, array $expectedResult): void
6767
{
68-
$result = $this->command->parseKeyValueArguments(...$params);
68+
$result = $this->command->parseCommandArguments(...$params);
6969
$this->assertEquals($expectedResult, $result);
7070
}
7171

@@ -219,81 +219,99 @@ public function assertKeyIsValidDataProvider(): array
219219

220220
/**
221221
* @return array
222-
* @see EnvironmentSetCommandTest::testParseKeyValueArguments
222+
* @see EnvironmentSetCommandTest::testParseCommandArguments
223223
*/
224224
public function parseKeyValueArgumentsDataProvider(): array
225225
{
226226
return [
227-
// Normal usage.
227+
// Normal syntax.
228228
[
229-
['SOME_KEY=some_value', null],
230-
['SOME_KEY', 'some_value'],
229+
['SOME_KEY', 'some_value', null],
230+
['SOME_KEY', 'some_value', null],
231231
],
232232
[
233-
['SOME_KEY=some_value', null],
234-
['SOME_KEY', 'some_value'],
233+
['SOME_KEY', 'some_value', __FILE__],
234+
['SOME_KEY', 'some_value', __FILE__],
235+
],
236+
// key=value syntax.
237+
[
238+
['SOME_KEY=some_value', null, null],
239+
['SOME_KEY', 'some_value', null],
240+
],
241+
[
242+
['SOME_KEY=some_value', __FILE__, null],
243+
['SOME_KEY', 'some_value', __FILE__],
244+
],
245+
[
246+
['SOME_KEY=some_value', __FILE__, 'ambiguous_third_parameter'],
247+
['SOME_KEY', 'some_value', __FILE__],
248+
],
249+
// Equals signs in the key=value parameter.
250+
[
251+
['SOME_KEY==some=value===', null, null],
252+
['SOME_KEY', '=some=value===', null],
235253
],
236254
// Test without neither value argument nor value in the key.
237255
[
238-
['some_key=', null],
239-
['SOME_KEY', ''],
256+
['some_key=', null, null],
257+
['SOME_KEY', '', null],
240258
],
241259
[
242-
['some_key', null],
243-
['SOME_KEY', ''],
260+
['some_key', null, null],
261+
['SOME_KEY', '', null],
244262
],
245263
// Test lowercase in the key.
246264
[
247-
['some_key=some_value', null],
248-
['SOME_KEY', 'some_value'],
265+
['some_key=some_value', null, null],
266+
['SOME_KEY', 'some_value', null],
249267
],
250268
[
251-
['some_key=some_value', null],
252-
['SOME_KEY', 'some_value'],
269+
['some_key=some_value', null, null],
270+
['SOME_KEY', 'some_value', null],
253271
],
254272
// Test double quotes in value.
255273
[
256-
['some_key="some_value"', null],
257-
['SOME_KEY', '"some_value"'],
274+
['some_key="some_value"', null, null],
275+
['SOME_KEY', '"some_value"', null],
258276
],
259277
[
260-
['some_key', '"some_value"'],
261-
['SOME_KEY', '"some_value"'],
278+
['some_key', '"some_value"', null],
279+
['SOME_KEY', '"some_value"', null],
262280
],
263281
// Test single quotes in value.
264282
[
265-
["some_key='some_value'", null],
266-
['SOME_KEY', "'some_value'"],
283+
["some_key='some_value'", null, null],
284+
['SOME_KEY', "'some_value'", null],
267285
],
268286
[
269-
['some_key', "'some_value'"],
270-
['SOME_KEY', "'some_value'"],
287+
['some_key', "'some_value'", null],
288+
['SOME_KEY', "'some_value'", null],
271289
],
272290
// Test spaces in value.
273291
[
274-
['some_key=some value', null],
275-
['SOME_KEY', '"some value"'],
292+
['some_key=some value', null, null],
293+
['SOME_KEY', '"some value"', null],
276294
],
277295
[
278-
['some_key', 'some value'],
279-
['SOME_KEY', '"some value"'],
296+
['some_key', 'some value', null],
297+
['SOME_KEY', '"some value"', null],
280298
],
281299
// Test spaces in value that are enclosed in quotes.
282300
[
283-
['some_key="some value"', null],
284-
['SOME_KEY', '"some value"'],
301+
['some_key="some value"', null, null],
302+
['SOME_KEY', '"some value"', null],
285303
],
286304
[
287-
['some_key', '"some value"'],
288-
['SOME_KEY', '"some value"'],
305+
['some_key', '"some value"', null],
306+
['SOME_KEY', '"some value"', null],
289307
],
290308
[
291-
["some_key='some value'", null],
292-
['SOME_KEY', "'some value'"],
309+
["some_key='some value'", null, null],
310+
['SOME_KEY', "'some value'", null],
293311
],
294312
[
295-
['some_key', "'some value'"],
296-
['SOME_KEY', "'some value'"],
313+
['some_key', "'some value'", null],
314+
['SOME_KEY', "'some value'", null],
297315
],
298316
];
299317
}

0 commit comments

Comments
 (0)