-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix some bugs and pre-commit for updater
- Loading branch information
Showing
5 changed files
with
126 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
{ | ||
"Version": "0.0.0", | ||
"Changes": [] | ||
"Changes": [""] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package modules | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/anonyindian/gotgproto/dispatcher" | ||
"github.com/anonyindian/gotgproto/dispatcher/handlers" | ||
"github.com/anonyindian/gotgproto/ext" | ||
"github.com/anonyindian/gotgproto/parsemode/entityhelper" | ||
"github.com/gigauserbot/giga/utils" | ||
"github.com/gotd/td/tg" | ||
) | ||
|
||
func (m *module) LoadUpdate(dp *dispatcher.CustomDispatcher) { | ||
dp.AddHandler(handlers.NewCommand("changelog", changeLog)) | ||
} | ||
|
||
func changeLog(ctx *ext.Context, u *ext.Update) error { | ||
chat := u.EffectiveChat() | ||
newUpdate, changed := utils.CheckChanges() | ||
if !changed { | ||
ctx.EditMessage(chat.GetID(), &tg.MessagesEditMessageRequest{ | ||
Message: "You're currently running the latest version.", | ||
ID: u.EffectiveMessage.ID, | ||
}) | ||
return dispatcher.EndGroups | ||
} | ||
text := entityhelper.Combine("New GIGA Update", entityhelper.BoldEntity, entityhelper.UnderlineEntity) | ||
text.Bold("\nVersion: ").Code(newUpdate.Version) | ||
text.Bold("\nChange-log:") | ||
for _, change := range newUpdate.Changes { | ||
text.Plain(fmt.Sprintf("\n • %s", change)) | ||
} | ||
ctx.EditMessage(chat.GetID(), &tg.MessagesEditMessageRequest{ | ||
Message: text.String, | ||
Entities: text.Entities, | ||
ID: u.EffectiveMessage.ID, | ||
}) | ||
return dispatcher.EndGroups | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package utils | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"io/ioutil" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/anonyindian/logger" | ||
) | ||
|
||
var CurrentUpdate = &Update{} | ||
var currentVersion *version | ||
|
||
type Update struct { | ||
Version string | ||
Changes []string | ||
} | ||
|
||
func InitUpdate(l *logger.Logger) { | ||
b, err := ioutil.ReadFile("changelog.json") | ||
if err != nil { | ||
l.ChangeLevel(logger.LevelCritical).Printlnf("Failed to open changelog.json: %s\n", err.Error()) | ||
return | ||
} | ||
err = json.Unmarshal(b, CurrentUpdate) | ||
if err != nil { | ||
l.ChangeLevel(logger.LevelCritical).Printlnf("Failed to parse changelog.json: %s\n", err.Error()) | ||
return | ||
} | ||
currentVersion, err = parseVersion(CurrentUpdate.Version) | ||
if err != nil { | ||
l.ChangeLevel(logger.LevelCritical).Printlnf("Failed to parse current version: %s\n", err.Error()) | ||
return | ||
} | ||
} | ||
|
||
func CheckChanges() (*Update, bool) { | ||
var u Update | ||
origin := "https://raw.githubusercontent.com/GigaUserbot/GIGA/dev/changelog.json" | ||
json.Unmarshal([]byte(origin), &u) | ||
return &u, CompareVersion(u.Version) | ||
} | ||
|
||
// CompareVersion returns true if input version is greater than current one. | ||
func CompareVersion(version string) bool { | ||
parsedVersion, err := parseVersion(version) | ||
if err != nil { | ||
return false | ||
} | ||
return currentVersion.compare(parsedVersion) | ||
} | ||
|
||
type version struct { | ||
minor int | ||
major int | ||
patch int | ||
} | ||
|
||
func parseVersion(s string) (*version, error) { | ||
v := version{} | ||
for index, velem := range strings.Split(s, ".") { | ||
vint, err := strconv.Atoi(velem) | ||
if err != nil { | ||
return nil, errors.New("failed to parse version") | ||
} | ||
switch index { | ||
case 0: | ||
v.major = vint | ||
case 1: | ||
v.minor = vint | ||
case 2: | ||
v.patch = vint | ||
} | ||
} | ||
return &v, nil | ||
} | ||
|
||
func (v *version) compare(v1 *version) bool { | ||
return (v1.major > v.major || v1.minor > v.minor || v1.patch > v.patch) | ||
} |