Skip to content

Commit

Permalink
Merge pull request #314 from michaelsauter/experiment/override
Browse files Browse the repository at this point in the history
Implement config override
  • Loading branch information
michaelsauter authored Nov 14, 2016
2 parents 97ab431 + b7133a3 commit f25c978
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@
[submodule "vendor/github.com/stretchr/testify"]
path = vendor/github.com/stretchr/testify
url = https://github.com/stretchr/testify
[submodule "vendor/github.com/imdario/mergo"]
path = vendor/github.com/imdario/mergo
url = https://github.com/imdario/mergo.git
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

## Unreleased

* Allow to override configuration by another configuration file. By default,
Crane is looking for e.g. `crane.override.yml` if the config file in use is
`crane.yml`. This can be customized via `--override`/`CRANE_OVERRIDE`.

_@michaelsauter_

## 2.10.3 (2016-10-27)

* Add `--debug` flag to `crane mac-sync start` which turns on verbose logging
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ continuous integration.
* [Hooks](#hooks)
* [Parallelism](#parallelism)
* [Container Prefixes](#container-prefixes)
* [Override configuration](#override-configuration)
* [Override image tag](#override-image-tag)
* [Generate command](#generate-command)
* [YAML advanced usage](#yaml-advanced-usage)
Expand Down Expand Up @@ -489,6 +490,12 @@ in parallel, e.g. for CI builds. Container prefixes can also be supplied by the
`CRANE_PREFIX` environment variable.


### Override configuration
The configuration can be overriden by another configuration file. By default,
Crane is looking for e.g. `crane.override.yml` if the config file in use is
`crane.yml`. This can be customized via `--override`/`CRANE_OVERRIDE`.


### Override image tag
By using a the `--tag` flag, it is possible to globally overrides image tags. If
you specify `--tag 2.0-rc2`, an image name `repo/app:1.0` is treated as
Expand Down
12 changes: 8 additions & 4 deletions crane/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ var (
"config",
"Location of config file.",
).Short('c').PlaceHolder("~/crane.yaml").String()
overrideFlag = app.Flag(
"override",
"Location of override file.",
).String()
prefixFlag = app.Flag(
"prefix",
"Container prefix.",
Expand Down Expand Up @@ -238,7 +242,7 @@ func isDryRun() bool {

func commandAction(targetFlag string, wrapped func(unitOfWork *UnitOfWork), mightStartRelated bool) {

cfg = NewConfig(*configFlag, *prefixFlag, *tagFlag)
cfg = NewConfig(*configFlag, *overrideFlag, *prefixFlag, *tagFlag)
allowed = allowedContainers(*excludeFlag, *onlyFlag)
dependencyMap := cfg.DependencyMap()
target, err := NewTarget(dependencyMap, targetFlag)
Expand Down Expand Up @@ -388,7 +392,7 @@ func runCli() {
}, false)

case syncStartCommand.FullCommand():
cfg = NewConfig(*configFlag, *prefixFlag, *tagFlag)
cfg = NewConfig(*configFlag, *overrideFlag, *prefixFlag, *tagFlag)
sync := cfg.MacSync(*syncStartVolumeArg)
if sync == nil {
printErrorf("ERROR: No such sync configured: %s.", *syncStartVolumeArg)
Expand All @@ -397,7 +401,7 @@ func runCli() {
sync.Start(*syncStartDebugFlag)

case syncStopCommand.FullCommand():
cfg = NewConfig(*configFlag, *prefixFlag, *tagFlag)
cfg = NewConfig(*configFlag, *overrideFlag, *prefixFlag, *tagFlag)
sync := cfg.MacSync(*syncStopVolumeArg)
if sync == nil {
printErrorf("ERROR: No such sync configured: %s.", *syncStartVolumeArg)
Expand All @@ -406,7 +410,7 @@ func runCli() {
sync.Stop()

case syncStatusCommand.FullCommand():
cfg = NewConfig(*configFlag, *prefixFlag, *tagFlag)
cfg = NewConfig(*configFlag, *overrideFlag, *prefixFlag, *tagFlag)
w := new(tabwriter.Writer)
w.Init(os.Stdout, 0, 8, 1, '\t', 0)
fmt.Fprintln(w, "VOLUME\tCONTAINER\tSTATUS")
Expand Down
40 changes: 38 additions & 2 deletions crane/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"strconv"
"strings"
"time"
"github.com/imdario/mergo"
)

type Config interface {
Expand Down Expand Up @@ -117,6 +118,31 @@ func readConfig(filename string) *config {
return unmarshal(data, ext)
}

func findOverride(filename, override string) string {
if len(override) > 0 {
if !path.IsAbs(override) {
override = path.Join(path.Dir(filename), override)
}
if _, err := os.Stat(override); err == nil {
return override
} else {
panic(StatusError{fmt.Errorf("Override %v not found", override), 78})
}
}

overrideFile := ""
lastIndex := strings.LastIndex(filename, ".")
if lastIndex > 0 {
overrideFile = filename[:lastIndex] + ".override" + filename[lastIndex:]
} else {
overrideFile = filename + ".override"
}
if _, err := os.Stat(overrideFile); err == nil {
return overrideFile
}
return ""
}

// displaySyntaxError will display more information
// such as line and error type given an error and
// the data that was unmarshalled.
Expand Down Expand Up @@ -164,13 +190,23 @@ func unmarshal(data []byte, ext string) *config {
// location.
// Containers will be ordered so that they can be
// brought up and down with Docker.
func NewConfig(location string, prefix string, tag string) Config {
func NewConfig(location string, override string, prefix string, tag string) Config {
var config *config
configFile := findConfig(location)
if isVerbose() {
printInfof("Using configuration file `%s`\n", configFile)
}
config = readConfig(configFile)
configBase := readConfig(configFile)
overrideFile := findOverride(configFile, override)
if len(overrideFile) > 0 {
if isVerbose() {
printInfof("Using override file `%s`\n", overrideFile)
}
config = readConfig(overrideFile)
mergo.Merge(config, configBase)
} else {
config = configBase
}
config.path = path.Dir(configFile)
config.initialize()
config.validate()
Expand Down
1 change: 1 addition & 0 deletions vendor/github.com/imdario/mergo
Submodule mergo added at 50d4db

0 comments on commit f25c978

Please sign in to comment.