Globals and collection cache
This commit is contained in:
parent
ecae58b1a5
commit
23da938cc9
|
@ -1,18 +1,19 @@
|
||||||
package cop
|
package cop
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/user"
|
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/go-git/go-git/v5"
|
"github.com/go-git/go-git/v5"
|
||||||
|
|
||||||
|
"gitea.mmo.to/ppForge/ppforge/globals"
|
||||||
"gitea.mmo.to/ppForge/ppforge/protocol"
|
"gitea.mmo.to/ppForge/ppforge/protocol"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -21,6 +22,30 @@ type ProtocolCollectionList struct {
|
||||||
PCs []COP
|
PCs []COP
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WriteCache updates the cache file of the available protocols
|
||||||
|
func (pcl *ProtocolCollectionList) WriteCache() error {
|
||||||
|
// [impl->dsn~protocol-collection-cache~0>>utest]
|
||||||
|
data, err := json.MarshalIndent(*pcl, "", " ")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = os.WriteFile(path.Join(globals.CollectionOfProtocolsDir, globals.COPCacheFileName), data, fs.ModeAppend)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReadCache reads the cache for display
|
||||||
|
func (pcl *ProtocolCollectionList) ReadCache() error {
|
||||||
|
// [impl->dsn~protocol-collection-cache~0>>utest]
|
||||||
|
// TODO: Think about if it makes sense to have this function as a member of PCL,
|
||||||
|
// or if it makes more sense to make this function standalone and return a new PCL?
|
||||||
|
data, err := os.ReadFile(path.Join(globals.CollectionOfProtocolsDir, globals.COPCacheFileName))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = json.Unmarshal(data, pcl)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// ProtocolCollectionEntry is a single entry in the file database, additionally to the meta data the path is required
|
// ProtocolCollectionEntry is a single entry in the file database, additionally to the meta data the path is required
|
||||||
type ProtocolCollectionEntry struct {
|
type ProtocolCollectionEntry struct {
|
||||||
Path string
|
Path string
|
||||||
|
@ -32,25 +57,14 @@ func Init() *ProtocolCollectionList {
|
||||||
// initialize main list
|
// initialize main list
|
||||||
databases := ProtocolCollectionList{[]COP{}}
|
databases := ProtocolCollectionList{[]COP{}}
|
||||||
// initialize default db
|
// initialize default db
|
||||||
user, err := user.Current()
|
fdb := FileCOP{COP{map[string]ProtocolCollectionEntry{}, false}, globals.CollectionOfProtocolsDir}
|
||||||
if err != nil {
|
fdb.Open(globals.CollectionOfProtocolsDir)
|
||||||
log.Printf("Current user not obtainable: %s", err)
|
fdb.Sync()
|
||||||
log.Fatal("Impossible to open default database")
|
|
||||||
}
|
|
||||||
pathlist := []string{user.HomeDir, ".config", "ppforge", "cop"}
|
|
||||||
fdb := FileCOP{COP{map[string]ProtocolCollectionEntry{}, false}, path.Join(pathlist...)}
|
|
||||||
fdb.Open(path.Join(pathlist...))
|
|
||||||
fdb.UpdateCollection()
|
|
||||||
databases.PCs = append(databases.PCs, fdb.COP)
|
databases.PCs = append(databases.PCs, fdb.COP)
|
||||||
log.Printf("Amount of databases available %d", len(databases.PCs))
|
log.Printf("Amount of databases available %d", len(databases.PCs))
|
||||||
return &databases
|
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
|
// COP represents a Collection of Protocols
|
||||||
type COP struct {
|
type COP struct {
|
||||||
protocols map[string]ProtocolCollectionEntry
|
protocols map[string]ProtocolCollectionEntry
|
||||||
|
@ -98,7 +112,7 @@ func (fd *FileCOP) Protocols() ([]ProtocolCollectionEntry, error) {
|
||||||
entries := []ProtocolCollectionEntry{}
|
entries := []ProtocolCollectionEntry{}
|
||||||
if !fd.closed && len(fd.protocols) == 0 {
|
if !fd.closed && len(fd.protocols) == 0 {
|
||||||
fd.protocols = map[string]ProtocolCollectionEntry{}
|
fd.protocols = map[string]ProtocolCollectionEntry{}
|
||||||
err := fd.UpdateCollection()
|
err := fd.Sync()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -137,10 +151,10 @@ func (fd *FileCOP) Update(prot *protocol.Protocol) error {
|
||||||
return fd.Add(prot)
|
return fd.Add(prot)
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateCollection just rereads all files
|
// Sync just rereads all files
|
||||||
func (fd *FileCOP) UpdateCollection() error {
|
func (fd *FileCOP) Sync() error {
|
||||||
if fd.closed {
|
if fd.closed {
|
||||||
return errors.New("Cannot update, FileCOP is closed")
|
return errors.New("Cannot sync, FileCOP is closed")
|
||||||
}
|
}
|
||||||
// recurse recursively through the path
|
// recurse recursively through the path
|
||||||
if fd.protocols == nil {
|
if fd.protocols == nil {
|
||||||
|
|
|
@ -51,7 +51,7 @@ func TestClosed(t *testing.T) {
|
||||||
fmt.Println("Update did not deliver error")
|
fmt.Println("Update did not deliver error")
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
err = fcop.UpdateCollection()
|
err = fcop.Sync()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
fmt.Println("UpdateCollection did not deliver error")
|
fmt.Println("UpdateCollection did not deliver error")
|
||||||
t.Fail()
|
t.Fail()
|
||||||
|
|
|
@ -0,0 +1,19 @@
|
||||||
|
package globals
|
||||||
|
|
||||||
|
// Global variables, Mocks, etc.
|
||||||
|
// This package shall not have any dependency towards the application itself!
|
||||||
|
// [impl->dsn~properly-defined-globals~1>>utest]
|
||||||
|
|
||||||
|
// GLOBAL VARIABLES
|
||||||
|
|
||||||
|
// ConfigDirectoryList Configuration directory list
|
||||||
|
var ConfigDirectoryList []string
|
||||||
|
|
||||||
|
// ConfigDirectory Configuration directory string
|
||||||
|
var ConfigDirectory string
|
||||||
|
|
||||||
|
// CollectionOfProtocolsDir is the default directory for protocol collections
|
||||||
|
var CollectionOfProtocolsDir string
|
||||||
|
|
||||||
|
// COPCacheFileName
|
||||||
|
var COPCacheFileName string = "cop-cache.json"
|
|
@ -0,0 +1,25 @@
|
||||||
|
package globals
|
||||||
|
|
||||||
|
// Global variables, Mocks, etc.
|
||||||
|
// This package shall not have any dependency towards the application itself!
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"os/user"
|
||||||
|
"path"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
user, err := user.Current()
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Current user not obtainable: %s", err)
|
||||||
|
log.Fatal("Impossible to open default database")
|
||||||
|
}
|
||||||
|
|
||||||
|
// ConfigDirectoryList Configuration directory list Linux
|
||||||
|
ConfigDirectoryList = []string{user.HomeDir, ".config", "ppforge"}
|
||||||
|
|
||||||
|
// ConfigDirectory Configuration directory string Linux
|
||||||
|
ConfigDirectory = path.Join(ConfigDirectoryList...)
|
||||||
|
|
||||||
|
CollectionOfProtocolsDir = path.Join(append(ConfigDirectoryList, "cop")...)
|
||||||
|
}
|
|
@ -0,0 +1,16 @@
|
||||||
|
package globals
|
||||||
|
|
||||||
|
// Global variables, Mocks, etc.
|
||||||
|
// This package shall not have any dependency towards the application itself!
|
||||||
|
import (
|
||||||
|
"os/user"
|
||||||
|
"path"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
// ConfigDirectoryList Configuration directory list Windows TODO
|
||||||
|
var ConfigDirectoryList []string = []string{user.HomeDir, TODO!!!}
|
||||||
|
|
||||||
|
// ConfigDirectory Configuration directory string Windows
|
||||||
|
var ConfigDirectory string = path.Join(ConfigDirectoryList...)
|
||||||
|
}
|
Loading…
Reference in New Issue