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 @@ ...@@ -2,19 +2,19 @@
"users.json": { "users.json": {
"type": "json", "type": "json",
"template": "./cfgen/users.json.hbs", "template": "./cfgen/users.json.hbs",
"values": "./cfgen/users.values.json", "values": "./cfgen/values.json",
"destination": "./output/users.json" "destination": "./output/users.json"
}, },
"users.txt": { "users.txt": {
"type": "text", "type": "text",
"template": "./cfgen/users.txt.hbs", "template": "./cfgen/users.txt.hbs",
"values": "./cfgen/users.values.json", "values": "./cfgen/values.json",
"destination": "./output/users.txt" "destination": "./output/users.txt"
}, },
"users.yaml": { "users.yaml": {
"type": "yaml", "type": "yaml",
"template": "./cfgen/users.yaml.hbs", "template": "./cfgen/users.yaml.hbs",
"values": "./cfgen/users.values.json", "values": "./cfgen/values.json",
"destination": "./output/users.yaml" "destination": "./output/users.yaml"
} }
} }
...@@ -10,5 +10,6 @@ ...@@ -10,5 +10,6 @@
}, },
"version": 2, "version": 2,
"secret": "{{secret}}", "secret": "{{secret}}",
"env": "{{_env}}" "env": "{{_env}}",
"fromEnvJson": "{{sampleEnvValue}}"
} }
...@@ -4,3 +4,4 @@ users_{{this.username}}_cleartext='{{this.password}}' ...@@ -4,3 +4,4 @@ users_{{this.username}}_cleartext='{{this.password}}'
{{/each}} {{/each}}
version=2 version=2
secret='{{this.secret}}' secret='{{this.secret}}'
sample_env_value='{{this.sampleEnvValue}}'
...@@ -7,3 +7,4 @@ users: ...@@ -7,3 +7,4 @@ users:
{{/each}} {{/each}}
version: 2 version: 2
secret: '{{this.secret}}' secret: '{{this.secret}}'
sampleEnvValue: '{{sampleEnvValue}}'
...@@ -18,5 +18,6 @@ ...@@ -18,5 +18,6 @@
"secret": { "secret": {
"$type": "secret", "$type": "secret",
"arg": "secret" "arg": "secret"
} },
"sampleEnvValue": "defaultValueForAllEnvs"
} }
{
"sampleEnvValue": "sampleValueOverridenInTestEnv"
}
...@@ -9,33 +9,20 @@ use HHIT\ConfigGenerator\Generator\IO\Path; ...@@ -9,33 +9,20 @@ use HHIT\ConfigGenerator\Generator\IO\Path;
class Definition class Definition
{ {
/** private string $id;
* @var string private File $templateFile;
*/ private string $type;
private $id; private File $valuesFile;
/** private File $valuesEnvFile;
* @var File
*/
private $templateFile;
/**
* @var string
*/
private $type;
/**
* @var File
*/
private $valuesFile;
/**
* @var File
*/
private $destinationFile; 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->id = $id;
$this->templateFile = $templateFile; $this->templateFile = $templateFile;
$this->type = $type; $this->type = $type;
$this->valuesFile = $valuesFile; $this->valuesFile = $valuesFile;
$this->valuesEnvFile = $valuesEnvFile;
$this->destinationFile = $destinationFile; $this->destinationFile = $destinationFile;
} }
...@@ -59,6 +46,15 @@ class Definition ...@@ -59,6 +46,15 @@ class Definition
return $this->valuesFile; return $this->valuesFile;
} }
public function getValuesEnvFile(): ?File
{
if ($this->valuesEnvFile->exists()) {
return $this->valuesEnvFile;
} else {
return null;
}
}
public function getDestinationFile(): File public function getDestinationFile(): File
{ {
return $this->destinationFile; return $this->destinationFile;
......
...@@ -10,14 +10,16 @@ use HHIT\ConfigGenerator\Generator\StringUtils; ...@@ -10,14 +10,16 @@ use HHIT\ConfigGenerator\Generator\StringUtils;
class DefinitionReader class DefinitionReader
{ {
/** private const RELATIVE_PATH_PREFIX = './';
* @var string
*/ private string $projectDir;
private $projectDir;
public function __construct(string $projectDir) private string $env;
public function __construct(string $projectDir, string $env)
{ {
$this->projectDir = $projectDir; $this->projectDir = $projectDir;
$this->env = $env;
} }
/** /**
...@@ -32,18 +34,34 @@ class DefinitionReader ...@@ -32,18 +34,34 @@ class DefinitionReader
$definitions = []; $definitions = [];
foreach ($reader->readAsJson() as $id => $definition) { 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']); 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']); 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']); 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; 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; ...@@ -18,14 +18,8 @@ use function HHIT\ConfigGenerator\cfgen_secrets_directory;
class Factory class Factory
{ {
/** private string $projectDir;
* @var string private string $env;
*/
private $projectDir;
/**
* @var string
*/
private $env;
public function __construct(string $projectDir, string $env = 'dev') public function __construct(string $projectDir, string $env = 'dev')
{ {
...@@ -35,7 +29,7 @@ class Factory ...@@ -35,7 +29,7 @@ class Factory
private function createDefinitionReader(): DefinitionReader private function createDefinitionReader(): DefinitionReader
{ {
return new DefinitionReader($this->projectDir); return new DefinitionReader($this->projectDir, $this->env);
} }
public function dumpPrivateKey() public function dumpPrivateKey()
......
...@@ -52,7 +52,7 @@ class Generator ...@@ -52,7 +52,7 @@ class Generator
$this->writeln($output, "<info>- processing {$definition->getId()}</info>"); $this->writeln($output, "<info>- processing {$definition->getId()}</info>");
try { try {
$valuesLoader = $this->valuesLoaderFactory->create($definition->getValuesFile(), $vaultType); $valuesLoader = $this->valuesLoaderFactory->create($definition->getValuesFile(), $definition->getValuesEnvFile(), $vaultType);
$compiler = new Compiler($definition->getTemplateFile(), $valuesLoader); $compiler = new Compiler($definition->getTemplateFile(), $valuesLoader);
$content = $compiler->compile(); $content = $compiler->compile();
try { try {
......
...@@ -8,32 +8,33 @@ use HHIT\ConfigGenerator\Generator\IO\File; ...@@ -8,32 +8,33 @@ use HHIT\ConfigGenerator\Generator\IO\File;
use HHIT\ConfigGenerator\Generator\IO\JsonReader; use HHIT\ConfigGenerator\Generator\IO\JsonReader;
use HHIT\ConfigGenerator\Generator\Secrets\SecretProviderInterface; use HHIT\ConfigGenerator\Generator\Secrets\SecretProviderInterface;
class ValuesLoader extends JsonReader class ValuesLoader
{ {
/** private JsonReader $valuesReader;
* @var array private ?JsonReader $valuesEnvReader;
*/ private array $context = [];
private $context = []; private SecretProviderInterface $provider;
/** private string $env;
* @var SecretProviderInterface
*/
private $provider;
/**
* @var string
*/
private $env;
public function __construct(File $file, SecretProviderInterface $provider, string $env) public function __construct(
{ File $valuesFile,
parent::__construct($file); ?File $valuesEnvFile,
SecretProviderInterface $provider,
string $env
) {
$this->valuesReader = new JsonReader($valuesFile);
$this->valuesEnvReader = $valuesEnvFile ? new JsonReader($valuesEnvFile) : null;
$this->provider = $provider; $this->provider = $provider;
$this->env = $env; $this->env = $env;
} }
public function getValues() public function getValues(): array
{ {
if (!$this->context) { 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)) { if (array_key_exists('_env', $this->context)) {
throw new \RuntimeException('_env is a reserved value - do not define it manually!'); throw new \RuntimeException('_env is a reserved value - do not define it manually!');
} }
...@@ -42,7 +43,7 @@ class ValuesLoader extends JsonReader ...@@ -42,7 +43,7 @@ class ValuesLoader extends JsonReader
return $this->context; return $this->context;
} }
private function postProcess(array $array) private function postProcess(array $array): array
{ {
foreach ($array as $key => $value) { foreach ($array as $key => $value) {
if (is_array($value)) { if (is_array($value)) {
......
...@@ -9,14 +9,8 @@ use HHIT\ConfigGenerator\Generator\Secrets\SecretProviderFactory; ...@@ -9,14 +9,8 @@ use HHIT\ConfigGenerator\Generator\Secrets\SecretProviderFactory;
class ValuesLoaderFactory class ValuesLoaderFactory
{ {
/** private SecretProviderFactory $secretProviderFactory;
* @var SecretProviderFactory private string $env;
*/
private $secretProviderFactory;
/**
* @var string
*/
private $env;
public function __construct(SecretProviderFactory $secretProviderFactory, string $env) public function __construct(SecretProviderFactory $secretProviderFactory, string $env)
{ {
...@@ -24,8 +18,11 @@ class ValuesLoaderFactory ...@@ -24,8 +18,11 @@ class ValuesLoaderFactory
$this->env = $env; $this->env = $env;
} }
public function create(File $file, string $vaultType): ValuesLoader public function create(
{ File $valuesFile,
return new ValuesLoader($file, $this->secretProviderFactory->create($vaultType), $this->env); ?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