Skip to content

Commit

Permalink
Added the ship command with the find server feature
Browse files Browse the repository at this point in the history
- Utilized the Laravel Pipeline
- Set up the configuration file with proper validation
  • Loading branch information
mehrancodes committed Oct 1, 2023
1 parent 101a05d commit bda0e39
Show file tree
Hide file tree
Showing 15 changed files with 2,511 additions and 272 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/.vscode
/.vagrant
.phpunit.result.cache
.env
47 changes: 0 additions & 47 deletions app/Commands/InspireCommand.php

This file was deleted.

38 changes: 38 additions & 0 deletions app/Commands/ShipCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

/**
* This file is part of Harbor CLI.
*
* (c) Mehran Rasulian <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace App\Commands;

use App\Services\Forge\ForgeService;
use App\Services\Forge\Pipeline\FindServer;
use App\Traits\Outputifier;
use Illuminate\Support\Facades\Pipeline;
use LaravelZero\Framework\Commands\Command;

class ShipCommand extends Command
{
use Outputifier;

protected $signature = 'ship';

protected $description = 'Prepares the preview environment for your project.';

public function handle(ForgeService $service): void
{
Pipeline::send($service)
->through([
FindServer::class,
])
->then(fn () => $this->success('It is done.'));
}
}
10 changes: 3 additions & 7 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,13 @@

class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*/
public function boot(): void
{
//
$this->app->setLocale('en');

$this->loadTranslationsFrom(base_path('lang'), 'harbor');
}

/**
* Register any application services.
*/
public function register(): void
{
//
Expand Down
104 changes: 104 additions & 0 deletions app/Services/Forge/ForgeService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

declare(strict_types=1);

/**
* This file is part of Harbor CLI.
*
* (c) Mehran Rasulian <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace App\Services\Forge;

use App\Actions\GenerateDomainName;
use Laravel\Forge\Forge;
use Laravel\Forge\Resources\Server;
use Laravel\Forge\Resources\Site;

class ForgeService
{
/**
* The Forge SDK instance.
*/
public Forge $forge;

/**
* The Forge wrapper setting instance.
*/
public ForgeSetting $setting;

/**
* The Forge server instance.
*/
public ?Server $server = null;

/**
* The Forge site instance.
*/
public ?Site $site = null;

/**
* New database credentials for updating the site's DB environment keys.
*/
public ?array $database = [];

/**
* To check weather the site is created now.
*/
public bool $siteNewlyMade = false;

/**
* Get the formatted domain based on subdomain pattern.
*/
private ?string $formattedDomain = null;

public function __construct()
{
$this->setting = new ForgeSetting();

$this->forge = new Forge($this->setting->token);
}

public function setServer(Server $server): void
{
$this->server = $server;
}

public function setSite(Site $site): void
{
$this->site = $site;
}

public function setDatabase(array $database): void
{
$this->database = $database;
}

public function getFormattedDomainName(): ?string
{
if (is_null($this->formattedDomain)) {
$this->formattedDomain = GenerateDomainName::run();
}

return $this->formattedDomain;
}

public function findSite(string $serverId): ?Site
{
foreach ($this->forge->sites($serverId) as $site) {
if ($site->name === $this->getFormattedDomainName()) {
return $site;
}
}

return null;
}

public function markSiteAsNewlyMade(): void
{
$this->siteNewlyMade = true;
}
}
174 changes: 174 additions & 0 deletions app/Services/Forge/ForgeSetting.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
<?php

declare(strict_types=1);

/**
* This file is part of Harbor CLI.
*
* (c) Mehran Rasulian <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace App\Services\Forge;

use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Str;
use InvalidArgumentException;

class ForgeSetting
{
/**
* The API token.
*/
public string $token;

/**
* The server ID.
*/
public string $server;

/**
* The site domain.
*/
public string $domain;

/**
* The git provider.
*/
public string $gitProvider;

/**
* The git repository.
*/
public string $repository;

/**
* The git branch..
*/
public string $branch;

/**
* The subdomain pattern.
*/
public ?string $subdomainPattern = null;

/**
* The project type
*/
public string $projectType;

/**
* The PHP version
*/
public string $phpVersion;

/**
* The Nginx template
*/
public string $nginxTemplate;

/**
* Weather enable Quick Deploy.
*/
public bool $quickDeploy;

/**
* Weather required to run site isolation.
*/
public bool $siteIsolationRequired;

/**
* Weather required to run jobs scheduler.
*/
public bool $jobSchedulerRequired;

/**
* Weather required to create database.
*/
public bool $dbCreationRequired;

/**
* Whether to automatically source environment variables into the deployment script.
*/
public bool $autoSourceRequired;

/**
* Whether to create the SSL certification.
*/
public bool $sslRequired;

/**
* Wait on the site deployment during the provision.
*/
public bool $waitOnDeploy;

/**
* Wait on the site deployment during the provision.
*/
public bool $waitOnSsl;

/**
* A comma-separated string of key/values to update custom keys in the Nginx site template.
*/
public ?string $nginxSubstitute = null;

/**
* The contents of the deployment script.
*/
public ?string $deployScript = null;

/**
* The contents of the custom key/values to get added in the environment file on runtime.
*/
public ?string $envVars = null;

/**
* The validation rules.
*/
private array $validationRules = [
'token' => ['required'],
'server' => ['required'],
'domain' => ['required'],
'git_provider' => ['required'],
'repository' => ['required'],
'branch' => ['required'],
'subdomain_pattern' => ['nullable', 'string'],
'project_type' => ['string'],
'php_version' => ['string'],
'nginx_template' => ['nullable', 'int'],
'quick_deploy' => ['boolean'],
'site_isolation_required' => ['boolean'],
'job_scheduler_required' => ['boolean'],
'db_creation_required' => ['boolean'],
'auto_source_required' => ['boolean'],
'ssl_required' => ['boolean'],
'wait_on_ssl' => ['boolean'],
'wait_on_deploy' => ['boolean'],
];

public function __construct()
{
$this->init(config('forge'));
}

private function init(array $configurations): void
{
$validator = Validator::make($configurations, $this->validationRules);

if ($validator->fails()) {
// Handle validation failures.
throw new InvalidArgumentException(
'Invalid configuration values: '.implode(', ', $validator->errors()->all())
);
}

// If validation passes, set properties
foreach ($configurations as $key => $value) {
if (property_exists($this, $key = Str::camel($key))) {
$this->$key = $value;
}
}
}
}
Loading

0 comments on commit bda0e39

Please sign in to comment.