Skip to content

Commit

Permalink
Add LoadEnv and WalkDir
Browse files Browse the repository at this point in the history
  • Loading branch information
g41797 committed Nov 23, 2023
1 parent 903648d commit 4b4adee
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 9 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down
17 changes: 8 additions & 9 deletions sidecar/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
58 changes: 58 additions & 0 deletions sidecar/env.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 4b4adee

Please sign in to comment.