-
Notifications
You must be signed in to change notification settings - Fork 0
/
provider.go
executable file
·67 lines (56 loc) · 1.45 KB
/
provider.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
package gobprovider
import (
"encoding/gob"
"os"
)
// GobProvider provides a map with all configuration settings set in a go binary file
// The prefix will be removed from the key name. Returns a map or error.
type GobProvider struct {
Filename string
}
// Provide implements the Provider interface
func (g GobProvider) Provide() (map[string]string, error) {
// Create an empty map to store the configuration
cfg := map[string]string{}
// Check if the filename has been set
if len(g.Filename) == 0 {
return nil, ErrEmptyFilename
}
// Open the file and ensure the file will be properly closed after all operations
file, err := os.Open(g.Filename)
if err != nil {
return nil, err
}
defer file.Close()
dec := gob.NewDecoder(file)
if err := dec.Decode(&cfg); err != nil {
return nil, err
}
return cfg, nil
}
// Save will store the provided configuration
func (g GobProvider) Save(cfg *Config) error {
// Check if the filename has been set
if len(g.Filename) == 0 {
return ErrEmptyFilename
}
// Open or create the file and ensure the file will be properly closed after all operations
file, err := os.Open(g.Filename)
if err != nil {
if os.IsNotExist(err) {
file, err = os.Create(g.Filename)
if err != nil {
return err
}
} else {
return err
}
}
defer file.Close()
// Encode and save the configuration map
encoder := gob.NewEncoder(file)
if err := encoder.Encode(cfg.Map()); err != nil {
return err
}
return nil
}