aboutsummaryrefslogtreecommitdiff
path: root/config/config.go
blob: e925be3c00728e001203f72038f47a8bf67ea1fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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))
}