bold-brew/internal/services/selfupdate.go
Vito 5546ad1b33
feat: general improvement and removed command service (#20)
* improved quality code and add general logic fix

* feat: removed command service
2025-06-26 15:43:26 +02:00

47 lines
1.1 KiB
Go

package services
import (
"context"
"encoding/json"
"fmt"
"os/exec"
)
type SelfUpdateServiceInterface interface {
CheckForUpdates(ctx context.Context) (string, error)
}
type SelfUpdateService struct{}
type boldBrewStatusInfo struct {
Versions struct {
Stable string `json:"stable"`
} `json:"versions"`
}
var NewSelfUpdateService = func() SelfUpdateServiceInterface {
return &SelfUpdateService{}
}
// CheckForUpdates checks for the latest version of the Bold Brew package using Homebrew.
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("context cancelled")
}
return "", fmt.Errorf("failed to fetch latest version from tap: %v", err)
}
var info []boldBrewStatusInfo
if err := json.Unmarshal(output, &info); err != nil {
return "", fmt.Errorf("failed to parse version info: %v", err)
}
if len(info) == 0 {
return "", fmt.Errorf("no version information found")
}
return info[0].Versions.Stable, nil
}