mirror of
https://github.com/Valkyrie00/bold-brew.git
synced 2026-03-16 07:15:53 +01:00
47 lines
1.1 KiB
Go
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
|
|
}
|