*/ class UpdateCommand extends Command { /** * {@inheritdoc} */ protected function configure() { $types = implode(', ', $this->getTypes()); $this ->setName('update') ->setDescription('Update a gist using the API') ->addArgument('input', InputArgument::REQUIRED, 'Input') ->addOption('gist', null, InputOption::VALUE_REQUIRED, 'Id or File of the gist') ->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') ->setHelp(<<input Identify the source of the content: path of the file (eg: /path/to/file) or standard input (-) type Defines the type of code: {$types} Default value: text Options: --gist Defines the Gist to update by using its Id or its File --id, -i Display only the id of the gist --all, -a Display all the response --json, -j Format the response to json EOF ); } /** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $file = $input->getArgument('input'); $gist = $input->getOption('gist'); $json = $input->getOption('json'); if ($file === '-') { $content = file_get_contents('php://stdin'); } else { if (!is_readable($file)) { $output->writeln(sprintf('%s: No such file.', $file)); return false; } if (!is_file($file)) { $output->writeln(sprintf('"%s" must be a file.', $file)); return false; } $content = file_get_contents($file); } if (trim($content) === '') { $output->writeln(sprintf('You can not create an empty gist.', $type)); } $gist = $this->getSilexApplication()['api_client']->update($gist, $content); 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; } $output->writeln($result); } /** * Returns the list of types. * * @return array */ protected function getTypes() { $types = array( 'html', 'css', 'javascript', 'php', 'sql', 'xml', 'yaml', 'perl', 'c', 'asp', 'python', 'bash', 'actionscript3', 'text', ); return $types; } }