Cleanup of some console commands

This commit is contained in:
Nabeel Shahzad 2017-12-29 10:23:42 -06:00
parent a5c5518a12
commit 5ec99167e8
7 changed files with 131 additions and 52 deletions

View File

@ -0,0 +1,51 @@
<?php
namespace App\Console;
use Illuminate\Console\Command;
use Symfony\Component\Process\Process;
class BaseCommand extends Command
{
/**
* Streaming file read
* @param $filename
* @return \Generator
*/
public function readFile($filename)
{
$fp = fopen($filename, 'rb');
while (($line = fgets($fp)) !== false) {
$line = rtrim($line, "\r\n");
if ($line[0] === ';') {
continue;
}
yield $line;
}
fclose($fp);
}
/**
* @param $cmd
*/
public function runCommand($cmd)
{
if (is_array($cmd))
$cmd = join(' ', $cmd);
$this->info('Running "' . $cmd . '"');
$process = new Process($cmd);
$process->run(function ($type, $buffer) {
if (Process::ERR === $type) {
echo $buffer;
} else {
echo $buffer;
}
});
}
}

View File

@ -2,14 +2,13 @@
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Collection;
use GuzzleHttp\Client;
use App\Console\BaseCommand;
use App\Facades\Utils;
use Symfony\Component\Console\Input\InputOption;
class AcarsReplay extends Command
class AcarsReplay extends BaseCommand
{
protected $signature = 'phpvms:replay {files} {--manual} {--write-all} {--no-submit}';
protected $description = 'Replay an ACARS file';

View File

@ -2,13 +2,9 @@
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Console\BaseCommand;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;
class CreateDatabase extends Command
class CreateDatabase extends BaseCommand
{
protected $signature = 'database:create {--reset} {--conn=?}';
protected $description = 'Create a database';
@ -20,21 +16,6 @@ class CreateDatabase extends Command
$this->os = new \Tivie\OS\Detector();
}
protected function runCommand($cmd)
{
$cmd = join(' ', $cmd);
$this->info('Running "' . $cmd . '"');
$proc = new Process($cmd);
$proc->run();
if (!$proc->isSuccessful()) {
throw new ProcessFailedException($proc);
}
echo $proc->getOutput();
}
/**
* create the mysql database
* @param $dbkey

View File

@ -0,0 +1,69 @@
<?php
namespace App\Console\Commands;
use DB;
use App\Console\BaseCommand;
use App\Models\Acars;
use App\Models\Pirep;
class DevCommands extends BaseCommand
{
protected $signature = 'phpvms:dev {cmd}';
protected $description = 'Developer commands';
/**
* Run dev related commands
*/
public function handle()
{
$command = trim($this->argument('cmd'));
if (!$command) {
$this->error('No command specified!');
exit();
}
$commands = [
'clear-acars' => 'clearAcars',
'compile-assets' => 'compileAssets',
];
if(!array_key_exists($command, $commands)) {
$this->error('Command not found!');
exit();
}
$this->{$commands[$command]}();
}
/**
* Delete all the data from the ACARS and PIREP tables
*/
protected function clearAcars()
{
if(config('database.default') === 'mysql') {
DB::statement('SET foreign_key_checks=0');
}
Acars::truncate();
Pirep::truncate();
if (config('database.default') === 'mysql') {
DB::statement('SET foreign_key_checks=1');
}
$this->info('ACARS and PIREPs cleared!');
}
/**
* Compile all the CSS/JS assets into their respective files
* Calling the webpack compiler
*/
protected function compileAssets()
{
$this->runCommand('npm update');
$this->runCommand('npm run dev');
}
}

View File

@ -2,10 +2,9 @@
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Console\BaseCommand;
class Install extends Command
class Install extends BaseCommand
{
protected $signature = 'phpvms:install
{--update}

View File

@ -2,39 +2,16 @@
namespace App\Console\Commands;
use Illuminate\Console\Command;
use League\Geotools\Coordinate\Coordinate;
use App\Console\BaseCommand;
use App\Models\Navdata;
use App\Models\Enums\NavaidType;
class NavdataCommand extends Command
class NavdataCommand extends BaseCommand
{
protected $signature = 'phpvms:navdata';
protected $description = '';
/**
* Streaming file read
* @param $filename
* @return \Generator
*/
protected function readFile($filename)
{
$fp = fopen($filename, 'rb');
while (($line = fgets($fp)) !== false) {
$line = rtrim($line, "\r\n");
if($line[0] === ';') {
continue;
}
yield $line;
}
fclose($fp);
}
/**
* Read and parse in the navaid file
* @throws \League\Geotools\Exception\InvalidArgumentException

View File

@ -13,8 +13,11 @@ class Kernel extends ConsoleKernel
* @var array
*/
protected $commands = [
Commands\AcarsReplay::class,
Commands\CreateDatabase::class,
Commands\DevCommands::class,
Commands\Install::class,
Commands\NavdataCommand::class,
];
/**