forked from pavel-d/go-openvpn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
64 lines (54 loc) · 1.14 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
package config
import (
"bytes"
"html/template"
"io/ioutil"
)
var defaultConfig = Config{
ServerAddress: "127.0.0.1",
Port: 1194,
Proto: "udp",
Cipher: "AES-256-CBC",
Keysize: 256,
Auth: "SHA256",
Ca: "ca.crt",
}
//Config model
type Config struct {
ServerAddress string
Port int
Proto string
Ca string
Cert string
Key string
Cipher string
Keysize int
Auth string
}
//New returns config object with default values
func New() Config {
return defaultConfig
}
//GetText injects config values into template
func GetText(tpl string, c Config) (string, error) {
t := template.New("config")
t, err := t.Parse(tpl)
if err != nil {
return "", err
}
buf := new(bytes.Buffer)
t.Execute(buf, c)
return buf.String(), nil
}
//SaveToFile reads teamplate and writes result to destination file
func SaveToFile(tplPath string, c Config, destPath string) error {
template, err := ioutil.ReadFile(tplPath)
if err != nil {
return err
}
str, err := GetText(string(template), c)
if err != nil {
return err
}
return ioutil.WriteFile(destPath, []byte(str), 0644)
}