Commit a84ab813 authored by Hendrik Heneke's avatar Hendrik Heneke
Browse files

Additional values per environment

parent ff1a433e
Pipeline #544 passed with stage
in 43 seconds
......@@ -2,19 +2,19 @@
"users.json": {
"type": "json",
"template": "./cfgen/users.json.hbs",
"values": "./cfgen/users.values.json",
"values": "./cfgen/values.json",
"destination": "./output/users.json"
},
"users.txt": {
"type": "text",
"template": "./cfgen/users.txt.hbs",
"values": "./cfgen/users.values.json",
"values": "./cfgen/values.json",
"destination": "./output/users.txt"
},
"users.yaml": {
"type": "yaml",
"template": "./cfgen/users.yaml.hbs",
"values": "./cfgen/users.values.json",
"values": "./cfgen/values.json",
"destination": "./output/users.yaml"
}
}
......@@ -10,5 +10,6 @@
},
"version": 2,
"secret": "{{secret}}",
"env": "{{_env}}"
"env": "{{_env}}",
"fromEnvJson": "{{sampleEnvValue}}"
}
......@@ -4,3 +4,4 @@ users_{{this.username}}_cleartext='{{this.password}}'
{{/each}}
version=2
secret='{{this.secret}}'
sample_env_value='{{this.sampleEnvValue}}'
......@@ -7,3 +7,4 @@ users:
{{/each}}
version: 2
secret: '{{this.secret}}'
sampleEnvValue: '{{sampleEnvValue}}'
......@@ -18,5 +18,6 @@
"secret": {
"$type": "secret",
"arg": "secret"
}
},
"sampleEnvValue": "defaultValueForAllEnvs"
}
{
"sampleEnvValue": "sampleValueOverridenInTestEnv"
}
......@@ -9,33 +9,20 @@ use HHIT\ConfigGenerator\Generator\IO\Path;
class Definition
{
/**
* @var string
*/
private $id;
/**
* @var File
*/
private $templateFile;
/**
* @var string
*/
private $type;
/**
* @var File
*/
private $valuesFile;
/**
* @var File
*/
private string $id;
private File $templateFile;
private string $type;
private File $valuesFile;
private File $valuesEnvFile;
private $destinationFile;
public function __construct(string $id, File $templateFile, string $type, File $valuesFile, File $destinationFile)
public function __construct(string $id, File $templateFile, string $type, File $valuesFile, File $valuesEnvFile, File $destinationFile)
{
$this->id = $id;
$this->templateFile = $templateFile;
$this->type = $type;
$this->valuesFile = $valuesFile;
$this->valuesEnvFile = $valuesEnvFile;
$this->destinationFile = $destinationFile;
}
......@@ -59,6 +46,15 @@ class Definition
return $this->valuesFile;
}
public function getValuesEnvFile(): ?File
{
if ($this->valuesEnvFile->exists()) {
return $this->valuesEnvFile;
} else {
return null;
}
}
public function getDestinationFile(): File
{
return $this->destinationFile;
......
......@@ -10,14 +10,16 @@ use HHIT\ConfigGenerator\Generator\StringUtils;
class DefinitionReader
{
/**
* @var string
*/
private $projectDir;
private const RELATIVE_PATH_PREFIX = './';
private string $projectDir;
public function __construct(string $projectDir)
private string $env;
public function __construct(string $projectDir, string $env)
{
$this->projectDir = $projectDir;
$this->env = $env;
}
/**
......@@ -32,18 +34,34 @@ class DefinitionReader
$definitions = [];
foreach ($reader->readAsJson() as $id => $definition) {
$templateFile = StringUtils::startsWith($definition['template'], './') ?
$templateFile = StringUtils::startsWith($definition['template'], self::RELATIVE_PATH_PREFIX) ?
new File($definition['template'], $definitionFile) : new File($definition['template']);
$valuesFile = StringUtils::startsWith($definition['values'], './') ?
$valuesFile = StringUtils::startsWith($definition['values'], self::RELATIVE_PATH_PREFIX) ?
new File($definition['values'], $definitionFile) : new File($definition['values']);
$destinationFile = StringUtils::startsWith($definition['destination'], './') ?
$valuesEnvFileName = $this->valuesFileForEnv($definition['values'], $this->env);
$valuesEnvFile = StringUtils::startsWith($valuesEnvFileName, self::RELATIVE_PATH_PREFIX) ?
new File($valuesEnvFileName, $definitionFile) : new File($valuesEnvFileName);
$destinationFile = StringUtils::startsWith($definition['destination'], self::RELATIVE_PATH_PREFIX) ?
new File($definition['destination'], $definitionFile) : new File($definition['destination']);
$definitions[] = new Definition($id, $templateFile, $definition['type'], $valuesFile, $destinationFile);
$definitions[] = new Definition($id, $templateFile, $definition['type'], $valuesFile, $valuesEnvFile, $destinationFile);
}
return $definitions;
}
private function valuesFileForEnv(string $valuesFile, string $env): ?string
{
$lastPos = strrpos($valuesFile, '.');
if ($lastPos !== false) {
$prefix = substr($valuesFile, 0, $lastPos);
$suffix = substr($valuesFile, $lastPos);
return "{$prefix}_{$env}{$suffix}";
}
return null;
}
}
......@@ -18,14 +18,8 @@ use function HHIT\ConfigGenerator\cfgen_secrets_directory;
class Factory
{
/**
* @var string
*/
private $projectDir;
/**
* @var string
*/
private $env;
private string $projectDir;
private string $env;
public function __construct(string $projectDir, string $env = 'dev')
{
......@@ -35,7 +29,7 @@ class Factory
private function createDefinitionReader(): DefinitionReader
{
return new DefinitionReader($this->projectDir);
return new DefinitionReader($this->projectDir, $this->env);
}
public function dumpPrivateKey()
......
......@@ -52,7 +52,7 @@ class Generator
$this->writeln($output, "<info>- processing {$definition->getId()}</info>");
try {
$valuesLoader = $this->valuesLoaderFactory->create($definition->getValuesFile(), $vaultType);
$valuesLoader = $this->valuesLoaderFactory->create($definition->getValuesFile(), $definition->getValuesEnvFile(), $vaultType);
$compiler = new Compiler($definition->getTemplateFile(), $valuesLoader);
$content = $compiler->compile();
try {
......
......@@ -8,32 +8,33 @@ use HHIT\ConfigGenerator\Generator\IO\File;
use HHIT\ConfigGenerator\Generator\IO\JsonReader;
use HHIT\ConfigGenerator\Generator\Secrets\SecretProviderInterface;
class ValuesLoader extends JsonReader
class ValuesLoader
{
/**
* @var array
*/
private $context = [];
/**
* @var SecretProviderInterface
*/
private $provider;
/**
* @var string
*/
private $env;
private JsonReader $valuesReader;
private ?JsonReader $valuesEnvReader;
private array $context = [];
private SecretProviderInterface $provider;
private string $env;
public function __construct(File $file, SecretProviderInterface $provider, string $env)
{
parent::__construct($file);
public function __construct(
File $valuesFile,
?File $valuesEnvFile,
SecretProviderInterface $provider,
string $env
) {
$this->valuesReader = new JsonReader($valuesFile);
$this->valuesEnvReader = $valuesEnvFile ? new JsonReader($valuesEnvFile) : null;
$this->provider = $provider;
$this->env = $env;
}
public function getValues()
public function getValues(): array
{
if (!$this->context) {
$this->context = $this->postProcess($this->readAsJson());
$this->context = $this->postProcess(array_merge(
$this->valuesReader->readAsJson(),
$this->valuesEnvReader ? $this->valuesEnvReader->readAsJson() : []
));
if (array_key_exists('_env', $this->context)) {
throw new \RuntimeException('_env is a reserved value - do not define it manually!');
}
......@@ -42,7 +43,7 @@ class ValuesLoader extends JsonReader
return $this->context;
}
private function postProcess(array $array)
private function postProcess(array $array): array
{
foreach ($array as $key => $value) {
if (is_array($value)) {
......
......@@ -9,14 +9,8 @@ use HHIT\ConfigGenerator\Generator\Secrets\SecretProviderFactory;
class ValuesLoaderFactory
{
/**
* @var SecretProviderFactory
*/
private $secretProviderFactory;
/**
* @var string
*/
private $env;
private SecretProviderFactory $secretProviderFactory;
private string $env;
public function __construct(SecretProviderFactory $secretProviderFactory, string $env)
{
......@@ -24,8 +18,11 @@ class ValuesLoaderFactory
$this->env = $env;
}
public function create(File $file, string $vaultType): ValuesLoader
{
return new ValuesLoader($file, $this->secretProviderFactory->create($vaultType), $this->env);
public function create(
File $valuesFile,
?File $valuesEnvFile,
string $vaultType
): ValuesLoader {
return new ValuesLoader($valuesFile, $valuesEnvFile, $this->secretProviderFactory->create($vaultType), $this->env);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment