Rssmon2/config/config.go

45 lines
834 B
Go

package config
import (
"flag"
"fmt"
"os"
"path"
"strings"
)
const cdbpath = "DBPath"
const port = "port"
const toraddr = "toraddr"
type Config struct {
DBPath string
Port string
TorAddr string
}
func New() *Config {
lookups := make(map[string]*string)
add(cdbpath, path.Join(os.Getenv("MNT"), "db.db"), lookups)
add(port, ":9101", lookups)
add(toraddr, "http://192.168.0.86:9091/transmission/rpc", lookups)
flag.Parse()
return &Config{
DBPath: *lookups[cdbpath],
Port: *lookups[port],
TorAddr: *lookups[toraddr],
}
}
func (c *Config) String() string {
return fmt.Sprintf("Port:%q, DBPath:%q", c.Port, c.DBPath)
}
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, "")
}