feat: self update

This commit is contained in:
krau
2024-10-10 23:38:23 +08:00
parent 1cbcebc148
commit e168874c9a
5 changed files with 122 additions and 2 deletions

View File

@@ -1,6 +1,8 @@
package cmd
import (
"fmt"
"github.com/spf13/cobra"
)
@@ -13,5 +15,7 @@ var rootCmd = &cobra.Command{
}
func Execute() {
rootCmd.Execute()
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
}
}

View File

@@ -5,6 +5,9 @@ import (
"runtime"
"github.com/krau/SaveAny-Bot/common"
"github.com/rhysd/go-github-selfupdate/selfupdate"
"github.com/blang/semver"
"github.com/spf13/cobra"
)
@@ -16,3 +19,28 @@ var VersionCmd = &cobra.Command{
fmt.Printf("saveany-bot version: %s %s/%s\nBuildTime: %s, Commit: %s\n", common.Version, runtime.GOOS, runtime.GOARCH, common.BuildTime, common.GitCommit)
},
}
var upgradeCmd = &cobra.Command{
Use: "upgrade",
Aliases: []string{"up"},
Short: "Upgrade saveany-bot to the latest version",
Run: func(cmd *cobra.Command, args []string) {
v := semver.MustParse(common.Version)
latest, err := selfupdate.UpdateSelf(v, "krau/SaveAny-Bot")
if err != nil {
fmt.Println("Binary update failed:", err)
return
}
if latest.Version.Equals(v) {
fmt.Println("Current binary is the latest version", common.Version)
} else {
fmt.Println("Successfully updated to version", latest.Version)
fmt.Println("Release note:\n", latest.ReleaseNotes)
}
},
}
func init() {
rootCmd.AddCommand(VersionCmd)
rootCmd.AddCommand(upgradeCmd)
}