Skip to content

Commit

Permalink
fix: make distrobox automatically disable, fix versioning (#45)
Browse files Browse the repository at this point in the history
* fix: make distrobox automatically disable, fix versioning

* fix: disable OSC when run noninteractively

* docs(README): fix formatting

* fix: remove "automatic" versioning from RPM spec

* style: fetch disable-osc-progress flag and handle error

* fix: detect dry run in distrobox.go

* fix: use dry-run in unit tests

* fix: don't disable updates within drv.new()

* feat: debug logging for updater configs

* fix: disable OSC incrementer
  • Loading branch information
gerblesh authored Dec 26, 2024
1 parent 758e07f commit a9f5e57
Show file tree
Hide file tree
Showing 15 changed files with 72 additions and 36 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ $ sudo uupd
# CLI Options

```
$ uupd --help
$ uupd --help
```

# Troubleshooting
Expand Down
4 changes: 3 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/spf13/cobra"
appLogging "github.com/ublue-os/uupd/pkg/logging"
"golang.org/x/term"
)

func assertRoot(cmd *cobra.Command, args []string) {
Expand Down Expand Up @@ -111,7 +112,8 @@ func init() {
rootCmd.Flags().BoolP("dry-run", "n", false, "Do a dry run")
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")

isTerminal := term.IsTerminal(int(os.Stdout.Fd()))
rootCmd.Flags().Bool("disable-osc-progress", !isTerminal, "Disable the GUI progress indicator")
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")
Expand Down
36 changes: 26 additions & 10 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ func Update(cmd *cobra.Command, args []string) {
slog.Error("Failed to get verbose flag", "error", err)
return
}
disableOsc, err := cmd.Flags().GetBool("disable-osc-progress")
if err != nil {
slog.Error("Failed to get disable-osc-progress flag", "error", err)
return
}

if hwCheck {
err := checks.RunHwChecks()
Expand All @@ -73,14 +78,23 @@ func Update(cmd *cobra.Command, args []string) {
initConfiguration.Verbose = verboseRun

brewUpdater, err := brew.BrewUpdater{}.New(*initConfiguration)
brewUpdater.Config.Enabled = err == nil
if err != nil {
brewUpdater.Config.Enabled = false
slog.Debug("Brew driver failed to initialize", slog.Any("error", err))
}

flatpakUpdater, err := flatpak.FlatpakUpdater{}.New(*initConfiguration)
flatpakUpdater.Config.Enabled = err == nil
if err != nil {
flatpakUpdater.Config.Enabled = false
slog.Debug("Flatpak driver failed to initialize", slog.Any("error", err))
}
flatpakUpdater.SetUsers(users)

distroboxUpdater, err := distrobox.DistroboxUpdater{}.New(*initConfiguration)
distroboxUpdater.Config.Enabled = err == nil
if err != nil {
distroboxUpdater.Config.Enabled = false
slog.Debug("Distrobox driver failed to initialize", slog.Any("error", err))
}
distroboxUpdater.SetUsers(users)

mainSystemDriver, mainSystemDriverConfig, _, _ := system.InitializeSystemDriver(*initConfiguration)
Expand All @@ -98,11 +112,12 @@ func Update(cmd *cobra.Command, args []string) {
totalSteps += mainSystemDriver.Steps()
}

// FIXME: check if is interactive
percent.ResetOscProgress()
if !disableOsc {
percent.ResetOscProgress()
}

// -1 because 0 index
tracker := &percent.Incrementer{MaxIncrements: totalSteps - 1}
tracker := &percent.Incrementer{MaxIncrements: totalSteps - 1, OscEnabled: !disableOsc}

flatpakUpdater.Tracker = tracker
distroboxUpdater.Tracker = tracker
Expand All @@ -129,7 +144,7 @@ func Update(cmd *cobra.Command, args []string) {

if mainSystemDriverConfig.Enabled {
slog.Debug(fmt.Sprintf("%s module", mainSystemDriverConfig.Title), slog.String("module_name", mainSystemDriverConfig.Title), slog.Any("module_configuration", mainSystemDriverConfig))
percent.ReportStatusChange(tracker, percent.TrackerMessage{Title: mainSystemDriverConfig.Title, Description: mainSystemDriverConfig.Description})
tracker.ReportStatusChange(mainSystemDriverConfig.Title, mainSystemDriverConfig.Description)
var out *[]drv.CommandOutput
out, err = mainSystemDriver.Update()
outputs = append(outputs, *out...)
Expand All @@ -138,7 +153,7 @@ 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.ReportStatusChange(tracker, percent.TrackerMessage{Title: brewUpdater.Config.Title, Description: brewUpdater.Config.Description})
tracker.ReportStatusChange(brewUpdater.Config.Title, brewUpdater.Config.Description)
var out *[]drv.CommandOutput
out, err = brewUpdater.Update()
outputs = append(outputs, *out...)
Expand All @@ -161,8 +176,9 @@ func Update(cmd *cobra.Command, args []string) {
tracker.IncrementSection(err)
}

// FIXME: detect interactive session
percent.ResetOscProgress()
if !disableOsc {
percent.ResetOscProgress()
}
if verboseRun {
slog.Info("Verbose run requested")

Expand Down
2 changes: 1 addition & 1 deletion drv/brew/brew_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func InitBaseConfig() brew.BrewUpdater {
var initConfiguration = generic.UpdaterInitConfiguration{
DryRun: false,
DryRun: true,
Ci: false,
Verbose: false,
Environment: nil,
Expand Down
20 changes: 16 additions & 4 deletions drv/distrobox/distrobox.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package distrobox

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

. "github.com/ublue-os/uupd/drv/generic"
Expand Down Expand Up @@ -45,6 +46,17 @@ func (up DistroboxUpdater) New(config UpdaterInitConfiguration) (DistroboxUpdate

up.binaryPath = EnvOrFallback(up.Config.Environment, "UUPD_DISTROBOX_BINARY", "/usr/bin/distrobox")

if up.Config.DryRun {
return up, nil
}

inf, err := os.Stat(up.binaryPath)
if err != nil {
return up, err
}
// check if file is executable using bitmask
up.Config.Enabled = inf.Mode()&0111 != 0

return up, nil
}

Expand All @@ -61,18 +73,18 @@ func (up DistroboxUpdater) Update() (*[]CommandOutput, error) {
var finalOutput = []CommandOutput{}

if up.Config.DryRun {
percent.ReportStatusChange(up.Tracker, percent.TrackerMessage{Title: up.Config.Title, Description: up.Config.Description})
up.Tracker.ReportStatusChange(up.Config.Title, up.Config.Description)
up.Tracker.IncrementSection(nil)

var err error = nil
for _, user := range up.users {
up.Tracker.IncrementSection(err)
percent.ReportStatusChange(up.Tracker, percent.TrackerMessage{Title: up.Config.Title, Description: *up.Config.UserDescription + " " + user.Name})
up.Tracker.ReportStatusChange(up.Config.Title, *up.Config.UserDescription+" "+user.Name)
}
return &finalOutput, nil
}

percent.ReportStatusChange(up.Tracker, percent.TrackerMessage{Title: up.Config.Title, Description: up.Config.Description})
up.Tracker.ReportStatusChange(up.Config.Title, up.Config.Description)
cli := []string{up.binaryPath, "upgrade", "-a"}
out, err := session.RunUID(up.Config.Logger, slog.LevelDebug, 0, cli, nil)
tmpout := CommandOutput{}.New(out, err)
Expand All @@ -85,7 +97,7 @@ func (up DistroboxUpdater) Update() (*[]CommandOutput, error) {
for _, user := range up.users {
up.Tracker.IncrementSection(err)
context := *up.Config.UserDescription + " " + user.Name
percent.ReportStatusChange(up.Tracker, percent.TrackerMessage{Title: up.Config.Title, Description: *up.Config.UserDescription + " " + user.Name})
up.Tracker.ReportStatusChange(up.Config.Title, *up.Config.UserDescription+" "+user.Name)
cli := []string{up.binaryPath, "upgrade", "-a"}
out, err := session.RunUID(up.Config.Logger, slog.LevelDebug, user.UID, cli, nil)
tmpout = CommandOutput{}.New(out, err)
Expand Down
2 changes: 1 addition & 1 deletion drv/distrobox/distrobox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

func InitBaseConfig() distrobox.DistroboxUpdater {
var initConfiguration = generic.UpdaterInitConfiguration{
DryRun: false,
DryRun: true,
Ci: false,
Verbose: false,
Environment: nil,
Expand Down
8 changes: 4 additions & 4 deletions drv/flatpak/flatpak.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,18 @@ func (up FlatpakUpdater) Update() (*[]CommandOutput, error) {
var finalOutput = []CommandOutput{}

if up.Config.DryRun {
percent.ReportStatusChange(up.Tracker, percent.TrackerMessage{Title: up.Config.Title, Description: up.Config.Description})
up.Tracker.ReportStatusChange(up.Config.Title, up.Config.Description)
up.Tracker.IncrementSection(nil)

var err error = nil
for _, user := range up.users {
up.Tracker.IncrementSection(err)
percent.ReportStatusChange(up.Tracker, percent.TrackerMessage{Title: up.Config.Title, Description: *up.Config.UserDescription + " " + user.Name})
up.Tracker.ReportStatusChange(up.Config.Title, *up.Config.UserDescription+" "+user.Name)
}
return &finalOutput, nil
}

percent.ReportStatusChange(up.Tracker, percent.TrackerMessage{Title: up.Config.Title, Description: up.Config.Description})
up.Tracker.ReportStatusChange(up.Config.Title, up.Config.Description)
cli := []string{up.binaryPath, "update", "-y", "--noninteractive"}
flatpakCmd := exec.Command(cli[0], cli[1:]...)
out, err := session.RunLog(up.Config.Logger, slog.LevelDebug, flatpakCmd)
Expand All @@ -87,7 +87,7 @@ func (up FlatpakUpdater) Update() (*[]CommandOutput, error) {
for _, user := range up.users {
up.Tracker.IncrementSection(err)
context := *up.Config.UserDescription + " " + user.Name
percent.ReportStatusChange(up.Tracker, percent.TrackerMessage{Title: up.Config.Title, Description: context})
up.Tracker.ReportStatusChange(up.Config.Title, context)
cli := []string{up.binaryPath, "update", "-y"}
out, err := session.RunUID(up.Config.Logger, slog.LevelDebug, user.UID, cli, nil)
tmpout = CommandOutput{}.New(out, err)
Expand Down
2 changes: 1 addition & 1 deletion drv/flatpak/flatpak_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

func InitBaseConfig() flatpak.FlatpakUpdater {
var initConfiguration = generic.UpdaterInitConfiguration{
DryRun: false,
DryRun: true,
Ci: false,
Verbose: false,
Environment: nil,
Expand Down
2 changes: 1 addition & 1 deletion drv/rpmostree/rpmostree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func InitBaseConfig() rpmostree.RpmOstreeUpdater {
var initConfiguration = generic.UpdaterInitConfiguration{
DryRun: false,
DryRun: true,
Ci: false,
Verbose: false,
Environment: nil,
Expand Down
2 changes: 1 addition & 1 deletion drv/system/system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func InitBaseConfig() system.SystemUpdater {
var initConfiguration = generic.UpdaterInitConfiguration{
DryRun: false,
DryRun: true,
Ci: false,
Verbose: false,
Environment: nil,
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/godbus/dbus/v5 v5.1.0
github.com/shirou/gopsutil/v4 v4.24.10
github.com/spf13/cobra v1.8.1
golang.org/x/term v0.27.0
gopkg.in/yaml.v3 v3.0.1
)

Expand All @@ -19,5 +20,5 @@ require (
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
golang.org/x/sys v0.27.0 // indirect
golang.org/x/sys v0.28.0 // indirect
)
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.27.0 h1:WP60Sv1nlK1T6SupCHbXzSaN0b9wUmsPoRS9b61A23Q=
golang.org/x/term v0.27.0/go.mod h1:iMsnZpn0cago0GOrHO2+Y7u7JPn5AylBrcoWkElMTSM=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
1 change: 1 addition & 0 deletions pkg/percent/incrementer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package percent
type Incrementer struct {
DoneIncrements int
MaxIncrements int
OscEnabled bool
}

func (it *Incrementer) IncrementSection(err error) {
Expand Down
16 changes: 9 additions & 7 deletions pkg/percent/progressmanager.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package percent

import (
"fmt"
"log/slog"
"math"
)

type TrackerMessage struct {
Title string
Description string
}
func (tracker Incrementer) ReportStatusChange(title string, description string) {
if tracker.OscEnabled {
percentage := math.Round((float64(tracker.CurrentStep()) / float64(tracker.MaxIncrements)) * 100)
fmt.Printf("\033]9;4;1;%d\a", int(percentage))
}

func ReportStatusChange(tracker *Incrementer, message TrackerMessage) {
slog.Info("Updating",
slog.String("title", message.Title),
slog.String("description", message.Description),
slog.String("title", title),
slog.String("description", description),
slog.Int("progress", tracker.CurrentStep()),
slog.Int("total", tracker.MaxIncrements),
)
Expand Down
2 changes: 1 addition & 1 deletion uupd.spec
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Name: uupd
Version: 1.0
Version: 0.5.1
Release: 1%{?dist}
Summary: Centralized update service/checker made for Universal Blue
Vendor: ublue-os
Expand Down

0 comments on commit a9f5e57

Please sign in to comment.