diff --git a/.gitignore b/.gitignore index f93b75e..ce16017 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ /.idea /.vscode /.vagrant +/tests/.spinupwp .phpunit.result.cache .php-cs-fixer.cache composer.lock diff --git a/README.md b/README.md index e8c9033..5403fb9 100644 --- a/README.md +++ b/README.md @@ -10,3 +10,29 @@ To get started, require the package globally via [Composer](https://getcomposer. ```bash composer global require deliciousbrains/spinupwp-cli ``` +In addition, you should make sure the `~/.composer/vendor/bin` directory is in your system's "PATH". + +## Usage +Installing the SpinupWP CLI provides access to the `spinupwp` command. +```bash +spinupwp +```` +You will need to generate an API token to interact with the SpinupWP CLI. After you have generated an API token, you should configure your default profile: +```bash +spinupwp configure +```` +You can configure multiple profiles, which is useful if you're a member of multiple teams: +```bash +spinupwp configure --profile=hellfishmedia +``` +To run a command using a specific profile, pass the profile option: +```bash +spinupwp servers:list --profile=hellfishmedia +``` +If no profile is supplied, your default profile will be used (if configured). + +## Upgrade +To update the SpinupWP CLI to the latest version, run: +```bash +composer global update +``` diff --git a/app/Helpers/Configuration.php b/app/Helpers/Configuration.php index 3b94fcc..5e60641 100644 --- a/app/Helpers/Configuration.php +++ b/app/Helpers/Configuration.php @@ -6,13 +6,13 @@ class Configuration { - /** - * @var array - */ - protected $config; + protected string $path; - public function __construct() + protected array $config; + + public function __construct(string $path) { + $this->path = $path; $this->config = $this->readConfig(); } @@ -74,17 +74,10 @@ protected function readConfig(): array public function configFilePath(): string { - return $this->getConfigPath() . 'config.json'; - } - - protected function getConfigPath(): string - { - $userHome = config('app.config_path') . '/.spinupwp/'; - - if (!file_exists($userHome)) { - mkdir($userHome); + if (!file_exists($this->path)) { + mkdir($this->path); } - return $userHome; + return $this->path . 'config.json'; } } diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index f36fe3f..10d884e 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -27,6 +27,14 @@ public function register() { $this->app->singleton(SpinupWp::class, fn ($app) => new SpinupWp()); - $this->app->singleton(Configuration::class, fn ($app) => new Configuration()); + $this->app->singleton(Configuration::class, function () { + $path = isset($_ENV['APP_ENV']) && $_ENV['APP_ENV'] === 'testing' + ? base_path('tests') + : ($_SERVER['HOME'] ?? $_SERVER['USERPROFILE']); + + $path .= '/.spinupwp/'; + + return new Configuration($path); + }); } } diff --git a/builds/spinupwp b/builds/spinupwp index b761202..801b777 100755 Binary files a/builds/spinupwp and b/builds/spinupwp differ diff --git a/config/app.php b/config/app.php index 5f1784d..f16cd66 100644 --- a/config/app.php +++ b/config/app.php @@ -26,7 +26,7 @@ | */ - 'version' => app('git.version'), + 'version' => '0.1.1', /* |-------------------------------------------------------------------------- @@ -62,6 +62,4 @@ 'table', ], - 'config_path' => $_SERVER['HOME'] ?? $_SERVER['USERPROFILE'], - ]; diff --git a/phpunit.xml.dist b/phpunit.xml.dist index dba39c3..8971436 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -21,4 +21,7 @@ ./app + + + diff --git a/tests/Feature/Commands/ConfigureCommandTest.php b/tests/Feature/Commands/ConfigureCommandTest.php index 0e36ba9..6f9103d 100644 --- a/tests/Feature/Commands/ConfigureCommandTest.php +++ b/tests/Feature/Commands/ConfigureCommandTest.php @@ -3,7 +3,6 @@ use App\Helpers\Configuration; beforeEach(function () { - setConfigPath(); deleteTestConfigFile(); }); @@ -16,5 +15,5 @@ ->expectsQuestion('Default output format (json/table)', 'json') ->expectsOutput('SpinupWP CLI configured successfully'); - expect((new Configuration)->get('api_token'))->toEqual('my-spinupwp-api-token'); + expect((resolve(Configuration::class))->get('api_token'))->toEqual('my-spinupwp-api-token'); }); diff --git a/tests/Pest.php b/tests/Pest.php index 0052ac6..92517b1 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -51,16 +51,9 @@ | */ -function setConfigPath() -{ - $mockPath = storage_path(); - Config::set('app.config_path', $mockPath); -} - function setTestConfigFile() { - setConfigPath(); - $config = new Configuration(); + $config = resolve(Configuration::class); file_put_contents($config->configFilePath(), json_encode([ 'default' => [ 'api_token' => 'myapikey123', @@ -71,8 +64,7 @@ function setTestConfigFile() function deleteTestConfigFile($test = '') { - setConfigPath(); - $configFile = (new Configuration)->configFilePath(); + $configFile = (resolve(Configuration::class))->configFilePath(); if (!file_exists($configFile)) { return; } diff --git a/tests/Unit/Helpers/ConfigurationTest.php b/tests/Unit/Helpers/ConfigurationTest.php index d94f814..ea93fce 100644 --- a/tests/Unit/Helpers/ConfigurationTest.php +++ b/tests/Unit/Helpers/ConfigurationTest.php @@ -7,7 +7,7 @@ }); test('isConfigured method', function () { - $config = new Configuration(); + $config = resolve(Configuration::class); $isConfigured = $config->isConfigured(); expect($isConfigured)->toBeFalse(); setTestConfigFile(); @@ -17,12 +17,12 @@ test('get method', function () { setTestConfigFile(); - $config = new Configuration(); + $config = resolve(Configuration::class); expect($config->get('api_token'))->toEqual('myapikey123'); }); test('set method', function () { - $config = new Configuration(); + $config = resolve(Configuration::class); // first time $config->set('api_token', 'mynewapitoken');