Skip to content

Commit dddbf34

Browse files
committed
feat: Add env commands
1 parent aede702 commit dddbf34

11 files changed

Lines changed: 419 additions & 0 deletions

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,45 @@ The current feature set consists of:
1919

2020
- Clone this repository into custom/plugins of your Shopware 6 installation
2121

22+
## Commands
23+
24+
### `frosh:env:list` - Listing of all environment variables
25+
26+
`bin/console frosh:env:list`
27+
28+
`bin/console frosh:env:list --json`
29+
Lists as json output
30+
31+
### `frosh:env:get` - Get environment variables
32+
33+
```bash
34+
bin/console frosh:env:get APP_URL
35+
http://localhost
36+
```
37+
38+
```bash
39+
bin/console frosh:env:get APP_URL --key-value
40+
APP_URL=http://localhost
41+
```
42+
43+
```bash
44+
bin/console frosh:env:get APP_URL --json
45+
{
46+
"APP_URL": "http:\/\/localhost"
47+
}
48+
```
49+
50+
### `frosh:env:set` - Set environment variables
51+
52+
```bash
53+
bin/console frosh:env:set VARIABLE VALUE
54+
```
55+
56+
### `frosh:env:del` - Delete environment variables
57+
58+
```bash
59+
bin/console frosh:env:del VARIABLE
60+
```
2261

2362
## Screenshots
2463

src/Command/EnvDelCommand.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Frosh\Tools\Command;
4+
5+
use Frosh\Tools\Components\Environment\EnvironmentManager;
6+
use Symfony\Component\Console\Command\Command;
7+
use Symfony\Component\Console\Input\InputArgument;
8+
use Symfony\Component\Console\Input\InputInterface;
9+
use Symfony\Component\Console\Output\OutputInterface;
10+
use Symfony\Component\Console\Style\SymfonyStyle;
11+
12+
class EnvDelCommand extends Command
13+
{
14+
public static $defaultName = 'frosh:env:Del';
15+
public static $defaultDescription = 'Delete environment variable';
16+
private string $envPath;
17+
18+
public function __construct(string $envPath)
19+
{
20+
parent::__construct();
21+
$this->envPath = $envPath;
22+
}
23+
24+
public function configure(): void
25+
{
26+
$this->addArgument('variable', InputArgument::REQUIRED, 'Variable name');
27+
}
28+
29+
protected function execute(InputInterface $input, OutputInterface $output): int
30+
{
31+
$manager = new EnvironmentManager();
32+
$file = $manager->read($this->envPath);
33+
34+
$file->delete($input->getArgument('variable'));
35+
$manager->save($this->envPath, $file);
36+
37+
$io = new SymfonyStyle($input, $output);
38+
$io->success('Saved .env file');
39+
40+
return self::SUCCESS;
41+
}
42+
}

src/Command/EnvGetCommand.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
namespace Frosh\Tools\Command;
4+
5+
use Frosh\Tools\Components\Environment\EnvironmentManager;
6+
use Symfony\Component\Console\Command\Command;
7+
use Symfony\Component\Console\Input\InputArgument;
8+
use Symfony\Component\Console\Input\InputInterface;
9+
use Symfony\Component\Console\Input\InputOption;
10+
use Symfony\Component\Console\Output\OutputInterface;
11+
12+
class EnvGetCommand extends Command
13+
{
14+
public static $defaultName = 'frosh:env:get';
15+
public static $defaultDescription = 'Get an environment variable';
16+
private string $envPath;
17+
18+
public function __construct(string $envPath)
19+
{
20+
parent::__construct();
21+
$this->envPath = $envPath;
22+
}
23+
24+
public function configure(): void
25+
{
26+
$this->addArgument('variable', InputArgument::OPTIONAL, 'Get specific environment variable');
27+
$this->addOption('key-value', null, InputOption::VALUE_NONE, 'Get value as key value');
28+
$this->addOption('json', null, InputOption::VALUE_NONE, 'Get value as json');
29+
}
30+
31+
public function execute(InputInterface $input, OutputInterface $output): int
32+
{
33+
if (!is_file($this->envPath)) {
34+
throw new \RuntimeException(\sprintf('Cannot use this command as env file is missing at %s', $this->envPath));
35+
}
36+
37+
$manager = new EnvironmentManager();
38+
$file = $manager->read($this->envPath);
39+
$mode = $input->getOption('json') ? 'json' : ($input->getOption('key-value') ? 'kv' : '');
40+
41+
if ($input->getArgument('variable') === null) {
42+
if ($mode === 'json') {
43+
$output->writeln(json_encode($file->values(), JSON_PRETTY_PRINT));
44+
} else if ($mode) {
45+
$output->writeln($file->__toString());
46+
}
47+
48+
return self::SUCCESS;
49+
}
50+
51+
$var = $file->get($input->getArgument('variable'));
52+
53+
if ($var === null) {
54+
throw new \RuntimeException(sprintf('Cannot find variable with name: %s', $input->getArgument('variable')));
55+
}
56+
57+
if ($mode === 'json') {
58+
$output->writeln(json_encode([$var->getKey() => $var->getValue()], JSON_PRETTY_PRINT));
59+
} else if ($mode === 'kv') {
60+
$output->writeln($var->getLine());
61+
} else {
62+
$output->writeln($var->getValue());
63+
}
64+
65+
return self::SUCCESS;
66+
}
67+
}

src/Command/EnvListCommand.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Frosh\Tools\Command;
4+
5+
class EnvListCommand extends EnvGetCommand
6+
{
7+
public static $defaultName = 'frosh:env:list';
8+
}

src/Command/EnvSetCommand.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Frosh\Tools\Command;
4+
5+
use Frosh\Tools\Components\Environment\EnvironmentManager;
6+
use Symfony\Component\Console\Command\Command;
7+
use Symfony\Component\Console\Input\InputArgument;
8+
use Symfony\Component\Console\Input\InputInterface;
9+
use Symfony\Component\Console\Output\OutputInterface;
10+
use Symfony\Component\Console\Style\SymfonyStyle;
11+
12+
class EnvSetCommand extends Command
13+
{
14+
public static $defaultName = 'frosh:env:set';
15+
public static $defaultDescription = 'Set an environment variable';
16+
private string $envPath;
17+
18+
public function __construct(string $envPath)
19+
{
20+
parent::__construct();
21+
$this->envPath = $envPath;
22+
}
23+
24+
public function configure(): void
25+
{
26+
$this->addArgument('variable', InputArgument::REQUIRED, 'Variable name');
27+
$this->addArgument('value', InputArgument::REQUIRED, 'Variable value');
28+
}
29+
30+
protected function execute(InputInterface $input, OutputInterface $output): int
31+
{
32+
$manager = new EnvironmentManager();
33+
$file = $manager->read($this->envPath);
34+
35+
$file->set($input->getArgument('variable'), $input->getArgument('value'));
36+
$manager->save($this->envPath, $file);
37+
38+
$io = new SymfonyStyle($input, $output);
39+
$io->success('Saved .env file');
40+
41+
return self::SUCCESS;
42+
}
43+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Frosh\Tools\Components\Environment;
4+
5+
class EnvironmentCommentLine implements EnvironmentLine
6+
{
7+
private string $line;
8+
9+
public function getLine(): string
10+
{
11+
return $this->line;
12+
}
13+
14+
public static function parse(string $line): EnvironmentLine
15+
{
16+
$self = new self();
17+
$self->line = $line;
18+
19+
return $self;
20+
}
21+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
namespace Frosh\Tools\Components\Environment;
4+
5+
class EnvironmentFile
6+
{
7+
/**
8+
* @var array<string, EnvironmentLine>
9+
*/
10+
private array $items;
11+
12+
public function __construct(array $items)
13+
{
14+
$this->items = $items;
15+
}
16+
17+
public function has(string $key): bool
18+
{
19+
return $this->get($key) !== null;
20+
}
21+
22+
public function get(string $key): ?EnvironmentKeyValue
23+
{
24+
foreach ($this->items as $item) {
25+
if ($item instanceof EnvironmentKeyValue && $item->getKey() === $key) {
26+
return $item;
27+
}
28+
}
29+
30+
return null;
31+
}
32+
33+
public function set(string $key, string $value): void
34+
{
35+
$v = $this->get($key);
36+
37+
if ($v) {
38+
$v->setValue($value);
39+
40+
return;
41+
}
42+
43+
$this->items[] = EnvironmentKeyValue::parse($key . '=' . $value);
44+
}
45+
46+
public function delete(string $key): void
47+
{
48+
foreach ($this->items as $i => $item) {
49+
if ($item instanceof EnvironmentKeyValue && $item->getKey() === $key) {
50+
unset($this->items[$i]);
51+
}
52+
}
53+
}
54+
55+
public function keys(): array
56+
{
57+
$keys = [];
58+
59+
foreach ($this->items as $item) {
60+
if ($item instanceof EnvironmentKeyValue) {
61+
$keys[] = $item->getKey();
62+
}
63+
}
64+
65+
return $keys;
66+
}
67+
68+
public function values(): array
69+
{
70+
$values = [];
71+
72+
foreach ($this->items as $item) {
73+
if ($item instanceof EnvironmentKeyValue) {
74+
$values[$item->getKey()] = $item->getValue();
75+
}
76+
}
77+
78+
return $values;
79+
}
80+
81+
public function __toString(): string
82+
{
83+
$content = '';
84+
85+
foreach ($this->items as $item) {
86+
$content .= $item->getLine() . PHP_EOL;
87+
}
88+
89+
return $content;
90+
}
91+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Frosh\Tools\Components\Environment;
4+
5+
class EnvironmentKeyValue implements EnvironmentLine
6+
{
7+
private string $key;
8+
private string $value;
9+
10+
public function getKey(): string
11+
{
12+
return $this->key;
13+
}
14+
15+
public function setKey(string $key): void
16+
{
17+
$this->key = $key;
18+
}
19+
20+
public function getValue(): string
21+
{
22+
return $this->value;
23+
}
24+
25+
public function setValue(string $value): void
26+
{
27+
$this->value = $value;
28+
}
29+
30+
public function getLine(): string
31+
{
32+
return $this->key . '=' . $this->value;
33+
}
34+
35+
public static function parse(string $line): EnvironmentKeyValue
36+
{
37+
$self = new self();
38+
[$self->key, $self->value] = explode('=', $line, 2);
39+
40+
return $self;
41+
}
42+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Frosh\Tools\Components\Environment;
4+
5+
interface EnvironmentLine
6+
{
7+
public function getLine(): string;
8+
9+
public static function parse(string $line): self;
10+
}

0 commit comments

Comments
 (0)