mirror of
https://github.com/bitinflow/bunny-cli.git
synced 2026-03-18 16:15:51 +00:00
Build 0.1.2
Add env and init command Update documentation
This commit is contained in:
42
app/Commands/Env/EnvBackupCommand.php
Normal file
42
app/Commands/Env/EnvBackupCommand.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace App\Commands\Env;
|
||||
|
||||
use Dotenv\Dotenv;
|
||||
use Illuminate\Console\Scheduling\Schedule;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use LaravelZero\Framework\Commands\Command;
|
||||
|
||||
class EnvBackupCommand extends Command
|
||||
{
|
||||
/**
|
||||
* The signature of the command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'env:backup {file : Location of the backup file}';
|
||||
|
||||
/**
|
||||
* The description of the command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Backup .env file into a given file';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function handle(): int
|
||||
{
|
||||
$envFilePath = App::environmentFilePath();
|
||||
$this->info(sprintf("The following environment file is used: '%s'", $envFilePath));
|
||||
|
||||
file_put_contents($this->argument('file'), file_get_contents($envFilePath));
|
||||
|
||||
$this->info('The environment file was successfully backed up.');
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
54
app/Commands/Env/EnvListCommand.php
Normal file
54
app/Commands/Env/EnvListCommand.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace App\Commands\Env;
|
||||
|
||||
use Dotenv\Dotenv;
|
||||
use Illuminate\Console\Scheduling\Schedule;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use LaravelZero\Framework\Commands\Command;
|
||||
|
||||
class EnvListCommand extends Command
|
||||
{
|
||||
/**
|
||||
* The signature of the command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'env:list';
|
||||
|
||||
/**
|
||||
* The description of the command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'List all current environment variables';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function handle(): int
|
||||
{
|
||||
$envFilePath = App::environmentFilePath();
|
||||
$this->info(sprintf("The following environment file is used: '%s'", $envFilePath));
|
||||
|
||||
if (file_exists($envFilePath)) {
|
||||
$env = Dotenv::parse(file_get_contents($envFilePath));
|
||||
} else {
|
||||
$this->warn('The environment file does not exist.');
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
if(empty($env)) {
|
||||
$this->warn('The environment file is empty.');
|
||||
|
||||
return 2;
|
||||
}
|
||||
|
||||
$this->table(['Key', 'Value'], array_map(fn($k, $v) => [$k, $v], array_keys($env), $env));
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
41
app/Commands/Env/EnvRestoreCommand.php
Normal file
41
app/Commands/Env/EnvRestoreCommand.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace App\Commands\Env;
|
||||
|
||||
use Illuminate\Console\Scheduling\Schedule;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use LaravelZero\Framework\Commands\Command;
|
||||
|
||||
class EnvRestoreCommand extends Command
|
||||
{
|
||||
/**
|
||||
* The signature of the command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'env:restore {file : Location of the backup file}';
|
||||
|
||||
/**
|
||||
* The description of the command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Restore .env file from a given file';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function handle(): int
|
||||
{
|
||||
$envFilePath = App::environmentFilePath();
|
||||
$this->info(sprintf("The following environment file is used: '%s'", $envFilePath));
|
||||
|
||||
file_put_contents($envFilePath, file_get_contents($this->argument('file')));
|
||||
|
||||
$this->info('The environment file was successfully restored.');
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
72
app/Commands/Env/EnvSetCommand.php
Normal file
72
app/Commands/Env/EnvSetCommand.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace App\Commands\Env;
|
||||
|
||||
use Dotenv\Dotenv;
|
||||
use Illuminate\Console\Scheduling\Schedule;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use LaravelZero\Framework\Commands\Command;
|
||||
|
||||
class EnvSetCommand extends Command
|
||||
{
|
||||
/**
|
||||
* The signature of the command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'env:set
|
||||
{key : Key of the environment}
|
||||
{value : Value of the environment}';
|
||||
|
||||
/**
|
||||
* The description of the command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Set and save an environment variable in the .env file';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function handle(): int
|
||||
{
|
||||
$envFilePath = App::environmentFilePath();
|
||||
$this->info(sprintf("The following environment file is used: '%s'", $envFilePath));
|
||||
|
||||
if (file_exists($envFilePath)) {
|
||||
$env = Dotenv::parse(file_get_contents($envFilePath));
|
||||
} else {
|
||||
$this->warn('The environment file does not exist. Creating a new one...');
|
||||
$env = [];
|
||||
}
|
||||
|
||||
$env[strtoupper($this->argument('key'))] = $this->argument('value');
|
||||
|
||||
file_put_contents($envFilePath, self::updateEnv($env));
|
||||
|
||||
$this->info('The environment file was successfully updated.');
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static function updateEnv($data = []): string
|
||||
{
|
||||
if (!count($data)) {
|
||||
return PHP_EOL;
|
||||
}
|
||||
|
||||
$lines = [];
|
||||
|
||||
foreach ($data as $key => $value) {
|
||||
if (preg_match('/\s/', $value) || strpos($value, '=') !== false) {
|
||||
$value = '"' . $value . '"';
|
||||
}
|
||||
|
||||
$lines[] = sprintf('%s=%s', $key, $value);
|
||||
}
|
||||
|
||||
return implode(PHP_EOL, $lines);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user