github.com/cloudfoundry-attic/ltc@v0.0.0-20151123212628-098adc7919fc/config/persister/file_persister.go (about) 1 package persister 2 3 import ( 4 "encoding/json" 5 "io/ioutil" 6 "os" 7 "path/filepath" 8 ) 9 10 type filePersister struct { 11 filePath string 12 } 13 14 func NewFilePersister(filepath string) Persister { 15 return &filePersister{filepath} 16 } 17 18 func (f *filePersister) Load(i interface{}) error { 19 jsonBytes, err := ioutil.ReadFile(f.filePath) 20 if err != nil { 21 return nil 22 } 23 24 err = json.Unmarshal(jsonBytes, i) 25 if err != nil { 26 return err 27 } 28 29 return nil 30 } 31 32 func (f *filePersister) Save(i interface{}) error { 33 err := os.MkdirAll(filepath.Dir(f.filePath), 0700) 34 if err != nil { 35 return err 36 } 37 38 jsonBytes, err := json.Marshal(i) 39 if err != nil { 40 return err 41 } 42 43 err = ioutil.WriteFile(f.filePath, jsonBytes, 0700) 44 if err != nil { 45 return err 46 } 47 48 return nil 49 }