Skip to content

Commit

Permalink
GIGA: Updater: v2
Browse files Browse the repository at this point in the history
  • Loading branch information
celestix committed Sep 8, 2022
1 parent 8d36231 commit 78a96be
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 10 deletions.
4 changes: 2 additions & 2 deletions changelog.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"Version": "0.0.0",
"Changes": ["initial updater"]
"Version": "0.0.2",
"Changes": ["May be seen on github"]
}
2 changes: 1 addition & 1 deletion modules/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func update(ctx *ext.Context, u *ext.Update) error {
Entities: text.Entities,
ID: msg.ID,
})
if err := utils.DoUpdate(chat.GetID(), msg.ID); err != nil {
if err := utils.DoUpdate(update.Version, chat.GetID(), msg.ID); err != nil {
ctx.EditMessage(chat.GetID(), &tg.MessagesEditMessageRequest{
Message: fmt.Sprintf("failed to update: %s", err.Error()),
ID: msg.ID,
Expand Down
29 changes: 29 additions & 0 deletions utils/dwdutils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package utils

import (
"io"
"net/http"
"os"
)

func DownloadFile(file, url string) error {
// Remove any existing file
_ = os.Remove(file)
out, err := os.Create(file)
if err != nil {
return err
}
defer out.Close()

resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()

_, err = io.Copy(out, resp.Body)
if err != nil {
return err
}
return nil
}
24 changes: 24 additions & 0 deletions utils/osinfo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package utils

import (
"errors"
"runtime"
)

func GetSupportedOS() (string, error) {
switch runtime.GOOS {
case "windows", "darwin", "linux":
return runtime.GOOS, nil
default:
return "", errors.New("unsupported OS")
}
}

func GetSupportedARCH() (string, error) {
switch runtime.GOARCH {
case "amd64", "arm64":
return runtime.GOARCH, nil
default:
return "", errors.New("unsupported ARCH")
}
}
38 changes: 31 additions & 7 deletions utils/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package utils
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strconv"
Expand All @@ -11,17 +12,40 @@ import (
"github.com/anonyindian/logger"
)

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

func downloadUpdate(version string) error {
os, err := GetSupportedOS()
if err != nil {
buildWithClone(".")
restart("giga", []string{}, 5, chatId, msgId, "Updated Successfully.")
return fmt.Errorf("failed to download update: %w", err)
}
err = buildBinary()
arch, err := GetSupportedARCH()
if err != nil {
return err
return fmt.Errorf("failed to download update: %w", err)
}
url := fmt.Sprintf(
"https://github.com/GigaUserbot/GIGA/releases/download/v%s/giga_%s_%s_%s",
version, version, os, arch,
)
err = DownloadFile("giga", url)
if err != nil {
return fmt.Errorf("failed to download update: %w", err)
}
return Restart(5, chatId, msgId, "Updated Successfully.")
return nil
}

var CurrentUpdate = &Update{}
Expand Down

0 comments on commit 78a96be

Please sign in to comment.