forked from vonwenm/gobackup2
/
config.go
119 lines (103 loc) · 2.74 KB
/
config.go
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package main
import (
"code.google.com/p/gcfg"
"errors"
"fmt"
"github.com/rdwilliamson/aws"
)
/**
* Config is the main Config struct
* for the program
*/
type Config struct {
Threads struct {
Hash int
Upload int
}
Aws struct {
Secret string
Access string
}
Backup map[string]*struct {
Region MyAwsRegion
Path string
Db string
Exclude []string
Include []string
AwsAccess string `gcfg:"aws-access"`
AwsSecret string `gcfg:"aws-secret"`
Vault string
}
}
/**
* MyAwsRegion is a simple wrapper for aws.Region
* Allowing us to add a custom unmarshal method
*/
type MyAwsRegion struct {
*aws.Region
}
/**
* UnmarshalText is a custom unmarshaller for MyAwsRegion
* It looks up the region in aws.Regions map
* @return error Returns error if region does not exist in aws.Regions
*/
func (m *MyAwsRegion) UnmarshalText(text []byte) error {
region := string(text)
for _, r := range aws.Regions {
if r.Name == region {
*m = MyAwsRegion{r}
return nil
}
}
return fmt.Errorf("Unable to find region %s", region)
}
/**
* ReadConfig reads config from a string
* @param string The configuration definition
* @return Config A Config struct, or nil if something went wrong
* @return error Returns error if something went wrong. Config will be nil in this case.
*/
func ReadConfig(configDef string) (*Config, error) {
cfg := Config{}
if err := gcfg.ReadStringInto(&cfg, configDef); err != nil {
return nil, err
}
if cfg.Threads.Hash < 1 {
return nil, fmt.Errorf("Need at least one hash thread")
}
if cfg.Threads.Upload < 1 {
return nil, fmt.Errorf("Need at least one upload thread")
}
if len(cfg.Backup) == 0 {
return nil, errors.New("No configurations given")
}
for key, backup := range cfg.Backup {
if backup.Region.Region == nil {
return nil, fmt.Errorf("No region supplied for config `%s`", key)
}
if backup.Path == "" {
return nil, fmt.Errorf("No path supplied for config `%s`", key)
}
if backup.Db == "" {
return nil, fmt.Errorf("No db supplied for config `%s`", key)
}
if backup.Vault == "" {
return nil, fmt.Errorf("No vault supplied for config `%s`", key)
}
if backup.AwsAccess != "" && backup.AwsSecret == "" {
return nil, fmt.Errorf("AWS Access code suplied, but no AWS Secret for config `%s`", key)
}
if backup.AwsAccess == "" && backup.AwsSecret != "" {
return nil, fmt.Errorf("AWS Secret suplied, but no AWS Access code for config `%s`", key)
}
if backup.AwsAccess == "" && backup.AwsSecret == "" {
if cfg.Aws.Access != "" && cfg.Aws.Secret != "" {
backup.AwsAccess = cfg.Aws.Access
backup.AwsSecret = cfg.Aws.Secret
} else {
return nil, fmt.Errorf("No AWS credentials supplied for backup `%s`", key)
}
}
}
return &cfg, nil
}