apkupdater

This commit is contained in:
HeshamTB 2023-09-16 00:53:53 +03:00
parent c36275d07b
commit fea50ca37e
3 changed files with 128 additions and 0 deletions

70
apkupdater/apkupdater.go Normal file
View File

@ -0,0 +1,70 @@
package apkupdater
import (
"fmt"
"log"
"sync"
"time"
)
type PackageUpdater interface {
Update()
Run()
Stop()
ValidPackages() bool
}
type AlpineLinuxPackageUpdate struct {
Interval time.Duration
Packages []string
doneChan chan string
apkLock *sync.Mutex
}
func (u *AlpineLinuxPackageUpdate) Update() {
u.apkLock.Lock()
log.Println("Updating packages...")
u.apkLock.Unlock()
log.Println("Done updating packges")
}
func (u *AlpineLinuxPackageUpdate) Run() {
go func() {
mainloop:
for {
select {
case <- u.doneChan:
fmt.Println("Stopping updater...")
break mainloop
case <- time.After(u.Interval):
u.Update()
}
}
fmt.Println("Updater stopped")
}()
log.Println("Started apline linux package updater")
}
func (u *AlpineLinuxPackageUpdate) ValidPackages() bool {
return true
}
func (u *AlpineLinuxPackageUpdate) Stop() {
u.doneChan <- ""
}
func NewAlpineLinuxPackageUpdater (
interval time.Duration,
packages []string,
) AlpineLinuxPackageUpdate {
return AlpineLinuxPackageUpdate{
Interval: interval,
Packages: packages,
doneChan: make(chan string),
apkLock: &sync.Mutex{},
}
}

View File

@ -0,0 +1,55 @@
package apkupdater
import (
"testing"
"time"
)
func TestNewUpdater(t *testing.T) {
interval := time.Second * 1
packages := []string{"yt-dlp", "ffmpeg"}
updater := NewAlpineLinuxPackageUpdater(
interval,
packages,
)
errs := make([]string, 0)
for _, p := range packages {
contains := false
inner:
for _, pp := range updater.Packages {
if p == pp {
contains = true
break inner
}
}
if !contains {
errs = append(errs, p + "Not found")
}
}
if len(errs) != 0 {
for _, e := range errs {
t.Log(e)
}
t.Fail()
}
}
func TestRunStop(t *testing.T) {
interval := time.Second * 1
packages := []string{"yt-dlp", "ffmpeg"}
updater := NewAlpineLinuxPackageUpdater(
interval,
packages,
)
updater.Run()
updater.Stop()
// TODO: Wie teste ich das?
}

3
apkupdater/go.mod Normal file
View File

@ -0,0 +1,3 @@
module gitea.hbanafa.com/hesham/viddl/apkupdater
go 1.21.1