add Save()
This commit is contained in:
parent
c0a3642ed2
commit
5f886d7c07
57
save.go
57
save.go
|
@ -1,7 +1,47 @@
|
|||
package config
|
||||
|
||||
// functions to import and export the protobuf
|
||||
// data to and from config files
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"go.wit.com/log"
|
||||
"google.golang.org/protobuf/encoding/prototext"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
var ErrProtoFilename error = log.Errorf("proto does not have Filename")
|
||||
|
||||
func ConfigSave(pb proto.Message) error {
|
||||
// get pb.Filename if it is there in the .proto file
|
||||
fullname, ok := GetFilename(pb)
|
||||
if !ok {
|
||||
return ErrProtoFilename
|
||||
}
|
||||
|
||||
// Unmarshal()
|
||||
data, err := prototext.Marshal(pb)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Infof("ConfigSave() filename=%s %d\n", fullname, len(data))
|
||||
return configWrite(fullname, data)
|
||||
}
|
||||
|
||||
func configWrite(fullname string, data []byte) error {
|
||||
if _, base := filepath.Split(fullname); base == "" {
|
||||
return fmt.Errorf("--config option not set")
|
||||
}
|
||||
cfgfile, err := os.OpenFile(fullname, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
|
||||
defer cfgfile.Close()
|
||||
if err != nil {
|
||||
log.Warn("open config file :", err)
|
||||
return err
|
||||
}
|
||||
_, err = cfgfile.Write(data)
|
||||
return err
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
|
@ -20,19 +60,6 @@ func (e *Events) Save() {
|
|||
}
|
||||
|
||||
|
||||
func configWrite(fullname string, data []byte) error {
|
||||
if _, base := filepath.Split(fullname); base == "" {
|
||||
return fmt.Errorf("--config option not set")
|
||||
}
|
||||
cfgfile, err := os.OpenFile(fullname, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
|
||||
defer cfgfile.Close()
|
||||
if err != nil {
|
||||
log.Warn("open config file :", err)
|
||||
return err
|
||||
}
|
||||
cfgfile.Write(data)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Portmaps) configWrite(fullname string, data []byte) error {
|
||||
if _, base := filepath.Split(fullname); base == "" {
|
||||
|
|
Loading…
Reference in New Issue