From 4b4adee20d5dee1382c76c2a9ad9ae7a0b25673b Mon Sep 17 00:00:00 2001 From: g41797 Date: Thu, 23 Nov 2023 07:47:21 +0200 Subject: [PATCH] Add LoadEnv and WalkDir --- go.mod | 1 + go.sum | 2 ++ sidecar/config.go | 17 +++++++------- sidecar/env.go | 58 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 9 deletions(-) create mode 100644 sidecar/env.go diff --git a/go.mod b/go.mod index 5a3dc4b..bcd7237 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/g41797/sputnik go 1.19 require ( + github.com/g41797/godotenv v1.5.3 github.com/g41797/gonfig v1.0.1 github.com/g41797/kissngoqueue v0.1.5 ) diff --git a/go.sum b/go.sum index 25b2374..12e2379 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/g41797/godotenv v1.5.3 h1:/36/ofF29iNsZBvSS+L/yZk9Nw1SBULvbrV/tG17JMw= +github.com/g41797/godotenv v1.5.3/go.mod h1:0l6Y57HWL8UW+JO772pthnF0QCvi9zlJigl/JW7ug9Q= github.com/g41797/gonfig v1.0.1 h1:ywgkhF3SbNJuFN9Hlm9n7Lp+KxqeupkNmFObn2ZpWs8= github.com/g41797/gonfig v1.0.1/go.mod h1:5iL4oHFpYi+c7AE4D1dZnlBleJzM71dX6HZBhEELF90= github.com/g41797/kissngoqueue v0.1.5 h1:UrnpxbjOnTnKj/EqpbLA9LjhYkHjnLWMZQqsz3AOBG8= diff --git a/sidecar/config.go b/sidecar/config.go index e030132..586e3da 100644 --- a/sidecar/config.go +++ b/sidecar/config.go @@ -90,15 +90,14 @@ func ConfFolder() (confFolder string, err error) { // go:embed configdir // var cnffiles embed.FS // -// func main() { -// cleanup, err := sidecar.UseEmbeddedConfiguration(&cnffiles) -// if err != nil { -// return err -// } -// defer cleanup() -// sidecar.Start(new(adapter.BrokerConnector)) -// } - +// func main() { +// cleanup, err := sidecar.UseEmbeddedConfiguration(&cnffiles) +// if err != nil { +// return err +// } +// defer cleanup() +// sidecar.Start(new(adapter.BrokerConnector)) +// } func UseEmbeddedConfiguration(efs *embed.FS) (cleanUp func(), err error) { tmpDir, err := os.MkdirTemp("", "config") diff --git a/sidecar/env.go b/sidecar/env.go new file mode 100644 index 0000000..3287c32 --- /dev/null +++ b/sidecar/env.go @@ -0,0 +1,58 @@ +package sidecar + +import ( + "io/fs" + "path/filepath" + "strings" + + "github.com/g41797/godotenv" +) + +// Loads all *.env file from configuration folder of the process. +// Overwrites non-existing environment variables. +// Supports "prefixed" environment variables +// (https://github.com/g41797/gonfig#using-prefixes-for-environment-variables-name) +func LoadEnv() error { + cf, err := ConfFolder() + if err != nil { + return err + } + + envf, err := WalkDir(cf, []string{"env"}) + if err != nil { + return err + } + + if len(envf) == 0 { + return nil + } + + for _, ef := range envf { + if err := godotenv.Load(ef); err != nil { + return err + } + } + + return nil +} + +// Finds all the files matching a particular suffix in all the directories +// https://stackoverflow.com/questions/70537979/how-to-efficiently-find-all-the-files-matching-a-particular-suffix-in-all-the-di +func WalkDir(root string, exts []string) ([]string, error) { + var files []string + err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error { + if d.IsDir() { + return nil + } + + for _, s := range exts { + if strings.HasSuffix(path, "."+s) { + files = append(files, path) + return nil + } + } + + return nil + }) + return files, err +}