Files

101 lines
2.1 KiB
Go

package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"net/http"
"os"
"strings"
)
const (
CurrentVersion = "v0.1.0"
ReleaseURL = "https://gitea.sanplex.xyz/Sansan/dimma/releases/latest"
)
type ReleaseInfo struct {
TagName string `json:"tag_name"`
}
func main() {
check := flag.Bool("check", false, "Check for updates")
flag.Parse()
if !*check {
flag.Usage()
return
}
fmt.Printf("Current version: %s\n", CurrentVersion)
latestVersion, err := fetchLatestVersion()
if err != nil {
fmt.Printf("Error fetching latest version: %v\n", err)
return
}
fmt.Printf("Latest version: %s\n", latestVersion)
if compareVersions(latestVersion, CurrentVersion) > 0 {
fmt.Printf("Update available: %s\n", latestVersion)
} else {
fmt.Println("Up to date")
}
}
func fetchLatestVersion() (string, error) {
resp, err := http.Get(ReleaseURL)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("HTTP %d", resp.StatusCode)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
// Gitea releases API returns JSON with tag_name
var release ReleaseInfo
if err := json.Unmarshal(body, &release); err != nil {
// Try to extract from HTML if not JSON
release.TagName = extractVersionFromHTML(string(body))
if release.TagName == "" {
return "", fmt.Errorf("failed to parse release info")
}
}
return release.TagName, nil
}
func extractVersionFromHTML(html string) string {
// Simple extraction from HTML (fallback for non-API URLs)
// Looks for patterns like "v0.1.0" or "0.1.0"
parts := strings.Split(html, "v")
for _, part := range parts {
if len(part) > 0 && part[0] >= '0' && part[0] <= '9' {
version := "v" + strings.Split(part, "\"")[0]
version = strings.Split(version, " ")[0]
version = strings.Split(version, ">")[0]
version = strings.TrimSpace(version)
if version != "" {
return version
}
}
}
return ""
}
func compareVersions(a, b string) int {
// Simple version comparison (v0.1.0 format)
a = strings.TrimPrefix(a, "v")
b = strings.TrimPrefix(b, "v")
return strings.Compare(a, b)
}