Skip to content

Commit

Permalink
feat: add proper logging + debug logs everywhere (#41)
Browse files Browse the repository at this point in the history
* feat: add logging support for all modules + ton of logs

* fix: make updateCheck work with bootc and rpm-ostree
  • Loading branch information
tulilirockz authored Dec 21, 2024
1 parent 9b5bd00 commit 6fd108a
Show file tree
Hide file tree
Showing 13 changed files with 225 additions and 94 deletions.
4 changes: 3 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ var (
fLogFile string
fLogLevel string
fNoLogging bool
fLogJson bool
)

func Execute() {
Expand Down Expand Up @@ -93,7 +94,7 @@ func initLogging(cmd *cobra.Command, args []string) error {
return err
}

main_app_logger := slog.New(appLogging.SetupAppLogger(logWriter, logLevel, fLogFile != "-"))
main_app_logger := slog.New(appLogging.SetupAppLogger(logWriter, logLevel, fLogFile != "-" || fLogJson))

if fNoLogging {
slog.SetDefault(appLogging.NewMuteLogger())
Expand All @@ -113,6 +114,7 @@ func init() {
rootCmd.Flags().BoolP("verbose", "v", false, "Display command outputs after run")
rootCmd.Flags().Bool("ci", false, "Makes some modifications to behavior if is running in CI")

rootCmd.PersistentFlags().BoolVar(&fLogJson, "json", false, "Print logs as json (used for testing)")
rootCmd.PersistentFlags().StringVar(&fLogFile, "log-file", "-", "File where user-facing logs will be written to")
rootCmd.PersistentFlags().StringVar(&fLogLevel, "log-level", "info", "Log level for user-facing logs")
rootCmd.PersistentFlags().BoolVar(&fNoLogging, "quiet", false, "Make logs quiet")
Expand Down
40 changes: 25 additions & 15 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ func Update(cmd *cobra.Command, args []string) {
distroboxUpdater.SetUsers(users)

var enableUpd bool = true
var systemOutdated bool

rpmOstreeUpdater, err := drv.RpmOstreeUpdater{}.New(*initConfiguration)
if err != nil {
Expand All @@ -97,22 +96,21 @@ func Update(cmd *cobra.Command, args []string) {
slog.Debug("Using rpm-ostree fallback as system driver")
}

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

var mainSystemDriver drv.SystemUpdateDriver = systemUpdater
if !systemUpdater.Config.Enabled {
mainSystemDriver = rpmOstreeUpdater
// 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
}

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

if !enableUpd {
slog.Debug("No system update found, disabiling module")
}
slog.Debug("System Updater module status", slog.Bool("enabled", enableUpd))

totalSteps := brewUpdater.Steps() + flatpakUpdater.Steps() + distroboxUpdater.Steps()
if enableUpd {
Expand Down Expand Up @@ -149,7 +147,7 @@ func Update(cmd *cobra.Command, args []string) {

var outputs = []drv.CommandOutput{}

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

if err != nil {
slog.Error("Failed checking if system is out of date")
Expand All @@ -164,7 +162,11 @@ func Update(cmd *cobra.Command, args []string) {
slog.Warn(OUTDATED_WARNING)
}

// 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})
var out *[]drv.CommandOutput
out, err = mainSystemDriver.Update()
Expand All @@ -173,20 +175,28 @@ func Update(cmd *cobra.Command, args []string) {
}

if brewUpdater.Config.Enabled {
slog.Debug(fmt.Sprintf("%s module", brewUpdater.Config.Title), slog.String("module_name", brewUpdater.Config.Title), slog.Any("module_configuration", brewUpdater.Config))
percent.ChangeTrackerMessageFancy(pw, tracker, progressEnabled, percent.TrackerMessage{Title: brewUpdater.Config.Title, Description: brewUpdater.Config.Description})
out, err := brewUpdater.Update()
var out *[]drv.CommandOutput
out, err = brewUpdater.Update()
outputs = append(outputs, *out...)
tracker.IncrementSection(err)
}

if flatpakUpdater.Config.Enabled {
out, err := flatpakUpdater.Update()
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...)
tracker.IncrementSection(err)
}

if distroboxUpdater.Config.Enabled {
out, err := distroboxUpdater.Update()
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...)
tracker.IncrementSection(err)
}
Expand All @@ -199,7 +209,7 @@ func Update(cmd *cobra.Command, args []string) {
slog.Info("Verbose run requested")

for _, output := range outputs {
slog.Info(output.Context, slog.String("stdout", output.Stdout), slog.Any("stderr", output.Stderr), slog.Any("cli", output.Cli))
slog.Info(output.Context, slog.Any("output", output))
}

return
Expand All @@ -216,7 +226,7 @@ func Update(cmd *cobra.Command, args []string) {
slog.Warn("Exited with failed updates.")

for _, output := range failures {
slog.Info(output.Context, slog.String("stdout", output.Stdout), slog.Any("stderr", output.Stderr), slog.Any("cli", output.Cli))
slog.Info(output.Context, slog.Any("output", output))
}

return
Expand Down
37 changes: 33 additions & 4 deletions cmd/updateCheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,41 @@ import (
)

func UpdateCheck(cmd *cobra.Command, args []string) {
systemUpdater, err := drv.SystemUpdater{}.New(drv.UpdaterInitConfiguration{})
var enableUpd bool = true

initConfiguration := drv.UpdaterInitConfiguration{}.New()
rpmOstreeUpdater, err := drv.RpmOstreeUpdater{}.New(*initConfiguration)
if err != nil {
slog.Error("Failed getting system driver", slog.Any("error", err))
return
enableUpd = false
}

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

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

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
}

updateAvailable, err := mainSystemDriver.Check()
if err != nil {
slog.Error("Failed checking for updates", slog.Any("error", err))
return
Expand Down
21 changes: 15 additions & 6 deletions drv/brew.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package drv

import (
"fmt"
"log/slog"
"os"
"strings"
"syscall"

"github.com/ublue-os/uupd/pkg/session"
Expand Down Expand Up @@ -31,9 +33,8 @@ func (up BrewUpdater) Steps() int {
return 0
}

func (up BrewUpdater) Check() (*[]CommandOutput, error) {
// TODO: implement
return nil, nil
func (up BrewUpdater) Check() (bool, error) {
return true, nil
}

func (up BrewUpdater) Update() (*[]CommandOutput, error) {
Expand All @@ -44,7 +45,7 @@ func (up BrewUpdater) Update() (*[]CommandOutput, error) {
}

cli := []string{up.BrewPath, "update"}
out, err := session.RunUID(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
Expand All @@ -56,7 +57,7 @@ func (up BrewUpdater) Update() (*[]CommandOutput, error) {
}

cli = []string{up.BrewPath, "upgrade"}
out, err = session.RunUID(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 @@ -75,7 +76,6 @@ type BrewUpdater struct {
}

func (up BrewUpdater) New(config UpdaterInitConfiguration) (BrewUpdater, error) {

up.Config = DriverConfiguration{
Title: "Brew",
Description: "CLI Apps",
Expand All @@ -84,6 +84,7 @@ 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)))

brewPrefix, exists := up.Config.Environment["HOMEBREW_PREFIX"]
if !exists || brewPrefix == "" {
Expand Down Expand Up @@ -122,3 +123,11 @@ 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
}
22 changes: 17 additions & 5 deletions drv/distrobox.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package drv

import (
"log/slog"
"strings"

"github.com/ublue-os/uupd/pkg/percent"
"github.com/ublue-os/uupd/pkg/session"
)
Expand Down Expand Up @@ -35,6 +38,7 @@ func (up DistroboxUpdater) New(config UpdaterInitConfiguration) (DistroboxUpdate
DryRun: config.DryRun,
Environment: config.Environment,
}
up.Config.logger = config.Logger.With(slog.String("module", strings.ToLower(up.Config.Title)))
up.usersEnabled = false
up.Tracker = nil

Expand All @@ -53,11 +57,11 @@ func (up *DistroboxUpdater) SetUsers(users []session.User) {
up.usersEnabled = true
}

func (up DistroboxUpdater) Check() (*[]CommandOutput, error) {
return nil, nil
func (up DistroboxUpdater) Check() (bool, error) {
return true, nil
}

func (up *DistroboxUpdater) Update() (*[]CommandOutput, error) {
func (up DistroboxUpdater) Update() (*[]CommandOutput, error) {
var finalOutput = []CommandOutput{}

if up.Config.DryRun {
Expand All @@ -74,7 +78,7 @@ func (up *DistroboxUpdater) Update() (*[]CommandOutput, error) {

percent.ChangeTrackerMessageFancy(*up.Tracker.Writer, up.Tracker.Tracker, up.Tracker.Progress, percent.TrackerMessage{Title: up.Config.Title, Description: up.Config.Description})
cli := []string{up.binaryPath, "upgrade", "-a"}
out, err := session.RunUID(0, cli, nil)
out, err := session.RunUID(up.Config.logger, slog.LevelDebug, 0, cli, nil)
tmpout := CommandOutput{}.New(out, err)
tmpout.Context = up.Config.Description
tmpout.Cli = cli
Expand All @@ -87,7 +91,7 @@ func (up *DistroboxUpdater) Update() (*[]CommandOutput, error) {
context := *up.Config.UserDescription + " " + user.Name
percent.ChangeTrackerMessageFancy(*up.Tracker.Writer, up.Tracker.Tracker, up.Tracker.Progress, percent.TrackerMessage{Title: up.Config.Title, Description: *up.Config.UserDescription + " " + user.Name})
cli := []string{up.binaryPath, "upgrade", "-a"}
out, err := session.RunUID(user.UID, cli, nil)
out, err := session.RunUID(up.Config.logger, slog.LevelDebug, user.UID, cli, nil)
tmpout = CommandOutput{}.New(out, err)
tmpout.Context = context
tmpout.Cli = cli
Expand All @@ -96,3 +100,11 @@ func (up *DistroboxUpdater) Update() (*[]CommandOutput, error) {
}
return &finalOutput, nil
}

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

func (up *DistroboxUpdater) SetLogger(logger *slog.Logger) {
up.Config.logger = logger
}
21 changes: 16 additions & 5 deletions drv/flatpak.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package drv

import (
"log/slog"
"os/exec"
"strings"

"github.com/ublue-os/uupd/pkg/percent"
"github.com/ublue-os/uupd/pkg/session"
Expand Down Expand Up @@ -37,6 +39,7 @@ func (up FlatpakUpdater) New(config UpdaterInitConfiguration) (FlatpakUpdater, e
DryRun: config.DryRun,
Environment: config.Environment,
}
up.Config.logger = config.Logger.With(slog.String("module", strings.ToLower(up.Config.Title)))
up.usersEnabled = false
up.Tracker = nil

Expand All @@ -55,8 +58,8 @@ func (up *FlatpakUpdater) SetUsers(users []session.User) {
up.usersEnabled = true
}

func (up FlatpakUpdater) Check() (*[]CommandOutput, error) {
return nil, nil
func (up FlatpakUpdater) Check() (bool, error) {
return true, nil
}

func (up FlatpakUpdater) Update() (*[]CommandOutput, error) {
Expand All @@ -75,9 +78,9 @@ func (up FlatpakUpdater) Update() (*[]CommandOutput, error) {
}

percent.ChangeTrackerMessageFancy(*up.Tracker.Writer, up.Tracker.Tracker, up.Tracker.Progress, percent.TrackerMessage{Title: up.Config.Title, Description: up.Config.Description})
cli := []string{up.binaryPath, "update", "-y"}
cli := []string{up.binaryPath, "update", "-y", "--noninteractive"}
flatpakCmd := exec.Command(cli[0], cli[1:]...)
out, err := flatpakCmd.CombinedOutput()
out, err := session.RunLog(up.Config.logger, slog.LevelDebug, flatpakCmd)
tmpout := CommandOutput{}.New(out, err)
tmpout.Context = up.Config.Description
tmpout.Cli = cli
Expand All @@ -90,7 +93,7 @@ func (up FlatpakUpdater) Update() (*[]CommandOutput, error) {
context := *up.Config.UserDescription + " " + user.Name
percent.ChangeTrackerMessageFancy(*up.Tracker.Writer, up.Tracker.Tracker, up.Tracker.Progress, percent.TrackerMessage{Title: up.Config.Title, Description: context})
cli := []string{up.binaryPath, "update", "-y"}
out, err := session.RunUID(user.UID, cli, nil)
out, err := session.RunUID(up.Config.logger, slog.LevelDebug, user.UID, cli, nil)
tmpout = CommandOutput{}.New(out, err)
tmpout.Context = context
tmpout.Cli = cli
Expand All @@ -99,3 +102,11 @@ func (up FlatpakUpdater) Update() (*[]CommandOutput, error) {
}
return &finalOutput, nil
}

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

func (up *FlatpakUpdater) SetLogger(logger *slog.Logger) {
up.Config.logger = logger
}
Loading

0 comments on commit 6fd108a

Please sign in to comment.