From 75735c0e437630c01f716613da27332a6922df6c Mon Sep 17 00:00:00 2001 From: = Date: Wed, 8 Dec 2021 06:17:11 +0100 Subject: [PATCH] Add list to binarystore --- store/bolt.go | 25 ++++++++++++++++++++++++- store/memory.go | 14 +++++++++++++- store/store.go | 1 + store/store_test.go | 23 +++++++++++++++++++++++ 4 files changed, 61 insertions(+), 2 deletions(-) diff --git a/store/bolt.go b/store/bolt.go index 7a43956..0d615b5 100644 --- a/store/bolt.go +++ b/store/bolt.go @@ -4,6 +4,7 @@ import ( "crypto/ecdsa" "crypto/x509" "fmt" + "strings" "gitea.benny.dog/torjus/ezshare/pb" "github.com/google/uuid" @@ -303,7 +304,11 @@ func (s *BoltStore) GetUserByUsername(username string) (*pb.User, error) { return nil, ErrNoSuchItem } -// BinaryStore +///////////////// +// BinaryStore // +///////////////// + +var _ BinaryStore = &BoltStore{} func binaryKey(version string, os string, arch string) []byte { return []byte(fmt.Sprintf("%s,%s,%s", version, os, arch)) @@ -334,3 +339,21 @@ func (s *BoltStore) GetBinary(version string, os string, arch string) (*pb.Binar return binary, nil } + +func (s *BoltStore) List() ([]string, error) { + var releases []string + err := s.db.View(func(tx *bolt.Tx) error { + bkt := tx.Bucket(bktKeyBinary) + + return bkt.ForEach(func(k, _ []byte) error { + key := strings.Split(string(k), ",") + releases = append(releases, fmt.Sprintf("ezshare-%s-%s-%s", key[0][1:], key[1], key[2])) + return nil + }) + }) + if err != nil { + return nil, err + } + + return releases, nil +} diff --git a/store/memory.go b/store/memory.go index 137a37b..cd3c410 100644 --- a/store/memory.go +++ b/store/memory.go @@ -223,7 +223,12 @@ func (s *MemoryStore) GetUserByUsername(username string) (*pb.User, error) { return nil, ErrNoSuchItem } -// Binary store +////////////////// +// Binary store // +////////////////// + +var _ BinaryStore = &MemoryStore{} + func (s *MemoryStore) StoreBinary(release *pb.Binary) error { s.binaryLock.Lock() defer s.binaryLock.Unlock() @@ -255,3 +260,10 @@ func (s *MemoryStore) GetBinary(version string, os string, arch string) (*pb.Bin return nil, ErrNoSuchItem } +func (s *MemoryStore) List() ([]string, error) { + var releases []string + for _, release := range s.binaries { + releases = append(releases, fmt.Sprintf("ezshare-%s-%s-%s", release.Version[1:], release.Os, release.Arch)) + } + return releases, nil +} diff --git a/store/store.go b/store/store.go index 9f0d31a..b4d5e48 100644 --- a/store/store.go +++ b/store/store.go @@ -37,4 +37,5 @@ type UserStore interface { type BinaryStore interface { StoreBinary(release *pb.Binary) error GetBinary(version, os, arch string) (*pb.Binary, error) + List() ([]string, error) } diff --git a/store/store_test.go b/store/store_test.go index da2ee7f..3df0956 100644 --- a/store/store_test.go +++ b/store/store_test.go @@ -248,4 +248,27 @@ func doBinaryStoreTests(s store.BinaryStore, t *testing.T) { if string(windows.Data) != string(winBinary.Data) { t.Fatalf("Data is not the same in linux-binary") } + + // List + list, err := s.List() + if err != nil { + t.Fatalf("List returned error: %s", err) + } + + var lFound, wFound bool + for _, item := range list { + if item == "ezshare-1.0.0-linux-arm" { + lFound = true + } + if item == "ezshare-1.0.0-windows-amd64" { + wFound = true + } + } + + if !lFound { + t.Fatalf("Linux binary not in list. %+v", list) + } + if !wFound { + t.Fatalf("Windows binary not in list. %+v", list) + } }