add auto updater

This commit is contained in:
olebeck 2022-09-05 23:57:11 +02:00
parent 146e8dc00d
commit d240a125be
8 changed files with 114 additions and 22 deletions

View File

@ -33,8 +33,19 @@ jobs:
- run: go get ./cmd/bedrocktool
- name: Install SSH Key
uses: shimataro/ssh-key-action@v2
with:
key: ${{ secrets.SSH_PRIVATE_KEY }}
- name: Adding Known Hosts
run: ssh-keyscan -H ${{ secrets.SSH_HOST }} >> ~/.ssh/known_hosts
- name: build
run: make -j
run: make -j dists updates
- name: Deploy with rsync
run: rsync -avz ./public/ olebeck@${{ secrets.SSH_HOST }}:/var/www/updates/bedrocktool/
- uses: actions-ecosystem/action-get-latest-tag@v1
id: get-latest-tag

2
.gitignore vendored
View File

@ -16,3 +16,5 @@ keys.db
/skins/
/worlds/
/dist/
/public/
/builds/

View File

@ -1,12 +1,14 @@
TAG = $(shell git describe --tags)
NAME = bedrocktool-${TAG}
SRCS = $(wildcard *.go)
SRCS = $(wildcard **/*.go)
GC = go build -ldflags "-s -w -X main.version=${TAG}"
GC = go build -ldflags "-s -w -X utils.Version=${TAG}"
.PHONY: dists clean updates
# check if packs are supported
HAVE_PACKS = false
ifeq ($(shell head -c 7 cmd/bedrocktool/utils/resourcepack-ace.go.ignore),package)
ifeq ($(shell head -c 7 ./utils/resourcepack-ace.go.ignore),package)
HAVE_PACKS = true
endif
@ -15,6 +17,9 @@ ifeq ($(HAVE_PACKS),true)
GC += -overlay overlay.json
endif
bedrocktool: $(SRCS)
$(GC) -o $@ ./cmd/bedrocktool
BUILDS=\
windows_386.exe\
windows_amd64.exe\
@ -27,22 +32,28 @@ BUILDS=\
linux_arm64\
linux_arm
DISTS=$(BUILDS:%=dist/$(NAME)_%)
all: $(DISTS)
.PHONY: all clean
dists: $(DISTS)
$(DISTS): OS = $(word 2,$(subst _, ,$@))
$(DISTS): ARCH = $(word 1,$(subst ., ,$(word 3,$(subst _, ,$@))))
$(DISTS): BUILD = builds/$(OS)-$(ARCH)
dist:
mkdir -p dist
dist builds:
mkdir -p dist builds
$(DISTS): dist $(SRCS)
@echo "building: $@"
GOOS=$(OS) GOARCH=$(ARCH) $(GC) -o $@ ./cmd/bedrocktool
$(DISTS): dist builds $(SRCS)
$(info building: $@)
GOOS=$(OS) GOARCH=$(ARCH) $(GC) -o $(BUILD) ./cmd/bedrocktool
cp $(BUILD) $@
UPDATES=$(BUILDS)
$(UPDATES): OS = $(word 1,$(subst _, ,$@))
$(UPDATES): ARCH = $(word 1,$(subst ., ,$(word 2,$(subst _, ,$@))))
updates: $(UPDATES)
$(UPDATES): $(DISTS)
go-selfupdate -platform $(OS)-$(ARCH) builds/ $(TAG)
clean:
rm -r dist
rm -r dist builds public

View File

@ -20,8 +20,6 @@ import (
"github.com/sirupsen/logrus"
)
var version string
func exit() {
logrus.Info("\nExiting\n")
for i := len(utils.G_exit) - 1; i >= 0; i-- { // go through cleanup functions reversed
@ -32,8 +30,17 @@ func exit() {
func main() {
logrus.SetLevel(logrus.DebugLevel)
if version != "" {
logrus.Infof("bedrocktool version: %s\n", version)
if utils.Version != "" {
logrus.Infof("bedrocktool version: %s", utils.Version)
}
newVersion, err := utils.Updater.UpdateAvailable()
if err != nil {
logrus.Error(err)
}
if err == nil && newVersion != utils.Version {
logrus.Infof("Update Available: %s", newVersion)
}
ctx, cancel := context.WithCancel(context.Background())
@ -41,8 +48,7 @@ func main() {
flag.BoolVar(&utils.G_debug, "debug", false, "debug mode")
flag.BoolVar(&utils.G_preload_packs, "preload", false, "preload resourcepacks for proxy")
enable_dns := flag.Bool("dns", false, "enable dns server for consoles")
println(utils.A)
println("")
subcommands.Register(subcommands.HelpCommand(), "")
subcommands.ImportantFlag("debug")
subcommands.ImportantFlag("dns")

4
go.mod
View File

@ -18,8 +18,12 @@ require (
)
require (
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0 // indirect
github.com/kr/binarydist v0.1.0 // indirect
github.com/mattn/go-colorable v0.1.9 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/sanbornm/go-selfupdate v0.0.0-20210106163404-c9b625feac49 // indirect
gopkg.in/inconshreveable/go-update.v0 v0.0.0-20150814200126-d8b0b1d421aa // indirect
)
//replace github.com/sandertv/gophertunnel => ./gophertunnel

8
go.sum
View File

@ -31,9 +31,13 @@ github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/jinzhu/copier v0.3.5 h1:GlvfUwHk62RokgqVNvYsku0TATCF7bAHVwEXoBh3iJg=
github.com/jinzhu/copier v0.3.5/go.mod h1:DfbEm0FYsaqBcKcFuvmOZb218JkPGtvSHsKg8S8hyyg=
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0 h1:iQTw/8FWTuc7uiaSepXwyf3o52HaUYcV+Tu66S3F5GA=
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0/go.mod h1:1NbS8ALrpOvjt0rHPNLyCIeMtbizbir8U//inJ+zuB8=
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
github.com/klauspost/compress v1.15.9 h1:wKRjX6JRtDdrE9qwa4b/Cip7ACOshUI4smpCQanqjSY=
github.com/klauspost/compress v1.15.9/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU=
github.com/kr/binarydist v0.1.0 h1:6kAoLA9FMMnNGSehX0s1PdjbEaACznAv/W219j2uvyo=
github.com/kr/binarydist v0.1.0/go.mod h1:DY7S//GCoz1BCd0B0EVrinCKAZN3pXe+MDaIZbXQVgM=
github.com/mattn/go-colorable v0.1.9 h1:sqDoxXbdeALODt0DAeJCVp38ps9ZogZEAXjus69YV3U=
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
@ -53,6 +57,8 @@ github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+W
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sanbornm/go-selfupdate v0.0.0-20210106163404-c9b625feac49 h1:LuxslTBxJrrNeKfqoywIERWWhH43TgiVAiPEVlhgNBA=
github.com/sanbornm/go-selfupdate v0.0.0-20210106163404-c9b625feac49/go.mod h1:fY313ZGG810aWruFYcyq3coFpHDrWJVoMfSRI81y1r4=
github.com/sandertv/go-raknet v1.11.1 h1:0auvhHoZNyC/Z1l5xqniE3JE+w3MGO3n3JXEGHzdlRE=
github.com/sandertv/go-raknet v1.11.1/go.mod h1:Gx+WgZBMQ0V2UoouGoJ8Wj6CDrMBQ4SB2F/ggpl5/+Y=
github.com/segmentio/fasthash v1.0.3 h1:EI9+KE1EwvMLBWwjpRDc+fEM+prwxDYbslddQGtrmhM=
@ -132,6 +138,8 @@ google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/inconshreveable/go-update.v0 v0.0.0-20150814200126-d8b0b1d421aa h1:drvf2JoUL1fz3ttkGNkw+rf3kZa2//7XkYGpSO4NHNA=
gopkg.in/inconshreveable/go-update.v0 v0.0.0-20150814200126-d8b0b1d421aa/go.mod h1:tuNm0ntQ7IH9VSA39XxzLMpee5c2DwgIbjD4x3ydo8Y=
gopkg.in/square/go-jose.v2 v2.6.0 h1:NGk74WTnPKBNUhNzQX7PYcTLUjoq7mzKk2OKbvwk2iI=
gopkg.in/square/go-jose.v2 v2.6.0/go.mod h1:M9dMgbHiYLoDGQrXy7OpJDJWiKiU//h+vD76mk0e1AI=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=

33
subcommands/update.go Normal file
View File

@ -0,0 +1,33 @@
package subcommands
import (
"context"
"flag"
"github.com/bedrock-tool/bedrocktool/utils"
"github.com/sirupsen/logrus"
"github.com/google/subcommands"
)
type UpdateCMD struct{}
func (*UpdateCMD) Name() string { return "update" }
func (*UpdateCMD) Synopsis() string { return "self updates to latest version" }
func (c *UpdateCMD) SetFlags(f *flag.FlagSet) {}
func (c *UpdateCMD) Usage() string {
return c.Name() + ": " + c.Synopsis() + "\n"
}
func (c *UpdateCMD) Execute(ctx context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
if err := utils.Updater.Update(); err != nil {
logrus.Error(err)
}
return 0
}
func init() {
utils.RegisterCommand(&UpdateCMD{})
}

17
utils/updater.go Normal file
View File

@ -0,0 +1,17 @@
package utils
import "github.com/sanbornm/go-selfupdate/selfupdate"
var Version string
const updateServer = "https://updates.yuv.pink/"
var Updater = &selfupdate.Updater{
CurrentVersion: Version,
ApiURL: updateServer,
BinURL: updateServer,
DiffURL: updateServer,
Dir: "update/",
CmdName: "bedrocktool", // app name
}