Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/titusjaka/kong-dotenv-go
ENV File Resolver for kong
https://github.com/titusjaka/kong-dotenv-go
Last synced: about 1 month ago
JSON representation
ENV File Resolver for kong
- Host: GitHub
- URL: https://github.com/titusjaka/kong-dotenv-go
- Owner: titusjaka
- Created: 2024-04-12T08:41:09.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2024-11-25T19:44:14.000Z (about 2 months ago)
- Last Synced: 2024-11-25T20:34:39.644Z (about 2 months ago)
- Language: Go
- Size: 25.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# ENV File Resolver for [kong](https://github.com/alecthomas/kong)
This tiny package adds ENV-files loader to the [kong](https://github.com/alecthomas/kong) CLI parser.
## Usage
The easiest way to add ENV-file support to the app is the following.
```dotenv
# ./.env
VAR_STRING="Any value here"
``````go
// ./main.gopackage main
import (
"fmt""github.com/alecthomas/kong"
kongdotenv "github.com/titusjaka/kong-dotenv-go"
)type CLI struct {
EnvFile kongdotenv.ENVFileConfig `kong:"optional,default='.env',name=env-file"`EnvVarString string `kong:"optional,name=var-string,env=VAR_STRING"`
}func (c CLI) Run() error {
fmt.Printf(`Env File: %s; Env Var String: %s.`, c.EnvFile, c.EnvVarString,
)return nil
}func main() {
var cli CLIkCtx := kong.Parse(&cli)
kCtx.FatalIfErrorf(kCtx.Run())
}
``````shell
go run ./main.go --env-file=./.env# Output: Env File: .env; Env Var String: Any value here.
```## Limits
The package has the following limitations:
- Values are applied only to variables with an env tag.
- ENV variables have higher priority than ENV-file ones. Therefore, if you have an ENV set, the same variable in the ENV file will be ignored.## Examples
- example with minimum configuration: [./examples/config_type](./examples/config_type);
- obsolete example with `kong.ConfigurationLoader` resolver: [./examples/resolver](./examples/resolver).