EnvParser
is a Go package designed for parsing environment variables from .env
files. It provides functionalities to load, convert, and retrieve environment variable values, including support for encrypted variables. This package is useful for managing configuration settings in Go applications.
To use this package, include it in your Go module. You can install it via go get
:
go get github.com/alexanderthegreat96/envparser
Simply initialize the function and then provide it
package main
import (
"fmt"
"github.com/alexanderthegreat96/envparser"
)
func main() {
// By default, it will use the .env in the root folter
// You may specify a different file by providing it
// if your file is not in the project root, set the second argument to false
// third argument allows you to specify additional env files to be parsed
parser := envparser.NewEnvParser()
// Get a variable
// first argument is variable name
// second argument type to be converted to
// third is the default value
value, err := parser.GetValue("YOUR_VAR", "string", "default_value")
if err != nil {
fmt.Println("Error:", err)
} else {
fmt.Println("Value:", value)
}
}
Below, there is a list with all the available functions.
func NewEnvParser(params ...interface{}) *EnvData
Will initialize the parser itself. It accepts the following arguments:
- environment file name -> default is .env
- use root path -> default is true (set to false if your file is somewhere else and not in your project)
- env files -> you may specify an array of env file names. they will be merged together and you will have access to all of them
parser := envparser.NewEnvParser("my.env", true, []string{"another.env", "another_one.env"})
func (env *EnvData) GetVars() map[interface{}]interface{}
Will return a map with all the variables found across your specified environment file(s). It will also auto-convert
them to the correct type
.
func (env *EnvData) GetValue(which, kind string, defaultValue interface{}) (interface{}, error)
This function will grab a value from the file by key, will convert it to the type you specify and if not found, will return the default value specified. Arguments:
- key -> your variable name
- kind -> what do you want this converted to? (check the types below)
- default value -> if not found, will default to this
Supported types:
- str
- string
- bool
- boolean
- float
- int
- integer
- list
- array
- tuple
- dict
- map
- json
func (env *EnvData) GetEncryptedValue(which, kind string, defaultValue interface{}, decryptionKey string) (interface{}, error)
Same structure as above, except, you may provide a decryption key. It supports base64
and AES
.
Usage:
- for
base64
hashes, simply call it without providing an encryption key - for
aes
encryption, provide the basee64 hash + the encryption key
func (env *EnvData) GetError() string
Any errors that may have occured can be captured with this.
The package supports variable substitution, allowing you to reference other environment variables within the .env file. For example:
API_URL=https://${API_HOST}:${API_PORT}/api
The values will be replaced with the ones found in other env files.
MIT License
Copyright (c) [2024] [alexanderthegreat96]
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
-
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.