-
-
Notifications
You must be signed in to change notification settings - Fork 342
/
persister.go
48 lines (39 loc) · 1.14 KB
/
persister.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
package config
import (
"fmt"
"os"
"path/filepath"
"github.com/spf13/viper"
)
// Persister saves viper configs.
type Persister interface {
Save(*viper.Viper, string) error
}
// FilePersister saves viper configs to the file system.
type FilePersister struct {
Dir string
}
// Save writes the viper config to the target location on the filesystem.
func (p FilePersister) Save(v *viper.Viper, basename string) error {
v.SetConfigType("json")
v.AddConfigPath(p.Dir)
v.SetConfigName(basename)
if _, err := os.Stat(p.Dir); os.IsNotExist(err) {
if err := os.MkdirAll(p.Dir, os.FileMode(0755)); err != nil {
return err
}
}
// WriteConfig is broken.
// Someone proposed a fix in https://github.com/spf13/viper/pull/503,
// but the fix doesn't work yet.
// When it's fixed and merged we can get rid of `path`
// and use viperConfig.WriteConfig() directly.
path := filepath.Join(p.Dir, fmt.Sprintf("%s.json", basename))
return v.WriteConfigAs(path)
}
// InMemoryPersister is a noop persister for use in unit tests.
type InMemoryPersister struct{}
// Save does nothing.
func (p InMemoryPersister) Save(*viper.Viper, string) error {
return nil
}