Allow to read env from dotenv file
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
@@ -4,7 +4,6 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"github.com/docker/cli/cli/compose/loader"
|
||||
composetypes "github.com/docker/cli/cli/compose/types"
|
||||
@@ -15,28 +14,22 @@ func parseCompose(dt []byte) (*composetypes.Config, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
envs, err := readEnv()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return loader.Load(composetypes.ConfigDetails{
|
||||
ConfigFiles: []composetypes.ConfigFile{
|
||||
{
|
||||
Config: parsed,
|
||||
},
|
||||
},
|
||||
Environment: envMap(os.Environ()),
|
||||
Environment: envs,
|
||||
})
|
||||
}
|
||||
|
||||
func envMap(env []string) map[string]string {
|
||||
result := make(map[string]string, len(env))
|
||||
for _, s := range env {
|
||||
kv := strings.SplitN(s, "=", 2)
|
||||
if len(kv) != 2 {
|
||||
continue
|
||||
}
|
||||
result[kv[0]] = kv[1]
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func ParseCompose(dt []byte) (*Config, error) {
|
||||
cfg, err := parseCompose(dt)
|
||||
if err != nil {
|
||||
|
||||
27
bake/env.go
Normal file
27
bake/env.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package bake
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/imdario/mergo"
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
func readEnv() (envs map[string]string, err error) {
|
||||
envs, _ = godotenv.Read()
|
||||
err = mergo.Merge(&envs, envMap(os.Environ()), mergo.WithOverride)
|
||||
return
|
||||
}
|
||||
|
||||
func envMap(env []string) map[string]string {
|
||||
result := make(map[string]string, len(env))
|
||||
for _, s := range env {
|
||||
kv := strings.SplitN(s, "=", 2)
|
||||
if len(kv) != 2 {
|
||||
continue
|
||||
}
|
||||
result[kv[0]] = kv[1]
|
||||
}
|
||||
return result
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
package bake
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/go-cty-funcs/cidr"
|
||||
@@ -189,9 +188,11 @@ func parseHCL(dt []byte, fn string) (_ *Config, err error) {
|
||||
}
|
||||
|
||||
// Override default with values from environment.
|
||||
for _, env := range os.Environ() {
|
||||
parts := strings.SplitN(env, "=", 2)
|
||||
name, value := parts[0], parts[1]
|
||||
envs, err := readEnv()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for name, value := range envs {
|
||||
if _, ok := variables[name]; ok {
|
||||
variables[name] = cty.StringVal(value)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user