2023-05-13 21:42:50 +02:00
|
|
|
package dop
|
2023-04-12 19:49:12 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/fs"
|
2023-05-12 16:02:06 +02:00
|
|
|
"log"
|
2023-04-12 19:49:12 +02:00
|
|
|
"os"
|
2023-05-12 16:02:06 +02:00
|
|
|
"os/user"
|
2023-04-13 11:27:03 +02:00
|
|
|
"path"
|
2023-04-12 19:49:12 +02:00
|
|
|
"path/filepath"
|
|
|
|
|
2023-05-12 10:37:13 +02:00
|
|
|
"github.com/go-git/go-git/v5"
|
|
|
|
|
2023-05-12 09:10:24 +02:00
|
|
|
"gitea.mmo.to/ppForge/ppforge/protocol"
|
|
|
|
"gitea.mmo.to/ppForge/ppforge/protocolctl"
|
2023-04-12 19:49:12 +02:00
|
|
|
)
|
|
|
|
|
2023-05-13 21:42:50 +02:00
|
|
|
// ProtocolDictionaryList is a list of all databases, with the most simple interface
|
|
|
|
type ProtocolDictionaryList struct {
|
|
|
|
PDs []ProtocolDictionaryProvider
|
2023-04-12 19:49:12 +02:00
|
|
|
}
|
|
|
|
|
2023-05-13 21:42:50 +02:00
|
|
|
// ProtocolDictionaryProvider is a simple protocol database interface, which only provides open/close and retrieving the contents
|
|
|
|
type ProtocolDictionaryProvider interface {
|
2023-04-13 11:27:03 +02:00
|
|
|
// Open opens a new database
|
2023-04-12 19:49:12 +02:00
|
|
|
Open(path string) error
|
2023-04-13 11:27:03 +02:00
|
|
|
// Close closes a database (only internal flag set)
|
2023-04-12 19:49:12 +02:00
|
|
|
Close() error
|
2023-04-13 11:27:03 +02:00
|
|
|
// Protocols returns the content of the database as map, an error if the first read of the database failed
|
2023-05-13 21:42:50 +02:00
|
|
|
Protocols() ([]ProtocolDictionaryEntry, error)
|
2023-04-12 19:49:12 +02:00
|
|
|
}
|
|
|
|
|
2023-04-13 11:27:03 +02:00
|
|
|
// ProtocolDatabaseManager is a complex protocol database interface, which allows adding, updating and refreshing of the content
|
2023-04-12 19:49:12 +02:00
|
|
|
type ProtocolDatabaseManager interface {
|
2023-04-13 11:27:03 +02:00
|
|
|
Add(prot *protocol.ProtocolStructure) error
|
|
|
|
Update(prot *protocol.ProtocolStructure) error
|
2023-04-12 19:49:12 +02:00
|
|
|
// no remove.
|
|
|
|
UpdateDatabase() error // scans for new protocols
|
|
|
|
}
|
|
|
|
|
2023-05-13 21:42:50 +02:00
|
|
|
// ProtocolDictionaryEntry is a single entry in the file database, additionally to the meta data the path is required
|
|
|
|
type ProtocolDictionaryEntry struct {
|
2023-04-12 19:49:12 +02:00
|
|
|
Path string
|
|
|
|
Protocol protocol.DOPMeta
|
|
|
|
}
|
|
|
|
|
2023-05-12 16:02:06 +02:00
|
|
|
// Init initializes the databases, opens the default one and checks for others if available
|
2023-05-13 21:42:50 +02:00
|
|
|
func Init() *ProtocolDictionaryList {
|
2023-05-12 16:02:06 +02:00
|
|
|
// initialize main list
|
2023-05-13 21:42:50 +02:00
|
|
|
databases := ProtocolDictionaryList{[]ProtocolDictionaryProvider{}}
|
2023-05-12 16:02:06 +02:00
|
|
|
// 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")
|
|
|
|
}
|
|
|
|
pathlist := []string{user.HomeDir, ".config", "ppforge"}
|
2023-05-13 21:42:50 +02:00
|
|
|
fdb := FileDOP{path.Join(pathlist...), map[string]ProtocolDictionaryEntry{}, false}
|
2023-05-12 16:02:06 +02:00
|
|
|
fdb.Open(path.Join(pathlist...))
|
|
|
|
fdb.UpdateDatabase()
|
2023-05-13 21:42:50 +02:00
|
|
|
databases.PDs = append(databases.PDs, fdb)
|
|
|
|
log.Printf("Amount of databases available %d", len(databases.PDs))
|
2023-05-12 16:02:06 +02:00
|
|
|
return &databases
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get Retrieves the full set of databases
|
2023-05-13 21:42:50 +02:00
|
|
|
func (ppfdb *ProtocolDictionaryList) Get() []ProtocolDictionaryProvider {
|
|
|
|
return ppfdb.PDs
|
2023-05-12 16:02:06 +02:00
|
|
|
}
|
|
|
|
|
2023-04-13 11:27:03 +02:00
|
|
|
// Get a full protocol from a database entry
|
2023-05-13 21:42:50 +02:00
|
|
|
func (de *ProtocolDictionaryEntry) Get() (*protocol.ProtocolStructure, error) {
|
2023-04-13 11:27:03 +02:00
|
|
|
return protocolctl.LoadNew(de.Path)
|
|
|
|
}
|
|
|
|
|
2023-05-13 21:42:50 +02:00
|
|
|
// FileDOP implements ProtocolDatabaseProvider and ProtocolDatabaseManager
|
|
|
|
type FileDOP struct {
|
2023-04-12 19:49:12 +02:00
|
|
|
Path string
|
2023-05-13 21:42:50 +02:00
|
|
|
protocols map[string]ProtocolDictionaryEntry
|
2023-04-12 19:49:12 +02:00
|
|
|
closed bool
|
|
|
|
}
|
|
|
|
|
2023-05-13 21:42:50 +02:00
|
|
|
// FileDOPFromGitRemote implements retrieving the latest stuff from an external git remote additionally to the FileDatabase functionality
|
2023-04-13 11:27:03 +02:00
|
|
|
// TODO
|
2023-05-13 21:42:50 +02:00
|
|
|
type FileDOPFromGitRemote struct {
|
|
|
|
FileDOP
|
2023-04-12 19:49:12 +02:00
|
|
|
RemoteGitRepository string
|
2023-05-12 10:37:13 +02:00
|
|
|
gitRepo git.Repository
|
2023-04-12 19:49:12 +02:00
|
|
|
}
|
|
|
|
|
2023-04-13 11:27:03 +02:00
|
|
|
// Open the database
|
2023-05-13 21:42:50 +02:00
|
|
|
func (fd FileDOP) Open(path string) error {
|
2023-04-12 19:49:12 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-04-13 11:27:03 +02:00
|
|
|
// Close the database and free the data
|
2023-05-13 21:42:50 +02:00
|
|
|
func (fd FileDOP) Close() error {
|
2023-04-12 19:49:12 +02:00
|
|
|
for k := range fd.protocols {
|
|
|
|
delete(fd.protocols, k)
|
|
|
|
}
|
|
|
|
fd.closed = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-13 11:27:03 +02:00
|
|
|
// Protocols returns the map of protocols or an error if the first read failed
|
2023-05-13 21:42:50 +02:00
|
|
|
func (fd FileDOP) Protocols() ([]ProtocolDictionaryEntry, error) {
|
|
|
|
entries := []ProtocolDictionaryEntry{}
|
2023-04-13 11:27:03 +02:00
|
|
|
if !fd.closed && len(fd.protocols) == 0 {
|
2023-05-13 21:42:50 +02:00
|
|
|
fd.protocols = map[string]ProtocolDictionaryEntry{}
|
2023-04-12 19:49:12 +02:00
|
|
|
// recurse recursively through the path
|
2023-04-13 11:27:03 +02:00
|
|
|
err := filepath.Walk(fd.Path, func(path string, info fs.FileInfo, err error) error {
|
2023-04-12 19:49:12 +02:00
|
|
|
if !info.IsDir() {
|
|
|
|
prot, err := protocolctl.LoadNew(path)
|
2023-04-13 11:27:03 +02:00
|
|
|
if err == nil {
|
2023-04-12 19:49:12 +02:00
|
|
|
// add to map
|
2023-05-13 21:42:50 +02:00
|
|
|
entry := ProtocolDictionaryEntry{path, prot.Metadata}
|
2023-05-12 16:02:06 +02:00
|
|
|
|
|
|
|
fd.protocols[prot.Metadata.Name] = entry
|
|
|
|
entries = append(entries, entry)
|
2023-04-12 19:49:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2023-04-13 11:27:03 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-04-12 19:49:12 +02:00
|
|
|
}
|
2023-05-12 16:02:06 +02:00
|
|
|
if entries == nil || len(entries) == 0 {
|
2023-05-13 21:42:50 +02:00
|
|
|
entries = make([]ProtocolDictionaryEntry, len(fd.protocols))
|
2023-05-12 16:02:06 +02:00
|
|
|
for _, v := range fd.protocols {
|
|
|
|
entries = append(entries, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return entries, nil
|
2023-04-12 19:49:12 +02:00
|
|
|
}
|
|
|
|
|
2023-04-13 11:27:03 +02:00
|
|
|
// Add puts a new protocol into the path of the file database
|
2023-05-13 21:42:50 +02:00
|
|
|
func (fd *FileDOP) Add(prot *protocol.ProtocolStructure) error {
|
2023-04-13 11:27:03 +02:00
|
|
|
p := path.Join(fd.Path, prot.Metadata.Name, ".protocoljson")
|
|
|
|
err := protocolctl.Save(prot, p)
|
|
|
|
if err == nil {
|
2023-05-13 21:42:50 +02:00
|
|
|
fd.protocols[prot.Metadata.Name] = ProtocolDictionaryEntry{p, prot.Metadata}
|
2023-04-13 11:27:03 +02:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2023-04-12 19:49:12 +02:00
|
|
|
|
2023-04-13 11:27:03 +02:00
|
|
|
// Update updates an existing entry through saving the contents of the protocol to the file, or just adding it anew
|
2023-05-13 21:42:50 +02:00
|
|
|
func (fd *FileDOP) Update(prot *protocol.ProtocolStructure) error {
|
2023-04-13 11:27:03 +02:00
|
|
|
if v, ok := fd.protocols[prot.Metadata.Name]; ok {
|
|
|
|
v.Protocol = prot.Metadata
|
|
|
|
return protocolctl.Save(prot, v.Path)
|
|
|
|
}
|
|
|
|
return fd.Add(prot)
|
2023-04-12 19:49:12 +02:00
|
|
|
}
|
|
|
|
|
2023-04-13 11:27:03 +02:00
|
|
|
// UpdateDatabase just rereads all files
|
2023-05-13 21:42:50 +02:00
|
|
|
func (fd *FileDOP) UpdateDatabase() error {
|
2023-04-13 11:27:03 +02:00
|
|
|
// recurse recursively through the path
|
|
|
|
err := filepath.Walk(fd.Path, func(path string, info fs.FileInfo, err error) error {
|
|
|
|
if !info.IsDir() {
|
|
|
|
prot, err := protocolctl.LoadNew(path)
|
|
|
|
if err == nil {
|
|
|
|
// add to map
|
2023-05-13 21:42:50 +02:00
|
|
|
fd.protocols[prot.Metadata.Name] = ProtocolDictionaryEntry{path, prot.Metadata}
|
2023-04-13 11:27:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return err
|
2023-04-12 19:49:12 +02:00
|
|
|
}
|