Skip to content

Commit

Permalink
refactor: separate system drivers in new style (#37)
Browse files Browse the repository at this point in the history
* refactor: move system updater-related logic to unified driver

* refactor: separate rpm-ostree and bootc drivers w/ new format

* fix: linting

* refactor: use interface for single system driver

* fix: proper number of steps for main system driver
  • Loading branch information
tulilirockz authored Dec 12, 2024
1 parent a0430fc commit 14f9233
Show file tree
Hide file tree
Showing 6 changed files with 286 additions and 226 deletions.
14 changes: 6 additions & 8 deletions cmd/imageOutdated.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
package cmd

import (
"log"
"log/slog"

"github.com/spf13/cobra"
"github.com/ublue-os/uupd/drv"
)

func ImageOutdated(cmd *cobra.Command, args []string) {
systemDriver, err := drv.GetSystemUpdateDriver()
systemUpdater, err := drv.SystemUpdater{}.New(drv.UpdaterInitConfiguration{})
if err != nil {
log.Fatalf("Failed to get system update driver: %v", err)
slog.Error("Failed getting system driver", slog.Any("error", err))
return
}
outdated, err := systemDriver.ImageOutdated()
if err != nil {
log.Fatalf("Cannot determine if image is outdated: %v", err)
}
log.Printf("%t", outdated)

println(systemUpdater.Outdated)
}
71 changes: 53 additions & 18 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,6 @@ func Update(cmd *cobra.Command, args []string) {
initConfiguration.DryRun = dryRun
initConfiguration.Verbose = verboseRun

systemUpdater, err := drv.SystemUpdater{}.New(*initConfiguration)
if err != nil {
systemUpdater.Config.Enabled = false
} else {
enableUpd, err := systemUpdater.Check()
if err != nil {
slog.Error("Failed checking for updates")
}
systemUpdater.Config.Enabled = enableUpd
if !enableUpd {
slog.Debug("No system update found, disabiling module")
}
}

brewUpdater, err := drv.BrewUpdater{}.New(*initConfiguration)
brewUpdater.Config.Enabled = err == nil

Expand All @@ -87,7 +73,49 @@ func Update(cmd *cobra.Command, args []string) {
distroboxUpdater.Config.Enabled = err == nil
distroboxUpdater.SetUsers(users)

totalSteps := brewUpdater.Steps() + systemUpdater.Steps() + flatpakUpdater.Steps() + distroboxUpdater.Steps()
var enableUpd bool = true
var systemOutdated bool

rpmOstreeUpdater, err := drv.RpmOstreeUpdater{}.New(*initConfiguration)
if err != nil {
enableUpd = false
}

systemUpdater, err := drv.SystemUpdater{}.New(*initConfiguration)
if err != nil {
enableUpd = false
}

isBootc, err := drv.BootcCompatible(systemUpdater.BinaryPath)
if err != nil {
isBootc = false
}

if !isBootc {
slog.Debug("Using rpm-ostree fallback as system driver")
}

systemUpdater.Config.Enabled = enableUpd && isBootc
rpmOstreeUpdater.Config.Enabled = enableUpd && !isBootc

var mainSystemDriver drv.SystemUpdateDriver = systemUpdater
if !systemUpdater.Config.Enabled {
mainSystemDriver = rpmOstreeUpdater
}

enableUpd, err = mainSystemDriver.Check()
if err != nil {
slog.Error("Failed checking for updates")
}

if !enableUpd {
slog.Debug("No system update found, disabiling module")
}

totalSteps := brewUpdater.Steps() + flatpakUpdater.Steps() + distroboxUpdater.Steps()
if enableUpd {
totalSteps += mainSystemDriver.Steps()
}
pw := lib.NewProgressWriter()
pw.SetNumTrackersExpected(1)
pw.SetAutoStop(false)
Expand Down Expand Up @@ -118,7 +146,13 @@ func Update(cmd *cobra.Command, args []string) {

var outputs = []drv.CommandOutput{}

if systemUpdater.Outdated {
systemOutdated, err = mainSystemDriver.Outdated()

if err != nil {
slog.Error("Failed checking if system is out of date")
}

if systemOutdated {
const OUTDATED_WARNING = "There hasn't been an update in over a month. Consider rebooting or running updates manually"
err := lib.Notify("System Warning", OUTDATED_WARNING)
if err != nil {
Expand All @@ -127,9 +161,10 @@ func Update(cmd *cobra.Command, args []string) {
slog.Warn(OUTDATED_WARNING)
}

if systemUpdater.Config.Enabled {
if enableUpd {
lib.ChangeTrackerMessageFancy(pw, tracker, progressEnabled, lib.TrackerMessage{Title: systemUpdater.Config.Title, Description: systemUpdater.Config.Description})
out, err := systemUpdater.Update()
var out *[]drv.CommandOutput
out, err = mainSystemDriver.Update()
outputs = append(outputs, *out...)
tracker.IncrementSection(err)
}
Expand Down
19 changes: 13 additions & 6 deletions cmd/updateCheck.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
package cmd

import (
"log/slog"

"github.com/spf13/cobra"
"github.com/ublue-os/uupd/drv"
"log"
)

func UpdateCheck(cmd *cobra.Command, args []string) {
systemDriver, err := drv.GetSystemUpdateDriver()
systemUpdater, err := drv.SystemUpdater{}.New(drv.UpdaterInitConfiguration{})
if err != nil {
log.Fatalf("Failed to get system update driver: %v", err)
slog.Error("Failed getting system driver", slog.Any("error", err))
return
}
update, err := systemDriver.UpdateAvailable()
updateAvailable, err := systemUpdater.Check()
if err != nil {
log.Fatalf("Failed to check for updates: %v", err)
slog.Error("Failed checking for updates", slog.Any("error", err))
return
}
if updateAvailable {
slog.Info("Update Available")
} else {
slog.Info("No updates available")
}
log.Printf("Update Available: %v", update)
}
157 changes: 0 additions & 157 deletions drv/bootc.go

This file was deleted.

Loading

0 comments on commit 14f9233

Please sign in to comment.