diff --git a/cmd/update.go b/cmd/update.go index 24c59c9..906a869 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -57,8 +57,8 @@ func Update(cmd *cobra.Command, args []string) { } initConfiguration := drv.UpdaterInitConfiguration{}.New() - _, empty := os.LookupEnv("CI") - initConfiguration.Ci = !empty + _, exists := os.LookupEnv("CI") + initConfiguration.Ci = exists initConfiguration.DryRun = dryRun initConfiguration.Verbose = verboseRun @@ -193,7 +193,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 @@ -210,7 +210,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 diff --git a/drv/brew.go b/drv/brew.go index 155862b..918c630 100644 --- a/drv/brew.go +++ b/drv/brew.go @@ -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 } @@ -42,59 +42,71 @@ 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.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") 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.Config.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 + Config DriverConfiguration + BaseUser int + BrewRepo string + BrewPrefix string + BrewCellar string + BrewPath string } func (up BrewUpdater) New(config UpdaterInitConfiguration) (BrewUpdater, error) { - brewPrefix, empty := os.LookupEnv("HOMEBREW_PREFIX") - if empty || brewPrefix == "" { - brewPrefix = "/home/linuxbrew/.linuxbrew" - } - brewRepo, empty := os.LookupEnv("HOMEBREW_REPOSITORY") - if empty || brewRepo == "" { - brewRepo = fmt.Sprintf("%s/Homebrew", brewPrefix) - } - brewCellar, empty := os.LookupEnv("HOMEBREW_CELLAR") - if empty || brewCellar == "" { - brewCellar = fmt.Sprintf("%s/Cellar", brewPrefix) - } - brewPath, empty := os.LookupEnv("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.Config = DriverConfiguration{ Title: "Brew", Description: "CLI Apps", Enabled: true, MultiUser: false, DryRun: config.DryRun, + Environment: config.Environment, + } + + 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 } if up.Config.DryRun { @@ -105,7 +117,7 @@ func (up BrewUpdater) New(config UpdaterInitConfiguration) (BrewUpdater, error) if err != nil { return up, err } - up.BaseUser = uid + return up, nil } diff --git a/drv/distrobox.go b/drv/distrobox.go index 958dfa1..115d893 100644 --- a/drv/distrobox.go +++ b/drv/distrobox.go @@ -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 } @@ -24,7 +23,7 @@ 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", @@ -32,11 +31,19 @@ func (up DistroboxUpdater) New(initconfig UpdaterInitConfiguration) (DistroboxUp UserDescription: &userdesc, Enabled: true, MultiUser: true, - DryRun: initconfig.DryRun, + DryRun: config.DryRun, + Environment: config.Environment, } up.usersEnabled = false up.Tracker = nil + binaryPath, exists := up.Config.Environment["UUPD_DISTROBOX_BINARY"] + if !exists || binaryPath == "" { + up.binaryPath = "/usr/bin/distrobox" + } else { + up.binaryPath = binaryPath + } + return up, nil } @@ -64,24 +71,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 diff --git a/drv/flatpak.go b/drv/flatpak.go index e2a7137..7b524c6 100644 --- a/drv/flatpak.go +++ b/drv/flatpak.go @@ -1,7 +1,6 @@ package drv import ( - "fmt" "os/exec" "github.com/ublue-os/uupd/lib" @@ -10,6 +9,7 @@ import ( type FlatpakUpdater struct { Config DriverConfiguration Tracker *TrackerConfiguration + binaryPath string users []lib.User usersEnabled bool } @@ -25,7 +25,7 @@ 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", @@ -33,11 +33,19 @@ func (up FlatpakUpdater) New(initconfig UpdaterInitConfiguration) (FlatpakUpdate UserDescription: &userdesc, Enabled: true, MultiUser: true, - DryRun: initconfig.DryRun, + DryRun: config.DryRun, + Environment: config.Environment, } up.usersEnabled = false up.Tracker = nil + binaryPath, exists := up.Config.Environment["UUPD_FLATPAK_BINARY"] + if !exists || binaryPath == "" { + up.binaryPath = "/usr/bin/flatpak" + } else { + up.binaryPath = binaryPath + } + return up, nil } @@ -66,23 +74,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 diff --git a/drv/generic.go b/drv/generic.go index c708ad8..133fe74 100644 --- a/drv/generic.go +++ b/drv/generic.go @@ -8,11 +8,13 @@ import ( "github.com/ublue-os/uupd/lib" ) +type EnvironmentMap map[string]string + type UpdaterInitConfiguration struct { DryRun bool Ci bool Verbose bool - Environment map[string]string + Environment EnvironmentMap } func GetEnvironment(data []string, getkeyval func(item string) (key, val string)) map[string]string { @@ -42,6 +44,7 @@ type CommandOutput struct { Failure bool Stderr error Context string + Cli []string } func (output CommandOutput) New(out []byte, err error) *CommandOutput { @@ -64,6 +67,7 @@ type DriverConfiguration struct { Enabled bool MultiUser bool DryRun bool + Environment EnvironmentMap UserDescription *string } diff --git a/drv/rpm-ostree.go b/drv/rpm-ostree.go index 8eae935..dca9bf4 100644 --- a/drv/rpm-ostree.go +++ b/drv/rpm-ostree.go @@ -96,13 +96,14 @@ func (up RpmOstreeUpdater) New(config UpdaterInitConfiguration) (RpmOstreeUpdate Description: "System Updates", Enabled: !config.Ci, DryRun: config.DryRun, + Environment: config.Environment, } if up.Config.DryRun { return up, nil } - binaryPath, exists := config.Environment["UUPD_RPMOSTREE_BINARY"] + binaryPath, exists := up.Config.Environment["UUPD_RPMOSTREE_BINARY"] if !exists || binaryPath == "" { up.BinaryPath = "/usr/bin/rpm-ostree" } else { diff --git a/drv/system.go b/drv/system.go index 21d4fec..f4c5784 100644 --- a/drv/system.go +++ b/drv/system.go @@ -99,13 +99,14 @@ func (up SystemUpdater) New(config UpdaterInitConfiguration) (SystemUpdater, err Description: "System Updates", Enabled: !config.Ci, DryRun: config.DryRun, + Environment: config.Environment, } if up.Config.DryRun { return up, nil } - bootcBinaryPath, exists := config.Environment["UUPD_BOOTC_BINARY"] + bootcBinaryPath, exists := up.Config.Environment["UUPD_BOOTC_BINARY"] if !exists || bootcBinaryPath == "" { up.BinaryPath = "/usr/bin/bootc" } else {