Browse Source

More verbose feedback on sample commands

Signed-off-by: Joas Schilling <coding@schilljs.com>
pull/1662/head
Joas Schilling 7 years ago
parent
commit
e267c6be4f
No known key found for this signature in database GPG Key ID: 7076EA9751AACDDA
  1. 90
      lib/Command/Command/AddSamples.php
  2. 14
      sample-commands/calc.sh

90
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);
}
}

14
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)
Loading…
Cancel
Save