summaryrefslogtreecommitdiff
path: root/src/config.go
blob: 0b7c1c70c050c093b4322a8660bb6423c981c28f (plain)
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
package eduvpn

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"os"
	"path"
)

func (eduvpn *VPNState) GetConfigName() string {
	pathString := path.Join(eduvpn.ConfigDirectory, eduvpn.Name)
	return fmt.Sprintf("%s.json", pathString)
}

func (eduvpn *VPNState) WriteConfig() error {
	mkdirErr := os.MkdirAll(eduvpn.ConfigDirectory, os.ModePerm)
	if mkdirErr != nil {
		return mkdirErr
	}
	jsonString, marshalErr := json.Marshal(eduvpn)
	if marshalErr != nil {
		return marshalErr
	}
	return ioutil.WriteFile(eduvpn.GetConfigName(), jsonString, 0644)
}

func (eduvpn *VPNState) LoadConfig() error {
	bytes, readErr := ioutil.ReadFile(eduvpn.GetConfigName())
	if readErr != nil {
		return readErr
	}
	return json.Unmarshal(bytes, eduvpn)
}