diff options
-rw-r--r-- | lib/Command/Command/AddSamples.php | 90 | ||||
-rw-r--r--[-rwxr-xr-x] | sample-commands/calc.sh | 14 |
2 files changed, 57 insertions, 47 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); } } diff --git a/sample-commands/calc.sh b/sample-commands/calc.sh index 7ec3c6301..73c514867 100755..100644 --- a/sample-commands/calc.sh +++ b/sample-commands/calc.sh @@ -1,4 +1,11 @@ #!/usr/bin/env bash + +CALCULATOR=$(which "bc") +if ! [ -x "$CALCULATOR" ]; then + echo "Basic calculator package (bc - https://www.gnu.org/software/bc/) not found" + exit 1 +fi + while test $# -gt 0; do case "$1" in --help) @@ -16,13 +23,6 @@ while test $# -gt 0; do esac done -CALCULATOR=$(which "bc") -if ! [ -x "$CALCULATOR" ]; then - echo "Basic calculator package (bc) not found" - exit 1 -fi - - set -f echo "$@ =" echo $(echo "$@" | bc) |