diff --git a/beszel/cmd/agent/agent.go b/beszel/cmd/agent/agent.go index 26190d3..6dfa653 100644 --- a/beszel/cmd/agent/agent.go +++ b/beszel/cmd/agent/agent.go @@ -3,7 +3,6 @@ package main import ( "beszel" "beszel/internal/agent" - "beszel/internal/update" "fmt" "log" "os" @@ -17,7 +16,7 @@ func main() { case "-v": fmt.Println(beszel.AppName+"-agent", beszel.Version) case "update": - update.UpdateBeszelAgent() + agent.Update() } os.Exit(0) } diff --git a/beszel/cmd/hub/hub.go b/beszel/cmd/hub/hub.go index 379470a..c79ab86 100644 --- a/beszel/cmd/hub/hub.go +++ b/beszel/cmd/hub/hub.go @@ -3,7 +3,6 @@ package main import ( "beszel" "beszel/internal/hub" - "beszel/internal/update" _ "beszel/migrations" "github.com/pocketbase/pocketbase" @@ -22,7 +21,7 @@ func main() { app.RootCmd.AddCommand(&cobra.Command{ Use: "update", Short: "Update " + beszel.AppName + " to the latest version", - Run: func(_ *cobra.Command, _ []string) { update.UpdateBeszel() }, + Run: hub.Update, }) hub.NewHub(app).Run() diff --git a/beszel/internal/update/update.go b/beszel/internal/agent/update.go similarity index 50% rename from beszel/internal/update/update.go rename to beszel/internal/agent/update.go index 9fc7ce7..e8fa736 100644 --- a/beszel/internal/update/update.go +++ b/beszel/internal/agent/update.go @@ -1,5 +1,4 @@ -// Package update handles updating beszel and beszel-agent. -package update +package agent import ( "beszel" @@ -11,51 +10,8 @@ import ( "github.com/rhysd/go-github-selfupdate/selfupdate" ) -func UpdateBeszel() { - var latest *selfupdate.Release - var found bool - var err error - currentVersion := semver.MustParse(beszel.Version) - fmt.Println("beszel", currentVersion) - fmt.Println("Checking for updates...") - updater, _ := selfupdate.NewUpdater(selfupdate.Config{ - Filters: []string{"beszel_"}, - }) - latest, found, err = updater.DetectLatest("henrygd/beszel") - - if err != nil { - fmt.Println("Error checking for updates:", err) - os.Exit(1) - } - - if !found { - fmt.Println("No updates found") - os.Exit(0) - } - - fmt.Println("Latest version:", latest.Version) - - if latest.Version.LTE(currentVersion) { - fmt.Println("You are up to date") - return - } - - var binaryPath string - fmt.Printf("Updating from %s to %s...\n", currentVersion, latest.Version) - binaryPath, err = os.Executable() - if err != nil { - fmt.Println("Error getting binary path:", err) - os.Exit(1) - } - err = selfupdate.UpdateTo(latest.AssetURL, binaryPath) - if err != nil { - fmt.Println("Please try rerunning with sudo. Error:", err) - os.Exit(1) - } - fmt.Printf("Successfully updated to %s\n\n%s\n", latest.Version, strings.TrimSpace(latest.ReleaseNotes)) -} - -func UpdateBeszelAgent() { +// Update updates beszel-agent to the latest version +func Update() { var latest *selfupdate.Release var found bool var err error diff --git a/beszel/internal/hub/update.go b/beszel/internal/hub/update.go new file mode 100644 index 0000000..cc30b45 --- /dev/null +++ b/beszel/internal/hub/update.go @@ -0,0 +1,57 @@ +package hub + +import ( + "beszel" + "fmt" + "os" + "strings" + + "github.com/blang/semver" + "github.com/rhysd/go-github-selfupdate/selfupdate" + "github.com/spf13/cobra" +) + +// Update updates beszel to the latest version +func Update(_ *cobra.Command, _ []string) { + var latest *selfupdate.Release + var found bool + var err error + currentVersion := semver.MustParse(beszel.Version) + fmt.Println("beszel", currentVersion) + fmt.Println("Checking for updates...") + updater, _ := selfupdate.NewUpdater(selfupdate.Config{ + Filters: []string{"beszel_"}, + }) + latest, found, err = updater.DetectLatest("henrygd/beszel") + + if err != nil { + fmt.Println("Error checking for updates:", err) + os.Exit(1) + } + + if !found { + fmt.Println("No updates found") + os.Exit(0) + } + + fmt.Println("Latest version:", latest.Version) + + if latest.Version.LTE(currentVersion) { + fmt.Println("You are up to date") + return + } + + var binaryPath string + fmt.Printf("Updating from %s to %s...\n", currentVersion, latest.Version) + binaryPath, err = os.Executable() + if err != nil { + fmt.Println("Error getting binary path:", err) + os.Exit(1) + } + err = selfupdate.UpdateTo(latest.AssetURL, binaryPath) + if err != nil { + fmt.Println("Please try rerunning with sudo. Error:", err) + os.Exit(1) + } + fmt.Printf("Successfully updated to %s\n\n%s\n", latest.Version, strings.TrimSpace(latest.ReleaseNotes)) +}