Skip to content

Commit 18c5f18

Browse files
committed
ID-31: read property content from file
1 parent a5ea4a5 commit 18c5f18

File tree

10 files changed

+106
-33
lines changed

10 files changed

+106
-33
lines changed

CHANGELOG.adoc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
= Changelog
22

3+
== Version 0.11.0
4+
[cols="1,2,<10a", options="header"]
5+
|===
6+
|ID|Type|Description
7+
|https://github.com/Axway-API-Management-Plus/apigw-maven-plugin/issues/31[#31]
8+
|Enhancement
9+
|The `buildfed` tool is enhanced to set properties from the content of a specified file.
10+
11+
A new `-F NAME:FILE` command line parameter is introduced.
12+
13+
Example:
14+
15+
$ buildfed ... -F description:config/description.txt
16+
17+
This will define a new property `description` which is populated from the content of the `description.txt` file.
18+
|===
19+
320
== Version 0.10.0
421
[cols="1,2,<10a", options="header"]
522
|===

doc/manual/_config-tool.adoc

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ Options:
4848
Path of output environment archive file (.env)
4949
[optional]
5050
-D NAME:VALUE, --define=NAME:VALUE
51-
Define a system property [multiple]
51+
Define a command line property [multiple]
52+
-F NAME:FILE, --fromFile=NAME:FILE
53+
Define a command line property from file content [multiple]
5254
--passphrase-in=PASSPHRASE
5355
Passphrase of input archive files [optional]
5456
--passphrase-out=PASSPHRASE
@@ -106,10 +108,16 @@ If missing, no `.fed` file is generated.
106108
If missing, no `.env` file is generated.
107109

108110
|--define
109-
|Define a system property `name:value`.
110-
System properties can be used instead of custom defined properties.
111+
|Define a command line property `name:value`.
112+
Command line properties can be used instead of custom defined properties.
111113
Multiple properties are supported by specifying the parameter multiple times.
112114

115+
|--fromFile
116+
|Define a command line property from file content `name:file`.
117+
The file has to contain an UTF-8 encoded text.
118+
The content of the file will be placed into the defined property.
119+
Multiple files are supported by specifying the parameter multiple times.
120+
113121
|--passphrase-in
114122
|Passphrase for source archives.
115123
This passphrase is used for reading the `.pol` and `.env` files.
@@ -163,7 +171,7 @@ Properties are name/value pairs.
163171
The value of the property is defined ... _(in order of precedence)_
164172

165173
. ... as predefined system property.
166-
. ... as a system property via the command line of the configuration tool.
174+
. ... as a command line property via the command line of the configuration tool.
167175
. ... within a separate property configuration file.
168176
. ... within the environmentalized fields file (under `properties`).
169177

example/config-tool/README.adoc

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ $ curl -ks https://localhost:8443/version
3333
{
3434
"version": {
3535
"artifact": "undefined",
36-
"buildTime": "1970-01-01T00:00:00"
36+
"buildTime": "1970-01-01T00:00:00",
37+
"name": "",
38+
"description": ""
3739
}
3840
}
3941
----
@@ -68,14 +70,17 @@ $ curl -ks https://localhost:18443/version <1>
6870
{
6971
"version": {
7072
"artifact": "demo-1.0.0", <2>
71-
"buildTime": "2019-08-23T23:09:46.684000" <3>
73+
"buildTime": "2019-12-16T20:38:11.080000", <3>
74+
"name", Demo, <4>
75+
"description": "Service for demonstration of configuration tool." <5>
7276
}
7377
}
7478
----
75-
<1> port is changed to 18443
76-
<2> artifact is replaced
77-
<3> build time is set
78-
79+
<1> Server and port is set to `localhost:18443`
80+
<2> Artifact is replaced.
81+
<3> Build time is automatically generated.
82+
<4> Name is set.
83+
<5> Description is set.
7984

8085
[NOTE]
8186
====
@@ -95,7 +100,8 @@ We have three configuration files in the `config` folder:
95100

96101
NOTE: For a description of the configuration files please check the link:../../doc/manual/user-guide.adoc[User Guide]
97102

98-
In this example the `buildfed` tool will be invoked with the following parameters:
103+
In this example some environment variables (`INFO_NAME` and `NEW_SERVER_PASSWORD`) are set.
104+
Then the `buildfed` tool will be invoked with the following parameters:
99105

100106
* `-e src\gateway.env`: specifies the path to the input `.env` archive
101107
* `-p src\gateway.pol`: specifies the path to the input `.pol` archive
@@ -107,6 +113,7 @@ In this example the `buildfed` tool will be invoked with the following parameter
107113
* `--passphrase-in=changeme`: passphrase for input archives (`.pol` and `.env`)
108114
* `--passphrase-out=changed`: passphrase for the generated `.fed` file
109115
* `-D artifact:demo-1.0.0`: specifies the value of property `artifact` via the command line instead of from the configuration files
116+
* `-F info.descr:config/description.txt`: specifies to set the value of property `info.descr` from the content of the `config/description.txt` file
110117

111118
The tool reads the source archives, configures the environmentalized fields, replaces the certificates and write the configured `.fed` file.
112119

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Service for demonstration of configuration tool.

example/config-tool/config/gateway.config.json

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,28 @@
2222
}
2323
}
2424
},
25+
"/[CircuitContainer]name=Demo/[FilterCircuit]name=Show Build Version/[SetAttributeFilter]name=Set description": {
26+
"description": "",
27+
"fields": {
28+
"attributeValue#0": {
29+
"source": "property",
30+
"type": "string",
31+
"used": true,
32+
"value": "info.descr"
33+
}
34+
}
35+
},
36+
"/[CircuitContainer]name=Demo/[FilterCircuit]name=Show Build Version/[SetAttributeFilter]name=Set name": {
37+
"description": "",
38+
"fields": {
39+
"attributeValue#0": {
40+
"source": "env",
41+
"type": "string",
42+
"used": true,
43+
"value": "INFO_NAME"
44+
}
45+
}
46+
},
2547
"/[NetService]name=Service/[HTTP]name=Default Service/[SSLInterface]name=HTTPS": {
2648
"description": "",
2749
"fields": {
@@ -32,10 +54,10 @@
3254
"value": "service.address"
3355
},
3456
"port#0": {
35-
"source": "env",
57+
"source": "property",
3658
"type": "string",
3759
"used": true,
38-
"value": "SERVICE_PORT"
60+
"value": "service.port"
3961
}
4062
}
4163
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"properties": {
3-
"service.address": "localhost"
3+
"service.address": "localhost",
4+
"service.port": 18443
45
}
56
}

example/config-tool/run.cmd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ CD /d "%CMD_HOME%"
55
SET BUILDFED="..\..\src\main\resources\scripts\buildfed.cmd"
66

77
REM Define environment variables for field value and password configuration
8-
SET SERVICE_PORT=18443
8+
SET INFO_NAME=Demo
99
SET NEW_SERVER_PASSWORD=changeme
1010

11-
CALL %BUILDFED% -e src\gateway.env -p src\gateway.pol -c config\gateway.config.json --cert=config\gateway.certs.json --prop=config\gateway.props.json --prop=config\passwords.props.json -D artifact:demo-1.0.0 --output-fed=gateway.fed --passphrase-in=changeme --passphrase-out=changed
11+
CALL %BUILDFED% -e src\gateway.env -p src\gateway.pol -c config\gateway.config.json --cert=config\gateway.certs.json --prop=config\gateway.props.json --prop=config\passwords.props.json -D artifact:demo-1.0.0 -F info.descr:config\description.txt --output-fed=gateway.fed --passphrase-in=changeme --passphrase-out=changed
1212
ENDLOCAL
113 Bytes
Binary file not shown.
200 Bytes
Binary file not shown.

src/main/resources/scripts/lib/buildfed.py

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import sys
2+
import codecs
3+
import os
24
import traceback
35
import logging
46

@@ -7,19 +9,22 @@
79
from envconfig import PropConfig
810
from datetime import datetime
911

10-
def parse_system_property(sys_prop):
11-
if not sys_prop:
12-
logging.error("Empty system property not allowed!")
12+
def parse_cli_property(cli_prop):
13+
if not cli_prop:
14+
logging.error("Empty property not allowed!")
1315
exit(1)
1416

15-
s = sys_prop.split(":", 1)
17+
s = cli_prop.split(":", 1)
1618
if not s[0]:
17-
logging.error("Empty system property name!")
19+
logging.error("Empty property name!")
1820
exit(1)
1921

2022
name = s[0]
2123
value = s[1] if len(s) > 1 else None
2224

25+
if not value:
26+
logging.warn("Empty value for property '%s'." % name)
27+
2328
return (name, value)
2429

2530

@@ -40,7 +45,8 @@ def main():
4045
parser.add_option("--cert-config-update", dest="cert_config_update", help="Enable writing of info section for 'update' certificates within the configuration file [optional]", action="store_true")
4146
parser.add_option("--output-fed", dest="out_fed_file_path", help="Path of output deployment archive file (.fed) [optional]", metavar="FILEPATH")
4247
parser.add_option("--output-env", dest="out_env_file_path", help="Path of output environment archive file (.env) [optional]", metavar="FILEPATH")
43-
parser.add_option("-D", "--define", dest="sys_properties", help="Define a system property [multiple]", metavar="NAME:VALUE", action="append")
48+
parser.add_option("-D", "--define", dest="cli_properties", help="Define a command line property [multiple]", metavar="NAME:VALUE", action="append")
49+
parser.add_option("-F", "--fromFile", dest="file_properties", help="Define a command line property from file content [multiple]", metavar="NAME:FILE", action="append")
4450
parser.add_option("--passphrase-in", dest="passphrase_in", help="Passphrase of input archive files [optional]", metavar="PASSPHRASE")
4551
parser.add_option("--passphrase-out", dest="passphrase_out", help="Passphrase for output archive files [optional]", metavar="PASSPHRASE")
4652
parser.add_option("-s", "--simulate", dest="simulate", help="Enable simulation mode [optional]", action="store_true")
@@ -59,20 +65,31 @@ def main():
5965
else:
6066
logging.getLogger().setLevel(logging.INFO)
6167

62-
# Add some standard system properties
63-
sys_properties = {}
64-
sys_properties["_system.build.datetime"] = datetime.now().isoformat()
65-
66-
if options.sys_properties:
67-
for sp in options.sys_properties:
68-
(name, value) = parse_system_property(sp)
69-
sys_properties[name] = value
68+
# Add some standard command line properties
69+
cli_properties = {}
70+
cli_properties["_system.build.datetime"] = datetime.now().isoformat()
71+
72+
# Add command line properties
73+
if options.cli_properties:
74+
for sp in options.cli_properties:
75+
(name, value) = parse_cli_property(sp)
76+
cli_properties[name] = value
77+
78+
if options.file_properties:
79+
for fp in options.file_properties:
80+
(name, prop_file) = parse_cli_property(fp)
81+
if not os.path.isfile(prop_file):
82+
raise ValueError("File for command line property '%s' doesn't exist: %s" % (name, prop_file))
83+
84+
logging.debug("Reading command line property '%s' from file '%s'" % (name, prop_file))
85+
with codecs.open(prop_file, encoding='utf-8', mode='r') as pf:
86+
cli_properties[name] = pf.read()
7087

7188
try:
72-
# Set properties from files and system properties
89+
# Set properties from files and command line properties
7390
properties = PropConfig(options.prop_file_path_list)
74-
for name, value in sys_properties.items():
75-
logging.info("Provided system property %s" % (name))
91+
for name, value in cli_properties.items():
92+
logging.info("Provided command line property %s" % (name))
7693
properties.set_property(name, value)
7794

7895
# Set passphrases

0 commit comments

Comments
 (0)