package cmd import ( "fmt" "os" "os/exec" "runtime/debug" "strings" "git.db.org.ai/dborg/internal/utils" "github.com/spf13/cobra" ) func getLatestVersion() (string, error) { cmd := exec.Command("git", "ls-remote", "--tags", "--refs", "--sort=-v:refname", utils.RepositoryURL) output, err := cmd.Output() if err != nil { return "", err } lines := strings.Split(strings.TrimSpace(string(output)), "\n") if len(lines) == 0 { return "", fmt.Errorf("no tags found") } parts := strings.Split(lines[0], "refs/tags/") if len(parts) < 2 { return "", fmt.Errorf("invalid tag format") } return strings.TrimSpace(parts[1]), nil } var updateCmd = &cobra.Command{ Use: "update", Short: "Update dborg to the latest version", Long: `Update dborg by running go install git.db.org.ai/dborg@`, Run: func(cmd *cobra.Command, args []string) { fmt.Println("Checking for updates...") latestVersion, err := getLatestVersion() if err != nil { fmt.Fprintf(os.Stderr, "Failed to get latest version: %v\n", err) os.Exit(1) } currentVersion := "dev" if info, ok := debug.ReadBuildInfo(); ok { if info.Main.Version != "" && info.Main.Version != "(devel)" { currentVersion = info.Main.Version } } if currentVersion != "dev" && currentVersion == latestVersion { fmt.Printf("You're already on the latest version: %s\n", latestVersion) return } fmt.Printf("Updating from %s to %s...\n", currentVersion, latestVersion) fmt.Printf("Installing dborg %s...\n", latestVersion) installTarget := fmt.Sprintf("git.db.org.ai/dborg@%s", latestVersion) installCmd := exec.Command("go", "install", installTarget) installCmd.Stdout = os.Stdout installCmd.Stderr = os.Stderr if err := installCmd.Run(); err != nil { fmt.Fprintf(os.Stderr, "Failed to update dborg: %v\n", err) os.Exit(1) } fmt.Printf("Updated to %s\n", latestVersion) }, } func init() { rootCmd.AddCommand(updateCmd) }