summaryrefslogtreecommitdiffstats
path: root/cmd/update.go
blob: d664bf0b1bc5277eada8bdd647c766a00f1aac04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package cmd

import (
	"fmt"
	"os"
	"os/exec"
	"strings"

	"github.com/spf13/cobra"
)

func getLatestVersion() (string, error) {
	cmd := exec.Command("git", "ls-remote", "--tags", "--refs", "--sort=-v:refname", "[email protected]:repos/dborg.git")
	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@<version>`,
	Run: func(cmd *cobra.Command, args []string) {
		fmt.Println("Updating dborg...")

		latestVersion, err := getLatestVersion()
		if err != nil {
			fmt.Fprintf(os.Stderr, "Failed to get latest version: %v\n", err)
			os.Exit(1)
		}

		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("dborg updated successfully to %s!\n", latestVersion)
	},
}

func init() {
	rootCmd.AddCommand(updateCmd)
}