Skip to content

Commit

Permalink
first implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
pgodel committed Mar 7, 2012
1 parent 274b709 commit 7a84053
Show file tree
Hide file tree
Showing 13 changed files with 1,429 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
/vendor/
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2012 ServerGrove Networks, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
13 changes: 13 additions & 0 deletions bin/compile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env php
<?php

if ((!@include __DIR__.'/../../../../.composer/autoload.php') && (!@include __DIR__.'/../vendor/.composer/autoload.php')) {
die('You must set up the project dependencies, run the following commands:'.PHP_EOL.
'curl -s http://getcomposer.org/installer | php'.PHP_EOL.
'php composer.phar install'.PHP_EOL);
}

use ServerGrove\Cli\Compiler;

$compiler = new Compiler();
$compiler->compile();
14 changes: 14 additions & 0 deletions bin/sgcli
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env php
<?php

if ((!@include __DIR__.'/../../../.composer/autoload.php') && (!@include __DIR__.'/../vendor/.composer/autoload.php')) {
die('You must set up the project dependencies, run the following commands:'.PHP_EOL.
'curl -s http://getcomposer.org/installer | php'.PHP_EOL.
'php composer.phar install'.PHP_EOL);
}

//use ServerGrove\Cli\Console\Application;

// run the command application
$application = new \ServerGrove\Cli\Console\Application();
$application->run();
28 changes: 28 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "servergrove/sgcli",
"description": "ServerGrove CLI",
"keywords": ["servergrove", "cli", "vps"],
"homepage": "http://servergrove.com/",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Pablo Godel",
"email": "pablo@servergrove.com",
"homepage": "http://twitter.com/pgodel"
}
],
"require": {
"php": ">=5.3.0",
"symfony/console": "dev-master",
"symfony/finder": "dev-master",
"symfony/process": "dev-master"
},
"recommend": {
"ext-zip": "*"
},
"autoload": {
"psr-0": { "ServerGrove": "src/" }
},
"bin": ["bin/sgcli"]
}
21 changes: 21 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

179 changes: 179 additions & 0 deletions src/ServerGrove/APIClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
<?php

namespace ServerGrove;

class APIClient
{
const FORMAT_JSON = 'json';
const FORMAT_ARRAY = 'array';
const FORMAT_RAW = 'raw';

protected $url;
protected $format = self::FORMAT_JSON;
protected $args = array();
protected $call;
protected $response;

public function __construct($url)
{
$this->url = $url;
}

public function getFullUrl($call, array $args = array())
{
return $this->url . '/api/' . $call . '.' . $this->format .(count($args) ? '?' . http_build_query($args) : '');
}

/**
* Executes API Call. Returns true|false depending on the api response. use getResponse() to retrieve response.
* @param $call
* @param array $args
* @return bool
* @throws \Exception
*/
public function call($call, array $args = array())
{
if (!function_exists('curl_init')) {
throw new \Exception("curl support not found. please install the curl extension.");
}

$args = array_merge($this->args, $args);

$post_data = http_build_query($args);

$url = $this->getFullUrl($call);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$this->response = curl_exec($ch);
curl_close($ch);
return $this->isSuccess($this->response);
}

public function getResponse($format = null)
{
if (!$format) {
$format = $this->format;
}
switch ($format) {
case 'json':
return json_decode($this->response);
break;
case 'array':
return json_decode($this->response, true);
break;
default:
return $this->response;
}
}

public function getRawResponse()
{
return $this->response;
}


public function isSuccess($result=null)
{
if (null === $result) {
$result = $this->getResponse();
}

if (is_string($result)) {
$json = json_decode($result);
if ($json) {
$result = $json;
} else {
return $result == true;
}
} elseif (is_array($result)) {
return $result['result'] == true;
}

return $result && $result->result == true;
}

public function getError($result=null)
{
if (null === $result) {
$result = $this->getResponse();
}

if (is_string($result)) {
$json = json_decode($result);
if ($json) {
$result = $json;
} else {
return $result;
}
} elseif (is_array($result)) {
return $result['msg'];
}

return $result && $result->msg ? $result->msg : 'Unknown error';
}


public function setFormat($format)
{
$this->format = $format;
return $this;
}

public function getFormat()
{
return $this->format;
}

public function setUrl($url)
{
$this->url = $url;
return $this;
}

public function getUrl()
{
return $this->url;
}

public function setArgs($args)
{
$this->args = $args;
return $this;
}

public function getArgs()
{
return $this->args;
}

public function setArg($name, $value)
{
$this->args[$name] = $value;
return $this;
}

public function setApiKey($value)
{
return $this->setArg('apiKey', $value);
}

public function setApiSecret($value)
{
return $this->setArg('apiSecret', $value);
}

public function dryRun($value = 1)
{
return $this->setArg('dryRun', $value);
}

public function debug($value = 1)
{
return $this->setArg('debug', $value);
}

}
74 changes: 74 additions & 0 deletions src/ServerGrove/Cli/Command/ClientCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace ServerGrove\Cli\Command;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;

class ClientCommand extends Command
{

protected function configure()
{
parent::configure();

$this
->setName('client')
->setDescription("Executes a call to the ServerGrove Control Panel API. For more information visit https://control.servergrove.com/docs/api")
->addArgument('call', InputArgument::REQUIRED, 'API Call')
->addArgument('args', InputArgument::OPTIONAL, 'API Arguments')
->addOption('url', null, null, 'API URL')
;
}

/**
* Executes the current command.
*
* @param InputInterface $input An InputInterface instance
* @param OutputInterface $output An OutputInterface instance
*
* @return integer 0 if everything went fine, or an error code
*
* @throws \LogicException When this abstract class is not implemented
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
// check command is valid
$call = strtolower($input->getArgument('call'));
$argStr = $input->getArgument('args');

$options = $input->getOptions();

parse_str($argStr, $args);

$apiclient = $this->getClient();
/* @var $apiclient \ServerGrove\APIClient */
if ($options['url']) {
$apiclient->setUrl($options['url']);
}

if ($options['verbose']) {
$output->writeln("Calling: <info>".$apiclient->getFullUrl($call, $args)."</info>");
}

if ($apiclient->call($call, $args)) {
if ($options['verbose']) {
$output->writeln("Response: <info>".print_r($apiclient->getResponse(), true)."</info>");
} else {
$output->writeln($apiclient->getRawResponse());
}
return 0;
} else {
if ($options['verbose']) {
$output->writeln("<error>".$apiclient->getError()."</error>");
} else {
$output->writeln($apiclient->getRawResponse());
}
return 1;
}
}


}
31 changes: 31 additions & 0 deletions src/ServerGrove/Cli/Command/Command.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/*
* This file is part of sgcli.
*
* (c) ServerGrove
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ServerGrove\Cli\Command;

use Symfony\Component\Console\Command\Command as BaseCommand;

/**
* Base class for sgcli commands
*
* @author Ryan Weaver <ryan@knplabs.com>
* @author Konstantin Kudryashov <ever.zet@gmail.com>
*/
abstract class Command extends BaseCommand
{
/**
* @return \ServerGrove\APIClient
*/
protected function getClient()
{
return $this->getApplication()->getClient();
}
}
Loading

0 comments on commit 7a84053

Please sign in to comment.