40 lines
631 B
Go
40 lines
631 B
Go
package config
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
const cdbpath = "DBPath"
|
|
const port = "port"
|
|
|
|
type Config struct {
|
|
DBPath string
|
|
Port string
|
|
}
|
|
|
|
func New() *Config {
|
|
lookups := make(map[string]*string)
|
|
add(cdbpath, "./db", lookups)
|
|
add(port, ":9101", lookups)
|
|
flag.Parse()
|
|
return &Config{
|
|
DBPath: *lookups[cdbpath],
|
|
Port: *lookups[port],
|
|
}
|
|
}
|
|
|
|
func (c *Config) String() string {
|
|
return fmt.Sprintf("Port:%q", c.Port)
|
|
}
|
|
|
|
func add(key string, value string, lookups map[string]*string) {
|
|
env := os.Getenv(strings.ToUpper(key))
|
|
if env != "" {
|
|
value = env
|
|
}
|
|
lookups[key] = flag.String(key, value, "")
|
|
}
|