Bumps the minor-and-patch group with 8 updates in the / directory: | Package | From | To | | --- | --- | --- | | [github.com/gookit/config/v2](https://github.com/gookit/config) | `2.2.8` | `2.2.9` | | [github.com/jellydator/ttlcache/v3](https://github.com/jellydator/ttlcache) | `3.4.0` | `3.4.1` | | [github.com/kovidgoyal/imaging](https://github.com/kovidgoyal/imaging) | `1.8.21` | `1.8.22` | | [github.com/nats-io/nats-server/v2](https://github.com/nats-io/nats-server) | `2.14.2` | `2.14.3` | | [github.com/onsi/ginkgo/v2](https://github.com/onsi/ginkgo) | `2.31.0` | `2.32.0` | | [github.com/onsi/gomega](https://github.com/onsi/gomega) | `1.42.0` | `1.42.1` | | [github.com/open-policy-agent/opa](https://github.com/open-policy-agent/opa) | `1.17.1` | `1.18.2` | | [google.golang.org/grpc](https://github.com/grpc/grpc-go) | `1.81.1` | `1.82.0` | Updates `github.com/gookit/config/v2` from 2.2.8 to 2.2.9 - [Release notes](https://github.com/gookit/config/releases) - [Commits](https://github.com/gookit/config/compare/v2.2.8...v2.2.9) Updates `github.com/jellydator/ttlcache/v3` from 3.4.0 to 3.4.1 - [Release notes](https://github.com/jellydator/ttlcache/releases) - [Commits](https://github.com/jellydator/ttlcache/compare/v3.4.0...v3.4.1) Updates `github.com/kovidgoyal/imaging` from 1.8.21 to 1.8.22 - [Release notes](https://github.com/kovidgoyal/imaging/releases) - [Commits](https://github.com/kovidgoyal/imaging/compare/v1.8.21...v1.8.22) Updates `github.com/nats-io/nats-server/v2` from 2.14.2 to 2.14.3 - [Release notes](https://github.com/nats-io/nats-server/releases) - [Changelog](https://github.com/nats-io/nats-server/blob/main/RELEASES.md) - [Commits](https://github.com/nats-io/nats-server/compare/v2.14.2...v2.14.3) Updates `github.com/onsi/ginkgo/v2` from 2.31.0 to 2.32.0 - [Release notes](https://github.com/onsi/ginkgo/releases) - [Changelog](https://github.com/onsi/ginkgo/blob/master/CHANGELOG.md) - [Commits](https://github.com/onsi/ginkgo/compare/v2.31.0...v2.32.0) Updates `github.com/onsi/gomega` from 1.42.0 to 1.42.1 - [Release notes](https://github.com/onsi/gomega/releases) - [Changelog](https://github.com/onsi/gomega/blob/master/CHANGELOG.md) - [Commits](https://github.com/onsi/gomega/compare/v1.42.0...v1.42.1) Updates `github.com/open-policy-agent/opa` from 1.17.1 to 1.18.2 - [Release notes](https://github.com/open-policy-agent/opa/releases) - [Changelog](https://github.com/open-policy-agent/opa/blob/main/CHANGELOG.md) - [Commits](https://github.com/open-policy-agent/opa/compare/v1.17.1...v1.18.2) Updates `google.golang.org/grpc` from 1.81.1 to 1.82.0 - [Release notes](https://github.com/grpc/grpc-go/releases) - [Commits](https://github.com/grpc/grpc-go/compare/v1.81.1...v1.82.0) --- updated-dependencies: - dependency-name: github.com/gookit/config/v2 dependency-version: 2.2.9 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: minor-and-patch - dependency-name: github.com/jellydator/ttlcache/v3 dependency-version: 3.4.1 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: minor-and-patch - dependency-name: github.com/kovidgoyal/imaging dependency-version: 1.8.22 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: minor-and-patch - dependency-name: github.com/nats-io/nats-server/v2 dependency-version: 2.14.3 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: minor-and-patch - dependency-name: github.com/onsi/ginkgo/v2 dependency-version: 2.32.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: minor-and-patch - dependency-name: github.com/onsi/gomega dependency-version: 1.42.1 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: minor-and-patch - dependency-name: github.com/open-policy-agent/opa dependency-version: 1.18.2 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: minor-and-patch - dependency-name: google.golang.org/grpc dependency-version: 1.82.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: minor-and-patch ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Roman Perekhod <2403905@gmail.com>
209 lines
4.4 KiB
Go
209 lines
4.4 KiB
Go
package envutil
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/gookit/goutil/internal/comfunc"
|
|
)
|
|
|
|
// DefaultEnvFile default file name
|
|
const DefaultEnvFile = ".env"
|
|
|
|
// Dotenv load and parse dotenv files
|
|
type Dotenv struct {
|
|
// Files dot env file paths, allow multi files.
|
|
// - filename support simple glob pattern. eg: ".env.*"
|
|
//
|
|
// default: [".env"]
|
|
Files []string
|
|
// BaseDir base dir for join files path
|
|
//
|
|
// default is workdir
|
|
BaseDir string
|
|
// UpperKey change key to upper on set ENV. default: true
|
|
UpperKey bool
|
|
// DotenvFirst overwrite existing os ENV values on load dotenv.
|
|
//
|
|
// default: false - os ENV first
|
|
DotenvFirst bool
|
|
// IgnoreNotExist only load exists.
|
|
//
|
|
// - default: false - will return error if not exists
|
|
IgnoreNotExist bool
|
|
// LoadFirstExist only load first exists env file on Files
|
|
LoadFirstExist bool
|
|
|
|
loadFiles []string
|
|
loadData map[string]string
|
|
}
|
|
|
|
// NewDotenv create a new dotenv config
|
|
func NewDotenv() *Dotenv {
|
|
return &Dotenv{
|
|
UpperKey: true,
|
|
Files: []string{DefaultEnvFile},
|
|
// init fields
|
|
loadData: make(map[string]string),
|
|
}
|
|
}
|
|
|
|
// LoadAndInit load dotenv files and parse to os.Environ
|
|
func (c *Dotenv) LoadAndInit() error {
|
|
return c.doLoadFiles(c.Files)
|
|
}
|
|
|
|
// LoadFiles append load dotenv files
|
|
// - filename support simple glob pattern. eg: ".env.*"
|
|
func (c *Dotenv) LoadFiles(files ...string) error {
|
|
return c.doLoadFiles(files)
|
|
}
|
|
|
|
// LoadText load dotenv contents and parse to os Env
|
|
func (c *Dotenv) LoadText(contents string) error {
|
|
return c.parseAndSetEnv(contents)
|
|
}
|
|
|
|
// do load dotenv files
|
|
func (c *Dotenv) doLoadFiles(files []string) error {
|
|
var filePath string
|
|
for _, file := range files {
|
|
filePath = strings.TrimSpace(file)
|
|
if c.BaseDir != "" && !filepath.IsAbs(file) {
|
|
filePath = filepath.Join(c.BaseDir, file)
|
|
}
|
|
|
|
// load and parse to ENV
|
|
if err := c.loadFile(filePath); err != nil {
|
|
return err
|
|
}
|
|
if c.LoadFirstExist && len(c.loadFiles) > 0 {
|
|
break
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (c *Dotenv) loadFile(filePath string) error {
|
|
// filename support simple glob pattern.
|
|
if strings.ContainsRune(filePath, '*') {
|
|
matches, err := filepath.Glob(filePath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, matchFile := range matches {
|
|
if err = c.parseFile(matchFile); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return err
|
|
}
|
|
|
|
// Load single file
|
|
return c.parseFile(filePath)
|
|
}
|
|
|
|
// parseFile load single file and parse to ENV
|
|
func (c *Dotenv) parseFile(filePath string) error {
|
|
contents, err := os.ReadFile(filePath)
|
|
if err != nil {
|
|
// IgnoreNotExist: skip non-existent files
|
|
if c.IgnoreNotExist && os.IsNotExist(err) {
|
|
return nil
|
|
}
|
|
return err
|
|
}
|
|
|
|
err = c.parseAndSetEnv(string(contents))
|
|
if err == nil {
|
|
c.loadFiles = append(c.loadFiles, filePath)
|
|
}
|
|
return err
|
|
}
|
|
|
|
func (c *Dotenv) parseAndSetEnv(contents string) error {
|
|
// Parse ENV lines
|
|
envMp, err := comfunc.ParseEnvLines(contents, comfunc.ParseEnvLineOption{
|
|
SkipOnErrorLine: true,
|
|
})
|
|
|
|
// Set to ENV
|
|
for key, val := range envMp {
|
|
key = strings.ToUpper(key)
|
|
if !c.DotenvFirst {
|
|
if _, ok := os.LookupEnv(key); ok {
|
|
if _, loaded := c.loadData[key]; !loaded {
|
|
continue
|
|
}
|
|
}
|
|
}
|
|
c.loadData[key] = val
|
|
_ = os.Setenv(key, val)
|
|
}
|
|
return err
|
|
}
|
|
|
|
// UnloadEnv remove loaded dotenv data from os.Environ
|
|
func (c *Dotenv) UnloadEnv() bool {
|
|
if len(c.loadData) == 0 {
|
|
return false
|
|
}
|
|
|
|
for key := range c.loadData {
|
|
_ = os.Unsetenv(key)
|
|
}
|
|
return true
|
|
}
|
|
|
|
// LoadedData get loaded dotenv data map
|
|
func (c *Dotenv) LoadedData() map[string]string {
|
|
return c.loadData
|
|
}
|
|
|
|
// LoadedFiles get loaded dotenv files
|
|
func (c *Dotenv) LoadedFiles() []string {
|
|
return c.loadFiles
|
|
}
|
|
|
|
// Reset unload all loaded ENV and reset data
|
|
func (c *Dotenv) Reset() {
|
|
c.UnloadEnv()
|
|
c.loadFiles = nil
|
|
c.loadData = make(map[string]string)
|
|
}
|
|
|
|
//
|
|
// region standard dotenv instance
|
|
//
|
|
|
|
var stdEnv = NewDotenv()
|
|
|
|
// StdDotenv get standard dotenv instance
|
|
func StdDotenv() *Dotenv { return stdEnv }
|
|
|
|
// DotenvLoad load dotenv file and parse to ENV
|
|
func DotenvLoad(fns ...func(cfg *Dotenv)) error {
|
|
for _, fn := range fns {
|
|
fn(stdEnv)
|
|
}
|
|
return stdEnv.LoadAndInit()
|
|
}
|
|
|
|
// LoadEnvFiles load dotenv files and parse to ENV
|
|
func LoadEnvFiles(baseDir string, files ...string) error {
|
|
return DotenvLoad(func(cfg *Dotenv) {
|
|
cfg.BaseDir = baseDir
|
|
if len(files) > 0 {
|
|
cfg.Files = files
|
|
}
|
|
})
|
|
}
|
|
|
|
// LoadedEnvFiles get loaded dotenv files
|
|
func LoadedEnvFiles() []string {
|
|
return stdEnv.LoadedFiles()
|
|
}
|