1
0
Fork 0

do configs through env

Esse commit está contido em:
Stephen McQuay 2013-12-20 23:04:33 -08:00
commit a8e03e721e
2 arquivos alterados com 48 adições e 0 exclusões

29
config.go Normal file
Ver arquivo

@ -0,0 +1,29 @@
package main
import (
"errors"
)
type Configs struct {
Verbose bool
Dbhost string
Dbname string
Dbport int
Port int
}
func NewConfig() *Configs {
return &Configs{
Verbose: true,
Dbname: "itslog",
Dbport: 5432,
Port: 80,
}
}
func (c *Configs) validate() error {
if c.Dbhost == "" {
return errors.New("must specify dbhost (export ITSLOG_DBHOST=blah.local)")
}
return nil
}

19
main.go Normal file
Ver arquivo

@ -0,0 +1,19 @@
package main
import (
"github.com/kelseyhightower/envconfig"
"log"
)
func main() {
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
config := NewConfig()
err := envconfig.Process("ITSLOG", config)
if err != nil {
log.Fatal(err)
}
if err := config.validate(); err != nil {
log.Fatal(err)
}
log.Printf("%+v", config)
}