Globals and collection cache
Go / lint (push) Failing after 17s Details
Go / build (push) Failing after 2m12s Details

This commit is contained in:
Marcel M. Otte 2023-10-13 16:13:33 +02:00
parent ecae58b1a5
commit 23da938cc9
5 changed files with 94 additions and 20 deletions

View File

@ -1,18 +1,19 @@
package cop
import (
"encoding/json"
"errors"
"fmt"
"io/fs"
"log"
"os"
"os/user"
"path"
"path/filepath"
"strings"
"github.com/go-git/go-git/v5"
"gitea.mmo.to/ppForge/ppforge/globals"
"gitea.mmo.to/ppForge/ppforge/protocol"
)
@ -21,6 +22,30 @@ type ProtocolCollectionList struct {
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
type ProtocolCollectionEntry struct {
Path string
@ -32,25 +57,14 @@ 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")
}
pathlist := []string{user.HomeDir, ".config", "ppforge", "cop"}
fdb := FileCOP{COP{map[string]ProtocolCollectionEntry{}, false}, path.Join(pathlist...)}
fdb.Open(path.Join(pathlist...))
fdb.UpdateCollection()
fdb := FileCOP{COP{map[string]ProtocolCollectionEntry{}, false}, globals.CollectionOfProtocolsDir}
fdb.Open(globals.CollectionOfProtocolsDir)
fdb.Sync()
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
@ -98,7 +112,7 @@ func (fd *FileCOP) Protocols() ([]ProtocolCollectionEntry, error) {
entries := []ProtocolCollectionEntry{}
if !fd.closed && len(fd.protocols) == 0 {
fd.protocols = map[string]ProtocolCollectionEntry{}
err := fd.UpdateCollection()
err := fd.Sync()
if err != nil {
return nil, err
}
@ -137,10 +151,10 @@ func (fd *FileCOP) Update(prot *protocol.Protocol) error {
return fd.Add(prot)
}
// UpdateCollection just rereads all files
func (fd *FileCOP) UpdateCollection() error {
// Sync just rereads all files
func (fd *FileCOP) Sync() error {
if fd.closed {
return errors.New("Cannot update, FileCOP is closed")
return errors.New("Cannot sync, FileCOP is closed")
}
// recurse recursively through the path
if fd.protocols == nil {

View File

@ -51,7 +51,7 @@ func TestClosed(t *testing.T) {
fmt.Println("Update did not deliver error")
t.Fail()
}
err = fcop.UpdateCollection()
err = fcop.Sync()
if err == nil {
fmt.Println("UpdateCollection did not deliver error")
t.Fail()

19
globals/globals.go Normal file
View File

@ -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"

25
globals/globals.linux.go Normal file
View File

@ -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")...)
}

View File

@ -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...)
}