This repository has been archived by the owner on Jan 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathconfig.go
121 lines (95 loc) · 2.91 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
120
121
package prifimobile
// Configuration files read & write and related structs
import (
"bytes"
"github.com/BurntSushi/toml"
prifi_protocol "github.com/dedis/prifi/sda/protocols"
"go.dedis.ch/onet/v3/app"
"go.dedis.ch/onet/v3/log"
"golang.org/x/mobile/asset"
"sync"
)
const prifiConfigFilename = "prifi.toml"
const cothorityConfigFilename = "identity.toml"
const cothorityGroupConfigFilename = "group.toml"
var prifiConfigSingleton *prifi_protocol.PrifiTomlConfig
var cothorityConfigSingleton *app.CothorityConfig
var groupConfigSingleton *app.Group
var onceClient, onceCothority, onceGroup sync.Once
var globalErr error
func getPrifiConfig() (*prifi_protocol.PrifiTomlConfig, error) {
onceClient.Do(func() {
prifiConfigSingleton, globalErr = initPrifiConfig()
})
return prifiConfigSingleton, globalErr
}
func getCothorityConfig() (*app.CothorityConfig, error) {
onceCothority.Do(func() {
cothorityConfigSingleton, globalErr = initCothorityConfig()
})
return cothorityConfigSingleton, globalErr
}
func getGroupConfig() (*app.Group, error) {
onceGroup.Do(func() {
groupConfigSingleton, globalErr = initCothorityGroupConfig()
})
return groupConfigSingleton, globalErr
}
// TODO: Reduce Code Duplication of both inits
func initPrifiConfig() (*prifi_protocol.PrifiTomlConfig, error) {
tomlRawDataString, err := readTomlFromAssets(prifiConfigFilename)
if err != nil {
return nil, err
}
config := &prifi_protocol.PrifiTomlConfig{}
_, err = toml.Decode(tomlRawDataString, config)
if err != nil {
log.Error("Could not parse toml file ", prifiConfigFilename)
return nil, err
}
return config, nil
}
func initCothorityConfig() (*app.CothorityConfig, error) {
tomlRawDataString, err := readTomlFromAssets(cothorityConfigFilename)
if err != nil {
return nil, err
}
config := &app.CothorityConfig{}
_, err = toml.Decode(tomlRawDataString, config)
if err != nil {
log.Error("Could not parse toml file ", cothorityConfigFilename)
return nil, err
}
return config, nil
}
// TODO: less code duplication?
func initCothorityGroupConfig() (*app.Group, error) {
file, err := asset.Open(cothorityGroupConfigFilename)
defer file.Close()
group, err := app.ReadGroupDescToml(file)
if err != nil {
log.Error("Could not parse toml file ", cothorityGroupConfigFilename)
return nil, err
}
if group == nil || group.Roster == nil || len(group.Roster.List) == 0 {
log.Error("No servers found in roster from ", cothorityGroupConfigFilename)
return nil, err
}
return group, nil
}
// TODO: Read from any given paths
func readTomlFromAssets(filename string) (string, error) {
file, err := asset.Open(filename)
defer file.Close()
if err != nil {
log.Error("Could not open file ", filename)
return "", err
}
tomlRawDataBuffer := new(bytes.Buffer)
_, err = tomlRawDataBuffer.ReadFrom(file)
if err != nil {
log.Error("Could not read file ", prifiConfigFilename)
return "", err
}
return tomlRawDataBuffer.String(), err
}