2023-09-17 12:49:41 +02:00
|
|
|
package cop
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/fs"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/user"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/go-git/go-git/v5"
|
|
|
|
|
|
|
|
"gitea.mmo.to/ppForge/ppforge/protocol"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ProtocolCollectionList is a list of all databases, with the most simple interface
|
|
|
|
type ProtocolCollectionList struct {
|
|
|
|
PCs []COP
|
|
|
|
}
|
|
|
|
|
|
|
|
// ProtocolCollectionEntry is a single entry in the file database, additionally to the meta data the path is required
|
|
|
|
type ProtocolCollectionEntry struct {
|
|
|
|
Path string
|
|
|
|
Protocol protocol.ProtocolMeta
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init initializes the databases, opens the default one and checks for others if available
|
|
|
|
func Init() *ProtocolCollectionList {
|
|
|
|
// initialize main list
|
|
|
|
databases := ProtocolCollectionList{[]COP{}}
|
|
|
|
// initialize default db
|
|
|
|
user, err := user.Current()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Current user not obtainable: %s", err)
|
|
|
|
log.Fatal("Impossible to open default database")
|
|
|
|
}
|
2023-10-02 17:47:53 +02:00
|
|
|
pathlist := []string{user.HomeDir, ".config", "ppforge", "cop"}
|
2023-09-17 12:49:41 +02:00
|
|
|
fdb := FileCOP{COP{map[string]ProtocolCollectionEntry{}, false}, path.Join(pathlist...)}
|
|
|
|
fdb.Open(path.Join(pathlist...))
|
|
|
|
fdb.UpdateCollection()
|
|
|
|
databases.PCs = append(databases.PCs, fdb.COP)
|
|
|
|
log.Printf("Amount of databases available %d", len(databases.PCs))
|
|
|
|
return &databases
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get a full protocol from a database entry
|
|
|
|
func (de *ProtocolCollectionEntry) Get() (*protocol.Protocol, error) {
|
|
|
|
return protocol.LoadNew(de.Path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// COP represents a Collection of Protocols
|
|
|
|
type COP struct {
|
|
|
|
protocols map[string]ProtocolCollectionEntry
|
|
|
|
closed bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// FileCOP implements
|
|
|
|
type FileCOP struct {
|
|
|
|
COP
|
|
|
|
Path string
|
|
|
|
}
|
|
|
|
|
|
|
|
// FileCOPFromGitRemote implements retrieving the latest stuff from an external git remote additionally to the FileDatabase functionality
|
|
|
|
type FileCOPFromGitRemote struct {
|
|
|
|
FileCOP
|
|
|
|
RemoteGitRepository string
|
|
|
|
gitRepo git.Repository
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open the database
|
|
|
|
func (fd *FileCOP) Open(path string) error {
|
|
|
|
fileinfo, err := os.Stat(path)
|
|
|
|
if err == os.ErrNotExist {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !fileinfo.IsDir() {
|
|
|
|
return fmt.Errorf("Path %s is not a directory", 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Protocols returns the map of protocols or an error if the first read failed
|
|
|
|
func (fd *FileCOP) Protocols() ([]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.UpdateCollection()
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add puts a new protocol into the path of the file database
|
|
|
|
func (fd *FileCOP) Add(prot *protocol.Protocol) error {
|
|
|
|
if fd.closed {
|
|
|
|
return errors.New("Cannot add, FileCOP is closed")
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update updates an existing entry through saving the contents of the protocol to the file, or just adding it anew
|
|
|
|
func (fd *FileCOP) Update(prot *protocol.Protocol) error {
|
|
|
|
if fd.closed {
|
|
|
|
return errors.New("Cannot update, FileCOP is closed")
|
|
|
|
}
|
|
|
|
if v, ok := fd.protocols[prot.Metadata.Name]; ok {
|
|
|
|
v.Protocol = prot.Metadata
|
|
|
|
return prot.Save(v.Path)
|
|
|
|
}
|
|
|
|
return fd.Add(prot)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateCollection just rereads all files
|
|
|
|
func (fd *FileCOP) UpdateCollection() error {
|
|
|
|
if fd.closed {
|
|
|
|
return errors.New("Cannot update, 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
|
|
|
|
}
|