diff --git a/cumulus b/cumulus index 267f134..f3110d3 100755 --- a/cumulus +++ b/cumulus @@ -2,6 +2,10 @@ 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. */ diff --git a/readme.md b/readme.md index 0e806cc..255065a 100644 --- a/readme.md +++ b/readme.md @@ -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 diff --git a/src/Commands/CloudflareLoginCommand.php b/src/Commands/CloudflareLoginCommand.php index 9ad6be4..166decc 100644 --- a/src/Commands/CloudflareLoginCommand.php +++ b/src/Commands/CloudflareLoginCommand.php @@ -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 @@ -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); @@ -35,7 +34,7 @@ public function handle() } } catch (Exception $e) { if ($e->getMessage() === 'Invalid request headers') { - VaporHelpers::abort( + Helpers::abort( 'Invalid credentials' ); } @@ -48,7 +47,7 @@ public function handle() ] ); - VaporHelpers::info('Authenticated successfully.' . PHP_EOL); + Helpers::info('Authenticated successfully.' . PHP_EOL); } /** diff --git a/src/Commands/CloudflareLogoutCommand.php b/src/Commands/CloudflareLogoutCommand.php index f2b7e0c..764d426 100644 --- a/src/Commands/CloudflareLogoutCommand.php +++ b/src/Commands/CloudflareLogoutCommand.php @@ -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 { @@ -23,7 +22,7 @@ public function handle() ] ); - VaporHelpers::info('Cloudflare credentials cleared.' . PHP_EOL); + Helpers::info('Cloudflare credentials cleared.' . PHP_EOL); } /** diff --git a/src/Commands/RecordsImportCommand.php b/src/Commands/RecordsImportCommand.php index cd1af8d..526f59f 100644 --- a/src/Commands/RecordsImportCommand.php +++ b/src/Commands/RecordsImportCommand.php @@ -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."); } } @@ -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) { @@ -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()}" ); } @@ -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()}" ); } diff --git a/src/Helpers.php b/src/Helpers.php index 87a0856..6116842 100644 --- a/src/Helpers.php +++ b/src/Helpers.php @@ -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."); } } @@ -52,7 +52,7 @@ 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); @@ -60,7 +60,7 @@ public static function getCloudflareZoneId($zoneInput) 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.'); } } @@ -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'); + } } \ No newline at end of file