diff --git a/cop/collection.go b/cop/collection.go index c13a7b0..476cf32 100644 --- a/cop/collection.go +++ b/cop/collection.go @@ -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 { diff --git a/cop/collection_test.go b/cop/collection_test.go index b9f197d..fadfea7 100644 --- a/cop/collection_test.go +++ b/cop/collection_test.go @@ -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() diff --git a/globals/globals.go b/globals/globals.go new file mode 100644 index 0000000..65e49d9 --- /dev/null +++ b/globals/globals.go @@ -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" diff --git a/globals/globals.linux.go b/globals/globals.linux.go new file mode 100644 index 0000000..6c4065f --- /dev/null +++ b/globals/globals.linux.go @@ -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")...) +} diff --git a/globals/globals.windows.go b/globals/globals.windows.go new file mode 100644 index 0000000..cfd6a80 --- /dev/null +++ b/globals/globals.windows.go @@ -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...) +}