Skip to content

Commit

Permalink
feat: add env support + verbose logging to all drivers
Browse files Browse the repository at this point in the history
  • Loading branch information
tulilirockz committed Dec 5, 2024
1 parent a28288e commit f6c1895
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 80 deletions.
4 changes: 2 additions & 2 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func Update(cmd *cobra.Command, args []string) {
slog.Info("Verbose run requested")

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

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

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

return
Expand Down
62 changes: 37 additions & 25 deletions drv/brew.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ import (
)

func (up BrewUpdater) GetBrewUID() (int, error) {
inf, err := os.Stat(up.Environment["HOMEBREW_PREFIX"])
inf, err := os.Stat(up.BrewPrefix)
if err != nil {
return -1, err
}

if !inf.IsDir() {
return -1, fmt.Errorf("Brew prefix: %v, is not a dir.", up.Environment["HOMEBREW_PREFIX"])
return -1, fmt.Errorf("Brew prefix: %v, is not a dir.", up.BrewPrefix)
}
stat, ok := inf.Sys().(*syscall.Stat_t)
if !ok {
return -1, fmt.Errorf("Unable to retriev UID info for %v", up.Environment["HOMEBREW_PREFIX"])
return -1, fmt.Errorf("Unable to retriev UID info for %v", up.BrewPrefix)
}
return int(stat.Uid), nil
}
Expand All @@ -42,52 +42,64 @@ func (up BrewUpdater) Update() (*[]CommandOutput, error) {
return &final_output, nil
}

out, err := lib.RunUID(up.BaseUser, []string{up.Environment["HOMEBREW_PATH"], "update"}, up.Environment)
cli := []string{up.BrewPath, "update"}
out, err := lib.RunUID(up.BaseUser, cli, up.Environment)
tmpout := CommandOutput{}.New(out, err)
tmpout.Context = "Brew Update"
tmpout.Cli = cli
tmpout.Failure = err != nil
if err != nil {
tmpout.SetFailureContext("Brew update")
final_output = append(final_output, *tmpout)
return &final_output, err
}

out, err = lib.RunUID(up.BaseUser, []string{up.Environment["HOMEBREW_PATH"], "upgrade"}, up.Environment)
cli = []string{up.BrewPath, "upgrade"}
out, err = lib.RunUID(up.BaseUser, cli, up.Environment)
tmpout = CommandOutput{}.New(out, err)
if err != nil {
tmpout.SetFailureContext("Brew upgrade")
}
tmpout.Context = "Brew Upgrade"
tmpout.Cli = cli
tmpout.Failure = err != nil
final_output = append(final_output, *tmpout)
return &final_output, err
}

type BrewUpdater struct {
Config DriverConfiguration
BaseUser int
Environment map[string]string
Environment EnvironmentMap
BrewRepo string
BrewPrefix string
BrewCellar string
BrewPath string
}

func (up BrewUpdater) New(config UpdaterInitConfiguration) (BrewUpdater, error) {
brewPrefix, empty := os.LookupEnv("HOMEBREW_PREFIX")
up.Environment = config.Environment
brewPrefix, empty := up.Environment["HOMEBREW_PREFIX"]
if empty || brewPrefix == "" {
brewPrefix = "/home/linuxbrew/.linuxbrew"
up.BrewPrefix = "/home/linuxbrew/.linuxbrew"
} else {
up.BrewPrefix = brewPrefix
}
brewRepo, empty := os.LookupEnv("HOMEBREW_REPOSITORY")
brewRepo, empty := up.Environment["HOMEBREW_REPOSITORY"]
if empty || brewRepo == "" {
brewRepo = fmt.Sprintf("%s/Homebrew", brewPrefix)
up.BrewRepo = fmt.Sprintf("%s/Homebrew", up.BrewPrefix)
} else {
up.BrewRepo = brewRepo
}
brewCellar, empty := os.LookupEnv("HOMEBREW_CELLAR")
brewCellar, empty := up.Environment["HOMEBREW_CELLAR"]
if empty || brewCellar == "" {
brewCellar = fmt.Sprintf("%s/Cellar", brewPrefix)
up.BrewCellar = fmt.Sprintf("%s/Cellar", up.BrewPrefix)
} else {

up.BrewCellar = brewCellar
}
brewPath, empty := os.LookupEnv("HOMEBREW_PATH")
brewPath, empty := up.Environment["HOMEBREW_PATH"]
if empty || brewPath == "" {
brewPath = fmt.Sprintf("%s/bin/brew", brewPrefix)
}

up.Environment = map[string]string{
"HOMEBREW_PREFIX": brewPrefix,
"HOMEBREW_REPOSITORY": brewRepo,
"HOMEBREW_CELLAR": brewCellar,
"HOMEBREW_PATH": brewPath,
up.BrewPath = fmt.Sprintf("%s/bin/brew", up.BrewPrefix)
} else {
up.BrewPath = brewPath
}
up.Config = DriverConfiguration{
Title: "Brew",
Expand All @@ -105,7 +117,7 @@ func (up BrewUpdater) New(config UpdaterInitConfiguration) (BrewUpdater, error)
if err != nil {
return up, err
}

up.BaseUser = uid

return up, nil
}
34 changes: 21 additions & 13 deletions drv/distrobox.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package drv

import (
"fmt"

"github.com/ublue-os/uupd/lib"
)

type DistroboxUpdater struct {
Config DriverConfiguration
Tracker *TrackerConfiguration
binaryPath string
users []lib.User
usersEnabled bool
}
Expand All @@ -24,19 +23,26 @@ func (up DistroboxUpdater) Steps() int {
return 0
}

func (up DistroboxUpdater) New(initconfig UpdaterInitConfiguration) (DistroboxUpdater, error) {
func (up DistroboxUpdater) New(config UpdaterInitConfiguration) (DistroboxUpdater, error) {
userdesc := "Distroboxes for User:"
up.Config = DriverConfiguration{
Title: "Distrobox",
Description: "Rootful Distroboxes",
UserDescription: &userdesc,
Enabled: true,
MultiUser: true,
DryRun: initconfig.DryRun,
DryRun: config.DryRun,
}
up.usersEnabled = false
up.Tracker = nil

binaryPath, empty := config.Environment["UUPD_DISTROBOX_BINARY"]
if empty || binaryPath == "" {
up.binaryPath = "/usr/bin/distrobox"
} else {
up.binaryPath = binaryPath
}

return up, nil
}

Expand Down Expand Up @@ -64,24 +70,26 @@ func (up *DistroboxUpdater) Update() (*[]CommandOutput, error) {
return &finalOutput, nil
}

// TODO: add env support for Flatpak and Distrobox updaters
lib.ChangeTrackerMessageFancy(*up.Tracker.Writer, up.Tracker.Tracker, up.Tracker.Progress, lib.TrackerMessage{Title: up.Config.Title, Description: up.Config.Description})
out, err := lib.RunUID(0, []string{"/usr/bin/distrobox", "upgrade", "-a"}, nil)
cli := []string{up.binaryPath, "upgrade", "-a"}
out, err := lib.RunUID(0, cli, nil)
tmpout := CommandOutput{}.New(out, err)
if err != nil {
tmpout.SetFailureContext("System Distroboxes")
}
tmpout.Context = up.Config.Description
tmpout.Cli = cli
tmpout.Failure = err != nil
finalOutput = append(finalOutput, *tmpout)

err = nil
for _, user := range up.users {
up.Tracker.Tracker.IncrementSection(err)
context := *up.Config.UserDescription + " " + user.Name
lib.ChangeTrackerMessageFancy(*up.Tracker.Writer, up.Tracker.Tracker, up.Tracker.Progress, lib.TrackerMessage{Title: up.Config.Title, Description: *up.Config.UserDescription + " " + user.Name})
out, err := lib.RunUID(user.UID, []string{"/usr/bin/distrobox", "upgrade", "-a"}, nil)
cli := []string{up.binaryPath, "upgrade", "-a"}
out, err := lib.RunUID(user.UID, cli, nil)
tmpout = CommandOutput{}.New(out, err)
if err != nil {
tmpout.SetFailureContext(fmt.Sprintf("Distroboxes for User: %s", user.Name))
}
tmpout.Context = context
tmpout.Cli = cli
tmpout.Failure = err != nil
finalOutput = append(finalOutput, *tmpout)
}
return &finalOutput, nil
Expand Down
34 changes: 22 additions & 12 deletions drv/flatpak.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package drv

import (
"fmt"
"os/exec"

"github.com/ublue-os/uupd/lib"
Expand All @@ -10,6 +9,7 @@ import (
type FlatpakUpdater struct {
Config DriverConfiguration
Tracker *TrackerConfiguration
binaryPath string
users []lib.User
usersEnabled bool
}
Expand All @@ -25,19 +25,26 @@ func (up FlatpakUpdater) Steps() int {
return 0
}

func (up FlatpakUpdater) New(initconfig UpdaterInitConfiguration) (FlatpakUpdater, error) {
func (up FlatpakUpdater) New(config UpdaterInitConfiguration) (FlatpakUpdater, error) {
userdesc := "Apps for User:"
up.Config = DriverConfiguration{
Title: "Flatpak",
Description: "System Apps",
UserDescription: &userdesc,
Enabled: true,
MultiUser: true,
DryRun: initconfig.DryRun,
DryRun: config.DryRun,
}
up.usersEnabled = false
up.Tracker = nil

binaryPath, empty := config.Environment["UUPD_FLATPAK_BINARY"]
if empty || binaryPath == "" {
up.binaryPath = "/usr/bin/flatpak"
} else {
up.binaryPath = binaryPath
}

return up, nil
}

Expand Down Expand Up @@ -66,23 +73,26 @@ func (up FlatpakUpdater) Update() (*[]CommandOutput, error) {
}

lib.ChangeTrackerMessageFancy(*up.Tracker.Writer, up.Tracker.Tracker, up.Tracker.Progress, lib.TrackerMessage{Title: up.Config.Title, Description: up.Config.Description})
flatpakCmd := exec.Command("/usr/bin/flatpak", "update", "-y")
cli := []string{up.binaryPath, "update", "-y"}
flatpakCmd := exec.Command(cli[0], cli[1:]...)
out, err := flatpakCmd.CombinedOutput()
tmpout := CommandOutput{}.New(out, err)
if err != nil {
tmpout.SetFailureContext("Flatpak System Apps")
}
tmpout.Context = up.Config.Description
tmpout.Cli = cli
tmpout.Failure = err != nil
finalOutput = append(finalOutput, *tmpout)

err = nil
for _, user := range up.users {
up.Tracker.Tracker.IncrementSection(err)
lib.ChangeTrackerMessageFancy(*up.Tracker.Writer, up.Tracker.Tracker, up.Tracker.Progress, lib.TrackerMessage{Title: up.Config.Title, Description: *up.Config.UserDescription + " " + user.Name})
out, err := lib.RunUID(user.UID, []string{"/usr/bin/flatpak", "update", "-y"}, nil)
context := *up.Config.UserDescription + " " + user.Name
lib.ChangeTrackerMessageFancy(*up.Tracker.Writer, up.Tracker.Tracker, up.Tracker.Progress, lib.TrackerMessage{Title: up.Config.Title, Description: context})
cli := []string{up.binaryPath, "update", "-y"}
out, err := lib.RunUID(user.UID, cli, nil)
tmpout = CommandOutput{}.New(out, err)
if err != nil {
tmpout.SetFailureContext(fmt.Sprintf("Flatpak User: %s", user.Name))
}
tmpout.Context = context
tmpout.Cli = cli
tmpout.Failure = err != nil
finalOutput = append(finalOutput, *tmpout)
}
return &finalOutput, nil
Expand Down
3 changes: 3 additions & 0 deletions drv/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/ublue-os/uupd/lib"
)

type EnvironmentMap map[string]string

type UpdaterInitConfiguration struct {
DryRun bool
Ci bool
Expand Down Expand Up @@ -42,6 +44,7 @@ type CommandOutput struct {
Failure bool
Stderr error
Context string
Cli []string
}

func (output CommandOutput) New(out []byte, err error) *CommandOutput {
Expand Down
Loading

0 comments on commit f6c1895

Please sign in to comment.