1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Tail; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Illuminate\Console\Command; |
7
|
|
|
use Spatie\Ssh\Ssh; |
8
|
|
|
use Symfony\Component\Process\Process; |
9
|
|
|
|
10
|
|
|
class TailCommand extends Command |
11
|
|
|
{ |
12
|
|
|
protected $signature = 'tail {environment?} |
13
|
|
|
{--file= : Name of the log file to tail} |
14
|
|
|
{--lines=0 : Output the last number of lines} |
15
|
|
|
{--clear : Clear the terminal screen} |
16
|
|
|
{--grep= : Grep specified string} |
17
|
|
|
{--debug : Display the underlying tail command}'; |
18
|
|
|
|
19
|
|
|
protected $description = 'Tail the latest logfile'; |
20
|
|
|
|
21
|
|
|
public function handle() |
22
|
|
|
{ |
23
|
|
|
if ($this->option('debug')) { |
24
|
|
|
$this->info($this->getTailCommand()); |
25
|
|
|
|
26
|
|
|
return; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
$this->handleClearOption(); |
30
|
|
|
|
31
|
|
|
$environment = $this->argument('environment'); |
32
|
|
|
|
33
|
|
|
is_null($environment) |
34
|
|
|
? $this->tailLocally() |
35
|
|
|
: $this->tailRemotely($environment); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
protected function handleClearOption() |
39
|
|
|
{ |
40
|
|
|
if (! $this->option('clear')) { |
41
|
|
|
return; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$this->output->write(sprintf("\033\143\e[3J")); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
protected function tailLocally(): void |
48
|
|
|
{ |
49
|
|
|
$logDirectory = storage_path('logs'); |
50
|
|
|
|
51
|
|
|
Process::fromShellCommandline($this->getTailCommand(), $logDirectory) |
52
|
|
|
->setTty(true) |
53
|
|
|
->setTimeout(null) |
54
|
|
|
->run(function ($type, $line) { |
55
|
|
|
$this->handleClearOption(); |
56
|
|
|
|
57
|
|
|
$this->output->write($line); |
58
|
|
|
}); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
protected function tailRemotely(string $environment): void |
62
|
|
|
{ |
63
|
|
|
$environmentConfig = $this->getEnvironmentConfiguration($environment); |
64
|
|
|
|
65
|
|
|
Ssh::create($environmentConfig['user'], $environmentConfig['host']) |
66
|
|
|
->configureProcess(fn (Process $process) => $process->setTty(true)) |
|
|
|
|
67
|
|
|
->onOutput(function ($type, $line) { |
68
|
|
|
$this->handleClearOption(); |
69
|
|
|
|
70
|
|
|
$this->output->write($line); |
71
|
|
|
}) |
72
|
|
|
->execute([ |
73
|
|
|
"cd {$environmentConfig['log_directory']}", |
74
|
|
|
$this->getTailCommand(), |
75
|
|
|
]); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
protected function getEnvironmentConfiguration(string $environment): array |
79
|
|
|
{ |
80
|
|
|
$config = config('tail'); |
81
|
|
|
|
82
|
|
|
if (! isset($config[$environment])) { |
83
|
|
|
throw new Exception("No configuration set for environment `{$environment}`. Make sure this environment is specified in the `tail` config file!"); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $config[$environment]; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function getTailFile(): string |
90
|
|
|
{ |
91
|
|
|
$environment = $this->argument('environment'); |
92
|
|
|
$environmentConfig = is_null($environment) |
93
|
|
|
? [] |
94
|
|
|
: $this->getEnvironmentConfiguration($environment); |
95
|
|
|
|
96
|
|
|
return $this->option('file') |
97
|
|
|
?? $environmentConfig['file'] |
98
|
|
|
?? "`ls -t | head -1`"; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function getTailCommand(): string |
102
|
|
|
{ |
103
|
|
|
$grep = $this->option('grep') |
104
|
|
|
? ' | grep "'.$this->option('grep').'"' |
105
|
|
|
: ''; |
106
|
|
|
$file = $this->getTailFile(); |
107
|
|
|
|
108
|
|
|
return 'tail -f -n '.$this->option('lines').' "'.$file.'"'.$grep; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|