-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
59 lines (43 loc) · 1.3 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
package main
import (
"encoding/json"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/zalando/go-keyring"
)
type Config struct {
Connection string `json:"connection"`
Password string `json:"password"`
OtpSecret string `json:"otp_secret"`
}
func (c *Config) read(instance string) {
service := "roly-poly-vpn"
config, err := keyring.Get(service, instance)
if err == keyring.ErrNotFound {
return
}
if err != nil {
clog.WithFields(log.Fields{"instance": instance, "error": err}).Fatal("Failed to read config.")
}
clog.WithFields(log.Fields{"instance": instance}).Info("Got config from keyring.")
err = json.Unmarshal([]byte(config), &c)
if err != nil {
clog.WithFields(log.Fields{"instance": instance}).Fatal("Failed to unmarshal config.")
}
}
func (c *Config) write(instance string) {
service := "roly-poly-vpn"
jsonData, err := json.Marshal(c)
if err != nil {
clog.WithFields(log.Fields{
"instance": instance,
"error": err,
}).Error("Config convertion to JSON failed.")
}
jsonConfig := fmt.Sprintf(string(jsonData))
err = keyring.Set(service, instance, jsonConfig)
if err != nil {
clog.WithFields(log.Fields{"instance": instance, "error": err}).Fatal("Failed to write config.")
}
clog.WithFields(log.Fields{"instance": instance}).Info("Wrote config to keyring.")
}