Skip to content

Commit

Permalink
CLI-1410: self:info command (#1806)
Browse files Browse the repository at this point in the history
* CLI-1410: self:status command

* fix tests

* cleanup

* coverage
  • Loading branch information
danepowell authored Oct 1, 2024
1 parent 8c65600 commit de7fb0e
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 16 deletions.
12 changes: 7 additions & 5 deletions src/Command/CommandBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -761,17 +761,19 @@ private function findCloudApplicationByGitUrl(
return null;
}

protected function createTable(OutputInterface $output, string $title, array $headers, mixed $widths): Table
protected function createTable(OutputInterface $output, string $title, array $headers, ?array $widths = null): Table
{
$terminalWidth = (new Terminal())->getWidth();
$terminalWidth *= .90;
$table = new Table($output);
$table->setHeaders($headers);
$table->setHeaderTitle($title);
$setWidths = static function (mixed $width) use ($terminalWidth) {
return (int) ($terminalWidth * $width);
};
$table->setColumnWidths(array_map($setWidths, $widths));
if ($widths !== null) {
$setWidths = static function (float $width) use ($terminalWidth) {
return (int) ($terminalWidth * $width);
};
$table->setColumnWidths(array_map($setWidths, $widths));
}
return $table;
}

Expand Down
32 changes: 32 additions & 0 deletions src/Command/Self/SelfInfoCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Acquia\Cli\Command\Self;

use Acquia\Cli\Command\CommandBase;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

#[AsCommand(name: 'self:info', description: 'Print information about the running version of Acquia CLI')]

final class SelfInfoCommand extends CommandBase
{
protected function execute(InputInterface $input, OutputInterface $output): int
{
$table = $this->createTable($output, 'Acquia CLI information', ['Property', 'Value']);
$table->addRow(['Version', $this->getApplication()->getVersion()]);
$table->addRow(['Cloud datastore', $this->datastoreCloud->filepath]);
$table->addRow(['ACLI datastore', $this->datastoreAcli->filepath]);
$table->addRow(['Telemetry enabled', var_export($this->telemetryHelper->telemetryEnabled(), true)]);
$table->addRow(['User ID', $this->telemetryHelper->getUserId()]);
foreach ($this->telemetryHelper->getTelemetryUserData() as $key => $value) {
$table->addRow([$key, $value]);
}
$table->render();
return Command::SUCCESS;
}
}
17 changes: 10 additions & 7 deletions src/Helpers/TelemetryHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ public function initializeBugsnag(): void
if (empty($this->bugSnagKey)) {
return;
}
$sendTelemetry = $this->datastoreCloud->get(DataStoreContract::SEND_TELEMETRY);
if ($sendTelemetry === false) {
if (!$this->telemetryEnabled()) {
return;
}
// It's safe-ish to make this key public.
Expand Down Expand Up @@ -89,11 +88,10 @@ public function initializeAmplitude(): void
if (empty($this->amplitudeKey)) {
return;
}
$sendTelemetry = $this->datastoreCloud->get(DataStoreContract::SEND_TELEMETRY);
$amplitude = Amplitude::getInstance();
$amplitude->setOptOut($sendTelemetry === false);
$amplitude->setOptOut($this->telemetryEnabled());

if ($sendTelemetry === false) {
if (!$this->telemetryEnabled()) {
return;
}
try {
Expand All @@ -109,6 +107,11 @@ public function initializeAmplitude(): void
}
}

public function telemetryEnabled(): bool
{
return (bool) $this->datastoreCloud->get(DataStoreContract::SEND_TELEMETRY);
}

/**
* @param string $ah_env
* Environment name from AH_ENV.
Expand Down Expand Up @@ -140,7 +143,7 @@ public static function normalizeAhEnv(string $ah_env): string
*
* @return array<mixed> Telemetry user data.
*/
private function getTelemetryUserData(): array
public function getTelemetryUserData(): array
{
$data = [
'ah_app_uuid' => getenv('AH_APPLICATION_UUID'),
Expand Down Expand Up @@ -179,7 +182,7 @@ public static function getEnvironmentProvider(): ?string
return null;
}

private function getUserId(): ?string
public function getUserId(): ?string
{
$user = $this->getUserData();
if ($user && isset($user['uuid'])) {
Expand Down
1 change: 1 addition & 0 deletions tests/phpunit/src/Application/KernelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ private function getEnd(): string
remote:ssh [ssh] Use SSH to open a shell or run a command in a Cloud Platform environment
self
self:clear-caches [cc|cr] Clears local Acquia CLI caches
self:info Print information about the running version of Acquia CLI
self:telemetry:disable [telemetry:disable] Disable anonymous sharing of usage and performance data
self:telemetry:enable [telemetry:enable] Enable anonymous sharing of usage and performance data
self:telemetry:toggle [telemetry] Toggle anonymous sharing of usage and performance data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Acquia\Cli\Tests\Commands;
namespace Acquia\Cli\Tests\Commands\Self;

use Acquia\Cli\Command\CommandBase;
use Acquia\Cli\Command\Ide\IdeListCommand;
Expand Down
38 changes: 38 additions & 0 deletions tests/phpunit/src/Commands/Self/SelfInfoCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Acquia\Cli\Tests\Commands\Self;

use Acquia\Cli\Command\CommandBase;
use Acquia\Cli\Command\Self\SelfInfoCommand;
use Acquia\Cli\Tests\CommandTestBase;

/**
* @property \Acquia\Cli\Command\Self\SelfInfoCommand $command
*/
class SelfInfoCommandTest extends CommandTestBase
{
protected function createCommand(): CommandBase
{
return $this->injectCommand(SelfInfoCommand::class);
}

/**
* @throws \Exception
*/
public function testSelfInfoCommand(): void
{
$this->mockRequest('getAccount');
$this->executeCommand();
$output = $this->getDisplay();
$this->assertStringContainsString('Property', $output);
$this->assertStringContainsString('--------', $output);
$this->assertStringContainsString('Version', $output);
$this->assertStringContainsString('Cloud datastore', $output);
$this->assertStringContainsString('ACLI datastore', $output);
$this->assertStringContainsString('Telemetry enabled', $output);
$this->assertStringContainsString('User ID', $output);
$this->assertStringContainsString('is_acquian', $output);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Acquia\Cli\Tests\Commands;
namespace Acquia\Cli\Tests\Commands\Self;

use Acquia\Cli\Command\App\LinkCommand;
use Acquia\Cli\Command\CommandBase;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Acquia\Cli\Tests\Commands;
namespace Acquia\Cli\Tests\Commands\Self;

use Acquia\Cli\Command\CommandBase;
use Acquia\Cli\Command\Self\TelemetryDisableCommand;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Acquia\Cli\Tests\Commands;
namespace Acquia\Cli\Tests\Commands\Self;

use Acquia\Cli\Command\CommandBase;
use Acquia\Cli\Command\Self\TelemetryEnableCommand;
Expand Down

0 comments on commit de7fb0e

Please sign in to comment.