Completed
Push — master ( 716db6...a32038 )
by Alireza
02:53
created

ServerSSHCommand::execute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 2
dl 0
loc 25
rs 9.52
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Ssh to server
5
 *
6
 * @author Alireza Josheghani <[email protected]>
7
 * @since  29 Sep 2018
8
 */
9
10
namespace Josh\Console\Commands;
11
12
use Josh\Console\ConsoleStyle as Style;
13
use Symfony\Component\Console\Input\InputArgument;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Output\OutputInterface;
16
17
class ServerSSHCommand extends ServerCommand
18
{
19
20
    /**
21
     * configure command
22
     *
23
     * @return void
24
     */
25
    public function configure()
26
    {
27
        $this->setName('server:ssh')
28
            ->setDescription('ssh to the server');
29
30
        $this->addArgument('server', InputArgument::REQUIRED, "Server id or name");
31
    }
32
33
    /**
34
     * execute command
35
     *
36
     * @param InputInterface $input
37
     * @param OutputInterface $output
38
     */
39
    protected function execute(InputInterface $input , OutputInterface $output)
40
    {
41
        $command = new Style($input, $output);
42
43
        $servers = $this->model->all();
44
45
        $serverIdOrName = $input->getArgument('server');
46
47
        if ($servers->count() > 0) {
48
49
            $server = $this->model->findBy("name", $serverIdOrName);
50
51
            $server = ( $server->exists() ? $server : $this->model->findBy("id", $serverIdOrName) );
52
53
            $ip = $server->getAttribute("ip");
54
55
            $command->info("Connecting to [$ip]...");
56
57
            echo system("ssh root@$ip");
58
59
        } else {
60
61
            $command->line("No server added to the list. use [ server:add ] to add one.");
62
        }
63
    }
64
65
    /** Get home directory
66
     *
67
     * @return array
68
     */
69
    public function getHomeDir()
70
    {
71
        if(empty($_SERVER['HOME'])){
72
            return posix_getpwuid(posix_getuid());
73
        }
74
75
        return $_SERVER['HOME'];
76
    }
77
}
78