diff options
author | Laria Carolin Chabowski <laria@laria.me> | 2017-10-01 01:07:05 +0200 |
---|---|---|
committer | Laria Carolin Chabowski <laria@laria.me> | 2017-10-03 15:01:38 +0200 |
commit | 7f0771d0a9caf2d3294bfced9e66fac03334d9ba (patch) | |
tree | 37f07c087e36b025503ffbae3a07eebcc3acc649 /config | |
parent | c04bbf9c82f216891ba44b8b5b621b8020963086 (diff) | |
download | petrific-7f0771d0a9caf2d3294bfced9e66fac03334d9ba.tar.gz petrific-7f0771d0a9caf2d3294bfced9e66fac03334d9ba.tar.bz2 petrific-7f0771d0a9caf2d3294bfced9e66fac03334d9ba.zip |
Redo storage config
Diffstat (limited to 'config')
-rw-r--r-- | config/config.go | 59 |
1 files changed, 33 insertions, 26 deletions
diff --git a/config/config.go b/config/config.go index 7454d7a..46d1b47 100644 --- a/config/config.go +++ b/config/config.go @@ -2,23 +2,35 @@ package config import ( "code.laria.me/petrific/gpg" + "errors" "fmt" "github.com/BurntSushi/toml" "github.com/adrg/xdg" "os" "os/user" - "reflect" "strings" ) -type StorageConfig map[string]interface{} +type StorageConfigErr struct { + Name string + Err error +} + +func (sce StorageConfigErr) Error() string { + return fmt.Sprintf("Could not get configuration for storage %s: %s", sce.Name, sce.Err.Error()) +} + +var ( + StorageConfigNotFound = errors.New("Not found") +) type Config struct { DefaultStorage string `toml:"default_storage"` Signing struct { Key string } - Storage map[string]StorageConfig + Storage map[string]toml.Primitive + meta toml.MetaData `toml:"-"` } func LoadConfig(path string) (config Config, err error) { @@ -29,42 +41,37 @@ func LoadConfig(path string) (config Config, err error) { } } - _, err = toml.DecodeFile(path, &config) - return + meta, err := toml.DecodeFile(path, &config) + config.meta = meta + return config, err } func (c Config) GPGSigner() gpg.Signer { return gpg.Signer{c.Signing.Key} } -// Get gets a value from the StorageConfig, taking care about type checking. -// ptr must be a pointer or this method will panic. -// ptr will only be changed, if returned error != nil -func (s StorageConfig) Get(k string, ptr interface{}) error { - ptrval := reflect.ValueOf(ptr) - - if ptrval.Kind() != reflect.Ptr { - panic("ptr must be a pointer") - } - - ptrelem := ptrval.Elem() - if !ptrelem.CanSet() { - panic("*ptr not settable?!") +func (c Config) GetStorageMethod(name string) (string, error) { + prim, ok := c.Storage[name] + if !ok { + return "", StorageConfigErr{name, StorageConfigNotFound} } - v, ok := s[k] - if !ok { - return fmt.Errorf("Key '%s' is missing", k) + var method_wrap struct{ Method string } + err := c.meta.PrimitiveDecode(prim, &method_wrap) + if err != nil { + return "", StorageConfigErr{name, err} } - vval := reflect.ValueOf(v) + return method_wrap.Method, nil +} - if vval.Type() != ptrelem.Type() { - return fmt.Errorf("Expected config '%s' to be of type %s, got %s", k, ptrelem.Type(), vval.Type()) +func (c Config) GetStorageConfData(name string, v interface{}) error { + prim, ok := c.Storage[name] + if !ok { + return StorageConfigErr{name, StorageConfigNotFound} } - ptrelem.Set(vval) - return nil + return c.meta.PrimitiveDecode(prim, v) } func ExpandTilde(path string) string { |