Skip to content

Commit

Permalink
Merge pull request #12 from tulilirockz/no-progress
Browse files Browse the repository at this point in the history
feat: add no-progress mode + tty detection
  • Loading branch information
gerblesh authored Dec 3, 2024
2 parents 808daf0 + edec3e4 commit aaaeeb3
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 32 deletions.
38 changes: 21 additions & 17 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package cmd
import (
"fmt"
"log"
"github.com/spf13/cobra"
"os"
"os/user"

"github.com/spf13/cobra"
"golang.org/x/term"
)

func assertRoot(cmd *cobra.Command, args []string) {
Expand All @@ -21,38 +23,38 @@ func assertRoot(cmd *cobra.Command, args []string) {

var (
rootCmd = &cobra.Command{
Use: "uupd",
Short: "uupd (Universal Update) is the successor to ublue-update, built for bootc",
Use: "uupd",
Short: "uupd (Universal Update) is the successor to ublue-update, built for bootc",
PreRun: assertRoot,
Run: Update,
Run: Update,
}

waitCmd = &cobra.Command{
Use: "wait",
Short: "Waits for ostree sysroot to unlock",
Use: "wait",
Short: "Waits for ostree sysroot to unlock",
PreRun: assertRoot,
Run: Wait,
Run: Wait,
}

updateCheckCmd = &cobra.Command{
Use: "update-check",
Short: "Check for updates to the booted image",
Use: "update-check",
Short: "Check for updates to the booted image",
PreRun: assertRoot,
Run: UpdateCheck,
Run: UpdateCheck,
}

hardwareCheckCmd = &cobra.Command{
Use: "hw-check",
Short: "Run hardware checks",
Use: "hw-check",
Short: "Run hardware checks",
PreRun: assertRoot,
Run: HwCheck,
Run: HwCheck,
}

imageOutdatedCmd = &cobra.Command{
Use: "is-img-outdated",
Short: "Print 'true' or 'false' based on if the current booted image is over 1 month old",
Use: "is-img-outdated",
Short: "Print 'true' or 'false' based on if the current booted image is over 1 month old",
PreRun: assertRoot,
Run: ImageOutdated,
Run: ImageOutdated,
}
)

Expand All @@ -68,6 +70,8 @@ func init() {
rootCmd.AddCommand(updateCheckCmd)
rootCmd.AddCommand(hardwareCheckCmd)
rootCmd.AddCommand(imageOutdatedCmd)
rootCmd.Flags().BoolP("hw-check", "c", false, "run hardware check before running updates")
isTerminal := term.IsTerminal(int(os.Stdout.Fd()))
rootCmd.Flags().BoolP("no-progress", "p", !isTerminal, "Do not show progress bars")
rootCmd.Flags().BoolP("hw-check", "c", false, "Run hardware check before running updates")
rootCmd.Flags().BoolP("dry-run", "n", false, "Do a dry run (used for testing)")
}
29 changes: 19 additions & 10 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,26 @@ func Update(cmd *cobra.Command, args []string) {
totalSteps := brewUpdate + systemUpdate + 1 + len(users) + 1 + len(users) // system + Brew + Flatpak (users + root) + Distrobox (users + root)
pw := lib.NewProgressWriter()
pw.SetNumTrackersExpected(1)
go pw.Render()
pw.SetAutoStop(false)

noProgress, err := cmd.Flags().GetBool("no-progress")
if err != nil {
log.Fatalf("Failed to get no-progress flag: %v", err)
}

if !noProgress {
go pw.Render()
}
// move this to its actual boolean value (progress bar = false)
noProgress = !noProgress
// -1 because 0 index
tracker := lib.NewIncrementTracker(&progress.Tracker{Message: "Updating", Units: progress.UnitsDefault, Total: int64(totalSteps - 1)}, totalSteps-1)
pw.AppendTracker(tracker.Tracker)

failures := make(map[string]Failure)

if updateAvailable {
tracker.IncrementSection()
lib.ChangeTrackerMessageFancy(pw, tracker, "Updating System")
lib.ChangeTrackerMessageFancy(pw, tracker, noProgress, "Updating System")
out, err := systemDriver.Update()
if err != nil {
failures[systemDriver.Name] = Failure{
Expand All @@ -106,7 +116,7 @@ func Update(cmd *cobra.Command, args []string) {
}

if brewUpdate == 1 {
lib.ChangeTrackerMessageFancy(pw, tracker, "Updating CLI apps (Brew)")
lib.ChangeTrackerMessageFancy(pw, tracker, noProgress, "Updating CLI apps (Brew)")
out, err := drv.BrewUpdate(brewUid)
if err != nil {
failures["Brew"] = Failure{
Expand All @@ -120,7 +130,7 @@ func Update(cmd *cobra.Command, args []string) {
}

// Run flatpak updates
lib.ChangeTrackerMessageFancy(pw, tracker, "Updating System Apps (Flatpak)")
lib.ChangeTrackerMessageFancy(pw, tracker, noProgress, "Updating System Apps (Flatpak)")
flatpakCmd := exec.Command("/usr/bin/flatpak", "update", "-y")
out, err := flatpakCmd.CombinedOutput()
if err != nil {
Expand All @@ -133,7 +143,7 @@ func Update(cmd *cobra.Command, args []string) {
tracker.IncrementSection()
}
for _, user := range users {
lib.ChangeTrackerMessageFancy(pw, tracker, fmt.Sprintf("Updating Apps for User: %s (Flatpak)", user.Name))
lib.ChangeTrackerMessageFancy(pw, tracker, noProgress, fmt.Sprintf("Updating Apps for User: %s (Flatpak)", user.Name))
out, err := lib.RunUID(user.UID, []string{"/usr/bin/flatpak", "update", "-y"}, nil)
if err != nil {
failures[fmt.Sprintf("Flatpak User: %s", user.Name)] = Failure{
Expand All @@ -147,7 +157,7 @@ func Update(cmd *cobra.Command, args []string) {
}

// Run distrobox updates
lib.ChangeTrackerMessageFancy(pw, tracker, "Updating System Distroboxes")
lib.ChangeTrackerMessageFancy(pw, tracker, noProgress, "Updating System Distroboxes")
// distrobox doesn't support sudo, run with systemd-run
out, err = lib.RunUID(0, []string{"/usr/bin/distrobox", "upgrade", "-a"}, nil)
if err != nil {
Expand All @@ -160,7 +170,7 @@ func Update(cmd *cobra.Command, args []string) {
tracker.IncrementSection()
}
for _, user := range users {
lib.ChangeTrackerMessageFancy(pw, tracker, fmt.Sprintf("Updating Distroboxes for User: %s", user.Name))
lib.ChangeTrackerMessageFancy(pw, tracker, noProgress, fmt.Sprintf("Updating Distroboxes for User: %s", user.Name))
out, err := lib.RunUID(user.UID, []string{"/usr/bin/distrobox", "upgrade", "-a"}, nil)
if err != nil {
failures[fmt.Sprintf("Distrobox User: %s", user.Name)] = Failure{
Expand All @@ -173,9 +183,8 @@ func Update(cmd *cobra.Command, args []string) {
}
}

pw.Stop()
if len(failures) > 0 {
pw.SetAutoStop(false)
pw.Stop()
failedSystemsList := make([]string, 0, len(failures))
for systemName := range failures {
failedSystemsList = append(failedSystemsList, systemName)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/jedib0t/go-pretty/v6 v6.6.3
github.com/shirou/gopsutil/v4 v4.24.10
github.com/spf13/cobra v1.8.1
golang.org/x/term v0.26.0
)

require (
Expand All @@ -22,5 +23,4 @@ require (
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/term v0.17.0 // indirect
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ 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/term v0.17.0 h1:mkTF7LCd6WGJNL3K1Ad7kwxNfYAW6a8a8QqtMblp/4U=
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
golang.org/x/term v0.26.0 h1:WEQa6V3Gja/BhNxg540hBip/kkaYtRg3cxg4oXSw4AU=
golang.org/x/term v0.26.0/go.mod h1:Si5m1o57C5nBNQo5z1iq+XDijt21BDBDp2bK0QI8e3E=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
10 changes: 8 additions & 2 deletions lib/percentmanager.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package lib

import (
"log"
"time"

"github.com/jedib0t/go-pretty/v6/progress"
"github.com/jedib0t/go-pretty/v6/text"
"time"
)

type Incrementer struct {
Expand Down Expand Up @@ -52,7 +54,11 @@ func NewIncrementTracker(tracker *progress.Tracker, max_increments int) *Increme
}
}

func ChangeTrackerMessageFancy(writer progress.Writer, tracker *IncrementTracker, message string) {
func ChangeTrackerMessageFancy(writer progress.Writer, tracker *IncrementTracker, progress bool, message string) {
if !progress {
log.Printf("[%d|%d] %s\n", tracker.Tracker.Value(), tracker.Tracker.Total, message)
return
}
writer.SetMessageLength(len(message))
tracker.Tracker.UpdateMessage(message)
}
Expand Down

0 comments on commit aaaeeb3

Please sign in to comment.