Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/datek/env
Library for parsing environmental variables into golang structs.
https://github.com/datek/env
config env environment environment-variables golang
Last synced: 19 days ago
JSON representation
Library for parsing environmental variables into golang structs.
- Host: GitHub
- URL: https://github.com/datek/env
- Owner: DAtek
- License: mit
- Created: 2024-08-30T11:55:49.000Z (4 months ago)
- Default Branch: main
- Last Pushed: 2024-08-30T12:10:22.000Z (4 months ago)
- Last Synced: 2024-10-20T12:26:47.597Z (2 months ago)
- Topics: config, env, environment, environment-variables, golang
- Language: Go
- Homepage:
- Size: 7.81 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![codecov](https://codecov.io/gh/DAtek/env/graph/badge.svg?token=MAjETYy681)](https://codecov.io/gh/DAtek/env) [![Go Report Card](https://goreportcard.com/badge/github.com/DAtek/env)](https://goreportcard.com/report/github.com/DAtek/env)
# env
Lightweight library, which parses environmental variables into structs.## Features
- Supports custom parsers
- Optional fields
- Default values
- Generics
- Returns friendly error message
- Returned error can be parsed into struct for further processing if needed## Examples
### Simple
```go
package mainimport (
"fmt"
"os""github.com/DAtek/env"
)type Config struct {
AppMaxWorkers int
AppLoggingType *string
}func main() {
os.Clearenv()
os.Setenv("APP_MAX_WORKERS", "10")loadEnv := env.NewLoader[Config]()
config, _ := loadEnv()fmt.Printf("config.AppMaxWorkers: %v\n", config.AppMaxWorkers)
fmt.Printf("config.AppLoggingType: %v\n", config.AppLoggingType)
}```
Output:
```
config.AppMaxWorkers: 10
config.AppLoggingType:
```### Using defaults
```go
package mainimport (
"fmt"
"os""github.com/DAtek/env"
)type Config struct {
AppMaxWorkers int
AppLoggingType *string
}type DefaultConfig struct {
AppMaxWorkers int
}func main() {
os.Clearenv()loadEnv := env.NewLoader[Config]()
defaultConfig := DefaultConfig{AppMaxWorkers: 8}
config, _ := loadEnv(defaultConfig)fmt.Printf("config.AppMaxWorkers: %v\n", config.AppMaxWorkers)
fmt.Printf("config.AppLoggingType: %v\n", config.AppLoggingType)
}```
Output:
```
config.AppMaxWorkers: 8
config.AppLoggingType:
```### Using custom parsers
```go
package mainimport (
"fmt"
"os"
"strconv"
"strings""github.com/DAtek/env"
)type Point struct {
X int
Y int
}type Config struct {
AppCenter Point
}var customParsers = env.ParserMap{
"Point": func(src string) (any, error) {
parts := strings.Split(src, ";")
x, _ := strconv.Atoi(parts[0])
y, _ := strconv.Atoi(parts[1])return Point{x, y}, nil
},
}func main() {
os.Clearenv()
os.Setenv("APP_CENTER", "12;5")loadEnv := env.NewLoader[Config](customParsers)
config, _ := loadEnv()fmt.Printf("config.AppCenter: %v\n", config.AppCenter)
}```
Output:
```
config.AppCenter: {12 5}
```### Error
```go
package mainimport (
"fmt"
"os""github.com/DAtek/env"
)type Point struct {
X int
Y int
}type Config struct {
AppCenter Point
AppMaxWorkers int
AppDbUrl string
}func main() {
os.Clearenv()
os.Setenv("APP_CENTER", "12;6")
os.Setenv("APP_MAX_WORKERS", "more than ever")loadEnv := env.NewLoader[Config]()
_, err := loadEnv()fmt.Println(err)
fmt.Println()errorCollection := err.(*env.ErrorCollection)
fmt.Printf("errorCollection: %v\n", errorCollection.Errors)
}```
Output:
```
Parser missing for environmental variable 'APP_CENTER'. Required type: 'Point'
Environmental variable 'APP_MAX_WORKERS' has wrong type. Required type: 'int'
Environmental variable 'APP_DB_URL' is unseterrorCollection: [{APP_CENTER unsupported_type Point PARSER_NOT_FOUND} {APP_MAX_WORKERS wrong_type int strconv.ParseInt: parsing "more than ever": invalid syntax} {APP_DB_URL required string }]
```