Skip to content

Commit

Permalink
Merge pull request #172 from pantheon-systems/update-checker
Browse files Browse the repository at this point in the history
Check github api for latest version and alert if new version is found
  • Loading branch information
mikevanwinkle committed Mar 5, 2015
2 parents 8603be6 + fa90070 commit 492e597
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
1 change: 0 additions & 1 deletion php/boot-fs.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
echo "Only CLI access.\n";
die(-1);
}

if ( version_compare( PHP_VERSION, '5.3.0', '<' ) ) {
printf( "Error: Terminus requires PHP %s or newer. You are running version %s.\n", '5.3.0', PHP_VERSION );
die(-1);
Expand Down
39 changes: 38 additions & 1 deletion php/class-terminus.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use \Terminus\Utils;
use \Terminus\Dispatcher;
use \Terminus\FileCache;
use \Terminus\Request;

/**
* Various utilities for Terminus commands.
Expand Down Expand Up @@ -170,6 +171,16 @@ static function log( $message, $params = array() ) {
self::$logger->info( $message );
}

/**
* Colorized logging
*
* @param $message string
* @param $params array
**/
static function color($message) {
\Terminus\Loggers\Regular::coloredOutput($message);
}

/**
* Display a success in the CLI and end with a newline
*
Expand All @@ -191,7 +202,10 @@ static function warning( $message, $params = array() ) {
if ( !empty($params) ) {
$message = vsprintf($message, $params);
}
self::$logger->warning( self::error_to_string( $message ) );
if (!Terminus::$logger) {
Terminus::set_logger(new \Terminus\Loggers\Regular(TRUE));
}
Terminus::$logger->warning( self::error_to_string( $message ) );
}

/**
Expand Down Expand Up @@ -423,4 +437,27 @@ static function is_test() {
return true;
return false;
}

/**
* Latest release
*/
static function version_available() {
$cache = Terminus::get_cache();
if (! $release = $cache->get_data(__FUNCTION__) ) {
$url = "https://api.github.com/repos/pantheon-systems/cli/releases?per_page=1";
$response = Request::send($url,'GET');
$json = $response->getBody(TRUE);
$release = array_shift(json_decode($json));
$cache->put_data(__FUNCTION__,$release);
}
return $release->tag_name;
}

static public function update_available() {
$latest = Terminus::version_available();
if (version_compare($latest, TERMINUS_VERSION, ">")) {
return true;
}
return false;
}
}
3 changes: 3 additions & 0 deletions php/terminus.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,8 @@
}
define( 'TERMINUS_PORT', '443' );

if (Terminus::update_available()) {
\cli\line(\cli\Colors::colorize("%3%KWarning: There is a newer version of Terminus (". Terminus::version_available().") avaiable %n"));
}

Terminus::get_runner()->run();

0 comments on commit 492e597

Please sign in to comment.