Build 0.1.2

Add env and init command
Update documentation
This commit is contained in:
René Preuß
2021-07-23 15:10:46 +02:00
parent feab09389a
commit 331881b2cc
10 changed files with 429 additions and 77 deletions

View File

@@ -37,4 +37,13 @@ class Client
return new Result($response);
}
public function getStorageZones(): Result
{
return $this->request('GET', 'storagezone', [
RequestOptions::HEADERS => [
'AccessKey' => config('bunny.api.access_key'),
],
]);
}
}

View File

@@ -29,7 +29,7 @@ class DeployCommand extends Command
*
* @var string
*/
protected $description = 'Deploy dist folder to edge storage';
protected $description = 'Deploy a dist folder to edge storage';
/**
* Execute the console command.
@@ -75,16 +75,4 @@ class DeployCommand extends Command
return 0;
}
/**
* Define the command's schedule.
*
* @param Schedule $schedule
* @return void
*/
public function schedule(Schedule $schedule): void
{
// $schedule->command(static::class)->everyMinute();
}
}

View 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;
}
}

View 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;
}
}

View 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;
}
}

View 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);
}
}

View File

@@ -0,0 +1,112 @@
<?php
namespace App\Commands;
use App\Bunny\Client;
use App\Commands\Env\EnvSetCommand;
use Dotenv\Dotenv;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\App;
use LaravelZero\Framework\Commands\Command;
class InitCommand extends Command
{
/**
* The signature of the command.
*
* @var string
*/
protected $signature = 'init
{--api-key= : API key of the Bunny account}
{--storage-zone= : Name of the storage zone}
{--pull-zone= : Name of the pull zone zone}';
/**
* The description of the command.
*
* @var string
*/
protected $description = 'Initialize a new .env file';
/**
* Execute the console command.
*
* @param Client $client
* @return int
*/
public function handle(Client $client): 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['BUNNY_API_ACCESS_KEY'] = $this->ask(
'What is your api key?',
$this->option('api-key') ?? $env['BUNNY_API_ACCESS_KEY'] ?? null
);
config()->set('bunny.api.access_key', $env['BUNNY_API_ACCESS_KEY']);
$storageZones = new Collection($client->getStorageZones()->getData());
if (!$this->option('no-interaction')) {
$storageZones->each(fn($item) => $this->info(sprintf(' - %s', $item->Name)));
}
$storageZoneName = $this->anticipate(
'Which storage zone do you want to use?',
function ($input) use ($storageZones) {
return $storageZones->filter(function ($item) use ($input) {
// replace stristr with your choice of matching function
return false !== stristr($item->Name, $input);
})->pluck('Name')->toArray();
},
$this->option('storage-zone')
);
$storageZone = $storageZones->where('Name', '===', $storageZoneName)->first();
$env['BUNNY_STORAGE_USERNAME'] = $storageZone->Name;
$env['BUNNY_STORAGE_PASSWORD'] = $storageZone->Password;
$pullZones = new Collection($storageZone->PullZones);
if (!$this->option('no-interaction')) {
$pullZones->each(fn($item) => $this->info(sprintf(' - %s', $item->Name)));
}
$firstPullZone = $pullZones->count() > 0 ? $pullZones->first()->Name : null;
$pullZoneName = $this->anticipate(
'Which pull zone do you want to use?',
function ($input) use ($storageZones) {
return $storageZones->filter(function ($item) use ($input) {
// replace stristr with your choice of matching function
return false !== stristr($item->Name, $input);
})->pluck('Name')->toArray();
},
$this->option('api-key') ?? $firstPullZone
);
$pullZone = $pullZones->where('Name', '===', $pullZoneName)->first();
$env['BUNNY_PULL_ZONE_ID'] = $pullZone->Id ?? null;
if (!$pullZone) {
$this->warn('No pull zone was specified, therefore no pull zone is flushed during deployment.');
}
file_put_contents($envFilePath, EnvSetCommand::updateEnv($env));
$this->info('The environment file was successfully updated.');
return 0;
}
}