Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add testing and refactor a ton of code + cleanups #40

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions cmd/imageOutdated.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,27 @@ import (
"log/slog"

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

func ImageOutdated(cmd *cobra.Command, args []string) {
systemUpdater, err := drv.SystemUpdater{}.New(drv.UpdaterInitConfiguration{})
initConfiguration := generic.UpdaterInitConfiguration{}.New()
initConfiguration.Ci = false
initConfiguration.DryRun = false
initConfiguration.Verbose = false

mainSystemDriver, _, _, err := system.InitializeSystemDriver(*initConfiguration)
if err != nil {
slog.Error("Failed getting system driver", slog.Any("error", err))
slog.Error("Failed")
return
}

println(systemUpdater.Outdated)
systemOutdated, err := mainSystemDriver.Outdated()

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

println(systemOutdated)
}
76 changes: 28 additions & 48 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,33 @@ import (
"github.com/jedib0t/go-pretty/v6/progress"
"github.com/spf13/cobra"
"github.com/ublue-os/uupd/checks"
"github.com/ublue-os/uupd/drv"
"github.com/ublue-os/uupd/drv/brew"
"github.com/ublue-os/uupd/drv/distrobox"
"github.com/ublue-os/uupd/drv/flatpak"
drv "github.com/ublue-os/uupd/drv/generic"
"github.com/ublue-os/uupd/drv/system"

"github.com/ublue-os/uupd/pkg/filelock"
"github.com/ublue-os/uupd/pkg/percent"
"github.com/ublue-os/uupd/pkg/session"
)

func Update(cmd *cobra.Command, args []string) {
lock, err := filelock.AcquireLock()
lockfile, err := filelock.OpenLockfile(filelock.GetDefaultLockfile())
if err != nil {
slog.Error(fmt.Sprintf("%v, is uupd already running?", err))
slog.Error("Failed creating and opening lockfile. Is uupd already running?", slog.Any("error", err))
return
}
defer func() {
err := filelock.ReleaseLock(lock)
defer func(lockfile *os.File) {
err := filelock.ReleaseLock(lockfile)
if err != nil {
slog.Error("Failed releasing lock")
slog.Error("Failed releasing lock", slog.Any("error", err))
}
}()
}(lockfile)
if err := filelock.AcquireLock(lockfile, filelock.TimeoutConfig{Tries: 5}); err != nil {
slog.Error(fmt.Sprintf("%v, is uupd already running?", err))
return
}

hwCheck, err := cmd.Flags().GetBool("hw-check")
if err != nil {
Expand Down Expand Up @@ -64,56 +73,29 @@ func Update(cmd *cobra.Command, args []string) {
initConfiguration.DryRun = dryRun
initConfiguration.Verbose = verboseRun

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

flatpakUpdater, err := drv.FlatpakUpdater{}.New(*initConfiguration)
flatpakUpdater, err := flatpak.FlatpakUpdater{}.New(*initConfiguration)
flatpakUpdater.Config.Enabled = err == nil
flatpakUpdater.SetUsers(users)

distroboxUpdater, err := drv.DistroboxUpdater{}.New(*initConfiguration)
distroboxUpdater, err := distrobox.DistroboxUpdater{}.New(*initConfiguration)
distroboxUpdater.Config.Enabled = err == nil
distroboxUpdater.SetUsers(users)

var enableUpd bool = true

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 = isBootc && enableUpd
rpmOstreeUpdater.Config.Enabled = !isBootc && enableUpd

// The system driver to be applied needs to have the correct "enabled" value since it will NOT update from here onwards.
var mainSystemDriver drv.SystemUpdateDriver = &systemUpdater
if !isBootc {
mainSystemDriver = &rpmOstreeUpdater
}
mainSystemDriver, mainSystemDriverConfig, _, _ := system.InitializeSystemDriver(*initConfiguration)

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

slog.Debug("System Updater module status", slog.Bool("enabled", enableUpd))
slog.Debug("System Updater module status", slog.Bool("enabled", mainSystemDriverConfig.Enabled))

totalSteps := brewUpdater.Steps() + flatpakUpdater.Steps() + distroboxUpdater.Steps()
if enableUpd {
if mainSystemDriverConfig.Enabled {
totalSteps += mainSystemDriver.Steps()
}
pw := percent.NewProgressWriter()
Expand Down Expand Up @@ -155,7 +137,7 @@ func Update(cmd *cobra.Command, args []string) {

if systemOutdated {
const OUTDATED_WARNING = "There hasn't been an update in over a month. Consider rebooting or running updates manually"
err := session.Notify("System Warning", OUTDATED_WARNING)
err := session.Notify(users, "System Warning", OUTDATED_WARNING)
if err != nil {
slog.Error("Failed showing warning notification")
}
Expand All @@ -165,9 +147,9 @@ func Update(cmd *cobra.Command, args []string) {
// This section is ugly but we cant really do much about it.
// Using interfaces doesn't preserve the "Config" struct state and I dont know any other way to make this work without cursed workarounds.

if enableUpd {
slog.Debug(fmt.Sprintf("%s module", systemUpdater.Config.Title), slog.String("module_name", systemUpdater.Config.Title), slog.Any("module_configuration", systemUpdater.Config))
percent.ChangeTrackerMessageFancy(pw, tracker, progressEnabled, percent.TrackerMessage{Title: systemUpdater.Config.Title, Description: systemUpdater.Config.Description})
if mainSystemDriverConfig.Enabled {
slog.Debug(fmt.Sprintf("%s module", mainSystemDriverConfig.Title), slog.String("module_name", mainSystemDriverConfig.Title), slog.Any("module_configuration", mainSystemDriverConfig))
percent.ChangeTrackerMessageFancy(pw, tracker, progressEnabled, percent.TrackerMessage{Title: mainSystemDriverConfig.Title, Description: mainSystemDriverConfig.Description})
var out *[]drv.CommandOutput
out, err = mainSystemDriver.Update()
outputs = append(outputs, *out...)
Expand All @@ -185,7 +167,6 @@ func Update(cmd *cobra.Command, args []string) {

if flatpakUpdater.Config.Enabled {
slog.Debug(fmt.Sprintf("%s module", flatpakUpdater.Config.Title), slog.String("module_name", flatpakUpdater.Config.Title), slog.Any("module_configuration", flatpakUpdater.Config))
percent.ChangeTrackerMessageFancy(pw, tracker, progressEnabled, percent.TrackerMessage{Title: flatpakUpdater.Config.Title, Description: flatpakUpdater.Config.Description})
var out *[]drv.CommandOutput
out, err = flatpakUpdater.Update()
outputs = append(outputs, *out...)
Expand All @@ -194,7 +175,6 @@ func Update(cmd *cobra.Command, args []string) {

if distroboxUpdater.Config.Enabled {
slog.Debug(fmt.Sprintf("%s module", distroboxUpdater.Config.Title), slog.String("module_name", distroboxUpdater.Config.Title), slog.Any("module_configuration", distroboxUpdater.Config))
percent.ChangeTrackerMessageFancy(pw, tracker, progressEnabled, percent.TrackerMessage{Title: distroboxUpdater.Config.Title, Description: distroboxUpdater.Config.Description})
var out *[]drv.CommandOutput
out, err = distroboxUpdater.Update()
outputs = append(outputs, *out...)
Expand Down
40 changes: 9 additions & 31 deletions cmd/updateCheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,20 @@ import (
"log/slog"

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

func UpdateCheck(cmd *cobra.Command, args []string) {
var enableUpd bool = true
initConfiguration := generic.UpdaterInitConfiguration{}.New()
initConfiguration.Ci = false
initConfiguration.DryRun = false
initConfiguration.Verbose = false

initConfiguration := drv.UpdaterInitConfiguration{}.New()
rpmOstreeUpdater, err := drv.RpmOstreeUpdater{}.New(*initConfiguration)
mainSystemDriver, _, _, err := system.InitializeSystemDriver(*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 = isBootc && enableUpd
rpmOstreeUpdater.Config.Enabled = !isBootc && enableUpd

var mainSystemDriver drv.SystemUpdateDriver
if !isBootc {
slog.Debug("Using the rpm-ostree driver")
mainSystemDriver = &rpmOstreeUpdater
} else {
slog.Debug("Using the bootc driver")
mainSystemDriver = &systemUpdater
slog.Error("Failed")
return
}

updateAvailable, err := mainSystemDriver.Check()
Expand Down
50 changes: 12 additions & 38 deletions drv/brew.go → drv/brew/brew.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package drv
package brew

import (
"fmt"
Expand All @@ -7,6 +7,7 @@ import (
"strings"
"syscall"

. "github.com/ublue-os/uupd/drv/generic"
"github.com/ublue-os/uupd/pkg/session"
)

Expand Down Expand Up @@ -45,19 +46,20 @@ func (up BrewUpdater) Update() (*[]CommandOutput, error) {
}

cli := []string{up.BrewPath, "update"}
out, err := session.RunUID(up.Config.logger, slog.LevelDebug, up.BaseUser, cli, up.Config.Environment)
out, err := session.RunUID(up.Config.Logger, slog.LevelDebug, up.BaseUser, cli, up.Config.Environment)
tmpout := CommandOutput{}.New(out, err)
tmpout.Context = "Brew Update"
tmpout.Cli = cli
tmpout.Failure = err != nil
if err != nil {
tmpout.SetFailureContext("Brew update")
if failure := err != nil; failure {
tmpout.Failure = failure
tmpout.Context = "Brew Update"
final_output = append(final_output, *tmpout)
return &final_output, err
}

cli = []string{up.BrewPath, "upgrade"}
out, err = session.RunUID(up.Config.logger, slog.LevelDebug, up.BaseUser, cli, up.Config.Environment)
out, err = session.RunUID(up.Config.Logger, slog.LevelDebug, up.BaseUser, cli, up.Config.Environment)
tmpout = CommandOutput{}.New(out, err)
tmpout.Context = "Brew Upgrade"
tmpout.Cli = cli
Expand All @@ -84,32 +86,12 @@ func (up BrewUpdater) New(config UpdaterInitConfiguration) (BrewUpdater, error)
DryRun: config.DryRun,
Environment: config.Environment,
}
up.Config.logger = config.Logger.With(slog.String("module", strings.ToLower(up.Config.Title)))
up.Config.Logger = config.Logger.With(slog.String("module", strings.ToLower(up.Config.Title)))

brewPrefix, exists := up.Config.Environment["HOMEBREW_PREFIX"]
if !exists || brewPrefix == "" {
up.BrewPrefix = "/home/linuxbrew/.linuxbrew"
} else {
up.BrewPrefix = brewPrefix
}
brewRepo, exists := up.Config.Environment["HOMEBREW_REPOSITORY"]
if !exists || brewRepo == "" {
up.BrewRepo = fmt.Sprintf("%s/Homebrew", up.BrewPrefix)
} else {
up.BrewRepo = brewRepo
}
brewCellar, exists := up.Config.Environment["HOMEBREW_CELLAR"]
if !exists || brewCellar == "" {
up.BrewCellar = fmt.Sprintf("%s/Cellar", up.BrewPrefix)
} else {
up.BrewCellar = brewCellar
}
brewPath, exists := up.Config.Environment["HOMEBREW_PATH"]
if !exists || brewPath == "" {
up.BrewPath = fmt.Sprintf("%s/bin/brew", up.BrewPrefix)
} else {
up.BrewPath = brewPath
}
up.BrewPrefix = EnvOrFallback(up.Config.Environment, "HOMEBREW_PREFIX", "/home/linuxbrew/.linuxbrew")
up.BrewPrefix = EnvOrFallback(up.Config.Environment, "HOMEBREW_REPOSITORY", fmt.Sprintf("%s/Homebrew", up.BrewPrefix))
up.BrewPrefix = EnvOrFallback(up.Config.Environment, "HOMEBREW_CELLAR", fmt.Sprintf("%s/Cellar", up.BrewPrefix))
up.BrewPrefix = EnvOrFallback(up.Config.Environment, "HOMEBREW_PATH", fmt.Sprintf("%s/bin/brew", up.BrewPrefix))

if up.Config.DryRun {
return up, nil
Expand All @@ -123,11 +105,3 @@ func (up BrewUpdater) New(config UpdaterInitConfiguration) (BrewUpdater, error)

return up, nil
}

func (up *BrewUpdater) Logger() *slog.Logger {
return up.Config.logger
}

func (up *BrewUpdater) SetLogger(logger *slog.Logger) {
up.Config.logger = logger
}
35 changes: 35 additions & 0 deletions drv/brew/brew_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package brew_test

import (
"testing"

"github.com/ublue-os/uupd/drv/brew"
"github.com/ublue-os/uupd/drv/generic"
appLogging "github.com/ublue-os/uupd/pkg/logging"
)

func InitBaseConfig() brew.BrewUpdater {
var initConfiguration = generic.UpdaterInitConfiguration{
DryRun: false,
Ci: false,
Verbose: false,
Environment: nil,
Logger: appLogging.NewMuteLogger(),
}
driv, _ := brew.BrewUpdater{}.New(initConfiguration)
return driv
}

func TestProperSteps(t *testing.T) {
brewUpdater := InitBaseConfig()
brewUpdater.Config.Enabled = false

if brewUpdater.Steps() != 0 {
t.Fatalf("Expected no steps when module is disabled")
}

brewUpdater.Config.Enabled = true
if brewUpdater.Steps() == 0 {
t.Fatalf("Expected steps to be added")
}
}
Loading