Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev/add custom key to fileloader #1917

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/Host/FileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,25 @@ public function expandOnLoad($datain)

return $dataout;
}

/**
* @param string $file
* @param string|null $key
* @return $this
* @throws Exception
*/
public function load($file)
public function load($file, $key = null)
{
if (!file_exists($file) || !is_readable($file)) {
throw new Exception("File `$file` doesn't exists or isn't readable.");
}

$data = Yaml::parse(file_get_contents($file));

if ($key !== null && $key !== '') {
$data = $data[$key];
}

$data = $this->expandOnLoad($data);

if (!is_array($data)) {
Expand Down
5 changes: 3 additions & 2 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,14 @@ function localhost(...$hostnames)
* Load list of hosts from file
*
* @param string $file
* @param string|null $key
* @return Proxy
*/
function inventory($file)
function inventory($file, $key = null)
{
$deployer = Deployer::get();
$fileLoader = new FileLoader();
$fileLoader->load($file);
$fileLoader->load($file, $key);

$hosts = $fileLoader->getHosts();
foreach ($hosts as $host) {
Expand Down
48 changes: 48 additions & 0 deletions test/fixture/inventory-with-custom-key.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
.base: &base
deploy_path: path
roles:
- a
- b
- c
param: param

custom-key:
foo:
<<: *base
extra: extra

bar:
hostname: bar.com
user: user
port: 22
configFile: configFile
identityFile: identityFile
forwardAgent: true
multiplexing: false
sshOptions:
Option: Value
sshFlags:
-f:
-someFlag: value
param: param

local:
local: -
deploy_to: /var/local
param: local

app.deployer.org:
stage: production
roles: app
deploy_path: ~/app

db[1:2].deployer.org:
stage: production
roles: db

beta.deployer.org:
stage: beta
roles:
- app
- db
deploy_path: ~/app
39 changes: 39 additions & 0 deletions test/src/Host/FileLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,45 @@ public function testLoad()
self::assertEquals('db2.deployer.org', $db2->getHostname());
}

public function testLoadFromCustomKey()
{
$this->hosts = (new FileLoader())
->load(__DIR__ . '/../../fixture/inventory-with-custom-key.yml', 'custom-key')
->getHosts();


// foo extends .base
$foo = $this->getHost('foo');
self::assertInstanceOf(Host::class, $foo);
self::assertEquals(['a', 'b', 'c'], $foo->get('roles'));

// local is Localhost
$local = $this->getHost('local');
self::assertInstanceOf(Localhost::class, $local);
self::assertEquals('/var/local', $local->get('deploy_to'));

// bar configured properly
$bar = $this->getHost('bar');
self::assertEquals('bar', $bar->getHostname());
self::assertEquals('user@bar.com', "$bar");
self::assertEquals('user', $bar->getUser());
self::assertEquals(22, $bar->getPort());
self::assertEquals('configFile', $bar->getConfigFile());
self::assertEquals('identityFile', $bar->getIdentityFile());
self::assertTrue($bar->isForwardAgent());
self::assertFalse($bar->isMultiplexing());
self::assertEquals('param', $bar->get('param'));
self::assertEquals(
'-f -A -someFlag value -p 22 -F configFile -i identityFile -o Option=Value',
$bar->getSshArguments()->getCliArguments()
);

$db1 = $this->getHost('db1.deployer.org');
self::assertEquals('db1.deployer.org', $db1->getHostname());
$db2 = $this->getHost('db2.deployer.org');
self::assertEquals('db2.deployer.org', $db2->getHostname());
}

/**
* @param $name
* @return Host|null
Expand Down