https://github.com/gokultp/go-envparser
a go generator based env to go-struct decoder.
https://github.com/gokultp/go-envparser
environment-variables generator go golang hacktoberfest hacktoberfest2020 struct
Last synced: 25 days ago
JSON representation
a go generator based env to go-struct decoder.
- Host: GitHub
- URL: https://github.com/gokultp/go-envparser
- Owner: gokultp
- License: mit
- Created: 2019-08-18T18:43:07.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2022-02-10T00:56:33.000Z (about 4 years ago)
- Last Synced: 2024-06-19T06:54:30.710Z (over 1 year ago)
- Topics: environment-variables, generator, go, golang, hacktoberfest, hacktoberfest2020, struct
- Language: Go
- Homepage:
- Size: 29.3 KB
- Stars: 15
- Watchers: 2
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# go-envparser
## Overview [](https://godoc.org/github.com/gokultp/go-envparser) [](https://codeclimate.com/github/gokultp/go-envparser) [](https://goreportcard.com/report/github.com/gokultp/go-envparser)
`go-envparser` generates static `DecodeEnv` functions for structures in Go to decode environment variables, will implement the interface [Decoder](./pkg/envdecoder/idecoder.go). The generated functions reduce the reliance upon runtime reflection to do serialization.
## Getting Started
### Installation
```shell
git clone https://github.com/gokultp/go-envparser.git
cd go-envparser
make
#check if the command is working
envparser version
```
should get an output in the following format
```
Version : V0.1.0
MinVersion : e3a5a007b94f51f46a64853f308e5a24daf98892
BuildTime : 2019-08-19T00:56:29+0530
```
### Commands
```bash
envparser generate -s -f
```
It will generate a file with name `decoder.go` (Lowercase) consist of a `DecodeEnv` function which will be implementing the interface [Decoder](./pkg/envdecoder/idecoder.go).
### Example
```go
package main
import (
"fmt"
"github.com/gokultp/go-envparser/pkg/envdecoder"
)
// Add go generate commands
//go:generate envparser generate -t GoEnv -f $GOFILE
// Dont forget to do goimport on generated files.
//go:generate goimports -w goenvdecoder.go
type GoEnv struct {
Paths Path
GoRoot string `env:"GOROOT"`
}
//go:generate envparser generate -t Path -f $GOFILE
// Dont forget to do goimport on generated files.
//go:generate goimports -w pathdecoder.go
type Path struct {
System []string `env:"PATH"`
Go string `env:"GOPATH"`
}
func main() {
env := GoEnv{}
if err := envdecoder.Decode(&env); err != nil {
panic(err)
}
fmt.Printf("%#v", env)
}
```
Here in the above file I have used go generate flags, will execute all the needed commands( envparser generate && goimports) in a single `go generate` command.
So here to generate the code execute
```bash
go generate
```
to run the code execute
``` bash
go build
./
```
## License
`go-envparser` is licensed under the [MIT License](./LICENSE)