code refactoring

This commit is contained in:
2024-04-10 12:02:04 +02:00
parent 566f0f2ac2
commit 2f02293a52
6 changed files with 16 additions and 57 deletions

View File

@@ -3,7 +3,6 @@ package internal
import (
"encoding/gob"
"errors"
"fmt"
"log/slog"
"os"
"path/filepath"
@@ -35,33 +34,6 @@ func (m *MemoryDB) Set(process *Process) string {
return id
}
// Update a process info/metadata, given the process id
//
// Deprecated: will be removed anytime soon.
func (m *MemoryDB) UpdateInfo(id string, info DownloadInfo) error {
entry, ok := m.table.Load(id)
if ok {
entry.(*Process).Info = info
m.table.Store(id, entry)
return nil
}
return fmt.Errorf("can't update row with id %s", id)
}
// Update a process progress data, given the process id
// Used for updating completition percentage or ETA.
//
// Deprecated: will be removed anytime soon.
func (m *MemoryDB) UpdateProgress(id string, progress DownloadProgress) error {
entry, ok := m.table.Load(id)
if ok {
entry.(*Process).Progress = progress
m.table.Store(id, entry)
return nil
}
return fmt.Errorf("can't update row with id %s", id)
}
// Removes a process progress, given the process id
func (m *MemoryDB) Delete(id string) {
m.table.Delete(id)
@@ -92,7 +64,7 @@ func (m *MemoryDB) All() *[]ProcessResponse {
return &running
}
// WIP: Persist the database in a single file named "session.dat"
// Persist the database in a single file named "session.dat"
func (m *MemoryDB) Persist() error {
running := m.All()
@@ -115,17 +87,16 @@ func (m *MemoryDB) Persist() error {
return nil
}
// WIP: Restore a persisted state
// Restore a persisted state
func (m *MemoryDB) Restore(logger *slog.Logger) {
fd, err := os.Open("session.dat")
if err != nil {
return
}
session := Session{}
var session Session
err = gob.NewDecoder(fd).Decode(&session)
if err != nil {
if err := gob.NewDecoder(fd).Decode(&session); err != nil {
return
}