gist/src/Gist/Command/CreateCommand.php

142 lines
4 KiB
PHP
Raw Normal View History

2015-07-19 18:19:43 +02:00
<?php
namespace Gist\Command;
use Knp\Command\Command;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
2017-06-25 18:52:27 +02:00
use Propel\Runtime\Parser\YamlParser;
use Symfony\Component\Yaml\Yaml;
2015-07-19 18:19:43 +02:00
2016-11-13 00:44:23 +01:00
/**
* class CreateCommand.
*
* @author Simon Vieille <simon@deblan.fr>
*/
2015-07-19 18:19:43 +02:00
class CreateCommand extends Command
{
2016-11-13 00:44:23 +01:00
/**
* {@inheritdoc}
*/
2015-07-19 18:19:43 +02:00
protected function configure()
{
$types = implode(', ', $this->getTypes());
$this
->setName('create')
->setDescription('Create a gist using the API')
->addArgument('input', InputArgument::REQUIRED, 'Input')
->addArgument('type', InputArgument::OPTIONAL, 'Type', 'text')
->addOption('title', 't', InputOption::VALUE_REQUIRED, 'Title of the gist')
2017-06-25 18:52:27 +02:00
->addOption('all', 'a', InputOption::VALUE_NONE, 'Display all the response')
->addOption('id', 'i', InputOption::VALUE_NONE, 'Display only the id of the gist')
->addOption('json', 'j', InputOption::VALUE_NONE, 'Format the response to json')
2015-07-19 18:19:43 +02:00
->setHelp(<<<EOF
2015-07-19 18:48:52 +02:00
Provides a client to create a gist using the API.
2015-07-19 18:19:43 +02:00
Arguments:
<info>input</info>
2015-07-19 18:45:01 +02:00
Identify the source of the content: path of the file (eg: <comment>/path/to/file</comment>) or standard input (<comment>-</comment>)
2015-07-19 18:19:43 +02:00
<info>type</info>
Defines the type of code: {$types}
Default value: <comment>text</comment>
Options:
<info>--title</info>, <info>-t</info>
Defines a title
2017-06-25 18:52:27 +02:00
<info>--id</info>, <info>-i</info>
Display only the id of the gist
<info>--all</info>, <info>-a</info>
Display all the response
<info>--json</info>, <info>-j</info>
Format the response to json
2015-07-19 18:19:43 +02:00
EOF
);
}
2016-11-13 00:44:23 +01:00
/**
* {@inheritdoc}
*/
2015-07-19 18:19:43 +02:00
protected function execute(InputInterface $input, OutputInterface $output)
{
$file = $input->getArgument('input');
$type = $input->getArgument('type');
$title = $input->getOption('title');
2017-06-25 18:52:27 +02:00
$json = $input->getOption('json');
2015-07-19 18:19:43 +02:00
if ($file === '-') {
$content = file_get_contents('php://stdin');
} else {
if (!is_readable($file)) {
$output->writeln(sprintf('<error>%s: No such file.</error>', $file));
return false;
}
if (!is_file($file)) {
$output->writeln(sprintf('<error>"%s" must be a file.</error>', $file));
return false;
}
$content = file_get_contents($file);
}
if (!in_array($type, $this->getTypes())) {
$output->writeln(sprintf('<error>%s: invalid type.</error>', $type));
return false;
}
if (trim($content) === '') {
$output->writeln(sprintf('<error>You can not create an empty gist.</error>', $type));
}
$gist = $this->getSilexApplication()['api_client']->create($title, $type, $content);
2017-06-25 18:52:27 +02:00
if ($input->getOption('id')) {
$id = isset($gist['gist']['id']) ? $gist['gist']['id'] : $gist['gist']['Id'];
$result = $json ? json_encode(array('id' => $id)) : $id;
} elseif ($input->getOption('all')) {
$result = $json ? json_encode($gist) : Yaml::dump($gist);
} else {
$url = $gist['url'];
$result = $json ? json_encode(array('url' => $url)) : $url;
2015-11-07 22:13:08 +01:00
}
2015-07-19 18:19:43 +02:00
2017-06-25 18:52:27 +02:00
$output->writeln($result);
2015-07-19 18:19:43 +02:00
}
2016-11-13 00:44:23 +01:00
/**
* Returns the list of types.
*
* @return array
*/
2015-07-19 18:19:43 +02:00
protected function getTypes()
{
$types = array(
'html',
'css',
'javascript',
'php',
'sql',
'xml',
2015-09-17 11:48:19 +02:00
'yaml',
2015-07-19 18:19:43 +02:00
'perl',
'c',
'asp',
'python',
'bash',
'actionscript3',
'text',
);
return $types;
}
}