ppforge/cop/filecop.go

138 lines
3.2 KiB
Go

package cop
import (
"encoding/json"
"errors"
"fmt"
"io/fs"
"log"
"os"
"path"
"path/filepath"
"strings"
"gitea.mmo.to/ppForge/ppforge/protocol"
)
// FileCOP implements
type FileCOP struct {
COP
Path string
}
// FileCOPFromJSON creates a FileCOP
func FileCOPFromJSON(data []byte) (COPer, error) {
fcop := &FileCOP{}
err := json.Unmarshal(data, fcop)
return fcop, err
}
// NewFileCOP returns a new FileCOP instance from a given path
func NewFileCOP(path string) (*FileCOP, error) {
fCOP := &FileCOP{}
err := fCOP.Open(path)
if err != nil {
log.Printf("Error opening file COP at %s: %v", path, err)
return nil, err
}
err = fCOP.Sync()
return fCOP, err
}
// Open the database
func (fd *FileCOP) Open(path string) error {
fileinfo, err := os.Stat(path)
if err == os.ErrNotExist {
return err
}
if fileinfo == nil || !fileinfo.IsDir() {
return fmt.Errorf("Path %s is not a directory or does not exist", path)
}
fd.Path = path
fd.closed = false
return nil
}
// Close the database and free the data
func (fd *FileCOP) Close() error {
fd.closed = true
return nil
}
// GetProtocols returns the map of protocols or an error if the first read failed
func (fd *FileCOP) GetProtocols() ([]ProtocolCollectionEntry, error) {
if fd.closed {
return nil, errors.New("COP closed, no protocols to deliver")
}
entries := []ProtocolCollectionEntry{}
if !fd.closed && len(fd.Protocols) == 0 {
fd.Protocols = map[string]ProtocolCollectionEntry{}
err := fd.Sync()
if err != nil {
return nil, err
}
}
if entries == nil || len(entries) == 0 {
entries = make([]ProtocolCollectionEntry, 0)
for _, v := range fd.Protocols {
entries = append(entries, v)
}
}
return entries, nil
}
// AddOrUpdate puts a new protocol into the path of the file database
func (fd *FileCOP) AddOrUpdate(prot *protocol.Protocol) error {
if fd.closed {
return errors.New("Cannot add or update, FileCOP is closed")
}
if v, ok := fd.Protocols[prot.Metadata.Name]; ok {
v.Protocol = prot.Metadata
return prot.Save(v.Path)
}
p := path.Join(fd.Path, prot.Metadata.Name, ".protocoljson")
err := prot.Save(p)
if err == nil {
fd.Protocols[prot.Metadata.Name] = ProtocolCollectionEntry{p, prot.Metadata}
}
return err
}
// Sync just rereads all files
func (fd *FileCOP) Sync() error {
if fd.closed {
return errors.New("Cannot sync, FileCOP is closed")
}
// recurse recursively through the path
if fd.Protocols == nil {
fd.Protocols = make(map[string]ProtocolCollectionEntry)
}
err := filepath.Walk(fd.Path, func(path string, info fs.FileInfo, err error) error {
if !info.IsDir() && strings.HasSuffix(info.Name(), ".protocoljson") {
prot, err := protocol.LoadNew(path)
if err == nil {
// add to map
// add to map
entry := ProtocolCollectionEntry{path, prot.Metadata}
fd.Protocols[prot.Metadata.Name] = entry
}
return err
}
return nil
})
return err
}
// Get a protocol by name
func (fd *FileCOP) Get(prot string) (*protocol.Protocol, error) {
entry := fd.Protocols[prot]
return protocol.LoadNew(entry.Path)
}
// ToJSON marshals the FileCOP to JSON
func (fd *FileCOP) ToJSON() (string, error) {
by, err := json.MarshalIndent(fd, "", " ")
return string(by), err
}