https://github.com/acoshift/configfile
Read config from file
https://github.com/acoshift/configfile
Last synced: 11 months 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 (about 9 years ago)
- Default Branch: master
- Last Pushed: 2023-07-12T09:30:43.000Z (almost 3 years ago)
- Last Synced: 2024-06-19T00:26:13.339Z (almost 2 years 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

[](https://codecov.io/gh/acoshift/configfile)
[](https://goreportcard.com/report/github.com/acoshift/configfile)
[](https://godoc.org/github.com/acoshift/configfile)
Read config from file, useful when read data from kubernetes configmaps, and secret.
## Example
```go
package main
import (
"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 main
import (
"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")) // ""
}
```