Page MenuHomeDevCentral

D77.diff
No OneTemporary

D77.diff

diff --git a/README b/README
--- a/README
+++ b/README
@@ -28,6 +28,7 @@
Available scripts:
------------------
+ * getconfig ........ retrieves a configuration file in rOPS
* getcredentials ... retrieves a credential stored on DevCentral
* getpublickeys .... fetches SSH public key allowed to use this account
* Makefile ......... runs getpublickeys to populate .ssh/authorized_keys
diff --git a/bin/getconfig b/bin/getconfig
new file mode 100755
--- /dev/null
+++ b/bin/getconfig
@@ -0,0 +1,17 @@
+#!/usr/bin/env php
+<?php
+#
+# Gets configuration from Nasqueron operations repository
+#
+# Usage: getconfig <configuration file>
+#
+
+require getenv('HOME') . '/lib/GetConfiguration.php';
+
+if ($argc == 1) {
+ $file = "php://stdin";
+} else {
+ $file = 'https://raw.githubusercontent.com/nasqueron/operations/master/config/' . $argv[1] . '.tmpl';
+}
+
+GetConfiguration::Run($file);
diff --git a/lib/GetConfiguration.php b/lib/GetConfiguration.php
new file mode 100644
--- /dev/null
+++ b/lib/GetConfiguration.php
@@ -0,0 +1,80 @@
+<?php
+
+/**
+ * Zemke-Rhyne
+ * Support tools for a Docker <--> Phabricator bridge
+ *
+ * (c) Sébastien Santoro aka Dereckson, 2015
+ * Released under BSD license.
+ */
+
+/**
+ * Class GetConfiguration
+ *
+ * Allows to get configuration from a template
+ */
+class GetConfiguration {
+ ///
+ /// Properties
+ ///
+
+ /**
+ * The configuration template content
+ * @var string
+ */
+ public $template;
+
+ ///
+ /// Templating methods
+ ///
+
+ /**
+ * Prints configuration from specified configuration template
+ * replacing expressions by relevant variables and credentials.
+ *
+ * @return string the configuration
+ */
+ public function printConfiguration () {
+ echo static::substituteTemplateVariables($this->template);
+ }
+
+ /**
+ * Substitutes template variables in the specified text
+ *
+ * @param string $text The template text
+ * @return string The substitued text
+ */
+ public static function substituteTemplateVariables ($text) {
+ $text = static::substituteExec($text);
+ return $text;
+ }
+
+ /**
+ * Substitutes %%`command`%% by the output of the command.
+ *
+ * @param string $text The template text
+ * @return string The substitued text
+ */
+ public static function substituteExec ($text) {
+ return preg_replace_callback(
+ '/%%`(.*)`%%/',
+ function ($matches) {
+ return trim(`$matches[1]`);
+ },
+ $text
+ );
+ }
+
+ ///
+ /// Script procedural code
+ ///
+
+ /**
+ * Runs the script
+ */
+ public static function run ($file) {
+ $instance = new self;
+ $instance->template = file_get_contents($file);
+ $instance->printConfiguration();
+ }
+}
diff --git a/tests/GetConfigurationTest.php b/tests/GetConfigurationTest.php
new file mode 100644
--- /dev/null
+++ b/tests/GetConfigurationTest.php
@@ -0,0 +1,42 @@
+<?php
+
+require '../lib/GetConfiguration.php';
+
+class GetConfigurationTest extends PHPUnit_Framework_TestCase {
+
+ public function testPrintConfiguration () {
+ $this->expectOutputString('foo');
+ $instance = new GetConfiguration();
+ $instance->template = 'foo';
+ $instance->printConfiguration();
+ }
+
+ public function testSubstituteTemplateVariables () {
+ //Empty string
+ $this->assertEquals('', GetConfiguration::substituteTemplateVariables(''));
+
+ //Equivalent to an empty string
+ $this->assertEquals('', GetConfiguration::substituteTemplateVariables(null));
+ $this->assertEquals('', GetConfiguration::substituteTemplateVariables(false));
+
+ //Nothing to substitute
+ $this->assertEquals('foo', GetConfiguration::substituteTemplateVariables('foo'));
+ }
+
+ /**
+ * @requires OS Linux|BSD|CYGWIN|Darwin
+ */
+ public function testSubstituteExec () {
+ $this->assertEquals('', GetConfiguration::substituteExec(''));
+
+ $template = '%%`echo -n ""`%%';
+ $this->assertEquals('', GetConfiguration::substituteExec($template));
+
+ $template = '%%`echo ""`%%';
+ $this->assertEquals('', GetConfiguration::substituteExec($template));
+
+ $template = 'File is %%`ls GetConfigurationTest.php`%%.';
+ $this->assertEquals('File is GetConfigurationTest.php.', GetConfiguration::substituteExec($template));
+ }
+
+}

File Metadata

Mime Type
text/plain
Expires
Sat, Nov 16, 19:34 (21 h, 39 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2247999
Default Alt Text
D77.diff (4 KB)

Event Timeline