-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathconfig.go
95 lines (82 loc) · 2.26 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
package config
import (
"bytes"
"encoding/json"
"fmt"
"github.com/BurntSushi/toml"
yaml "gopkg.in/yaml.v2"
)
const DefaultListenAddress = "tcp://:53431"
const DefaultChunkSize = 3 * (1 << 20) // 3 MiB
var DefaultHoardConfig = NewHoardConfig(DefaultListenAddress, DefaultChunkSize, NewDefaultStorage(), DefaultLogging)
type HoardConfig struct {
ListenAddress string
// Chunk size for data upload / download
ChunkSize int64
Storage *Storage
Logging *Logging
Secrets *Secrets
}
func NewHoardConfig(listenAddress string, chunkSize int64, storageConfig *Storage, loggingConfig *Logging) *HoardConfig {
return &HoardConfig{
ListenAddress: listenAddress,
ChunkSize: chunkSize,
Storage: storageConfig,
Logging: loggingConfig,
}
}
func HoardConfigFromYAMLString(yamlString string) (*HoardConfig, error) {
hoardConfig := new(HoardConfig)
buf := bytes.NewBufferString(yamlString)
decoder := yaml.NewDecoder(buf)
err := decoder.Decode(hoardConfig)
if err != nil {
return nil, err
}
return hoardConfig, nil
}
func HoardConfigFromJSONString(jsonString string) (*HoardConfig, error) {
hoardConfig := new(HoardConfig)
buf := bytes.NewBufferString(jsonString)
decoder := json.NewDecoder(buf)
err := decoder.Decode(hoardConfig)
if err != nil {
return nil, err
}
return hoardConfig, nil
}
func HoardConfigFromTOMLString(tomlString string) (*HoardConfig, error) {
hoardConfig := new(HoardConfig)
_, err := toml.Decode(tomlString, hoardConfig)
if err != nil {
return nil, err
}
return hoardConfig, nil
}
func (hoardConfig *HoardConfig) TOMLString() string {
buf := new(bytes.Buffer)
encoder := toml.NewEncoder(buf)
err := encoder.Encode(hoardConfig)
if err != nil {
return fmt.Sprintf("<Could not serialise HoardConfig>")
}
return buf.String()
}
func (hoardConfig *HoardConfig) JSONString() string {
buf := new(bytes.Buffer)
encoder := json.NewEncoder(buf)
err := encoder.Encode(hoardConfig)
if err != nil {
return fmt.Sprintf("<Could not serialise HoardConfig>")
}
return buf.String()
}
func (hoardConfig *HoardConfig) YAMLString() string {
buf := new(bytes.Buffer)
encoder := yaml.NewEncoder(buf)
err := encoder.Encode(hoardConfig)
if err != nil {
return fmt.Sprintf("<Could not serialise HoardConfig>")
}
return buf.String()
}