2021-01-09 15:14:47 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-01-09 15:28:08 +00:00
|
|
|
"errors"
|
2021-01-09 15:14:47 +00:00
|
|
|
"fmt"
|
2021-01-09 15:28:08 +00:00
|
|
|
"log"
|
2021-01-09 15:14:47 +00:00
|
|
|
"os"
|
2021-01-09 15:28:08 +00:00
|
|
|
"runtime"
|
|
|
|
|
|
|
|
selfupdate "github.com/creativeprojects/go-selfupdate"
|
2021-01-09 15:14:47 +00:00
|
|
|
)
|
|
|
|
|
2021-01-09 15:28:08 +00:00
|
|
|
//go:generate sh injectGitVars.sh
|
|
|
|
|
2021-01-09 15:14:47 +00:00
|
|
|
var version = "v0.1.0"
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
if len(os.Args) < 2 {
|
|
|
|
fmt.Println("provide a command")
|
|
|
|
os.Exit(1)
|
2021-01-09 15:41:31 +00:00
|
|
|
} else if os.Args[1] == "upgrade" || os.Args[1] == "up" {
|
2021-01-09 15:49:03 +00:00
|
|
|
fmt.Println("Trying Upgrade... ")
|
2021-01-09 15:40:57 +00:00
|
|
|
err := update(gitTag)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
} else {
|
|
|
|
fmt.Println("Success")
|
|
|
|
}
|
2021-01-09 15:14:47 +00:00
|
|
|
} else if os.Args[1] == "version" {
|
|
|
|
fmt.Print("Version ")
|
2021-01-09 15:28:08 +00:00
|
|
|
fmt.Println(gitTag)
|
2021-01-09 15:14:47 +00:00
|
|
|
} else {
|
|
|
|
fmt.Println("provide a valid command (version or upgrade)")
|
|
|
|
}
|
|
|
|
}
|
2021-01-09 15:28:08 +00:00
|
|
|
|
|
|
|
func update(version string) error {
|
2021-01-09 15:47:48 +00:00
|
|
|
source, _ := selfupdate.NewGiteaSource(selfupdate.GiteaConfig{BaseURL: "https://git.lbsfilm.at/"})
|
|
|
|
updater, err := selfupdate.NewUpdater(selfupdate.Config{
|
|
|
|
Source: source,
|
|
|
|
Validator: nil,
|
|
|
|
OS: runtime.GOOS,
|
|
|
|
Arch: runtime.GOARCH,
|
|
|
|
Arm: 0,
|
|
|
|
})
|
|
|
|
|
|
|
|
latest, found, err := updater.DetectLatest("lbsadmin/goselfupdatetest")
|
2021-01-09 15:28:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error occurred while detecting version: %v", err)
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
return fmt.Errorf("latest version for %s/%s could not be found from github repository", runtime.GOOS, runtime.GOARCH)
|
|
|
|
}
|
|
|
|
|
|
|
|
if latest.LessOrEqual(version) {
|
|
|
|
log.Printf("Current version (%s) is the latest", version)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
exe, err := os.Executable()
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("could not locate executable path")
|
|
|
|
}
|
2021-01-09 15:47:48 +00:00
|
|
|
if err := updater.UpdateTo(latest, exe); err != nil {
|
2021-01-09 15:28:08 +00:00
|
|
|
return fmt.Errorf("error occurred while updating binary: %v", err)
|
|
|
|
}
|
|
|
|
log.Printf("Successfully updated to version %s", latest.Version())
|
|
|
|
return nil
|
|
|
|
}
|