refactor: move version to config package

This commit is contained in:
krau
2025-08-23 16:10:02 +08:00
parent c023fd869d
commit 94f796d0e8
6 changed files with 23 additions and 22 deletions

View File

@@ -63,9 +63,9 @@ jobs:
README.md README.md
ldflags: >- ldflags: >-
-s -w -s -w
-X "github.com/krau/SaveAny-Bot/pkg/consts.Version=${{ env.VERSION }}" -X "github.com/krau/SaveAny-Bot/config.Version=${{ env.VERSION }}"
-X "github.com/krau/SaveAny-Bot/pkg/consts.BuildTime=${{ format(github.event.repository.updated_at, 'yyyy-MM-dd HH:mm:ss') }}" -X "github.com/krau/SaveAny-Bot/config.BuildTime=${{ format(github.event.repository.updated_at, 'yyyy-MM-dd HH:mm:ss') }}"
-X "github.com/krau/SaveAny-Bot/pkg/consts.GitCommit=${{ github.sha }}" -X "github.com/krau/SaveAny-Bot/config.GitCommit=${{ github.sha }}"
binary_name: saveany-bot binary_name: saveany-bot
env: env:
VERSION: ${{ env.VERSION }} VERSION: ${{ env.VERSION }}

View File

@@ -17,9 +17,9 @@ RUN --mount=type=cache,target=/root/.cache/go-build \
go build -trimpath \ go build -trimpath \
-ldflags=" \ -ldflags=" \
-s -w \ -s -w \
-X 'github.com/krau/SaveAny-Bot/common.Version=${VERSION}' \ -X 'github.com/krau/SaveAny-Bot/config.Version=${VERSION}' \
-X 'github.com/krau/SaveAny-Bot/common.GitCommit=${GitCommit}' \ -X 'github.com/krau/SaveAny-Bot/config.GitCommit=${GitCommit}' \
-X 'github.com/krau/SaveAny-Bot/common.BuildTime=${BuildTime}' \ -X 'github.com/krau/SaveAny-Bot/config.BuildTime=${BuildTime}' \
" \ " \
-o saveany-bot . -o saveany-bot .

View File

@@ -5,7 +5,7 @@ import (
"github.com/celestix/gotgproto/dispatcher" "github.com/celestix/gotgproto/dispatcher"
"github.com/celestix/gotgproto/ext" "github.com/celestix/gotgproto/ext"
"github.com/krau/SaveAny-Bot/pkg/consts" "github.com/krau/SaveAny-Bot/config"
) )
func handleHelpCmd(ctx *ext.Context, update *ext.Update) error { func handleHelpCmd(ctx *ext.Context, update *ext.Update) error {
@@ -24,10 +24,10 @@ Save Any Bot - 转存你的 Telegram 文件
使用帮助: https://sabot.unv.app/usage/ 使用帮助: https://sabot.unv.app/usage/
` `
shortHash := consts.GitCommit shortHash := config.GitCommit
if len(shortHash) > 7 { if len(shortHash) > 7 {
shortHash = shortHash[:7] shortHash = shortHash[:7]
} }
ctx.Reply(update, ext.ReplyTextString(fmt.Sprintf(helpText, consts.Version, shortHash)), nil) ctx.Reply(update, ext.ReplyTextString(fmt.Sprintf(helpText, config.Version, shortHash)), nil)
return dispatcher.EndGroups return dispatcher.EndGroups
} }

View File

@@ -4,7 +4,7 @@ import (
"fmt" "fmt"
"runtime" "runtime"
"github.com/krau/SaveAny-Bot/pkg/consts" "github.com/krau/SaveAny-Bot/config"
"github.com/rhysd/go-github-selfupdate/selfupdate" "github.com/rhysd/go-github-selfupdate/selfupdate"
"github.com/blang/semver" "github.com/blang/semver"
@@ -16,7 +16,7 @@ var VersionCmd = &cobra.Command{
Aliases: []string{"v"}, Aliases: []string{"v"},
Short: "Print the version number of saveany-bot", Short: "Print the version number of saveany-bot",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("saveany-bot version: %s %s/%s\nBuildTime: %s, Commit: %s\n", consts.Version, runtime.GOOS, runtime.GOARCH, consts.BuildTime, consts.GitCommit) fmt.Printf("saveany-bot version: %s %s/%s\nBuildTime: %s, Commit: %s\n", config.Version, runtime.GOOS, runtime.GOARCH, config.BuildTime, config.GitCommit)
}, },
} }
@@ -25,14 +25,14 @@ var upgradeCmd = &cobra.Command{
Aliases: []string{"up"}, Aliases: []string{"up"},
Short: "Upgrade saveany-bot to the latest version", Short: "Upgrade saveany-bot to the latest version",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
v := semver.MustParse(consts.Version) v := semver.MustParse(config.Version)
latest, err := selfupdate.UpdateSelf(v, "krau/SaveAny-Bot") latest, err := selfupdate.UpdateSelf(v, "krau/SaveAny-Bot")
if err != nil { if err != nil {
fmt.Println("Binary update failed:", err) fmt.Println("Binary update failed:", err)
return return
} }
if latest.Version.Equals(v) { if latest.Version.Equals(v) {
fmt.Println("Current binary is the latest version", consts.Version) fmt.Println("Current binary is the latest version", config.Version)
} else { } else {
fmt.Println("Successfully updated to version", latest.Version) fmt.Println("Successfully updated to version", latest.Version)
fmt.Println("Release note:\n", latest.ReleaseNotes) fmt.Println("Release note:\n", latest.ReleaseNotes)

View File

@@ -1,7 +1,7 @@
package consts package config
// inject version by '-X' flag // inject version by '-X' flag
// go build -ldflags "-X github.com/krau/SaveAny-Bot/pkg/consts.Version=${{ env.VERSION }}" // go build -ldflags "-X github.com/krau/SaveAny-Bot/config.Version=${{ env.VERSION }}"
var ( var (
Version string = "dev" Version string = "dev"
BuildTime string = "unknown" BuildTime string = "unknown"

View File

@@ -6,13 +6,14 @@ import (
type User struct { type User struct {
gorm.Model gorm.Model
ChatID int64 `gorm:"uniqueIndex;not null"` ChatID int64 `gorm:"uniqueIndex;not null"`
Silent bool Silent bool
DefaultStorage string DefaultStorage string
Dirs []Dir Dirs []Dir
ApplyRule bool ApplyRule bool
Rules []Rule Rules []Rule
WatchChats []WatchChat WatchChats []WatchChat
FilenameStrategy string
} }
type WatchChat struct { type WatchChat struct {