diff --git a/README.md b/README.md index 37f10a3..92553eb 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,12 @@ -# Bunny CLI - Replicate and store your files to the edge! +# Bunny CLI - Replicate and Store Your Files to the Edge! -## What is Bunny CLI? +## What Is Bunny CLI? Bunny CLI is a tool for the console to upload frontend frameworks such as Angular, Vue.js, React, or more recently, Blazor quickly to the Edge Store on Bunny CDN. With Bunny CDN's Storage Edge, your web applications benefit from replicated storage zones, a global content delivery network that hosts files in 5 different regions worldwide and accelerates everything through a worldwide content delivery network with over 54+ PoPs. -## How do I use Bunny CLI? +## How Do I Use Bunny CLI? To install Bunny CLI, you need to be using Composer. For more details about Composer, see the [Composer documentation](https://getcomposer.org/doc/). @@ -20,7 +20,33 @@ If you want to update the Bunny CLI, just execute the following command: composer global update own3d/bunny-cli ``` -Bunny CLI currently only comes with a `deploy` command. With this command, you can easily synconizise your `dist` folder with your edge storage. +After you install Bunny CLI, the next step is typically run the `bunny init` command to perform initial setup tasks. You can also run `bunny init` at a later time to change your settings or create a new configuration. + +### Run Bunny Init + +To initialize Bunny CLI: + +1. Run `bunny init`: + +```bash +bunny init +``` + +2. Configure your API credentials. + +In order for the Bunny CLI to work properly you need to store your Bunny CDN API token. You can find your API token in your [Account Settings](https://panel.bunny.net/account). + +3. Choose a current Storage Zone if prompted. + +If you only have access to one storage zone, including the default pull zone, `bunny init` selects it for you. + +When `bunny init` finishes, it saves the environment variables in your .env file. + +You can view these environment variables at any other time using the `bunny env:list` command. + +### Deploy Your First Project + +With the `bunny deploy` command, you can easily synchronize your `dist` folder with your edge storage. > **IMPORTANT**: All files in the edge storage that are **not** in your local `dist` directory will be deleted. @@ -57,11 +83,18 @@ We offer you a [GitHub Action for Bunny CLI](https://github.com/marketplace/acti ## Environment Variables +You can customize your environment file at any time. The following commands are available for this purpose: +| Command | Description | +|-------------------------|--------------------------------------------------------| +| `env:list` | List all current environment variables. | +| `env:set {key} {value}` | Set and save an environment variable in the .env file. | +| `env:backup {file}` | Backup .env file into a given file. | +| `env:restore {file}` | Restore .env file from a given file. | ## Secure your `.well-known/bunny-cli.lock` file -Bunny CLI generates a lock file, which by default is located at `.well-known/bunny-cli.lock`. This file locks the files of your project to a known state. To prevent this from being publicly accessible it is recommended to create a new edge rule in your pull zone. You can use the following example as a template: +Bunny CLI generates a lock file, which is located at `.well-known/bunny-cli.lock` by default. This file locks the files of your project to a known state. To prevent this from being publicly accessible it is recommended to create a new edge rule in your pull zone. You can use the following example as a template: Action: `Block Request` Condition Matching: `Match Any` diff --git a/app/Bunny/Client.php b/app/Bunny/Client.php index 9c53ea3..d5fce27 100644 --- a/app/Bunny/Client.php +++ b/app/Bunny/Client.php @@ -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'), + ], + ]); + } } diff --git a/app/Commands/DeployCommand.php b/app/Commands/DeployCommand.php index d1f2795..65250c4 100644 --- a/app/Commands/DeployCommand.php +++ b/app/Commands/DeployCommand.php @@ -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(); - } } diff --git a/app/Commands/Env/EnvBackupCommand.php b/app/Commands/Env/EnvBackupCommand.php new file mode 100644 index 0000000..e4abcdd --- /dev/null +++ b/app/Commands/Env/EnvBackupCommand.php @@ -0,0 +1,42 @@ +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; + } +} diff --git a/app/Commands/Env/EnvListCommand.php b/app/Commands/Env/EnvListCommand.php new file mode 100644 index 0000000..47ab931 --- /dev/null +++ b/app/Commands/Env/EnvListCommand.php @@ -0,0 +1,54 @@ +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; + } +} diff --git a/app/Commands/Env/EnvRestoreCommand.php b/app/Commands/Env/EnvRestoreCommand.php new file mode 100644 index 0000000..23caceb --- /dev/null +++ b/app/Commands/Env/EnvRestoreCommand.php @@ -0,0 +1,41 @@ +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; + } +} diff --git a/app/Commands/Env/EnvSetCommand.php b/app/Commands/Env/EnvSetCommand.php new file mode 100644 index 0000000..ce63ff2 --- /dev/null +++ b/app/Commands/Env/EnvSetCommand.php @@ -0,0 +1,72 @@ +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); + } +} diff --git a/app/Commands/InitCommand.php b/app/Commands/InitCommand.php new file mode 100644 index 0000000..67b9d5f --- /dev/null +++ b/app/Commands/InitCommand.php @@ -0,0 +1,112 @@ +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; + } +} diff --git a/builds/bunny b/builds/bunny index 7b81ff8..dc39b53 100755 Binary files a/builds/bunny and b/builds/bunny differ diff --git a/composer.lock b/composer.lock index e7f8c77..7052148 100644 --- a/composer.lock +++ b/composer.lock @@ -805,16 +805,16 @@ }, { "name": "illuminate/bus", - "version": "v8.50.0", + "version": "v8.51.0", "source": { "type": "git", "url": "https://github.com/illuminate/bus.git", - "reference": "5ff3d2e271dce9f34df1411c37d4d9158abc27af" + "reference": "0b9722013b908a67e9b815a3bad4f49e25828f7b" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/illuminate/bus/zipball/5ff3d2e271dce9f34df1411c37d4d9158abc27af", - "reference": "5ff3d2e271dce9f34df1411c37d4d9158abc27af", + "url": "https://api.github.com/repos/illuminate/bus/zipball/0b9722013b908a67e9b815a3bad4f49e25828f7b", + "reference": "0b9722013b908a67e9b815a3bad4f49e25828f7b", "shasum": "" }, "require": { @@ -854,20 +854,20 @@ "issues": "https://github.com/laravel/framework/issues", "source": "https://github.com/laravel/framework" }, - "time": "2021-07-08T14:57:43+00:00" + "time": "2021-07-16T12:35:20+00:00" }, { "name": "illuminate/cache", - "version": "v8.50.0", + "version": "v8.51.0", "source": { "type": "git", "url": "https://github.com/illuminate/cache.git", - "reference": "6f496f435e832fc15926677f7eac7213120a302e" + "reference": "7b16b5c4b5961aad0b3f43a8a6e22af872375099" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/illuminate/cache/zipball/6f496f435e832fc15926677f7eac7213120a302e", - "reference": "6f496f435e832fc15926677f7eac7213120a302e", + "url": "https://api.github.com/repos/illuminate/cache/zipball/7b16b5c4b5961aad0b3f43a8a6e22af872375099", + "reference": "7b16b5c4b5961aad0b3f43a8a6e22af872375099", "shasum": "" }, "require": { @@ -911,20 +911,20 @@ "issues": "https://github.com/laravel/framework/issues", "source": "https://github.com/laravel/framework" }, - "time": "2021-06-24T22:01:18+00:00" + "time": "2021-07-20T13:44:44+00:00" }, { "name": "illuminate/collections", - "version": "v8.50.0", + "version": "v8.51.0", "source": { "type": "git", "url": "https://github.com/illuminate/collections.git", - "reference": "f9311a35779750f38bed47456c031c4dc4962274" + "reference": "7942bc71aca503d2f2721469c650fce38b1058e3" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/illuminate/collections/zipball/f9311a35779750f38bed47456c031c4dc4962274", - "reference": "f9311a35779750f38bed47456c031c4dc4962274", + "url": "https://api.github.com/repos/illuminate/collections/zipball/7942bc71aca503d2f2721469c650fce38b1058e3", + "reference": "7942bc71aca503d2f2721469c650fce38b1058e3", "shasum": "" }, "require": { @@ -965,11 +965,11 @@ "issues": "https://github.com/laravel/framework/issues", "source": "https://github.com/laravel/framework" }, - "time": "2021-06-28T13:56:26+00:00" + "time": "2021-07-15T14:07:19+00:00" }, { "name": "illuminate/config", - "version": "v8.50.0", + "version": "v8.51.0", "source": { "type": "git", "url": "https://github.com/illuminate/config.git", @@ -1017,7 +1017,7 @@ }, { "name": "illuminate/console", - "version": "v8.50.0", + "version": "v8.51.0", "source": { "type": "git", "url": "https://github.com/illuminate/console.git", @@ -1077,7 +1077,7 @@ }, { "name": "illuminate/container", - "version": "v8.50.0", + "version": "v8.51.0", "source": { "type": "git", "url": "https://github.com/illuminate/container.git", @@ -1128,7 +1128,7 @@ }, { "name": "illuminate/contracts", - "version": "v8.50.0", + "version": "v8.51.0", "source": { "type": "git", "url": "https://github.com/illuminate/contracts.git", @@ -1176,16 +1176,16 @@ }, { "name": "illuminate/events", - "version": "v8.50.0", + "version": "v8.51.0", "source": { "type": "git", "url": "https://github.com/illuminate/events.git", - "reference": "bd2941d4d55f5d357b203dc2ed81ac5c138593dc" + "reference": "79b76fb37748584eaa21c569b001a6ccbc13c2b5" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/illuminate/events/zipball/bd2941d4d55f5d357b203dc2ed81ac5c138593dc", - "reference": "bd2941d4d55f5d357b203dc2ed81ac5c138593dc", + "url": "https://api.github.com/repos/illuminate/events/zipball/79b76fb37748584eaa21c569b001a6ccbc13c2b5", + "reference": "79b76fb37748584eaa21c569b001a6ccbc13c2b5", "shasum": "" }, "require": { @@ -1227,20 +1227,20 @@ "issues": "https://github.com/laravel/framework/issues", "source": "https://github.com/laravel/framework" }, - "time": "2021-04-06T19:21:57+00:00" + "time": "2021-07-14T15:12:04+00:00" }, { "name": "illuminate/filesystem", - "version": "v8.50.0", + "version": "v8.51.0", "source": { "type": "git", "url": "https://github.com/illuminate/filesystem.git", - "reference": "ae8d9051bc50c9551e6a251147b8dcdafcb60d32" + "reference": "f33219e5550f8f280169e933b91a95250920de06" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/illuminate/filesystem/zipball/ae8d9051bc50c9551e6a251147b8dcdafcb60d32", - "reference": "ae8d9051bc50c9551e6a251147b8dcdafcb60d32", + "url": "https://api.github.com/repos/illuminate/filesystem/zipball/f33219e5550f8f280169e933b91a95250920de06", + "reference": "f33219e5550f8f280169e933b91a95250920de06", "shasum": "" }, "require": { @@ -1289,11 +1289,11 @@ "issues": "https://github.com/laravel/framework/issues", "source": "https://github.com/laravel/framework" }, - "time": "2021-06-18T15:48:00+00:00" + "time": "2021-07-20T13:46:01+00:00" }, { "name": "illuminate/macroable", - "version": "v8.50.0", + "version": "v8.51.0", "source": { "type": "git", "url": "https://github.com/illuminate/macroable.git", @@ -1339,7 +1339,7 @@ }, { "name": "illuminate/pipeline", - "version": "v8.50.0", + "version": "v8.51.0", "source": { "type": "git", "url": "https://github.com/illuminate/pipeline.git", @@ -1387,16 +1387,16 @@ }, { "name": "illuminate/support", - "version": "v8.50.0", + "version": "v8.51.0", "source": { "type": "git", "url": "https://github.com/illuminate/support.git", - "reference": "383e46e8942402503b381c4994a968a8ee642087" + "reference": "ee397b851a411ad490363a47df7476a24f93ca2e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/illuminate/support/zipball/383e46e8942402503b381c4994a968a8ee642087", - "reference": "383e46e8942402503b381c4994a968a8ee642087", + "url": "https://api.github.com/repos/illuminate/support/zipball/ee397b851a411ad490363a47df7476a24f93ca2e", + "reference": "ee397b851a411ad490363a47df7476a24f93ca2e", "shasum": "" }, "require": { @@ -1451,20 +1451,20 @@ "issues": "https://github.com/laravel/framework/issues", "source": "https://github.com/laravel/framework" }, - "time": "2021-07-09T13:40:46+00:00" + "time": "2021-07-16T12:34:54+00:00" }, { "name": "illuminate/testing", - "version": "v8.50.0", + "version": "v8.51.0", "source": { "type": "git", "url": "https://github.com/illuminate/testing.git", - "reference": "12271f68f49bb1063754f7d9d168af1e719b251a" + "reference": "3008c52b58935e5f4713eed2d28779f3788fb481" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/illuminate/testing/zipball/12271f68f49bb1063754f7d9d168af1e719b251a", - "reference": "12271f68f49bb1063754f7d9d168af1e719b251a", + "url": "https://api.github.com/repos/illuminate/testing/zipball/3008c52b58935e5f4713eed2d28779f3788fb481", + "reference": "3008c52b58935e5f4713eed2d28779f3788fb481", "shasum": "" }, "require": { @@ -1509,7 +1509,7 @@ "issues": "https://github.com/laravel/framework/issues", "source": "https://github.com/laravel/framework" }, - "time": "2021-07-04T10:19:03+00:00" + "time": "2021-07-20T13:32:50+00:00" }, { "name": "jolicode/jolinotif", @@ -2358,16 +2358,16 @@ }, { "name": "nikic/php-parser", - "version": "v4.11.0", + "version": "v4.12.0", "source": { "type": "git", "url": "https://github.com/nikic/PHP-Parser.git", - "reference": "fe14cf3672a149364fb66dfe11bf6549af899f94" + "reference": "6608f01670c3cc5079e18c1dab1104e002579143" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/fe14cf3672a149364fb66dfe11bf6549af899f94", - "reference": "fe14cf3672a149364fb66dfe11bf6549af899f94", + "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/6608f01670c3cc5079e18c1dab1104e002579143", + "reference": "6608f01670c3cc5079e18c1dab1104e002579143", "shasum": "" }, "require": { @@ -2408,9 +2408,9 @@ ], "support": { "issues": "https://github.com/nikic/PHP-Parser/issues", - "source": "https://github.com/nikic/PHP-Parser/tree/v4.11.0" + "source": "https://github.com/nikic/PHP-Parser/tree/v4.12.0" }, - "time": "2021-07-03T13:36:55+00:00" + "time": "2021-07-21T10:44:31+00:00" }, { "name": "nunomaduro/collision", @@ -2693,16 +2693,16 @@ }, { "name": "pestphp/pest", - "version": "v1.10.0", + "version": "v1.11.0", "source": { "type": "git", "url": "https://github.com/pestphp/pest.git", - "reference": "d90ddf889cca8acf9f9a7ccbb36c34af8926f082" + "reference": "328427bfdb1c21687afff0eb83c64e417b4539c6" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/pestphp/pest/zipball/d90ddf889cca8acf9f9a7ccbb36c34af8926f082", - "reference": "d90ddf889cca8acf9f9a7ccbb36c34af8926f082", + "url": "https://api.github.com/repos/pestphp/pest/zipball/328427bfdb1c21687afff0eb83c64e417b4539c6", + "reference": "328427bfdb1c21687afff0eb83c64e417b4539c6", "shasum": "" }, "require": { @@ -2768,7 +2768,7 @@ ], "support": { "issues": "https://github.com/pestphp/pest/issues", - "source": "https://github.com/pestphp/pest/tree/v1.10.0" + "source": "https://github.com/pestphp/pest/tree/v1.11.0" }, "funding": [ { @@ -2796,7 +2796,7 @@ "type": "patreon" } ], - "time": "2021-07-12T10:54:40+00:00" + "time": "2021-07-21T11:59:45+00:00" }, { "name": "pestphp/pest-plugin", @@ -2872,16 +2872,16 @@ }, { "name": "phar-io/manifest", - "version": "2.0.1", + "version": "2.0.3", "source": { "type": "git", "url": "https://github.com/phar-io/manifest.git", - "reference": "85265efd3af7ba3ca4b2a2c34dbfc5788dd29133" + "reference": "97803eca37d319dfa7826cc2437fc020857acb53" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phar-io/manifest/zipball/85265efd3af7ba3ca4b2a2c34dbfc5788dd29133", - "reference": "85265efd3af7ba3ca4b2a2c34dbfc5788dd29133", + "url": "https://api.github.com/repos/phar-io/manifest/zipball/97803eca37d319dfa7826cc2437fc020857acb53", + "reference": "97803eca37d319dfa7826cc2437fc020857acb53", "shasum": "" }, "require": { @@ -2926,9 +2926,9 @@ "description": "Component for reading phar.io manifest information from a PHP Archive (PHAR)", "support": { "issues": "https://github.com/phar-io/manifest/issues", - "source": "https://github.com/phar-io/manifest/tree/master" + "source": "https://github.com/phar-io/manifest/tree/2.0.3" }, - "time": "2020-06-27T14:33:11+00:00" + "time": "2021-07-20T11:28:43+00:00" }, { "name": "phar-io/version", @@ -5071,6 +5071,7 @@ "type": "github" } ], + "abandoned": true, "time": "2020-09-28T06:45:17+00:00" }, {