diff options
Diffstat (limited to 'save.go')
| -rw-r--r-- | save.go | 57 |
1 files changed, 42 insertions, 15 deletions
@@ -1,24 +1,33 @@ 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" +) -func (e *Events) Save() { - var fullname string - base, _ := filepath.Split(argv.Config) - fullname = filepath.Join(base, "events.pb") +var ErrProtoFilename error = log.Errorf("proto does not have Filename") - data, err := e.Marshal() +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 { - log.Info("proto.Marshal() failed", err) - return + return err } - log.Info("proto.Marshal() worked len", len(data)) - configWrite(fullname, data) -} + 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 == "" { @@ -30,10 +39,28 @@ func configWrite(fullname string, data []byte) error { log.Warn("open config file :", err) return err } - cfgfile.Write(data) - return nil + _, err = cfgfile.Write(data) + return err } +/* + +func (e *Events) Save() { + var fullname string + base, _ := filepath.Split(argv.Config) + fullname = filepath.Join(base, "events.pb") + + data, err := e.Marshal() + if err != nil { + log.Info("proto.Marshal() failed", err) + return + } + log.Info("proto.Marshal() worked len", len(data)) + configWrite(fullname, data) +} + + + func (m *Portmaps) configWrite(fullname string, data []byte) error { if _, base := filepath.Split(fullname); base == "" { return fmt.Errorf("--config option not set") |
