From 044f11e8aa928b60ddbb1ebaf02e8c504858c4fd Mon Sep 17 00:00:00 2001 From: Mark Horn Date: Fri, 8 Dec 2017 15:43:56 -0800 Subject: [PATCH] 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 --- src/builder/builder.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/builder/builder.go b/src/builder/builder.go index 78cab59..5f5dc42 100644 --- a/src/builder/builder.go +++ b/src/builder/builder.go @@ -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]:]) } } }