Files
panic-cli/cmd/software/update.go
2025-12-08 06:40:30 -07:00

87 lines
2.0 KiB
Go

package software
import (
"bytes"
"context"
"fmt"
"os"
"strings"
"github.com/Print-and-Panic/panic-cli/internal/software"
"github.com/spf13/cobra"
)
var updateCmd = &cobra.Command{
Use: "update",
Short: "Update a software package",
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
fmt.Println("No software specified.")
os.Exit(1)
}
sw, exists := softwareMap[args[0]]
if !exists {
fmt.Printf("Software '%s' not found.\n", args[0])
os.Exit(1)
}
if updater, ok := sw.(software.Updateable); ok {
newestVersion, err := checkForUpdates(updater)
if err != nil {
fmt.Printf("Failed to check for updates: %v\n", err)
os.Exit(1)
}
if newestVersion != "" {
if summarizer, ok := sw.(software.Summarizer); ok {
notes, err := updater.GetReleaseNotes(newestVersion)
if err != nil {
fmt.Printf("Failed to get release notes: %v\n", err)
os.Exit(1)
}
summary, err := summarizeReleaseNotes(notes, summarizer)
if err != nil {
fmt.Printf("Failed to summarize release notes: %v\n", err)
}
fmt.Println(summary)
}
if err := updater.Install(newestVersion); err != nil {
fmt.Printf("Failed to update '%s': %v\n", sw.Name(), err)
os.Exit(1)
}
fmt.Printf("Software '%s' updated successfully.\n", sw.Name())
}
}
},
}
func init() {
SoftwareCmd.AddCommand(updateCmd)
}
func checkForUpdates(updater software.Updateable) (string, error) {
currentVersion, err := updater.GetVersion()
if err != nil {
return "", err
}
remoteVersion, err := updater.GetRemoteVersion()
if err != nil {
return "", err
}
if currentVersion != remoteVersion {
return remoteVersion, nil
}
return "", nil
}
func summarizeReleaseNotes(notes string, summarizer software.Summarizer) (string, error) {
var summary string
buffer := bytes.NewBufferString(summary)
summarizer.Summarize(context.Background(), strings.NewReader(notes), buffer)
return buffer.String(), nil
}