update.go

 1package cmd
 2
 3import (
 4	"context"
 5	"fmt"
 6	"time"
 7
 8	"github.com/charmbracelet/crush/internal/format"
 9	"github.com/charmbracelet/crush/internal/update"
10	"github.com/spf13/cobra"
11)
12
13func init() {
14	rootCmd.AddCommand(updateCmd)
15}
16
17var updateCmd = &cobra.Command{
18	Use:   "check-update",
19	Short: "Check for updates",
20	Long:  `Check if a new version of crush is available.`,
21	Example: `
22# Check for updates
23crush check-update
24  `,
25	RunE: func(cmd *cobra.Command, args []string) error {
26		ctx, cancel := context.WithTimeout(cmd.Context(), 10*time.Second)
27		defer cancel()
28
29		sp := format.NewSpinner(ctx, cancel, "Checking for updates")
30		sp.Start()
31		defer sp.Stop()
32
33		info, err := update.CheckForUpdate(ctx)
34		if err != nil {
35			return fmt.Errorf("failed to check for updates: %w", err)
36		}
37		sp.Stop()
38
39		if !info.Available {
40			fmt.Printf("You are running the latest version: %s\n", info.CurrentVersion)
41			return nil
42		}
43
44		fmt.Printf("\nšŸŽ‰ A new version of crush is available!\n\n")
45		fmt.Printf("Current version: %s\n", info.CurrentVersion)
46		fmt.Printf("Latest version:  %s\n\n", info.LatestVersion)
47		fmt.Printf("Visit %s to download the latest version.\n", info.ReleaseURL)
48
49		return nil
50	},
51}