Skip to content

Commit

Permalink
Merge pull request #3 from A-Lawrence/allow-env-variable
Browse files Browse the repository at this point in the history
Allow ENV Variables
  • Loading branch information
Orrison authored Feb 9, 2023
2 parents 0182ede + d7fff98 commit a8b3127
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 21 deletions.
39 changes: 39 additions & 0 deletions cumulus
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
<?php

use Cumulus\Cumulus\Commands;
use Dotenv\Dotenv;
use Dotenv\Repository\Adapter\EnvConstAdapter as V4orV5EnvConstAdapter;
use Dotenv\Repository\Adapter\ServerConstAdapter as V4orV5ServerConstAdapter;
use Dotenv\Repository\RepositoryBuilder;
use Laravel\VaporCli\Application;
use Illuminate\Container\Container;

Expand All @@ -14,6 +18,41 @@ if (file_exists(__DIR__ . '/../../autoload.php')) {
require __DIR__ . '/vendor/autoload.php';
}

/**
* Load the environment variables.
*/
(function () {
if (class_exists(RepositoryBuilder::class)) {
$adapters = [
V4orV5EnvConstAdapter::class,
V4orV5ServerConstAdapter::class,
];

if (method_exists(RepositoryBuilder::class, 'addReader')) { // V5
$repository = RepositoryBuilder::createWithNoAdapters();

foreach ($adapters as $adapter) {
$repository = $repository
->addReader($adapter)
->addWriter($adapter);
}
} else { // V4
$adapters = array_map(function ($adapterClass) {
return new $adapterClass();
}, $adapters);

$repository = RepositoryBuilder::create()
->withReaders($adapters)
->withWriters($adapters);
}

Dotenv::create(
$repository->immutable()->make(),
__DIR__
)->safeLoad();
}
})();

/**
* Create the container instance.
*/
Expand Down
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ If, for what ever reason, you would like to clear this token from local storage
cumulus cloudflare:logout
```

Alternatively, if you wish to load your API token from your server environment, you can do so by declaring `CLOUDFLARE_API_TOKEN` as an environment variable. When this is defined it will always take priority over your Cumulus config stored in your user profile.

---

### Importing DNS Records
Expand Down
7 changes: 3 additions & 4 deletions src/Commands/CloudflareLoginCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Cloudflare\API\Auth\APIToken;
use Cloudflare\API\Adapter\Guzzle;
use Laravel\VaporCli\Commands\Command;
use Laravel\VaporCli\Helpers as VaporHelpers;
use Cumulus\Cumulus\CloudflareEndpoints\UserApiToken;

class CloudflareLoginCommand extends Command
Expand All @@ -20,7 +19,7 @@ class CloudflareLoginCommand extends Command
*/
public function handle()
{
$apiToken = VaporHelpers::secret('API Token');
$apiToken = Helpers::secret('API Token');

$key = new APIToken($apiToken);
$adapter = new Guzzle($key);
Expand All @@ -35,7 +34,7 @@ public function handle()
}
} catch (Exception $e) {
if ($e->getMessage() === 'Invalid request headers') {
VaporHelpers::abort(
Helpers::abort(
'Invalid credentials'
);
}
Expand All @@ -48,7 +47,7 @@ public function handle()
]
);

VaporHelpers::info('Authenticated successfully.' . PHP_EOL);
Helpers::info('Authenticated successfully.' . PHP_EOL);
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/Commands/CloudflareLogoutCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Exception;
use Cumulus\Cumulus\Helpers;
use Laravel\VaporCli\Commands\Command;
use Laravel\VaporCli\Helpers as VaporHelpers;

class CloudflareLogoutCommand extends Command
{
Expand All @@ -23,7 +22,7 @@ public function handle()
]
);

VaporHelpers::info('Cloudflare credentials cleared.' . PHP_EOL);
Helpers::info('Cloudflare credentials cleared.' . PHP_EOL);
}

/**
Expand Down
22 changes: 11 additions & 11 deletions src/Commands/RecordsImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,34 +81,34 @@ function ($vaporRecord) {

if ($this->dryRun) {
if ($this->totalAddedRecords > 0) {
VaporHelpers::info("Records required to be added:");
Helpers::info("Records required to be added:");

$this->table(['Type', 'Name', 'Value'], $this->recordsToAdd);
} else {
VaporHelpers::info("No records need added to Cloudflare. All records are already imported.");
Helpers::info("No records need added to Cloudflare. All records are already imported.");
}

if ($this->totalUpdatedRecords > 0) {
VaporHelpers::warn("Records required to be updated:");
Helpers::warn("Records required to be updated:");

$this->table(['Type', 'Name', 'Value'], $this->recordsToUpdate);
} else {
VaporHelpers::info("No records need updated in Cloudflare. All records are already correct.");
Helpers::info("No records need updated in Cloudflare. All records are already correct.");
}

return;
}

if ($this->totalAddedRecords > 0) {
VaporHelpers::info("Added {$this->totalAddedRecords} records to Cloudflare.");
Helpers::info("Added {$this->totalAddedRecords} records to Cloudflare.");
} else {
VaporHelpers::info("No records were added to Cloudflare. All records are already imported.");
Helpers::info("No records were added to Cloudflare. All records are already imported.");
}

if ($this->totalUpdatedRecords > 0) {
VaporHelpers::info("Updated {$this->totalUpdatedRecords} records in Cloudflare.");
Helpers::info("Updated {$this->totalUpdatedRecords} records in Cloudflare.");
} else {
VaporHelpers::info("No records need updated in Cloudflare. All records are already correct.");
Helpers::info("No records need updated in Cloudflare. All records are already correct.");
}
}

Expand All @@ -119,7 +119,7 @@ protected function getVaporRecords(): Collection
}

if (is_null($vaporZoneId)) {
VaporHelpers::abort('Unable to find a zone with that name / ID in Vapor.');
Helpers::abort('Unable to find a zone with that name / ID in Vapor.');
}

return collect($this->vapor->records($vaporZoneId))->map(function ($record) {
Expand Down Expand Up @@ -158,7 +158,7 @@ protected function addRecord($type, $name, $value): void
$this->totalAddedRecords++;
Helpers::info("Added {$type} record {$name}");
} catch (Exception $e) {
VaporHelpers::danger(
Helpers::danger(
"Unable to create {$type} record in Cloudflare with name {$name}. Response: {$e->getMessage()}"
);
}
Expand All @@ -181,7 +181,7 @@ protected function updateRecord($record, $value)
$this->totalUpdatedRecords++;
Helpers::info("Updated {$record->type} record {$record->name}");
} catch (Exception $e) {
VaporHelpers::danger(
Helpers::danger(
"Unable to update record in Cloudflare. Response: {$e->getMessage()}"
);
}
Expand Down
15 changes: 11 additions & 4 deletions src/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Helpers extends VaporHelpers
*/
public static function ensureCloudFlareCredentialsAreAvailable()
{
if (!static::config('apiToken')) {
if (empty(static::getCloudFlareApiToken())) {
throw new Exception("Please authenticate using the 'cloudflare:login' command before proceeding.");
}
}
Expand Down Expand Up @@ -52,15 +52,15 @@ public static function config($key, $value = null)
*/
public static function getCloudflareZoneId($zoneInput)
{
$key = new APIToken(Config::get('apiToken'));
$key = new APIToken(static::getCloudFlareApiToken());
$adapter = new Guzzle($key);

$zone = new Zones($adapter);

try {
return $zone->getZoneId($zoneInput);
} catch (Exception $e) {
VaporHelpers::abort('Unable to find a zone with that name / ID in Cloudflare.');
parent::abort('Unable to find a zone with that name / ID in Cloudflare.');
}
}

Expand All @@ -69,9 +69,16 @@ public static function getCloudflareZoneId($zoneInput)
*/
public static function getCloudflareDnsApi()
{
$key = new APIToken(Config::get('apiToken'));
$key = new APIToken(static::getCloudFlareApiToken());
$adapter = new Guzzle($key);

return new DNS($adapter);
}

public static function getCloudFlareApiToken()
{
return $_ENV['CLOUDFLARE_API_TOKEN']
?? env('CLOUDFLARE_API_TOKEN')
?? Config::get('apiToken');
}
}

0 comments on commit a8b3127

Please sign in to comment.