|
| 1 | +package internal |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | +) |
| 10 | + |
| 11 | +const ( |
| 12 | + githubAPIURL = "https://api.github.com/repos/alpindale/ssh-dashboard/releases/latest" |
| 13 | + updateCheckTimeout = 3 * time.Second |
| 14 | +) |
| 15 | + |
| 16 | +type UpdateInfo struct { |
| 17 | + Available bool |
| 18 | + LatestVersion string |
| 19 | + CurrentVersion string |
| 20 | +} |
| 21 | + |
| 22 | +type githubRelease struct { |
| 23 | + TagName string `json:"tag_name"` |
| 24 | + HTMLURL string `json:"html_url"` |
| 25 | +} |
| 26 | + |
| 27 | +func CheckForUpdates() UpdateInfo { |
| 28 | + currentVersion := Version |
| 29 | + if currentVersion == "dev" { |
| 30 | + return UpdateInfo{Available: false, CurrentVersion: currentVersion} |
| 31 | + } |
| 32 | + |
| 33 | + latestVersion, err := fetchLatestVersion() |
| 34 | + if err != nil { |
| 35 | + return UpdateInfo{Available: false, CurrentVersion: currentVersion} |
| 36 | + } |
| 37 | + |
| 38 | + needsUpdate := compareVersions(currentVersion, latestVersion) |
| 39 | + |
| 40 | + return UpdateInfo{ |
| 41 | + Available: needsUpdate, |
| 42 | + LatestVersion: latestVersion, |
| 43 | + CurrentVersion: currentVersion, |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func fetchLatestVersion() (string, error) { |
| 48 | + client := &http.Client{ |
| 49 | + Timeout: updateCheckTimeout, |
| 50 | + } |
| 51 | + |
| 52 | + resp, err := client.Get(githubAPIURL) |
| 53 | + if err != nil { |
| 54 | + return "", err |
| 55 | + } |
| 56 | + defer resp.Body.Close() |
| 57 | + |
| 58 | + if resp.StatusCode != http.StatusOK { |
| 59 | + return "", fmt.Errorf("github api returned status %d", resp.StatusCode) |
| 60 | + } |
| 61 | + |
| 62 | + var release githubRelease |
| 63 | + if err := json.NewDecoder(resp.Body).Decode(&release); err != nil { |
| 64 | + return "", err |
| 65 | + } |
| 66 | + |
| 67 | + return release.TagName, nil |
| 68 | +} |
| 69 | + |
| 70 | +func compareVersions(current, latest string) bool { |
| 71 | + current = strings.TrimPrefix(current, "v") |
| 72 | + latest = strings.TrimPrefix(latest, "v") |
| 73 | + |
| 74 | + currentBase := strings.Split(strings.Split(current, "-")[0], "+")[0] |
| 75 | + latestBase := strings.Split(strings.Split(latest, "-")[0], "+")[0] |
| 76 | + |
| 77 | + currentParts := strings.Split(currentBase, ".") |
| 78 | + latestParts := strings.Split(latestBase, ".") |
| 79 | + |
| 80 | + for len(currentParts) < 3 { |
| 81 | + currentParts = append(currentParts, "0") |
| 82 | + } |
| 83 | + for len(latestParts) < 3 { |
| 84 | + latestParts = append(latestParts, "0") |
| 85 | + } |
| 86 | + |
| 87 | + for i := 0; i < 3; i++ { |
| 88 | + var currentNum, latestNum int |
| 89 | + fmt.Sscanf(currentParts[i], "%d", ¤tNum) |
| 90 | + fmt.Sscanf(latestParts[i], "%d", &latestNum) |
| 91 | + |
| 92 | + if latestNum > currentNum { |
| 93 | + return true |
| 94 | + } else if latestNum < currentNum { |
| 95 | + return false |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return false |
| 100 | +} |
0 commit comments