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

Add prompt/option for username parameter required by forge api #70

Merged
merged 4 commits into from
Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions app/Commands/SshConfigureCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ class SshConfigureCommand extends Command
protected $signature = 'ssh:configure
{server? : The server name}
{--key= : The path to the public key}
{--name= : The key name on Forge}';
{--name= : The key name on Forge}
{--user= : The server username}';

/**
* The description of the command.
Expand All @@ -38,7 +39,7 @@ public function handle()

$key = $this->getKey();

$privateKey = $this->ensureKeyExists($this->getKeyName($key), $key);
$privateKey = $this->ensureKeyExists($this->getKeyName($key), $key, $this->getServerUsername());

$this->callSilently('ssh:test', ['--key' => $privateKey]);

Expand All @@ -50,9 +51,10 @@ public function handle()
*
* @param string $name
* @param string|null $key
* @param string $username
* @return string
*/
protected function ensureKeyExists($name, $key = null)
protected function ensureKeyExists($name, $key = null, $username = 'forge')
{
$server = $this->currentServer();

Expand All @@ -66,7 +68,7 @@ protected function ensureKeyExists($name, $key = null)

$this->step('Adding Key <comment>['.$localName.']</comment>'.' With The Name <comment>['.$name.']</comment> To Server <comment>['.$server->name.']</comment>');

$this->forge->createSSHKey($server->id, ['key' => $key, 'name' => $name], true);
$this->forge->createSSHKey($server->id, ['key' => $key, 'name' => $name, 'username' => $username], true);

return $this->keys->keysPath().'/'.basename($localName, '.pub');
}
Expand Down Expand Up @@ -111,4 +113,16 @@ protected function getKeyName($key)

return $this->option('name') ?: $this->askStep($question, get_current_user());
}
}

/**
* Prompt for a "server user".
*
* @return string
*/
protected function getServerUsername()
{
$question = 'What username should we use for the selected server';

return $this->option('user') ?: $this->askStep($question, 'forge');
}
}
6 changes: 5 additions & 1 deletion tests/Feature/SshConfigureCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
$this->forge->shouldReceive('createSSHKey')->with(1, [
'name' => 'driesvints',
'key' => 'MY KEY Content',
'username' => 'morales2k',
], true)->once();

$this->remote->shouldReceive('resolvePrivateKeyUsing')->once();
Expand All @@ -41,6 +42,7 @@
->expectsQuestion('<fg=yellow>‣</> <options=bold>Which Server Would You Like To Configure The SSH Key Based Secure Authentication</>', 1)
->expectsQuestion('<fg=yellow>‣</> <options=bold>Which Key Would You Like To Use</>', 0)
->expectsQuestion('<fg=yellow>‣</> <options=bold>What Should The SSH Key Be Named</>', 'driesvints')
->expectsQuestion('<fg=yellow>‣</> <options=bold>What Username Should We Use For The Selected Server</>', 'morales2k')
->expectsOutput('==> Creating Key [driesvints_rsa.pub]')
->expectsOutput('==> Adding Key [driesvints_rsa.pub] With The Name [driesvints] To Server [production]')
->expectsOutput('==> SSH Key Based Secure Authentication Configured Successfully');
Expand Down Expand Up @@ -78,6 +80,7 @@
$this->forge->shouldReceive('createSSHKey')->with(2, [
'name' => 'driesvints',
'key' => 'MY KEY Content',
'username' => 'morales2k',
], true)->once();

$this->remote->shouldReceive('resolvePrivateKeyUsing')->once();
Expand All @@ -86,6 +89,7 @@
$this->artisan('ssh:configure', ['server' => 2])
->expectsQuestion('<fg=yellow>‣</> <options=bold>Which Key Would You Like To Use</>', 1)
->expectsQuestion('<fg=yellow>‣</> <options=bold>What Should The SSH Key Be Named In Forge</>', 'driesvints')
->expectsQuestion('<fg=yellow>‣</> <options=bold>What Username Should We Use For The Selected Server</>', 'morales2k')
->expectsOutput('==> Adding Key [id_rsa.pub] With The Name [driesvints] To Server [production]')
->expectsOutput('==> SSH Key Based Secure Authentication Configured Successfully');
});
});