Skip to content
This repository has been archived by the owner on Jun 12, 2022. It is now read-only.

Commit

Permalink
* The branch and port server attributes are no longer required an…
Browse files Browse the repository at this point in the history
…d will default to `master` and `22` respectively.

* If there is a missing attribute, the exception will now include the name of the attribute.
* Server configurations will now through a `ServerException` instead of a `RuntimeException`.
* Invalid script tags will now through a `ConfigurationException` instead of a `RuntimeException`.
  • Loading branch information
warrickbayman committed Apr 21, 2020
1 parent 8e30874 commit 63b26cf
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 17 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@
* A new `Ssh::setTask` method can be used to set a task on the `Ssh` instance.
* `Ssh::run` will now throw an exception if the task instance doesn't have a server, or if a task instance isn't set.

## [0.4.8] 21-04-2020
## [0.4.9] 21-04-2020
### Changed
* The `branch` and `port` server attributes are no longer required and will default to `master` and `22` respectively.
* If there is a missing attribute, the exception will now include the name of the attribute.
* Server configurations will now through a `ServerException` instead of a `RuntimeException`.
* Invalid script tags will now through a `ConfigurationException` instead of a `RuntimeException`.

### Fixed
* Fixed a bug causing a crash when intializing a project that does not have a Git remote yet.

Expand Down
2 changes: 1 addition & 1 deletion bin/attache
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ if (file_exists(__DIR__.'/../vendor/autoload.php')) {
require __DIR__.'/../../../autoload.php';
}

$app = new \Symfony\Component\Console\Application('Attaché', '0.4.8');
$app = new \Symfony\Component\Console\Application('Attaché', '0.4.9');

$app->add(new \TPG\Attache\Console\InitCommand());
$app->add(new \TPG\Attache\Console\ServersListCommand());
Expand Down
23 changes: 13 additions & 10 deletions src/ConfigurationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,23 @@ public function loadConfigFile(string $filename): void
throw new FileNotFoundException('Cannot find config file '.$filename);
}

try {
$this->setConfig(file_get_contents($filename));
} catch (\Exception $e) {
throw new \JsonException('Unable to read config.'."\n".$e->getMessage());
exit(1);
}
$this->setConfig(file_get_contents($filename));
}

/**
* Set the configuration.
*
* @param string|array $config
* @throws ConfigurationException
* @throws ConfigurationException|\JsonException
*/
public function setConfig($config): void
{
if (is_string($config)) {
$config = json_decode($config, true, 512, JSON_THROW_ON_ERROR);
try {
$config = json_decode($config, true, 512, JSON_THROW_ON_ERROR);
} catch (\Exception $e) {
throw new \JsonException('Unable to read config.' . "\n" . $e->getMessage());
}
}

$this->repository = Arr::get($config, 'repository');
Expand Down Expand Up @@ -108,8 +107,12 @@ protected function loadServers(array $servers, array $common): void
*/
protected function validateServer(array $config): void
{
if (! Arr::has($config, ['name', 'host', 'port', 'user', 'root', 'branch'])) {
throw new ConfigurationException('Missing server configuration key');
$required = ['name', 'host', 'user', 'root'];

foreach ($required as $attr) {
if (! Arr::has($config, $attr)) {
throw new ConfigurationException('Missing server configuration key: '.$attr);
}
}
}

Expand Down
13 changes: 13 additions & 0 deletions src/Exceptions/ServerException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace TPG\Attache\Exceptions;

use Throwable;

class ServerException extends \Exception
{
public function __construct($message = "", $code = 0, Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
}
9 changes: 5 additions & 4 deletions src/ReleaseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

namespace TPG\Attache;

use http\Exception\RuntimeException;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use TPG\Attache\Exceptions\ServerException;

class ReleaseService
{
/**
* @var array
* @var Server
*/
protected Server $server;

Expand All @@ -35,6 +35,7 @@ public function __construct(Server $server)
* Fetch release data from the server.
*
* @return $this
* @throws ServerException
*/
public function fetch(): self
{
Expand Down Expand Up @@ -78,7 +79,7 @@ protected function getReleaseData(): array
protected function validateOutput(array $output): bool
{
if (count($output) !== 2) {
throw new RuntimeException('Failed to fetch current releases from '.$this->server->name()
throw new ServerException('Failed to fetch current releases from '.$this->server->name()
.'. Double check your configuration and try again.');
}

Expand All @@ -94,7 +95,7 @@ protected function validateOutput(array $output): bool
protected function getReleasesFromOutput(string $output): array
{
if (! $output) {
throw new \RuntimeException('There was no response from '.$this->server->name()
throw new ServerException('There was no response from '.$this->server->name()
.'. Try again or double check your connection to the server.');
}

Expand Down
3 changes: 2 additions & 1 deletion src/ScriptCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace TPG\Attache;

use Illuminate\Support\Arr;
use TPG\Attache\Exceptions\ConfigurationException;

class ScriptCompiler
{
Expand Down Expand Up @@ -45,7 +46,7 @@ protected function tagValues(array $tags)
case 'release':
return $this->server->latestReleaseId();
default:
throw new \RuntimeException('No such tag @'.$tag);
throw new ConfigurationException('No such tag @'.$tag);
}
}, $tags);

Expand Down

0 comments on commit 63b26cf

Please sign in to comment.