Add list to binarystore

This commit is contained in:
Torjus Håkestad 2021-12-08 06:17:11 +01:00
parent 4d719daf39
commit 75735c0e43
4 changed files with 61 additions and 2 deletions

View File

@ -4,6 +4,7 @@ import (
"crypto/ecdsa" "crypto/ecdsa"
"crypto/x509" "crypto/x509"
"fmt" "fmt"
"strings"
"gitea.benny.dog/torjus/ezshare/pb" "gitea.benny.dog/torjus/ezshare/pb"
"github.com/google/uuid" "github.com/google/uuid"
@ -303,7 +304,11 @@ func (s *BoltStore) GetUserByUsername(username string) (*pb.User, error) {
return nil, ErrNoSuchItem return nil, ErrNoSuchItem
} }
// BinaryStore /////////////////
// BinaryStore //
/////////////////
var _ BinaryStore = &BoltStore{}
func binaryKey(version string, os string, arch string) []byte { func binaryKey(version string, os string, arch string) []byte {
return []byte(fmt.Sprintf("%s,%s,%s", version, os, arch)) 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 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
}

View File

@ -223,7 +223,12 @@ func (s *MemoryStore) GetUserByUsername(username string) (*pb.User, error) {
return nil, ErrNoSuchItem return nil, ErrNoSuchItem
} }
// Binary store //////////////////
// Binary store //
//////////////////
var _ BinaryStore = &MemoryStore{}
func (s *MemoryStore) StoreBinary(release *pb.Binary) error { func (s *MemoryStore) StoreBinary(release *pb.Binary) error {
s.binaryLock.Lock() s.binaryLock.Lock()
defer s.binaryLock.Unlock() defer s.binaryLock.Unlock()
@ -255,3 +260,10 @@ func (s *MemoryStore) GetBinary(version string, os string, arch string) (*pb.Bin
return nil, ErrNoSuchItem 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
}

View File

@ -37,4 +37,5 @@ type UserStore interface {
type BinaryStore interface { type BinaryStore interface {
StoreBinary(release *pb.Binary) error StoreBinary(release *pb.Binary) error
GetBinary(version, os, arch string) (*pb.Binary, error) GetBinary(version, os, arch string) (*pb.Binary, error)
List() ([]string, error)
} }

View File

@ -248,4 +248,27 @@ func doBinaryStoreTests(s store.BinaryStore, t *testing.T) {
if string(windows.Data) != string(winBinary.Data) { if string(windows.Data) != string(winBinary.Data) {
t.Fatalf("Data is not the same in linux-binary") 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)
}
} }