diff --git a/app/bootstrap.php.d/60-api.php b/app/bootstrap.php.d/60-api.php new file mode 100644 index 0000000..65e4e43 --- /dev/null +++ b/app/bootstrap.php.d/60-api.php @@ -0,0 +1,7 @@ + 'http://127.0.0.1:8080/']); +}; diff --git a/app/console b/app/console index 9f603e5..7aafa0c 100755 --- a/app/console +++ b/app/console @@ -1,6 +1,10 @@ #!/usr/bin/env php add(new CreateCommand()); + $app['console']->run(); diff --git a/composer.json b/composer.json index 77ef9d4..ff724f6 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,8 @@ "symfony/form": "~2.6", "symfony/security-csrf": "~2.6", "knplabs/console-service-provider": "~1.0", - "propel/propel": "~2.0@dev" + "propel/propel": "~2.0@dev", + "guzzlehttp/guzzle": "~6.0" }, "autoload": { "psr-0": { diff --git a/src/Gist/Api/Client.php b/src/Gist/Api/Client.php new file mode 100644 index 0000000..a3dc6b1 --- /dev/null +++ b/src/Gist/Api/Client.php @@ -0,0 +1,36 @@ + + */ +class Client extends BaseClient +{ + const CREATE = '/en/api/create'; + + public function create($title, $type, $content) + { + $response = $this->post( + self::CREATE, + array( + 'form_params' => array( + 'form' => array( + 'title' => $title, + 'type' => $type, + 'content' => $content, + ), + ), + ) + ); + + if ($response->getStatusCode() === 200) { + return json_decode($response->getBody()->getContents(), true); + } + + return false; + } +} diff --git a/src/Gist/Command/CreateCommand.php b/src/Gist/Command/CreateCommand.php new file mode 100644 index 0000000..b98d5da --- /dev/null +++ b/src/Gist/Command/CreateCommand.php @@ -0,0 +1,112 @@ +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') + ->addOption('show-url', 'u', InputOption::VALUE_NONE, 'Display only the gist url') + ->setHelp(<<%command.name% provides a client to create a gist using an API. + +Arguments: + input + Identify the source of the content: a file path (eg: /path/to/file) or standard input (-) + + type + Defines the type of code: {$types} + Default value: text + +Options: + --title, -t + Defines a title + + --show-url, -u + Display only the url of the gist +EOF + ); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + //$output->writeln(sprintf('%s bar.', 'test')); + + $file = $input->getArgument('input'); + $type = $input->getArgument('type'); + $title = $input->getOption('title'); + + 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 (!in_array($type, $this->getTypes())) { + $output->writeln(sprintf('%s: invalid type.', $type)); + + return false; + } + + if (trim($content) === '') { + $output->writeln(sprintf('You can not create an empty gist.', $type)); + } + + $gist = $this->getSilexApplication()['api_client']->create($title, $type, $content); + + if ($input->getOption('show-url')) { + $output->writeln($gist['url']); + + return true; + } + + $output->writeln(json_encode($gist)); + } + + protected function getTypes() + { + $types = array( + 'html', + 'css', + 'javascript', + 'php', + 'sql', + 'xml', + 'yaml'=> '', + 'perl', + 'c', + 'asp', + 'python', + 'bash', + 'actionscript3', + 'text', + ); + + return $types; + } +}