Skip to content

Commit

Permalink
Merge pull request #938 from aryehraber/feat/diagnose
Browse files Browse the repository at this point in the history
Add "diagnose" command
  • Loading branch information
mattstauffer authored Apr 28, 2020
2 parents 605af93 + 1535605 commit c9f3b14
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 0 deletions.
156 changes: 156 additions & 0 deletions cli/Valet/Diagnose.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<?php

namespace Valet;

use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Output\ConsoleOutput;

class Diagnose
{
var $commands = [
'sw_vers',
'valet --version',
'cat ~/.config/valet/config.json',
'cat ~/.composer/composer.json',
'composer global diagnose',
'composer global outdated',
'ls -al /etc/sudoers.d/',
'brew update > /dev/null 2>&1',
'brew config',
'brew services list',
'brew list --versions | grep -E "(php|nginx|dnsmasq|mariadb|mysql|mailhog|openssl)(@\d\..*)?\s"',
'brew outdated',
'php -v',
'which -a php',
'php --ini',
'nginx -v',
'curl --version',
'php --ri curl',
'~/.composer/vendor/laravel/valet/bin/ngrok version',
'ls -al ~/.ngrok2',
'brew info nginx',
'brew info php',
'brew info openssl',
'openssl version -a',
'openssl ciphers',
'sudo nginx -t',
'which -a php-fpm',
'/usr/local/opt/php/sbin/php-fpm -v',
'sudo /usr/local/opt/php/sbin/php-fpm -y '.PHP_SYSCONFDIR.'/php-fpm.conf --test',
'ls -al ~/Library/LaunchAgents',
'ls -al /Library/LaunchAgents',
'ls -al /Library/LaunchDaemons',
];

var $cli, $files, $print, $progressBar;

/**
* Create a new Diagnose instance.
*
* @param CommandLine $cli
* @param Filesystem $files
* @return void
*/
function __construct(CommandLine $cli, Filesystem $files)
{
$this->cli = $cli;
$this->files = $files;
}

/**
* Run diagnostics.
*/
function run($print, $plainText)
{
$this->print = $print;

$this->beforeRun();

$results = collect($this->commands)->map(function ($command) {
$this->beforeCommand($command);

$output = $this->runCommand($command);

if ($this->ignoreOutput($command)) return;

$this->afterCommand($command, $output);

return compact('command', 'output');
})->filter()->values();

$output = $this->format($results, $plainText);

$this->files->put('valet_diagnostics.txt', $output);

$this->cli->run('pbcopy < valet_diagnostics.txt');

$this->files->unlink('valet_diagnostics.txt');

$this->afterRun();
}

function beforeRun()
{
if ($this->print) {
return;
}

$this->progressBar = new ProgressBar(new ConsoleOutput, count($this->commands));

$this->progressBar->start();
}

function afterRun()
{
if ($this->progressBar) {
$this->progressBar->finish();
}

output('');
}

function runCommand($command)
{
return strpos($command, 'sudo ') === 0
? $this->cli->run($command)
: $this->cli->runAsUser($command);
}

function beforeCommand($command)
{
if ($this->print) {
info(PHP_EOL."$ $command");
}
}

function afterCommand($command, $output)
{
if ($this->print) {
output(trim($output));
} else {
$this->progressBar->advance();
}
}

function ignoreOutput($command)
{
return strpos($command, '> /dev/null 2>&1') !== false;
}

function format($results, $plainText)
{
return $results->map(function ($result) use ($plainText) {
$command = $result['command'];
$output = trim($result['output']);

if ($plainText) {
return implode(PHP_EOL, ["$ {$command}", $output]);
}

return sprintf(
'<details>%s<summary>%s</summary>%s<pre>%s</pre>%s</details>',
PHP_EOL, $command, PHP_EOL, $output, PHP_EOL
);
})->implode($plainText ? PHP_EOL.str_repeat('-', 20).PHP_EOL : PHP_EOL);
}
}
1 change: 1 addition & 0 deletions cli/includes/facades.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Brew extends Facade {}
class Nginx extends Facade {}
class CommandLine extends Facade {}
class Configuration extends Facade {}
class Diagnose extends Facade {}
class DnsMasq extends Facade {}
class Filesystem extends Facade {}
class Ngrok extends Facade {}
Expand Down
14 changes: 14 additions & 0 deletions cli/valet.php
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,20 @@
})->descriptions('Determine directory-listing behavior. Default is off, which means a 404 will display.', [
'status' => 'on or off. (default=off) will show a 404 page; [on] will display a listing if project folder exists but requested URI not found'
]);

/**
* Output diagnostics to aid in debugging Valet.
*/
$app->command('diagnose [-p|--print] [--plain]', function ($print, $plain) {
info('Running diagnostics... (this may take a while)');

Diagnose::run($print, $plain);

info('Diagnostics output has been copied to your clipboard.');
})->descriptions('Output diagnostics to aid in debugging Valet.', [
'--print' => 'print diagnostics output while running',
'--plain' => 'format clipboard output as plain text',
]);
}

/**
Expand Down

0 comments on commit c9f3b14

Please sign in to comment.