Skip to content

Commit

Permalink
feat: implement asynchronous version check with context support
Browse files Browse the repository at this point in the history
  • Loading branch information
Valkyrie00 committed Feb 23, 2025
1 parent 8e33e39 commit 74884ea
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
21 changes: 15 additions & 6 deletions internal/services/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package services

import (
"bbrew/internal/models"
"context"
"fmt"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
"strings"
"time"
)

var (
Expand Down Expand Up @@ -212,12 +214,6 @@ func (s *AppService) setResults(data *[]models.Formula, scrollToTop bool) {
}

func (s *AppService) BuildApp() {
// Evaluate if there is a new version available
latestVersion, err := s.SelfUpdateService.CheckForUpdates()
if err == nil && latestVersion != AppVersion {
AppVersion = fmt.Sprintf("%s ([orange]New Version Available: %s[-])", AppVersion, latestVersion)
}

// Build the layout
s.LayoutService.SetNotificationView()
s.LayoutService.SetHeaderView(AppName, AppVersion, s.brewVersion)
Expand All @@ -226,6 +222,19 @@ func (s *AppService) BuildApp() {
s.LayoutService.SetBuildOutputView()
s.LayoutService.SetFilterCounterView()

// Evaluate if there is a new version available
go func() {
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()

if latestVersion, err := s.SelfUpdateService.CheckForUpdates(ctx); err == nil && latestVersion != AppVersion {
s.app.QueueUpdateDraw(func() {
AppVersion = fmt.Sprintf("%s ([orange]New Version Available: %s[-])", AppVersion, latestVersion)
s.LayoutService.UpdateHeaderView(AppName, AppVersion, s.brewVersion)
})
}
}()

// Result table section
tableSelectionChangedFunc := func(row, _ int) {
if row > 0 && row-1 < len(*s.filteredPackages) {
Expand Down
5 changes: 5 additions & 0 deletions internal/services/layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type LayoutServiceInterface interface {

GetHeaderView() *tview.TextView
SetHeaderView(name, version, brewVersion string)
UpdateHeaderView(name, version, brewVersion string)

GetLegendView() *tview.TextView
SetLegendView()
Expand Down Expand Up @@ -109,6 +110,10 @@ func (s *LayoutService) SetHeaderView(name, version, brewVersion string) {
SetTextAlign(tview.AlignLeft)
}

func (s *LayoutService) UpdateHeaderView(name, version, brewVersion string) {
s.GetHeaderView().SetText(fmt.Sprintf(" %s %s - %s", name, version, brewVersion))
}

func (s *LayoutService) GetLegendView() *tview.TextView {
return s.legend
}
Expand Down
20 changes: 8 additions & 12 deletions internal/services/selfupdate.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package services

import (
"context"
"encoding/json"
"fmt"
"os/exec"
)

type SelfUpdateServiceInterface interface {
CheckForUpdates() (string, error)
CheckForUpdates(ctx context.Context) (string, error)
}

type SelfUpdateService struct{}
Expand All @@ -22,18 +23,13 @@ var NewSelfUpdateService = func() SelfUpdateServiceInterface {
return &SelfUpdateService{}
}

func (s *SelfUpdateService) CheckForUpdates() (string, error) {
latestVersion, err := s.getLatestVersionFromTap()
if err != nil {
return "", err
}
return latestVersion, nil
}

func (s *SelfUpdateService) getLatestVersionFromTap() (string, error) {
cmd := exec.Command("brew", "info", "--json=v1", "valkyrie00/bbrew/bbrew")
output, err := cmd.Output()
func (s *SelfUpdateService) CheckForUpdates(ctx context.Context) (string, error) {
cmd := exec.CommandContext(ctx, "brew", "info", "--json=v1", "valkyrie00/bbrew/bbrew")
output, err := cmd.CombinedOutput()
if err != nil {
if ctx.Err() != nil {
return "", fmt.Errorf("operazione annullata: %v", ctx.Err())
}
return "", fmt.Errorf("failed to fetch latest version from tap: %v", err)
}

Expand Down

0 comments on commit 74884ea

Please sign in to comment.