mirror of
https://github.com/Valkyrie00/bold-brew.git
synced 2026-03-14 14:25:53 +01:00
feat: implement asynchronous version check with context support
This commit is contained in:
parent
8e33e396cf
commit
74884eaabf
3 changed files with 28 additions and 18 deletions
|
|
@ -2,10 +2,12 @@ package services
|
|||
|
||||
import (
|
||||
"bbrew/internal/models"
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/gdamore/tcell/v2"
|
||||
"github.com/rivo/tview"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
|
|
@ -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)
|
||||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ type LayoutServiceInterface interface {
|
|||
|
||||
GetHeaderView() *tview.TextView
|
||||
SetHeaderView(name, version, brewVersion string)
|
||||
UpdateHeaderView(name, version, brewVersion string)
|
||||
|
||||
GetLegendView() *tview.TextView
|
||||
SetLegendView()
|
||||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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{}
|
||||
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue