Skip to content

Commit

Permalink
Implemented possibility to override custom properties during runtime. (
Browse files Browse the repository at this point in the history
…#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
  • Loading branch information
jaroslaw-bochniak authored May 15, 2019
1 parent 8766ec5 commit 64f72bc
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
APP_NAME=docker-bakery
VERSION=1.1.0
VERSION=1.2.0

.DEFAULT_GOAL: all

Expand Down
8 changes: 8 additions & 0 deletions bakery/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ func GetCommands() []cli.Command {
Name: "rootDir, rd",
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.",
},
cli.StringSliceFlag{
Name: "property, p",
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",
},
},
Usage: "Used to fill Dockerfile.template file. Values needed for template are taken from the config file and from dynamic properties provided during runtime.",
Before: commands.InitConfiguration,
Expand Down Expand Up @@ -59,6 +63,10 @@ func GetCommands() []cli.Command {
Name: "skip-dependants, sd",
Usage: "Optional. False be default. If this flag is set build of the parent will not trigger dependant builds.",
},
cli.StringSliceFlag{
Name: "property, p",
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",
},
},
Usage: "Used to build next version of the images in given scope. Optionally it can skip build of dependant images.",
Before: commands.InitConfiguration,
Expand Down
2 changes: 1 addition & 1 deletion bakery/commands/bakery_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
// InitConfiguration initializes configuration for the rest of invoked commands.
// Receives config file path and optionally root directory to override the config section.
func InitConfiguration(c *cli.Context) error {
return service.InitConfiguration(c.String("c"), c.String("rd"))
return service.InitConfiguration(c.String("c"), c.String("rd"), c.StringSlice("p"))
}

// FillTemplateCmd fills input dockerfile template and stores the result under provided output.
Expand Down
27 changes: 21 additions & 6 deletions bakery/service/bakery.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ import (
)

const (
unableToDetermine = "unable-to-determine"
dependencyPrefix = "FROM "
outputSeparator = "====================================================================\n"
propertyKeyValueSeparator = "="
unableToDetermine = "unable-to-determine"
dependencyPrefix = "FROM "
outputSeparator = "====================================================================\n"
)

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

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

updateConfigProperties()
updateConfigProperties(additionalProperties)

err = hierarchy.AnalyzeStructure(config.RootDir, versions)
if err != nil {
Expand Down Expand Up @@ -112,7 +113,7 @@ func filterOutNotExistingImages() {
}
}

func updateConfigProperties() {
func updateConfigProperties(additionalProperties []string) {
// update config properties with the latest versions of available images
// versions are determined from the git tags
// this is especially useful when for the first time new child image is about to be build (without being triggered by a parent build)
Expand All @@ -128,6 +129,20 @@ func updateConfigProperties() {
config.setBuilderName(name)
config.setBuilderEmail(email)
config.setBuilderHost(host)

overrideWithRuntimeProvidedProperties(additionalProperties)
}

func overrideWithRuntimeProvidedProperties(additionalProperties []string) {
for _, additionalProperty := range additionalProperties {
keyValuePair := strings.Split(additionalProperty, propertyKeyValueSeparator)
if len(keyValuePair) != 2 {
fmt.Printf("Unable to parse provided property: %s - expecting key and value to be separated with %s", additionalProperty, propertyKeyValueSeparator)
continue
}
commons.Debugf("Overriding config property: %s with %s", keyValuePair[0], keyValuePair[1])
config.Properties[keyValuePair[0]] = keyValuePair[1]
}
}

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

0 comments on commit 64f72bc

Please sign in to comment.