Skip to content

Commit

Permalink
up: will return script exit code on run script or plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed May 16, 2022
1 parent 3284567 commit b89a8a4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
35 changes: 19 additions & 16 deletions app/Console/Command/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ protected function beforeExecute(): bool
* @options
* -l, --list List information for all scripts or script files. type: file, cmd(default)
* -s, --search Display all matched scripts by the input name
* --info Show information for give script name or file
* -i, --info Show information for give script name or file
* --dry-run bool;Mock running, not real execute.
* --proxy bool;Enable proxy ENV setting
*
Expand All @@ -62,6 +62,7 @@ protected function beforeExecute(): bool
* @param Input $input
* @param Output $output
*
* @return int
* @help
* <b>TIPs</b>:
*
Expand All @@ -79,37 +80,38 @@ protected function beforeExecute(): bool
* # with proxy
* {binWithCmd} --proxy hello.sh one two three
*/
protected function execute(Input $input, Output $output)
protected function execute(Input $input, Output $output): int
{
$name = $this->flags->getArg('name');
$output->info('workdir: ' . $input->getWorkDir());

$listType = $this->flags->getOpt('list');
if ($listType === ScriptRunner::TYPE_FILE) {
$this->listScriptFiles($output, $name);
return;
return 0;
}

// support search
$kw = $this->flags->getOpt('search') ?: $name;
if ($this->flags->hasInputOpt('search')) {
$this->searchScripts($output, $kw);
return;
return 0;
}

// default list script commands
if ($listType) {
$this->listScripts($output, $name);
return;
return 0;
}

if (!$name) {
$output->liteError('please input an name for run or use -l TYPE see all scripts');
return;
return 0;
}

$runner = $this->sr;
$dryRun = $this->flags->getOpt('dry-run');
$this->sr->setDryRun($dryRun);
$runner->setDryRun($dryRun);

// proxy
$openProxy = $this->flags->getOpt('proxy');
Expand All @@ -126,28 +128,29 @@ protected function execute(Input $input, Output $output)
// $runArgs = $this->flags->getRawArgs(); // 会包含 arg: name
$runArgs = $this->flags->getRemainArgs();

if (!$this->sr->isScriptName($name)) {
if (!$runner->isScriptName($name)) {
// - found script file
if ($scriptFile = $this->sr->findScriptFile($name)) {
$this->sr->runScriptFile($scriptFile, $runArgs);
return;
if ($scriptFile = $runner->findScriptFile($name)) {
$runner->runScriptFile($scriptFile, $runArgs);
return $runner->getErrCode();
}

// - is an plugin
if (Kite::plugManager()->isPlugin($name)) {
$output->notice("input is an plugin name, will run plugin: $name");
Kite::plugManager()->run($name, $this->app, $runArgs);
return;
return 0;
}

// as script expr and run.
$this->sr->runInputScript($name);
$runner->runInputScript($name);
// $output->liteError("please input an exists script name for run. ('$name' not exists)");
return;
} else {
// run script by name
$runner->runScriptByName($name, $runArgs);
}

// run script by name
$this->sr->runScriptByName($name, $runArgs);
return $runner->getErrCode();
}

/**
Expand Down
6 changes: 4 additions & 2 deletions app/Console/Listener/NotFoundListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function __invoke(string $cmd, CliApplication $app): bool
array_shift($args); // first is script name.
$app->note("input is an script name, redirect to run script: $cmd, args: " . DataHelper::toString($args));
$ksr->runScriptByName($cmd, $args);

$app->setExitCode($ksr->getErrCode());
} elseif (Kite::plugManager()->isPlugin($cmd)) { // - is an plugin
array_shift($args); // first is $cmd
$app->notice("input is an plugin name, will run plugin: $cmd, args: " . DataHelper::toString($args));
Expand All @@ -58,6 +58,7 @@ public function __invoke(string $cmd, CliApplication $app): bool
$app->notice("input is an script file, will call it: $cmd, args: " . DataHelper::toString($args));

$ksr->runScriptFile($sFile, $args);
$app->setExitCode($ksr->getErrCode());
} else {
// - call system command.
$this->callSystemCmd($cmd, $app);
Expand Down Expand Up @@ -85,6 +86,7 @@ private function callSystemCmd(string $cmd, CliApplication $app): void
$app->notice("input command is not found, will call system command: $cmdLine");

// call system command
CmdRunner::new($cmdLine)->do(true);
$cr = CmdRunner::new($cmdLine)->do(true);
$app->setExitCode($cr->getCode());
}
}
5 changes: 4 additions & 1 deletion app/Helper/SysCmd.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ class SysCmd
/**
* @param string $command
* @param string $workDir
*
* @return int
*/
public static function quickExec(string $command, string $workDir = ''): void
public static function quickExec(string $command, string $workDir = ''): int
{
Color::println("run > $command", 'comment');

Expand All @@ -44,6 +46,7 @@ public static function quickExec(string $command, string $workDir = ''): void
// if (false === $hasOutput &&$lastLine) {
// echo $lastLine . "\n";
// }
return $exitCode;
}

/**
Expand Down

0 comments on commit b89a8a4

Please sign in to comment.