Skip to content

Commit 64f72bc

Browse files
Implemented possibility to override custom properties during runtime. (#7)
It is for example possible to invoke the build with additional properties in such way: docker-bakery build ... --property foo1=bar1 --property foo2=bar2
1 parent 8766ec5 commit 64f72bc

File tree

4 files changed

+31
-8
lines changed

4 files changed

+31
-8
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
APP_NAME=docker-bakery
2-
VERSION=1.1.0
2+
VERSION=1.2.0
33

44
.DEFAULT_GOAL: all
55

bakery/commands.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ func GetCommands() []cli.Command {
3030
Name: "rootDir, rd",
3131
Usage: "Optional. Used to override rootDir of the dockerfiles location. Can be defined in config.json, provided in this argument or determined dynamically from the base dir of config file.",
3232
},
33+
cli.StringSliceFlag{
34+
Name: "property, p",
35+
Usage: "Optional. Allows for providing additional multiple properties that can be used during templating. Overrides properties defined in config.json file. Expected format is: -p propertyName=propertyValue",
36+
},
3337
},
3438
Usage: "Used to fill Dockerfile.template file. Values needed for template are taken from the config file and from dynamic properties provided during runtime.",
3539
Before: commands.InitConfiguration,
@@ -59,6 +63,10 @@ func GetCommands() []cli.Command {
5963
Name: "skip-dependants, sd",
6064
Usage: "Optional. False be default. If this flag is set build of the parent will not trigger dependant builds.",
6165
},
66+
cli.StringSliceFlag{
67+
Name: "property, p",
68+
Usage: "Optional. Allows for providing additional multiple properties that can be used during templating. Overrides properties defined in config.json file. Expected format is: -p propertyName=propertyValue",
69+
},
6270
},
6371
Usage: "Used to build next version of the images in given scope. Optionally it can skip build of dependant images.",
6472
Before: commands.InitConfiguration,

bakery/commands/bakery_actions.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
// InitConfiguration initializes configuration for the rest of invoked commands.
1010
// Receives config file path and optionally root directory to override the config section.
1111
func InitConfiguration(c *cli.Context) error {
12-
return service.InitConfiguration(c.String("c"), c.String("rd"))
12+
return service.InitConfiguration(c.String("c"), c.String("rd"), c.StringSlice("p"))
1313
}
1414

1515
// FillTemplateCmd fills input dockerfile template and stores the result under provided output.

bakery/service/bakery.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ import (
2121
)
2222

2323
const (
24-
unableToDetermine = "unable-to-determine"
25-
dependencyPrefix = "FROM "
26-
outputSeparator = "====================================================================\n"
24+
propertyKeyValueSeparator = "="
25+
unableToDetermine = "unable-to-determine"
26+
dependencyPrefix = "FROM "
27+
outputSeparator = "====================================================================\n"
2728
)
2829

2930
var config *Config
@@ -36,7 +37,7 @@ var hierarchy = NewDockerHierarchy()
3637
var startTime = time.Now()
3738

3839
// InitConfiguration is called before execution of other commands, parses config and gathers docker image dependencies/hierarchy
39-
func InitConfiguration(configFile, rootDir string) error {
40+
func InitConfiguration(configFile, rootDir string, additionalProperties []string) error {
4041
var err error
4142
if configFile == "" {
4243
return fmt.Errorf("config file path has to be provided")
@@ -62,7 +63,7 @@ func InitConfiguration(configFile, rootDir string) error {
6263
return err
6364
}
6465

65-
updateConfigProperties()
66+
updateConfigProperties(additionalProperties)
6667

6768
err = hierarchy.AnalyzeStructure(config.RootDir, versions)
6869
if err != nil {
@@ -112,7 +113,7 @@ func filterOutNotExistingImages() {
112113
}
113114
}
114115

115-
func updateConfigProperties() {
116+
func updateConfigProperties(additionalProperties []string) {
116117
// update config properties with the latest versions of available images
117118
// versions are determined from the git tags
118119
// this is especially useful when for the first time new child image is about to be build (without being triggered by a parent build)
@@ -128,6 +129,20 @@ func updateConfigProperties() {
128129
config.setBuilderName(name)
129130
config.setBuilderEmail(email)
130131
config.setBuilderHost(host)
132+
133+
overrideWithRuntimeProvidedProperties(additionalProperties)
134+
}
135+
136+
func overrideWithRuntimeProvidedProperties(additionalProperties []string) {
137+
for _, additionalProperty := range additionalProperties {
138+
keyValuePair := strings.Split(additionalProperty, propertyKeyValueSeparator)
139+
if len(keyValuePair) != 2 {
140+
fmt.Printf("Unable to parse provided property: %s - expecting key and value to be separated with %s", additionalProperty, propertyKeyValueSeparator)
141+
continue
142+
}
143+
commons.Debugf("Overriding config property: %s with %s", keyValuePair[0], keyValuePair[1])
144+
config.Properties[keyValuePair[0]] = keyValuePair[1]
145+
}
131146
}
132147

133148
// valueGetterFn represents no-arg function that returns a string and possibly an error

0 commit comments

Comments
 (0)