Enable Environment Variables in Configuration File

This code change enables the use of Linux Environment
variables in the configuration file. This is very useful
for paths to avoid having to hard-code paths.

Added Environment Variable Checking

For proper use, it will also require a similar patch to
the bundle-chroot-builder project.

See https://github.com/clearlinux/bundle-chroot-builder/pull/12

Signed-off-by: Mark Horn <mark.d.horn@intel.com>
This commit is contained in:
Mark Horn
2017-12-08 15:57:38 -08:00
committed by tmarcu
parent 0f0497d0ec
commit 044f11e8aa
+13 -1
View File
@@ -115,9 +115,21 @@ func (b *Builder) ReadBuilderConf() {
for _, h := range fields {
r := regexp.MustCompile(h.re)
// Look for Environment variables in the config file
re := regexp.MustCompile(`\$\{?([[:word:]]+)\}?`)
for _, i := range lines {
if m := r.FindIndex([]byte(i)); m != nil {
*h.dest = i[m[1]:]
// We want the variable without the $ or {} for lookup checking
matches := re.FindAllStringSubmatch(i[m[1]:], -1)
for _, s := range matches {
if _, ok := os.LookupEnv(s[1]); !ok {
helpers.PrintError(fmt.Errorf("buildconf contains an undefined environment variable: %s", s[1]))
os.Exit(1)
}
}
// Replace valid Environment Variables
*h.dest = os.ExpandEnv(i[m[1]:])
}
}
}