|
| 1 | +package commands |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + "os" |
| 10 | + "os/exec" |
| 11 | + "runtime" |
| 12 | + "strings" |
| 13 | + "time" |
| 14 | + |
| 15 | + "github.com/spf13/cobra" |
| 16 | + |
| 17 | + "github.com/lpagent/cli/internal/version" |
| 18 | +) |
| 19 | + |
| 20 | +const ( |
| 21 | + githubRepo = "lpagent/cli" |
| 22 | + installURL = "https://raw.githubusercontent.com/lpagent/cli/main/install.sh" |
| 23 | +) |
| 24 | + |
| 25 | +// Testable function vars |
| 26 | +var latestVersionFetcher = fetchLatestVersion |
| 27 | + |
| 28 | +func NewUpgradeCmd() *cobra.Command { |
| 29 | + return &cobra.Command{ |
| 30 | + Use: "upgrade", |
| 31 | + Short: "Upgrade to the latest version", |
| 32 | + RunE: runUpgrade, |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func runUpgrade(cmd *cobra.Command, args []string) error { |
| 37 | + w := cmd.OutOrStdout() |
| 38 | + |
| 39 | + current := version.Version |
| 40 | + if current == "dev" { |
| 41 | + fmt.Fprintln(w, "Development build — upgrade not applicable. Build from source instead.") |
| 42 | + return nil |
| 43 | + } |
| 44 | + |
| 45 | + fmt.Fprintf(w, "Current version: %s\n", current) |
| 46 | + fmt.Fprint(w, "Checking for updates... ") |
| 47 | + |
| 48 | + latest, err := latestVersionFetcher() |
| 49 | + if err != nil { |
| 50 | + fmt.Fprintln(w, "failed") |
| 51 | + return fmt.Errorf("could not check for updates: %w", err) |
| 52 | + } |
| 53 | + |
| 54 | + if !isUpdateAvailable(current, latest) { |
| 55 | + fmt.Fprintln(w, "already up to date.") |
| 56 | + return nil |
| 57 | + } |
| 58 | + |
| 59 | + fmt.Fprintf(w, "update available: %s\n\n", latest) |
| 60 | + |
| 61 | + // On macOS/Linux, run the install script directly |
| 62 | + if runtime.GOOS == "darwin" || runtime.GOOS == "linux" { |
| 63 | + fmt.Fprintln(w, "Upgrading...") |
| 64 | + return runInstallScript(cmd.Context(), w) |
| 65 | + } |
| 66 | + |
| 67 | + // Windows or other: print download link |
| 68 | + fmt.Fprintf(w, "Download the latest release:\n") |
| 69 | + fmt.Fprintf(w, " https://github.com/%s/releases/tag/v%s\n", githubRepo, latest) |
| 70 | + return nil |
| 71 | +} |
| 72 | + |
| 73 | +func runInstallScript(ctx context.Context, w io.Writer) error { |
| 74 | + cmd := exec.CommandContext(ctx, "bash", "-c", |
| 75 | + fmt.Sprintf("curl -fsSL %s | bash", installURL)) |
| 76 | + cmd.Stdout = w |
| 77 | + cmd.Stderr = os.Stderr |
| 78 | + return cmd.Run() |
| 79 | +} |
| 80 | + |
| 81 | +func fetchLatestVersion() (string, error) { |
| 82 | + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 83 | + defer cancel() |
| 84 | + |
| 85 | + url := fmt.Sprintf("https://api.github.com/repos/%s/releases/latest", githubRepo) |
| 86 | + req, err := http.NewRequestWithContext(ctx, "GET", url, nil) |
| 87 | + if err != nil { |
| 88 | + return "", err |
| 89 | + } |
| 90 | + req.Header.Set("Accept", "application/vnd.github.v3+json") |
| 91 | + |
| 92 | + resp, err := (&http.Client{Timeout: 5 * time.Second}).Do(req) |
| 93 | + if err != nil { |
| 94 | + return "", err |
| 95 | + } |
| 96 | + defer resp.Body.Close() |
| 97 | + |
| 98 | + if resp.StatusCode != http.StatusOK { |
| 99 | + return "", fmt.Errorf("unexpected status: %d", resp.StatusCode) |
| 100 | + } |
| 101 | + |
| 102 | + var release struct { |
| 103 | + TagName string `json:"tag_name"` |
| 104 | + } |
| 105 | + if err := json.NewDecoder(io.LimitReader(resp.Body, 1<<20)).Decode(&release); err != nil { |
| 106 | + return "", err |
| 107 | + } |
| 108 | + |
| 109 | + return strings.TrimPrefix(release.TagName, "v"), nil |
| 110 | +} |
| 111 | + |
| 112 | +func isUpdateAvailable(current, latest string) bool { |
| 113 | + current = strings.TrimSpace(strings.TrimPrefix(current, "v")) |
| 114 | + latest = strings.TrimSpace(strings.TrimPrefix(latest, "v")) |
| 115 | + if current == "" || latest == "" || current == "dev" { |
| 116 | + return false |
| 117 | + } |
| 118 | + return latest != current && compareSemver(latest, current) > 0 |
| 119 | +} |
| 120 | + |
| 121 | +// compareSemver compares two semver strings. Returns >0 if a > b. |
| 122 | +func compareSemver(a, b string) int { |
| 123 | + partsA := strings.SplitN(a, ".", 3) |
| 124 | + partsB := strings.SplitN(b, ".", 3) |
| 125 | + for i := 0; i < 3; i++ { |
| 126 | + var va, vb int |
| 127 | + if i < len(partsA) { |
| 128 | + fmt.Sscanf(partsA[i], "%d", &va) |
| 129 | + } |
| 130 | + if i < len(partsB) { |
| 131 | + fmt.Sscanf(partsB[i], "%d", &vb) |
| 132 | + } |
| 133 | + if va != vb { |
| 134 | + return va - vb |
| 135 | + } |
| 136 | + } |
| 137 | + return 0 |
| 138 | +} |
0 commit comments