aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaria Carolin Chabowski <laria@laria.me>2017-08-02 08:04:28 +0200
committerLaria Carolin Chabowski <laria@laria.me>2017-09-26 21:35:40 +0200
commit03890a8b501475240b343664bdfc3bacebae1cbe (patch)
tree9944d740bf217fe46db07e72954639c7ef240d75
parent8c43d2969eaa4404675252480befa0642fca5dc4 (diff)
downloadpetrific-03890a8b501475240b343664bdfc3bacebae1cbe.tar.gz
petrific-03890a8b501475240b343664bdfc3bacebae1cbe.tar.bz2
petrific-03890a8b501475240b343664bdfc3bacebae1cbe.zip
Add config
-rw-r--r--config/config.go84
-rw-r--r--storage/memory_storage.go9
-rw-r--r--storage/storage.go9
3 files changed, 102 insertions, 0 deletions
diff --git a/config/config.go b/config/config.go
new file mode 100644
index 0000000..e925be3
--- /dev/null
+++ b/config/config.go
@@ -0,0 +1,84 @@
+package config
+
+import (
+ "fmt"
+ "github.com/BurntSushi/toml"
+ "github.com/adrg/xdg"
+ "os"
+ "os/user"
+ "reflect"
+ "strings"
+)
+
+type StorageConfig map[string]interface{}
+
+type Config struct {
+ DefaultStorage string `toml:"default_storage"`
+ Signing struct {
+ Key string
+ }
+ Storage map[string]StorageConfig
+}
+
+func LoadConfig(path string) (config Config, err error) {
+ if path == "" {
+ path, err = xdg.ConfigFile("petrific/config.toml")
+ if err != nil {
+ return
+ }
+ }
+
+ _, err = toml.DecodeFile(path, &config)
+ return
+}
+
+// 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?!")
+ }
+
+ v, ok := s[k]
+ if !ok {
+ return fmt.Errorf("Key '%s' is missing", k)
+ }
+
+ vval := reflect.ValueOf(v)
+
+ if vval.Type() != ptrelem.Type() {
+ return fmt.Errorf("Expected config '%s' to be of type %s, got %s", k, ptrelem.Type(), vval.Type())
+ }
+
+ ptrelem.Set(vval)
+ return nil
+}
+
+func ExpandTilde(path string) string {
+ home, ok := os.LookupEnv("HOME")
+ if !ok {
+ u, err := user.Current()
+ if err != nil {
+ return path
+ }
+ home = u.HomeDir
+ }
+
+ if home == "" {
+ return path
+ }
+
+ parts := strings.Split(path, string(os.PathSeparator))
+ if len(parts) > 0 && parts[0] == "~" {
+ parts[0] = home
+ }
+ return strings.Join(parts, string(os.PathSeparator))
+}
diff --git a/storage/memory_storage.go b/storage/memory_storage.go
index 0c90981..b9ed54a 100644
--- a/storage/memory_storage.go
+++ b/storage/memory_storage.go
@@ -1,6 +1,7 @@
package storage
import (
+ "code.laria.me/petrific/config"
"code.laria.me/petrific/objects"
)
@@ -16,6 +17,10 @@ func NewMemoryStorage() Storage {
}
}
+func MemoryStorageFromConfig(conf config.Config, name string) (Storage, error) {
+ return NewMemoryStorage(), nil
+}
+
func (ms MemoryStorage) Get(id objects.ObjectId) ([]byte, error) {
b, ok := ms.objects[id.String()]
if !ok {
@@ -39,3 +44,7 @@ func (ms MemoryStorage) Set(id objects.ObjectId, typ objects.ObjectType, raw []b
func (ms MemoryStorage) List(typ objects.ObjectType) ([]objects.ObjectId, error) {
return ms.bytype[typ], nil
}
+
+func (MemoryStorage) Close() error {
+ return nil
+}
diff --git a/storage/storage.go b/storage/storage.go
index 45b1973..0729fe1 100644
--- a/storage/storage.go
+++ b/storage/storage.go
@@ -2,6 +2,7 @@ package storage
import (
"bytes"
+ "code.laria.me/petrific/config"
"code.laria.me/petrific/objects"
"errors"
"fmt"
@@ -17,6 +18,14 @@ type Storage interface {
Has(id objects.ObjectId) (bool, error)
Set(id objects.ObjectId, typ objects.ObjectType, raw []byte) error
List(typ objects.ObjectType) ([]objects.ObjectId, error)
+
+ Close() error
+}
+
+type CreateStorageFromConfig func(conf config.Config, name string) (Storage, error)
+
+var StorageTypes = map[string]CreateStorageFromConfig{
+ "memory": MemoryStorageFromConfig,
}
func SetObject(s Storage, o objects.RawObject) (id objects.ObjectId, err error) {