Damnit again
Former-commit-id: b394f26caf0df7d113ac4cc7dacc9c544af6897f
This commit is contained in:
36
config/config.go
Normal file
36
config/config.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"local/storage"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var config Config
|
||||
var lock = &sync.RWMutex{}
|
||||
|
||||
type Config struct {
|
||||
db string
|
||||
DB storage.DB
|
||||
Port string
|
||||
Addr string
|
||||
Username string
|
||||
Password string
|
||||
DefaultNamespace string
|
||||
}
|
||||
|
||||
func Values() Config {
|
||||
lock.RLock()
|
||||
defer lock.RUnlock()
|
||||
return config
|
||||
}
|
||||
|
||||
func (c Config) String() string {
|
||||
return fmt.Sprintf(
|
||||
"port:%v db:%v addr:%v user:*** pass:*** ns:%s",
|
||||
c.Port,
|
||||
c.db,
|
||||
c.Addr,
|
||||
c.DefaultNamespace,
|
||||
)
|
||||
}
|
||||
41
config/new.go
Normal file
41
config/new.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"local/args"
|
||||
"local/storage"
|
||||
"os"
|
||||
)
|
||||
|
||||
func New() error {
|
||||
as := args.NewArgSet()
|
||||
as.Append(args.STRING, "addr", "address/path to database/file", "")
|
||||
as.Append(args.STRING, "user", "username to database", "")
|
||||
as.Append(args.STRING, "pass", "password to database", "")
|
||||
as.Append(args.STRING, "port", "port to listen on", "33419")
|
||||
as.Append(args.STRING, "db", "database type code", storage.MAP.String())
|
||||
as.Append(args.STRING, "ns", "namespace", storage.DefaultNamespace)
|
||||
if err := as.Parse(); err != nil {
|
||||
return err
|
||||
}
|
||||
config = Config{
|
||||
db: as.Get("db").GetString(),
|
||||
Addr: as.Get("addr").GetString(),
|
||||
Username: as.Get("user").GetString(),
|
||||
Password: as.Get("pass").GetString(),
|
||||
Port: as.Get("port").GetString(),
|
||||
DefaultNamespace: as.Get("ns").GetString(),
|
||||
}
|
||||
storage.DefaultNamespace = config.DefaultNamespace
|
||||
DB, err := storage.New(storage.TypeFromString(config.db), config.Addr, config.Username, config.Password)
|
||||
config.DB = DB
|
||||
return err
|
||||
}
|
||||
|
||||
func orEnv(def, key string, keys ...string) string {
|
||||
for _, key := range append(keys, key) {
|
||||
if v, ok := os.LookupEnv(key); ok {
|
||||
return v
|
||||
}
|
||||
}
|
||||
return def
|
||||
}
|
||||
40
config/new_test.go
Normal file
40
config/new_test.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNew(t *testing.T) {
|
||||
invalidEnv := "&&&&&&"
|
||||
keys := []string{
|
||||
"DB",
|
||||
"DATABASE",
|
||||
"ADDR",
|
||||
"PATH",
|
||||
"USER",
|
||||
"USERNAME",
|
||||
"PASS",
|
||||
"PASSWORD",
|
||||
}
|
||||
was := make(map[string]string)
|
||||
for _, k := range keys {
|
||||
v, ok := os.LookupEnv(k)
|
||||
if !ok {
|
||||
v = invalidEnv
|
||||
}
|
||||
was[k] = v
|
||||
}
|
||||
defer func() {
|
||||
for k, v := range was {
|
||||
if v != invalidEnv {
|
||||
os.Setenv(k, v)
|
||||
} else {
|
||||
os.Unsetenv(k)
|
||||
}
|
||||
}
|
||||
}()
|
||||
if err := New(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user