|
| 1 | +// Package cmd provides CLI commands for the gt tool. |
| 2 | +package cmd |
| 3 | + |
| 4 | +import ( |
| 5 | + "fmt" |
| 6 | + "os/exec" |
| 7 | + "regexp" |
| 8 | + "strconv" |
| 9 | + "strings" |
| 10 | +) |
| 11 | + |
| 12 | +// MinBeadsVersion is the minimum required beads version for Gas Town. |
| 13 | +// This version must include custom type support (bd-i54l). |
| 14 | +const MinBeadsVersion = "0.44.0" |
| 15 | + |
| 16 | +// beadsVersion represents a parsed semantic version. |
| 17 | +type beadsVersion struct { |
| 18 | + major int |
| 19 | + minor int |
| 20 | + patch int |
| 21 | +} |
| 22 | + |
| 23 | +// parseBeadsVersion parses a version string like "0.44.0" into components. |
| 24 | +func parseBeadsVersion(v string) (beadsVersion, error) { |
| 25 | + // Strip leading 'v' if present |
| 26 | + v = strings.TrimPrefix(v, "v") |
| 27 | + |
| 28 | + // Split on dots |
| 29 | + parts := strings.Split(v, ".") |
| 30 | + if len(parts) < 2 { |
| 31 | + return beadsVersion{}, fmt.Errorf("invalid version format: %s", v) |
| 32 | + } |
| 33 | + |
| 34 | + major, err := strconv.Atoi(parts[0]) |
| 35 | + if err != nil { |
| 36 | + return beadsVersion{}, fmt.Errorf("invalid major version: %s", parts[0]) |
| 37 | + } |
| 38 | + |
| 39 | + minor, err := strconv.Atoi(parts[1]) |
| 40 | + if err != nil { |
| 41 | + return beadsVersion{}, fmt.Errorf("invalid minor version: %s", parts[1]) |
| 42 | + } |
| 43 | + |
| 44 | + patch := 0 |
| 45 | + if len(parts) >= 3 { |
| 46 | + // Handle versions like "0.44.0-dev" - take only numeric prefix |
| 47 | + patchStr := parts[2] |
| 48 | + if idx := strings.IndexFunc(patchStr, func(r rune) bool { |
| 49 | + return r < '0' || r > '9' |
| 50 | + }); idx != -1 { |
| 51 | + patchStr = patchStr[:idx] |
| 52 | + } |
| 53 | + if patchStr != "" { |
| 54 | + patch, err = strconv.Atoi(patchStr) |
| 55 | + if err != nil { |
| 56 | + return beadsVersion{}, fmt.Errorf("invalid patch version: %s", parts[2]) |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + return beadsVersion{major: major, minor: minor, patch: patch}, nil |
| 62 | +} |
| 63 | + |
| 64 | +// compare returns -1 if v < other, 0 if equal, 1 if v > other. |
| 65 | +func (v beadsVersion) compare(other beadsVersion) int { |
| 66 | + if v.major != other.major { |
| 67 | + if v.major < other.major { |
| 68 | + return -1 |
| 69 | + } |
| 70 | + return 1 |
| 71 | + } |
| 72 | + if v.minor != other.minor { |
| 73 | + if v.minor < other.minor { |
| 74 | + return -1 |
| 75 | + } |
| 76 | + return 1 |
| 77 | + } |
| 78 | + if v.patch != other.patch { |
| 79 | + if v.patch < other.patch { |
| 80 | + return -1 |
| 81 | + } |
| 82 | + return 1 |
| 83 | + } |
| 84 | + return 0 |
| 85 | +} |
| 86 | + |
| 87 | +// getBeadsVersion executes `bd --version` and parses the output. |
| 88 | +// Returns the version string (e.g., "0.44.0") or error. |
| 89 | +func getBeadsVersion() (string, error) { |
| 90 | + cmd := exec.Command("bd", "--version") |
| 91 | + output, err := cmd.Output() |
| 92 | + if err != nil { |
| 93 | + if exitErr, ok := err.(*exec.ExitError); ok { |
| 94 | + return "", fmt.Errorf("bd --version failed: %s", string(exitErr.Stderr)) |
| 95 | + } |
| 96 | + return "", fmt.Errorf("failed to run bd: %w (is beads installed?)", err) |
| 97 | + } |
| 98 | + |
| 99 | + // Parse output like "bd version 0.44.0 (dev)" |
| 100 | + // or "bd version 0.44.0" |
| 101 | + re := regexp.MustCompile(`bd version (\d+\.\d+(?:\.\d+)?(?:-\w+)?)`) |
| 102 | + matches := re.FindStringSubmatch(string(output)) |
| 103 | + if len(matches) < 2 { |
| 104 | + return "", fmt.Errorf("could not parse beads version from: %s", strings.TrimSpace(string(output))) |
| 105 | + } |
| 106 | + |
| 107 | + return matches[1], nil |
| 108 | +} |
| 109 | + |
| 110 | +// CheckBeadsVersion verifies that the installed beads version meets the minimum requirement. |
| 111 | +// Returns nil if the version is sufficient, or an error with details if not. |
| 112 | +func CheckBeadsVersion() error { |
| 113 | + installedStr, err := getBeadsVersion() |
| 114 | + if err != nil { |
| 115 | + return fmt.Errorf("cannot verify beads version: %w", err) |
| 116 | + } |
| 117 | + |
| 118 | + installed, err := parseBeadsVersion(installedStr) |
| 119 | + if err != nil { |
| 120 | + return fmt.Errorf("cannot parse installed beads version %q: %w", installedStr, err) |
| 121 | + } |
| 122 | + |
| 123 | + required, err := parseBeadsVersion(MinBeadsVersion) |
| 124 | + if err != nil { |
| 125 | + // This would be a bug in our code |
| 126 | + return fmt.Errorf("cannot parse required beads version %q: %w", MinBeadsVersion, err) |
| 127 | + } |
| 128 | + |
| 129 | + if installed.compare(required) < 0 { |
| 130 | + return fmt.Errorf("beads version %s is required, but %s is installed\n\nPlease upgrade beads: go install github.com/steveyegge/beads/cmd/bd@latest", MinBeadsVersion, installedStr) |
| 131 | + } |
| 132 | + |
| 133 | + return nil |
| 134 | +} |
0 commit comments