add / update the update commands

This commit is contained in:
Henry Dollman
2024-07-20 18:27:01 -04:00
parent a05db7f3e4
commit be23486abf
6 changed files with 147 additions and 12 deletions

View File

@@ -1,6 +1,7 @@
package main
import (
"fmt"
"log"
"os"
"strings"
@@ -15,36 +16,37 @@ func updateBeszel(cmd *cobra.Command, args []string) {
var found bool
var err error
currentVersion := semver.MustParse(Version)
log.Println("Beszel", currentVersion)
log.Println("Checking for updates...")
fmt.Println("beszel", currentVersion)
fmt.Println("Checking for updates...")
latest, found, err = selfupdate.DetectLatest("henrygd/beszel")
if err != nil {
log.Fatal("Error checking for updates:", err)
}
if !found {
log.Fatal("No updates found")
fmt.Println("Error checking for updates:", err)
os.Exit(1)
}
log.Println("Latest version", "v", latest.Version)
if !found {
fmt.Println("No updates found")
os.Exit(0)
}
fmt.Println("Latest version", "v", latest.Version)
if latest.Version.LTE(currentVersion) {
log.Println("You are up to date")
fmt.Println("You are up to date")
return
}
var binaryPath string
log.Printf("Updating from %s to %s...", currentVersion, latest.Version)
fmt.Printf("Updating from %s to %s...", currentVersion, latest.Version)
binaryPath, err = os.Executable()
if err != nil {
log.Fatal("Error getting binary path:", err)
fmt.Println("Error getting binary path:", err)
os.Exit(1)
}
err = selfupdate.UpdateTo(latest.AssetURL, binaryPath)
if err != nil {
log.Fatal("Please try rerunning with sudo. Error:", err)
fmt.Println("Please try rerunning with sudo. Error:", err)
os.Exit(1)
}
log.Printf("Successfully updated: %s -> %s\n\n%s", currentVersion, latest.Version, strings.TrimSpace(latest.ReleaseNotes))