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 config implements functions for saving a struct to a file
// It then provides functions to later read it such that we can restore the same struct
package config
import (
"encoding/json"
"fmt"
"io/ioutil"
"path"
"github.com/eduvpn/eduvpn-common/internal/util"
"github.com/eduvpn/eduvpn-common/types"
)
// Config represents a configuration that saves the client's struct as JSON.
type Config struct {
// Directory represents the path to where the data is saved
Directory string
// Name defines the name of file excluding the .json extension
Name string
}
// Init initializes the configuration using the provided directory and name.
func (config *Config) Init(directory string, name string) {
config.Directory = directory
config.Name = name
}
// filename returns the filename of the configuration as a full path.
func (config *Config) filename() string {
pathString := path.Join(config.Directory, config.Name)
return fmt.Sprintf("%s.json", pathString)
}
// Save saves a structure 'readStruct' to the configuration
// If it was unusuccessful, an an error is returned.
func (config *Config) Save(readStruct interface{}) error {
errorMessage := "failed saving configuration"
configDirErr := util.EnsureDirectory(config.Directory)
if configDirErr != nil {
return types.NewWrappedError(errorMessage, configDirErr)
}
jsonString, marshalErr := json.Marshal(readStruct)
if marshalErr != nil {
return types.NewWrappedError(errorMessage, marshalErr)
}
return ioutil.WriteFile(config.filename(), jsonString, 0o600)
}
// Load loads the configuration and writes the structure to 'writeStruct'
// If it was unsuccessful, an error is returned.
func (config *Config) Load(writeStruct interface{}) error {
bytes, readErr := ioutil.ReadFile(config.filename())
if readErr != nil {
return types.NewWrappedError("failed loading configuration", readErr)
}
return json.Unmarshal(bytes, writeStruct)
}
|