Skip to content

Commit

Permalink
CLI-1191: Support XDG Base Dir (#1619)
Browse files Browse the repository at this point in the history
* CLI-1191: Support XDG Base Dir

* add tests
  • Loading branch information
danepowell authored Oct 31, 2023
1 parent b1a1914 commit 86af6e4
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 2 deletions.
3 changes: 3 additions & 0 deletions bin/acli
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ $input = new ArgvInput();
if (!getenv('HOME')) {
putenv('HOME=' . LocalMachineHelper::getHomeDir());
}
if (!getenv('ACLI_HOME')) {
putenv('ACLI_HOME=' . LocalMachineHelper::getConfigDir());
}
$kernel = new Kernel('prod', false);

// Handle a clear-kernel-cache pseudo command. This isn't implemented as a true console
Expand Down
2 changes: 1 addition & 1 deletion config/prod/services.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
parameters:
env(ACLI_REPO_ROOT): "%kernel.project_dir%"
app.data_dir: "%env(HOME)%/.acquia"
app.data_dir: "%env(ACLI_HOME)%"
app.project_dir: "%env(ACLI_REPO_ROOT)%"
app.ssh_dir: "%env(HOME)%/.ssh"
app.acli_config_filename: '.acquia-cli.yml'
Expand Down
12 changes: 12 additions & 0 deletions src/Helpers/LocalMachineHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,18 @@ public static function getHomeDir(): string {
return $home;
}

public static function getConfigDir(): string {
$home = self::getHomeDir();
$legacyDir = Path::join($home, '.acquia');
if (file_exists($legacyDir)) {
return $legacyDir;
}
if ($xdgHome = getenv('XDG_CONFIG_HOME')) {
return Path::join($xdgHome, 'acquia');
}
return Path::join($home, '.config', 'acquia');
}

/**
* Get the project root directory for the working directory.
*
Expand Down
21 changes: 21 additions & 0 deletions tests/phpunit/src/Misc/LocalMachineHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,25 @@ public function testHomeDirWindowsMing(): void {
LocalMachineHelper::getHomeDir();
}

public function testConfigDirLegacy(): void {
self::setEnvVars(['HOME' => 'vfs://root']);
$configDir = LocalMachineHelper::getConfigDir();
$this->assertEquals('vfs://root/.acquia', $configDir);
}

public function testConfigDirFromXdg(): void {
self::setEnvVars(['XDG_CONFIG_HOME' => 'vfs://root/.config']);
$configDir = LocalMachineHelper::getConfigDir();
$this->assertEquals('vfs://root/.config/acquia', $configDir);
}

public function testConfigDirDefault(): void {
self::setEnvVars(['HOME' => 'vfs://root']);
self::unsetEnvVars(['XDG_CONFIG_HOME']);
unlink('vfs://root/.acquia/cloud_api.conf');
rmdir('vfs://root/.acquia');
$configDir = LocalMachineHelper::getConfigDir();
$this->assertEquals('vfs://root/.config/acquia', $configDir);
}

}
2 changes: 1 addition & 1 deletion tests/phpunit/src/TestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ protected function setUp(): void {
$this->vfsRoot = vfsStream::setup();
$this->projectDir = vfsStream::newDirectory('project')->at($this->vfsRoot)->url();
$this->sshDir = vfsStream::newDirectory('ssh')->at($this->vfsRoot)->url();
$this->dataDir = vfsStream::newDirectory('data')->at($this->vfsRoot)->url();
$this->dataDir = vfsStream::newDirectory('.acquia')->at($this->vfsRoot)->url();
$this->cloudConfigFilepath = Path::join($this->dataDir, 'cloud_api.conf');
$this->acliConfigFilename = '.acquia-cli.yml';
$this->acliConfigFilepath = Path::join($this->projectDir, $this->acliConfigFilename);
Expand Down

0 comments on commit 86af6e4

Please sign in to comment.