diff --git a/apkupdater/apkupdater.go b/apkupdater/apkupdater.go new file mode 100644 index 0000000..a100f77 --- /dev/null +++ b/apkupdater/apkupdater.go @@ -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{}, + } +} + diff --git a/apkupdater/apkupdater_test.go b/apkupdater/apkupdater_test.go new file mode 100644 index 0000000..53b7775 --- /dev/null +++ b/apkupdater/apkupdater_test.go @@ -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? + +} diff --git a/apkupdater/go.mod b/apkupdater/go.mod new file mode 100644 index 0000000..b49948b --- /dev/null +++ b/apkupdater/go.mod @@ -0,0 +1,3 @@ +module gitea.hbanafa.com/hesham/viddl/apkupdater + +go 1.21.1