Skip to content

Commit

Permalink
bake: read env vars from alternate env-file
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <[email protected]>
  • Loading branch information
crazy-max committed Aug 9, 2022
1 parent 76252ed commit 21ce42f
Show file tree
Hide file tree
Showing 9 changed files with 374 additions and 131 deletions.
43 changes: 33 additions & 10 deletions bake/bake.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ func ReadLocalFiles(names []string) ([]File, error) {
return out, nil
}

func ReadTargets(ctx context.Context, files []File, targets, overrides []string, defaults map[string]string) (map[string]*Target, []*Group, error) {
c, err := ParseFiles(files, defaults)
func ReadTargets(ctx context.Context, files []File, targets []string, envs map[string]string, overrides []string, defaults map[string]string) (map[string]*Target, []*Group, error) {
c, err := ParseFiles(files, envs, defaults)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -181,15 +181,30 @@ func dedupMap(ms ...map[string]string) map[string]string {
return res
}

func ParseFiles(files []File, defaults map[string]string) (_ *Config, err error) {
func sliceToMap(env []string) (res map[string]string) {
res = make(map[string]string)
for _, s := range env {
kv := strings.SplitN(s, "=", 2)
key := kv[0]
switch {
case len(kv) == 1:
res[key] = ""
default:
res[key] = kv[1]
}
}
return
}

func ParseFiles(files []File, envs map[string]string, defaults map[string]string) (_ *Config, err error) {
defer func() {
err = formatHCLError(err, files)
}()

var c Config
var fs []*hcl.File
for _, f := range files {
cfg, isCompose, composeErr := ParseComposeFile(f.Data, f.Name)
cfg, isCompose, composeErr := ParseComposeFile(f.Data, f.Name, envs)
if isCompose {
if composeErr != nil {
return nil, composeErr
Expand All @@ -214,7 +229,15 @@ func ParseFiles(files []File, defaults map[string]string) (_ *Config, err error)

if len(fs) > 0 {
if err := hclparser.Parse(hcl.MergeFiles(fs), hclparser.Opt{
LookupVar: os.LookupEnv,
LookupVar: func(key string) (string, bool) {
if envs == nil {
return "", false
}
if v, ok := envs[key]; ok {
return v, true
}
return "", false
},
Vars: defaults,
ValidateLabel: validateTargetName,
}, &c); err.HasErrors() {
Expand All @@ -239,20 +262,20 @@ func dedupeConfig(c Config) Config {
return c2
}

func ParseFile(dt []byte, fn string) (*Config, error) {
return ParseFiles([]File{{Data: dt, Name: fn}}, nil)
func ParseFile(dt []byte, fn string, envs map[string]string) (*Config, error) {
return ParseFiles([]File{{Data: dt, Name: fn}}, envs, nil)
}

func ParseComposeFile(dt []byte, fn string) (*Config, bool, error) {
func ParseComposeFile(dt []byte, fn string, envs map[string]string) (*Config, bool, error) {
fnl := strings.ToLower(fn)
if strings.HasSuffix(fnl, ".yml") || strings.HasSuffix(fnl, ".yaml") {
cfg, err := ParseCompose(dt)
cfg, err := ParseCompose(dt, envs)
return cfg, true, err
}
if strings.HasSuffix(fnl, ".json") || strings.HasSuffix(fnl, ".hcl") {
return nil, false, nil
}
cfg, err := ParseCompose(dt)
cfg, err := ParseCompose(dt, envs)
return cfg, err == nil, err
}

Expand Down
Loading

0 comments on commit 21ce42f

Please sign in to comment.