From 3b53f480247b8ec98134a88fc37acaf22dc19546 Mon Sep 17 00:00:00 2001 From: "Marcel M. Otte" Date: Fri, 12 May 2023 16:02:06 +0200 Subject: [PATCH] Run Test, then Build, then linter for now --- .github/workflows/go.yml | 8 +- protocoldb/protocoldb.go | 77 ++++++++++--------- protocoldb/protocoldb_test.go | 9 +++ .../protocoldb/testprotocolempty.protocoljson | 1 + 4 files changed, 56 insertions(+), 39 deletions(-) create mode 100644 protocoldb/protocoldb_test.go create mode 100644 test/protocoldb/testprotocolempty.protocoljson diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 3a8cbc5..555d0aa 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -26,6 +26,10 @@ jobs: uses: https://github.com/goreleaser/goreleaser-action@v2.7.0 with: install-only: true + - name: Test + run: go test -v -cover + - name: Build + run: goreleaser build --snapshot --rm-dist - name: Go-linter # You may pin to the exact commit or the version. # uses: Jerome1337/golint-action@c5d17206a0a436bbf1edb91e314ed084f7c57589 @@ -33,8 +37,4 @@ jobs: #with: # Path used by golint command # golint-path: # optional, default is ./... - - name: Build - run: goreleaser build --snapshot --rm-dist - - name: Test - run: go test -v -cover diff --git a/protocoldb/protocoldb.go b/protocoldb/protocoldb.go index 0d1d5e9..6b1cc5c 100644 --- a/protocoldb/protocoldb.go +++ b/protocoldb/protocoldb.go @@ -3,10 +3,11 @@ package protocoldb import ( "fmt" "io/fs" + "log" "os" + "os/user" "path" "path/filepath" - "strings" "github.com/go-git/go-git/v5" @@ -26,15 +27,13 @@ type ProtocolDatabaseProvider interface { // Close closes a database (only internal flag set) Close() error // Protocols returns the content of the database as map, an error if the first read of the database failed - Protocols() (map[string]protocol.DOPMeta, error) + Protocols() ([]DatabaseEntry, error) } // ProtocolDatabaseManager is a complex protocol database interface, which allows adding, updating and refreshing of the content type ProtocolDatabaseManager interface { Add(prot *protocol.ProtocolStructure) error - AddFile(path string) error Update(prot *protocol.ProtocolStructure) error - UpdateFile(path string) error // no remove. UpdateDatabase() error // scans for new protocols } @@ -45,6 +44,30 @@ type DatabaseEntry struct { Protocol protocol.DOPMeta } +// Init initializes the databases, opens the default one and checks for others if available +func Init() *PPFDatabases { + // initialize main list + databases := PPFDatabases{[]ProtocolDatabaseProvider{}} + // 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"} + fdb := FileDatabase{path.Join(pathlist...), map[string]DatabaseEntry{}, false} + fdb.Open(path.Join(pathlist...)) + fdb.UpdateDatabase() + databases.Databases = append(databases.Databases, fdb) + log.Printf("Amount of databases available %d", len(databases.Databases)) + return &databases +} + +// Get Retrieves the full set of databases +func (ppfdb *PPFDatabases) Get() []ProtocolDatabaseProvider { + return ppfdb.Databases +} + // Get a full protocol from a database entry func (de *DatabaseEntry) Get() (*protocol.ProtocolStructure, error) { return protocolctl.LoadNew(de.Path) @@ -66,7 +89,7 @@ type FileDatabaseFromGitRemote struct { } // Open the database -func (fd *FileDatabase) Open(path string) error { +func (fd FileDatabase) Open(path string) error { fileinfo, err := os.Stat(path) if err == os.ErrNotExist { return err @@ -80,7 +103,7 @@ func (fd *FileDatabase) Open(path string) error { } // Close the database and free the data -func (fd *FileDatabase) Close() error { +func (fd FileDatabase) Close() error { for k := range fd.protocols { delete(fd.protocols, k) } @@ -89,7 +112,8 @@ func (fd *FileDatabase) Close() error { } // Protocols returns the map of protocols or an error if the first read failed -func (fd *FileDatabase) Protocols() (map[string]DatabaseEntry, error) { +func (fd FileDatabase) Protocols() ([]DatabaseEntry, error) { + entries := []DatabaseEntry{} if !fd.closed && len(fd.protocols) == 0 { fd.protocols = map[string]DatabaseEntry{} // recurse recursively through the path @@ -98,7 +122,10 @@ func (fd *FileDatabase) Protocols() (map[string]DatabaseEntry, error) { prot, err := protocolctl.LoadNew(path) if err == nil { // add to map - fd.protocols[prot.Metadata.Name] = DatabaseEntry{path, prot.Metadata} + entry := DatabaseEntry{path, prot.Metadata} + + fd.protocols[prot.Metadata.Name] = entry + entries = append(entries, entry) } } return nil @@ -107,7 +134,13 @@ func (fd *FileDatabase) Protocols() (map[string]DatabaseEntry, error) { return nil, err } } - return fd.protocols, nil + if entries == nil || len(entries) == 0 { + entries = make([]DatabaseEntry, len(fd.protocols)) + for _, v := range fd.protocols { + entries = append(entries, v) + } + } + return entries, nil } // Add puts a new protocol into the path of the file database @@ -120,20 +153,6 @@ func (fd *FileDatabase) Add(prot *protocol.ProtocolStructure) error { return err } -// AddFile copies a external protocoljson file into the database directory and updates the internal data -func (fd *FileDatabase) AddFile(p string) error { - prot, err := protocolctl.LoadNew(p) - if err == nil { - if !strings.HasPrefix(p, fd.Path) { - // add it to the database if not existing there - fd.Add(prot) - } - p = path.Join(fd.Path, prot.Metadata.Name, ".protocoljson") - fd.protocols[prot.Metadata.Name] = DatabaseEntry{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 *FileDatabase) Update(prot *protocol.ProtocolStructure) error { if v, ok := fd.protocols[prot.Metadata.Name]; ok { @@ -143,18 +162,6 @@ func (fd *FileDatabase) Update(prot *protocol.ProtocolStructure) error { return fd.Add(prot) } -// UpdateFile simply adds a file if it is outside the database, or updates the internal storage with the contents of the file -func (fd *FileDatabase) UpdateFile(p string) error { - if !strings.HasPrefix(p, fd.Path) { - return fd.AddFile(p) - } - prot, err := protocolctl.LoadNew(p) - if err == nil { - fd.protocols[prot.Metadata.Name] = DatabaseEntry{p, prot.Metadata} - } - return err -} - // UpdateDatabase just rereads all files func (fd *FileDatabase) UpdateDatabase() error { // recurse recursively through the path diff --git a/protocoldb/protocoldb_test.go b/protocoldb/protocoldb_test.go new file mode 100644 index 0000000..16e945f --- /dev/null +++ b/protocoldb/protocoldb_test.go @@ -0,0 +1,9 @@ +package protocoldb + +import ( + "testing" +) + +func Test(t *testing.T) { + +} diff --git a/test/protocoldb/testprotocolempty.protocoljson b/test/protocoldb/testprotocolempty.protocoljson new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/test/protocoldb/testprotocolempty.protocoljson @@ -0,0 +1 @@ +{} \ No newline at end of file