Skip to content

Commit

Permalink
Merge pull request #3 from Binternet/master
Browse files Browse the repository at this point in the history
Documentation and Multiple OS Support
  • Loading branch information
DivineOmega committed Jun 14, 2018
2 parents afdcc47 + 4ac7a51 commit 0be2757
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
19 changes: 18 additions & 1 deletion src/HCLParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,41 @@

namespace DivineOmega\HCLParser;

/**
* Class HCLParser
* @package DivineOmega\HCLParser
*/
class HCLParser
{
/**
* @var
*/
private $hcl;

/**
* HCLParser constructor.
* @param $hcl
*/
public function __construct($hcl)
{
$this->hcl = $hcl;
}

/**
* @return string
*/
private function getJSONString()
{
$command = __DIR__.'/../bin/json2hcl_v0.0.6_linux_amd64 --reverse <<\'EOF\''.PHP_EOL.$this->hcl.PHP_EOL.'EOF';
$command = __DIR__.'/../bin/' . Installer::getBinaryFilename() . ' --reverse <<\'EOF\''.PHP_EOL.$this->hcl.PHP_EOL.'EOF';

exec($command, $lines);

return implode(PHP_EOL, $lines);
}

/**
* @return mixed
*/
public function parse()
{
return json_decode($this->getJSONString());
Expand Down
47 changes: 45 additions & 2 deletions src/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,63 @@

namespace DivineOmega\HCLParser;

/**
* Class Installer
* @package DivineOmega\HCLParser
*/
abstract class Installer
{
/**
* json2hcl Version we want to use
* @see https://github.com/kvz/json2hcl/releases
*/
const JSON2HCL_VERSION = '0.0.6';

/**
* Returns the correct binary filename according to the Operating System and Architecture
*/
public static function getBinaryFilename()
{
# Defaults
$osString = 'linux';
$architecture = 'amd64';

# Switch architecture if needed
if (2147483647 == PHP_INT_MAX) {
$architecture = '386';
}

# Switch Operating System if needed
switch (TRUE) {
case stristr(PHP_OS, 'DAR'):
$osString = 'darwin';
break;
case stristr(PHP_OS, 'WIN'):
$osString = 'windows';
break;
}

return sprintf('json2hcl_v%s_%s_%s',self::JSON2HCL_VERSION, $osString, $architecture);
}

public static function installBinaries()
{
$binaryUrls = ['https://github.com/kvz/json2hcl/releases/download/v0.0.6/json2hcl_v0.0.6_linux_amd64'];
$binaryUrls = [
sprintf('https://github.com/kvz/json2hcl/releases/download/v%s/%s', self::JSON2HCL_VERSION, self::getBinaryFilename() )
];

foreach ($binaryUrls as $binaryUrl) {
$destination = __DIR__.'/../bin/'.basename($binaryUrl);
$destination = __DIR__ . '/../bin/' . basename($binaryUrl);

# Skip if file exists
if (file_exists($destination)) {
continue;
}

# Download
file_put_contents($destination, file_get_contents($binaryUrl));

# Make executable
chmod($destination, 0755);
}
}
Expand Down

0 comments on commit 0be2757

Please sign in to comment.