Skip to content

Commit

Permalink
GIGA: add updates module
Browse files Browse the repository at this point in the history
  • Loading branch information
celestix committed Aug 11, 2022
1 parent 73c0402 commit 313a7ff
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 19 deletions.
8 changes: 2 additions & 6 deletions changelog.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
{
"Version": "0.0.1",
"Changes": [
"fixed some bugs",
"added new features",
"new update space"
]
"Version": "0.0.0",
"Changes": []
}
14 changes: 14 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,41 @@ require (
github.com/PaulSonOfLars/gotgbot/v2 v2.0.0-rc.9
github.com/anonyindian/gotgproto v1.0.0-beta08.0.20220811082815-57b7254ee5d4
github.com/anonyindian/logger v1.0.0-alpha.3
github.com/go-git/go-git/v5 v5.4.2
github.com/go-redis/redis v6.15.9+incompatible
github.com/gotd/td v0.65.0
github.com/joho/godotenv v1.4.0
)

require (
github.com/Microsoft/go-winio v0.4.16 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7 // indirect
github.com/acomagu/bufpipe v1.0.3 // indirect
github.com/allegro/bigcache v1.2.1 // indirect
github.com/cenkalti/backoff/v4 v4.1.3 // indirect
github.com/emirpasic/gods v1.12.0 // indirect
github.com/go-faster/errors v0.6.1 // indirect
github.com/go-faster/jx v0.39.0 // indirect
github.com/go-faster/xor v0.3.0 // indirect
github.com/go-git/gcfg v1.5.0 // indirect
github.com/go-git/go-billy/v5 v5.3.1 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/gotd/ige v0.2.2 // indirect
github.com/gotd/neo v0.1.5 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 // indirect
github.com/klauspost/compress v1.15.9 // indirect
github.com/mattn/go-sqlite3 v1.14.14 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/onsi/ginkgo v1.16.5 // indirect
github.com/onsi/gomega v1.20.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/segmentio/asm v1.2.0 // indirect
github.com/sergi/go-diff v1.1.0 // indirect
github.com/xanzy/ssh-agent v0.3.0 // indirect
go.opentelemetry.io/otel v1.9.0 // indirect
go.opentelemetry.io/otel/trace v1.9.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
Expand All @@ -38,6 +51,7 @@ require (
golang.org/x/net v0.0.0-20220805013720-a33c5aa5df48 // indirect
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect
golang.org/x/sys v0.0.0-20220808155132-1c4a2a72c664 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gorm.io/driver/sqlite v1.3.6 // indirect
gorm.io/gorm v1.23.8 // indirect
nhooyr.io/websocket v1.8.7 // indirect
Expand Down
71 changes: 69 additions & 2 deletions go.sum

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions modules/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,29 @@ import (

func (m *module) LoadUpdate(dp *dispatcher.CustomDispatcher) {
dp.AddHandler(handlers.NewCommand("changelog", changeLog))
dp.AddHandler(handlers.NewCommand("update", update))
}

func update(ctx *ext.Context, u *ext.Update) error {
chat := u.EffectiveChat()
msg := u.EffectiveMessage
update, changed := utils.CheckChanges()
if !changed {
ctx.EditMessage(chat.GetID(), &tg.MessagesEditMessageRequest{
Message: "You're currently running the latest version.",
ID: msg.ID,
})
return dispatcher.EndGroups
}
text := entityhelper.Plain("Updating to ").Bold("GIGA ")
text.Code(fmt.Sprintf("v%s", update.Version))
ctx.EditMessage(chat.GetID(), &tg.MessagesEditMessageRequest{
Message: text.String,
Entities: text.Entities,
ID: msg.ID,
})
utils.DoUpdate(chat.GetID(), msg.ID)
return dispatcher.EndGroups
}

func changeLog(ctx *ext.Context, u *ext.Update) error {
Expand Down
37 changes: 37 additions & 0 deletions utils/execmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package utils

import (
"os/exec"

git "github.com/go-git/go-git/v5"
)

func buildBinary() error {
return exec.Command("go", "build", ".").Run()
}

func buildWithClone(tmpDir string) error {
if err := gitClone(tmpDir); err != nil {
return err
}
if err := exec.Command("go", "build", "tmp", "-o=bin/giga").Run(); err != nil {
return err
}
exec.Command("rm", "-rf", tmpDir).Run()
return nil
}

func gitPull() error {
return exec.Command("git", "pull").Run()
}

func gitClone(dir string) error {
_, err := git.PlainClone(dir, false, &git.CloneOptions{
URL: "https://github.com/GigaUserbot/GIGA",
RecurseSubmodules: git.DefaultSubmoduleRecursionDepth,
})
if err != nil {
return err
}
return nil
}
36 changes: 26 additions & 10 deletions utils/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,37 @@ import (

var tempDir = os.TempDir()

func Restart(delay int, chatId int64, msgId int, msgText string) {
// command := fmt.Sprintf("run main.go -delay=5 -chat=%d -msg=%d", chat.GetID(), u.EffectiveMessage.ID)
func restart(executable string, preArgs []string, delay int, chatId int64, msgId int, msgText string) {
args := []string{fmt.Sprintf("-delay=%d", delay), fmt.Sprintf("-chat=%d", chatId), fmt.Sprintf("-msg_id=%d", msgId), fmt.Sprintf("-msg_text=%s", msgText)}
command := []string{"run", "main.go"}
command = append(command, args...)
// fmt.Println(command)
executable, err := os.Executable()
if strings.Contains(executable, tempDir) || err != nil {
executable = "go"
}
cmd := exec.Command(executable, command...)
preArgs = append(preArgs, args...)
cmd := exec.Command(executable, preArgs...)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
cmd.Start()
cmd.Process.Release()
os.Exit(1)
}

func Restart(delay int, chatId int64, msgId int, msgText string) {
// // command := fmt.Sprintf("run main.go -delay=5 -chat=%d -msg=%d", chat.GetID(), u.EffectiveMessage.ID)
// args := []string{fmt.Sprintf("-delay=%d", delay), fmt.Sprintf("-chat=%d", chatId), fmt.Sprintf("-msg_id=%d", msgId), fmt.Sprintf("-msg_text=%s", msgText)}
// command := []string{"run", "main.go"}
// command = append(command, args...)
// // fmt.Println(command)

args := []string{}
executable, err := os.Executable()
if strings.Contains(executable, tempDir) || err != nil {
executable = "go"
args = []string{"run", "main.go"}
}
restart(executable, args, delay, chatId, msgId, msgText)
// cmd := exec.Command(executable, command...)
// cmd.Stderr = os.Stderr
// cmd.Stdout = os.Stdout
// cmd.Stdin = os.Stdin
// cmd.Start()
// cmd.Process.Release()
// os.Exit(1)
}
26 changes: 25 additions & 1 deletion utils/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,28 @@ import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"strconv"
"strings"

"github.com/anonyindian/logger"
)

func DoUpdate(chatId int64, msgId int) error {
err := gitPull()
if err != nil {
tmpDir := "."
buildWithClone(tmpDir)
restart("giga", []string{}, 5, chatId, msgId, "Updated Successfully.")
}
err = buildBinary()
if err != nil {
return err
}
Restart(5, chatId, msgId, "Updated Successfully.")
return nil
}

var CurrentUpdate = &Update{}
var currentVersion *version

Expand Down Expand Up @@ -39,7 +55,15 @@ func InitUpdate(l *logger.Logger) {
func CheckChanges() (*Update, bool) {
var u Update
origin := "https://raw.githubusercontent.com/GigaUserbot/GIGA/dev/changelog.json"
json.Unmarshal([]byte(origin), &u)
resp, err := http.Get(origin)
if err != nil {
return nil, false
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, false
}
json.Unmarshal(b, &u)
return &u, CompareVersion(u.Version)
}

Expand Down

0 comments on commit 313a7ff

Please sign in to comment.