diff options
author | Joas Schilling <coding@schilljs.com> | 2019-04-08 17:59:25 +0300 |
---|---|---|
committer | Joas Schilling <coding@schilljs.com> | 2019-04-08 18:00:54 +0300 |
commit | e267c6be4fe7bf116e64d50a0e08d8dee839d3bd (patch) | |
tree | ac580ef6eb9a50983cbad4bcb85d49e7f813244e /lib/Command | |
parent | 074d5e306296d63c9163401cbd385b498b23fa8c (diff) |
More verbose feedback on sample commands
Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'lib/Command')
-rw-r--r-- | lib/Command/Command/AddSamples.php | 90 |
1 files changed, 50 insertions, 40 deletions
diff --git a/lib/Command/Command/AddSamples.php b/lib/Command/Command/AddSamples.php index f1419f33f..96d9801da 100644 --- a/lib/Command/Command/AddSamples.php +++ b/lib/Command/Command/AddSamples.php @@ -40,6 +40,8 @@ class AddSamples extends Base { /** @var IAppManager */ protected $appManager; + protected $commands = []; + public function __construct(CommandService $service, IAppManager $appManager) { parent::__construct(); $this->service = $service; @@ -61,61 +63,69 @@ class AddSamples extends Base { return 1; } - $commands = []; - try { - $this->service->find('', 'wiki'); - } catch (DoesNotExistException $e) { - $commands[] = $this->service->create( - '', - 'wiki', - 'Wikipedia', - 'php ' . $appPath . '/sample-commands/wikipedia.php "{ARGUMENTS_DOUBLEQUOTE_ESCAPED}"', - Command::RESPONSE_ALL, - Command::ENABLED_ALL - ); - } + $this->installCommand( + $output, + 'wiki', + 'Wikipedia', + 'php ' . $appPath . '/sample-commands/wikipedia.php "{ARGUMENTS_DOUBLEQUOTE_ESCAPED}"' + ); - try { - $this->service->find('', 'calculator'); - } catch (DoesNotExistException $e) { - $commands[] = $this->service->create( - '', + $chmod = fileperms($appPath . '/sample-commands/calc.sh'); + if (!($chmod & 0x0040 || $chmod & 0x0008 || $chmod & 0x0001)) { + $output->writeln('<error>sample-commands/calc.sh is not executable</error>'); + } else { + $this->installCommand( + $output, 'calculator', 'Calculator', $appPath . '/sample-commands/calc.sh "{ARGUMENTS_DOUBLEQUOTE_ESCAPED}"', - Command::RESPONSE_USER, - Command::ENABLED_ALL + Command::RESPONSE_USER ); - } - try { - $this->service->find('', 'calc'); - } catch (DoesNotExistException $e) { - $commands[] = $this->service->create( - '', + $this->installCommand( + $output, 'calc', 'Calculator', - 'alias:calculator', - Command::RESPONSE_ALL, - Command::ENABLED_ALL + 'alias:calculator' ); } + + $this->installCommand( + $output, + 'hackernews', + 'Hacker News', + 'php ' . $appPath . '/sample-commands/hackernews.php "{ARGUMENTS_DOUBLEQUOTE_ESCAPED}"' + ); + + if (empty($this->commands)) { + return 1; + } + + $output->writeln('<info>Commands added</info>'); + $output->writeln(''); + $this->renderCommands(Base::OUTPUT_FORMAT_PLAIN, $output, $this->commands); + } + + protected function installCommand(OutputInterface $output, string $command, string $name, string $script, int $resonse = Command::RESPONSE_ALL, int $enable = Command::ENABLED_ALL): void { try { - $this->service->find('', 'hackernews'); + $this->service->find('', $command); + $output->writeln('<comment>Command ' . $command . ' already exists</comment>'); + return; } catch (DoesNotExistException $e) { - $commands[] = $this->service->create( + } + + try { + $this->commands[] = $this->service->create( '', - 'hackernews', - 'Hacker News', - 'php ' . $appPath . '/sample-commands/hackernews.php "{ARGUMENTS_DOUBLEQUOTE_ESCAPED}"', - Command::RESPONSE_ALL, - Command::ENABLED_ALL + $command, + $name, + $script, + $resonse, + $enable ); + } catch (\InvalidArgumentException $e) { + $output->writeln('<error>An error occured while setting up the ' . $command . ' command</error>'); } - - $output->writeln('<info>Commands added</info>'); - $output->writeln(''); - $this->renderCommands(Base::OUTPUT_FORMAT_PLAIN, $output, $commands); } } |