config file supports environment variables

This commit is contained in:
Shlomi Noach 2016-07-25 15:46:37 +02:00
parent a74c1c0fd6
commit 4774b67ffd
3 changed files with 26 additions and 1 deletions

View File

@ -1,7 +1,7 @@
#!/bin/bash
#
#
RELEASE_VERSION="1.0.3"
RELEASE_VERSION="1.0.4"
buildpath=/tmp/gh-ost
target=gh-ost

View File

@ -7,6 +7,8 @@ package base
import (
"fmt"
"os"
"regexp"
"strings"
"sync"
"sync/atomic"
@ -35,6 +37,10 @@ const (
CutOverTwoStep = iota
)
var (
envVariableRegexp = regexp.MustCompile("[$][{](.*)[}]")
)
// MigrationContext has the general, global state of migration. It is used by
// all components throughout the migration process.
type MigrationContext struct {
@ -441,8 +447,19 @@ func (this *MigrationContext) ReadConfigFile() error {
if this.ConfigFile == "" {
return nil
}
gcfg.RelaxedParserMode = true
if err := gcfg.ReadFileInto(&this.config, this.ConfigFile); err != nil {
return err
}
// We accept user & password in the form "${SOME_ENV_VARIABLE}" in which case we pull
// the given variable from os env
if submatch := envVariableRegexp.FindStringSubmatch(this.config.Client.User); len(submatch) > 1 {
this.config.Client.User = os.Getenv(submatch[1])
}
if submatch := envVariableRegexp.FindStringSubmatch(this.config.Client.Password); len(submatch) > 1 {
this.config.Client.Password = os.Getenv(submatch[1])
}
return nil
}

8
vendor/gopkg.in/gcfg.v1/set.go generated vendored
View File

@ -16,6 +16,8 @@ type tag struct {
intMode string
}
var RelaxedParserMode = false
func newTag(ts string) tag {
t := tag{}
s := strings.Split(ts, ",")
@ -197,6 +199,9 @@ func set(cfg interface{}, sect, sub, name string, blank bool, value string) erro
vCfg := vPCfg.Elem()
vSect, _ := fieldFold(vCfg, sect)
if !vSect.IsValid() {
if RelaxedParserMode {
return nil
}
return fmt.Errorf("invalid section: section %q", sect)
}
if vSect.Kind() == reflect.Map {
@ -232,6 +237,9 @@ func set(cfg interface{}, sect, sub, name string, blank bool, value string) erro
}
vVar, t := fieldFold(vSect, name)
if !vVar.IsValid() {
if RelaxedParserMode {
return nil
}
return fmt.Errorf("invalid variable: "+
"section %q subsection %q variable %q", sect, sub, name)
}