Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
hhit
config-generator
Commits
a84ab813
Commit
a84ab813
authored
May 06, 2024
by
Hendrik Heneke
Browse files
Additional values per environment
parent
ff1a433e
Pipeline
#544
passed with stage
in 43 seconds
Changes
12
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
87 additions
and
74 deletions
+87
-74
fixture/cfgen.json
fixture/cfgen.json
+3
-3
fixture/cfgen/users.json.hbs
fixture/cfgen/users.json.hbs
+2
-1
fixture/cfgen/users.txt.hbs
fixture/cfgen/users.txt.hbs
+1
-0
fixture/cfgen/users.yaml.hbs
fixture/cfgen/users.yaml.hbs
+1
-0
fixture/cfgen/values.json
fixture/cfgen/values.json
+2
-1
fixture/cfgen/values_test.json
fixture/cfgen/values_test.json
+3
-0
src/Generator/Definition/Definition.php
src/Generator/Definition/Definition.php
+16
-20
src/Generator/Definition/DefinitionReader.php
src/Generator/Definition/DefinitionReader.php
+27
-9
src/Generator/Factory.php
src/Generator/Factory.php
+3
-9
src/Generator/Generator.php
src/Generator/Generator.php
+1
-1
src/Generator/Values/ValuesLoader.php
src/Generator/Values/ValuesLoader.php
+20
-19
src/Generator/Values/ValuesLoaderFactory.php
src/Generator/Values/ValuesLoaderFactory.php
+8
-11
No files found.
fixture/cfgen.json
View file @
a84ab813
...
...
@@ -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"
}
}
fixture/cfgen/users.json.hbs
View file @
a84ab813
...
...
@@ -10,5 +10,6 @@
},
"version": 2,
"secret": "
{{
secret
}}
",
"env": "
{{
_env
}}
"
"env": "
{{
_env
}}
",
"fromEnvJson": "
{{
sampleEnvValue
}}
"
}
fixture/cfgen/users.txt.hbs
View file @
a84ab813
...
...
@@ -4,3 +4,4 @@ users_{{this.username}}_cleartext='{{this.password}}'
{{/
each
}}
version=2
secret='
{{
this
.
secret
}}
'
sample_env_value='
{{
this
.
sampleEnvValue
}}
'
fixture/cfgen/users.yaml.hbs
View file @
a84ab813
...
...
@@ -7,3 +7,4 @@ users:
{{/
each
}}
version: 2
secret: '
{{
this
.
secret
}}
'
sampleEnvValue: '
{{
sampleEnvValue
}}
'
fixture/cfgen/
users.
values.json
→
fixture/cfgen/values.json
View file @
a84ab813
...
...
@@ -18,5 +18,6 @@
"secret"
:
{
"$type"
:
"secret"
,
"arg"
:
"secret"
}
},
"sampleEnvValue"
:
"defaultValueForAllEnvs"
}
fixture/cfgen/values_test.json
0 → 100644
View file @
a84ab813
{
"sampleEnvValue"
:
"sampleValueOverridenInTestEnv"
}
src/Generator/Definition/Definition.php
View file @
a84ab813
...
...
@@ -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
;
...
...
src/Generator/Definition/DefinitionReader.php
View file @
a84ab813
...
...
@@ -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
;
}
}
src/Generator/Factory.php
View file @
a84ab813
...
...
@@ -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
()
...
...
src/Generator/Generator.php
View file @
a84ab813
...
...
@@ -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
{
...
...
src/Generator/Values/ValuesLoader.php
View file @
a84ab813
...
...
@@ -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
))
{
...
...
src/Generator/Values/ValuesLoaderFactory.php
View file @
a84ab813
...
...
@@ -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
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment