forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
118 lines (90 loc) · 2.63 KB
/
main.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
// Package config provides a common infrastructure for reading configuration
// data stored in local TOML files.
package config
import (
"fmt"
"io/ioutil"
"github.com/BurntSushi/toml"
"github.com/asaskevich/govalidator"
"github.com/stellar/go/amount"
"github.com/stellar/go/strkey"
"github.com/stellar/go/support/errors"
)
// TLS represents a common configuration snippet for configuring TLS in a server process
type TLS struct {
CertificateFile string `toml:"certificate-file" valid:"required"`
PrivateKeyFile string `toml:"private-key-file" valid:"required"`
}
// InvalidConfigError is the error that is returned when an invalid
// configuration is encountered by the `Read` func.
type InvalidConfigError struct {
InvalidFields map[string]string
}
// Read takes the TOML configuration file at `path`, parses it into `dest` and
// then uses github.com/asaskevich/govalidator to validate the struct.
func Read(path string, dest interface{}) error {
bs, err := ioutil.ReadFile(path)
if err != nil {
return err
}
return decode(string(bs), dest)
}
func decode(content string, dest interface{}) error {
metadata, err := toml.Decode(content, dest)
if err != nil {
return errors.Wrap(err, "decode-file failed")
}
// Undecoded keys correspond to keys in the TOML document
// that do not have a concrete type in config struct.
undecoded := metadata.Undecoded()
if len(undecoded) > 0 {
return errors.New("Unknown fields: " + fmt.Sprintf("%+v", undecoded))
}
valid, err := govalidator.ValidateStruct(dest)
if valid {
return nil
}
fields := govalidator.ErrorsByField(err)
return &InvalidConfigError{
InvalidFields: fields,
}
}
func init() {
govalidator.SetFieldsRequiredByDefault(true)
govalidator.CustomTypeTagMap.Set("stellar_accountid", govalidator.CustomTypeValidator(isStellarAccountID))
govalidator.CustomTypeTagMap.Set("stellar_seed", govalidator.CustomTypeValidator(isStellarSeed))
govalidator.CustomTypeTagMap.Set("stellar_amount", govalidator.CustomTypeValidator(isStellarAmount))
}
func isStellarAmount(i interface{}, context interface{}) bool {
enc, ok := i.(string)
if !ok {
return false
}
_, err := amount.Parse(enc)
if err == nil {
return true
}
return false
}
func isStellarAccountID(i interface{}, context interface{}) bool {
enc, ok := i.(string)
if !ok {
return false
}
_, err := strkey.Decode(strkey.VersionByteAccountID, enc)
if err == nil {
return true
}
return false
}
func isStellarSeed(i interface{}, context interface{}) bool {
enc, ok := i.(string)
if !ok {
return false
}
_, err := strkey.Decode(strkey.VersionByteSeed, enc)
if err == nil {
return true
}
return false
}