Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/acoshift/configfile
Read config from file
https://github.com/acoshift/configfile
Last synced: about 1 month ago
JSON representation
Read config from file
- Host: GitHub
- URL: https://github.com/acoshift/configfile
- Owner: acoshift
- License: mit
- Created: 2017-04-17T10:49:44.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-07-12T09:30:43.000Z (over 1 year ago)
- Last Synced: 2024-06-19T00:26:13.339Z (7 months ago)
- Language: Go
- Size: 33.2 KB
- Stars: 7
- Watchers: 2
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# configfile
![Build Status](https://github.com/acoshift/configfile/actions/workflows/test.yaml/badge.svg?branch=master)
[![codecov](https://codecov.io/gh/acoshift/configfile/branch/master/graph/badge.svg)](https://codecov.io/gh/acoshift/configfile)
[![Go Report Card](https://goreportcard.com/badge/github.com/acoshift/configfile)](https://goreportcard.com/report/github.com/acoshift/configfile)
[![GoDoc](https://godoc.org/github.com/acoshift/configfile?status.svg)](https://godoc.org/github.com/acoshift/configfile)Read config from file, useful when read data from kubernetes configmaps, and secret.
## Example
```go
package mainimport (
"fmt"
"net/http""github.com/acoshift/configfile"
"github.com/garyburd/redigo/redis"
)var config = configfile.NewReader("config")
var (
addr = config.StringDefault("addr", ":8080")
redisAddr = config.MustString("redis_addr")
redisPass = config.String("redis_pass")
redisDB = config.Int("redis_db")
)func main() {
pool := redis.Pool{
Dial: func() (redis.Conn, error) {
return redis.Dial(
"tcp",
redisAddr,
redis.DialPassword(redisPass),
redis.DialDatabase(redisDB),
)
},
}http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
c := pool.Get()
defer c.Close()
cnt, err := redis.Int64(c.Do("INCR", "cnt"))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Fprintf(w, "count: %d", cnt)
})http.ListenAndServe(addr, nil)
}
```## Example YAML
```go
package mainimport (
"log"
"net/http""github.com/acoshift/configfile"
)func main() {
var config = configfile.NewReader("testdata/config.yaml")
// or use NewYAMLReader
var config = configfile.NewYAMLReader("testdata/config.yaml")log.Println(config.Bool("data1")) // true
log.Println(config.String("data2")) // false
log.Println(config.Int("data3")) // 9
log.Println(config.Int("data4")) // 0
log.Println(config.String("empty")) // ""
}
```