From 3aed41e078e944e89b8aefd58f8dc3e5cc17f53b Mon Sep 17 00:00:00 2001 From: yusing Date: Thu, 9 Oct 2025 01:14:43 +0800 Subject: [PATCH] refactor: move version.go to goutils --- Makefile | 2 +- agent/cmd/main.go | 4 +- agent/pkg/agent/config.go | 8 +- agent/pkg/handler/handler.go | 4 +- cmd/main.go | 5 +- goutils | 2 +- internal/api/v1/version.go | 4 +- internal/route/reverse_proxy.go | 4 +- internal/watcher/health/monitor/http.go | 4 +- pkg/version.go | 130 ------------------------ 10 files changed, 18 insertions(+), 149 deletions(-) delete mode 100644 pkg/version.go diff --git a/Makefile b/Makefile index d653d5f3..5c49216f 100755 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ export GOOS = linux WEBUI_DIR ?= ../godoxy-frontend DOCS_DIR ?= ../godoxy-wiki -LDFLAGS = -X github.com/yusing/godoxy/pkg.version=${VERSION} -checklinkname=0 +LDFLAGS = -X github.com/yusing/goutils/version.version=${VERSION} -checklinkname=0 ifeq ($(agent), 1) NAME = godoxy-agent diff --git a/agent/cmd/main.go b/agent/cmd/main.go index 53fbf129..307422ed 100644 --- a/agent/cmd/main.go +++ b/agent/cmd/main.go @@ -9,11 +9,11 @@ import ( "github.com/yusing/godoxy/agent/pkg/env" "github.com/yusing/godoxy/agent/pkg/server" "github.com/yusing/godoxy/internal/metrics/systeminfo" - "github.com/yusing/godoxy/pkg" socketproxy "github.com/yusing/godoxy/socketproxy/pkg" httpServer "github.com/yusing/goutils/server" strutils "github.com/yusing/goutils/strings" "github.com/yusing/goutils/task" + "github.com/yusing/goutils/version" ) func main() { @@ -43,7 +43,7 @@ func main() { log.Fatal().Err(err).Msg("init SSL error") } - log.Info().Msgf("GoDoxy Agent version %s", pkg.GetVersion()) + log.Info().Msgf("GoDoxy Agent version %s", version.Get()) log.Info().Msgf("Agent name: %s", env.AgentName) log.Info().Msgf("Agent port: %d", env.AgentPort) log.Info().Msgf("Agent runtime: %s", env.Runtime) diff --git a/agent/pkg/agent/config.go b/agent/pkg/agent/config.go index 816f8770..34e68602 100644 --- a/agent/pkg/agent/config.go +++ b/agent/pkg/agent/config.go @@ -16,13 +16,13 @@ import ( "github.com/rs/zerolog" "github.com/rs/zerolog/log" "github.com/yusing/godoxy/agent/pkg/certs" - "github.com/yusing/godoxy/pkg" + "github.com/yusing/goutils/version" ) type AgentConfig struct { Addr string `json:"addr"` Name string `json:"name"` - Version pkg.Version `json:"version"` + Version version.Version `json:"version"` Runtime ContainerRuntime `json:"runtime"` httpClient *http.Client @@ -82,7 +82,7 @@ func (cfg *AgentConfig) Parse(addr string) error { return nil } -var serverVersion = pkg.GetVersion() +var serverVersion = version.Get() func (cfg *AgentConfig) StartWithCerts(ctx context.Context, ca, crt, key []byte) error { clientCert, err := tls.X509KeyPair(crt, key) @@ -151,7 +151,7 @@ func (cfg *AgentConfig) StartWithCerts(ctx context.Context, ca, crt, key []byte) return fmt.Errorf("failed to get agent runtime: HTTP %d %s", status, runtimeBytes) } - cfg.Version = pkg.ParseVersion(string(agentVersionBytes)) + cfg.Version = version.Parse(string(agentVersionBytes)) if serverVersion.IsNewerThanMajor(cfg.Version) { log.Warn().Msgf("agent %s major version mismatch: server: %s, agent: %s", cfg.Name, serverVersion, cfg.Version) diff --git a/agent/pkg/handler/handler.go b/agent/pkg/handler/handler.go index f330cadf..31401cf7 100644 --- a/agent/pkg/handler/handler.go +++ b/agent/pkg/handler/handler.go @@ -9,8 +9,8 @@ import ( "github.com/yusing/godoxy/agent/pkg/agent" "github.com/yusing/godoxy/agent/pkg/env" "github.com/yusing/godoxy/internal/metrics/systeminfo" - "github.com/yusing/godoxy/pkg" socketproxy "github.com/yusing/godoxy/socketproxy/pkg" + "github.com/yusing/goutils/version" ) type ServeMux struct{ *http.ServeMux } @@ -45,7 +45,7 @@ func NewAgentHandler() http.Handler { mux.HandleFunc(agent.EndpointProxyHTTP+"/{path...}", ProxyHTTP) mux.HandleEndpoint("GET", agent.EndpointVersion, func(w http.ResponseWriter, r *http.Request) { - fmt.Fprint(w, pkg.GetVersion()) + fmt.Fprint(w, version.Get()) }) mux.HandleEndpoint("GET", agent.EndpointName, func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, env.AgentName) diff --git a/cmd/main.go b/cmd/main.go index f3b541c2..fe6c77b7 100755 --- a/cmd/main.go +++ b/cmd/main.go @@ -16,11 +16,10 @@ import ( "github.com/yusing/godoxy/internal/metrics/systeminfo" "github.com/yusing/godoxy/internal/metrics/uptime" "github.com/yusing/godoxy/internal/net/gphttp/middleware" - "github.com/yusing/godoxy/pkg" gperr "github.com/yusing/goutils/errs" "github.com/yusing/goutils/server" "github.com/yusing/goutils/task" -) + "github.com/yusing/goutils/version" func parallel(fns ...func()) { var wg sync.WaitGroup @@ -34,7 +33,7 @@ func main() { initProfiling() logging.InitLogger(os.Stderr, memlogger.GetMemLogger()) - log.Info().Msgf("GoDoxy version %s", pkg.GetVersion()) + log.Info().Msgf("GoDoxy version %s", version.Get()) log.Trace().Msg("trace enabled") parallel( dnsproviders.InitProviders, diff --git a/goutils b/goutils index c9c0b8d9..27178436 160000 --- a/goutils +++ b/goutils @@ -1 +1 @@ -Subproject commit c9c0b8d9d0d2bae7448a1a912a69900053f63c79 +Subproject commit 27178436312997ffcf5bfdab9b80680195d8104f diff --git a/internal/api/v1/version.go b/internal/api/v1/version.go index a18034e3..7b55024b 100644 --- a/internal/api/v1/version.go +++ b/internal/api/v1/version.go @@ -4,7 +4,7 @@ import ( "net/http" "github.com/gin-gonic/gin" - "github.com/yusing/godoxy/pkg" + "github.com/yusing/goutils/version" ) // @x-id "version" @@ -17,5 +17,5 @@ import ( // @Success 200 {string} string "version" // @Router /version [get] func Version(c *gin.Context) { - c.JSON(http.StatusOK, pkg.GetVersion().String()) + c.JSON(http.StatusOK, version.Get().String()) } diff --git a/internal/route/reverse_proxy.go b/internal/route/reverse_proxy.go index 1a0e1ed9..60423072 100755 --- a/internal/route/reverse_proxy.go +++ b/internal/route/reverse_proxy.go @@ -15,10 +15,10 @@ import ( "github.com/yusing/godoxy/internal/route/routes" "github.com/yusing/godoxy/internal/types" "github.com/yusing/godoxy/internal/watcher/health/monitor" - "github.com/yusing/godoxy/pkg" gperr "github.com/yusing/goutils/errs" "github.com/yusing/goutils/http/reverseproxy" "github.com/yusing/goutils/task" + "github.com/yusing/goutils/version" ) type ReveseProxyRoute struct { @@ -74,7 +74,7 @@ func NewReverseProxyRoute(base *Route) (*ReveseProxyRoute, gperr.Error) { HTTPConfig: httpConfig, } setHeaderFunc := cfg.SetAgentProxyConfigHeaders - if !a.Version.IsOlderThan(pkg.Ver(0, 18, 6)) { + if !a.Version.IsOlderThan(version.New(0, 18, 6)) { setHeaderFunc = cfg.SetAgentProxyConfigHeadersLegacy } diff --git a/internal/watcher/health/monitor/http.go b/internal/watcher/health/monitor/http.go index 38bda6b5..194104f2 100644 --- a/internal/watcher/health/monitor/http.go +++ b/internal/watcher/health/monitor/http.go @@ -8,7 +8,7 @@ import ( "time" "github.com/yusing/godoxy/internal/types" - "github.com/yusing/godoxy/pkg" + "github.com/yusing/goutils/version" ) type HTTPHealthMonitor struct { @@ -52,7 +52,7 @@ func (mon *HTTPHealthMonitor) CheckHealth() (types.HealthCheckResult, error) { } req.Close = true req.Header.Set("Connection", "close") - req.Header.Set("User-Agent", "GoDoxy/"+pkg.GetVersion().String()) + req.Header.Set("User-Agent", "GoDoxy/"+version.Get().String()) start := time.Now() resp, respErr := pinger.Do(req) diff --git a/pkg/version.go b/pkg/version.go deleted file mode 100644 index 8ca5e3b3..00000000 --- a/pkg/version.go +++ /dev/null @@ -1,130 +0,0 @@ -package pkg - -import ( - "fmt" - "regexp" - "strconv" - "strings" -) - -func GetVersion() Version { - return currentVersion -} - -func GetLastVersion() Version { - return lastVersion -} - -func init() { - currentVersion = ParseVersion(version) - - // ignore errors - // versionFile := filepath.Join(common.DataDir, "version") - // var lastVersionStr string - // f, err := os.OpenFile(versionFile, os.O_RDWR|os.O_CREATE, 0o644) - // if err == nil { - // _, err = fmt.Fscanf(f, "%s", &lastVersionStr) - // lastVersion = ParseVersion(lastVersionStr) - // } - // if err != nil && !os.IsNotExist(err) { - // log.Warn().Err(err).Msg("failed to read version file") - // return - // } - // if err := f.Truncate(0); err != nil { - // log.Warn().Err(err).Msg("failed to truncate version file") - // return - // } - // _, err = f.WriteString(version) - // if err != nil { - // log.Warn().Err(err).Msg("failed to save version file") - // return - // } -} - -type Version struct{ Generation, Major, Minor int } - -func Ver(gen, major, minor int) Version { - return Version{gen, major, minor} -} - -func (v Version) String() string { - return fmt.Sprintf("v%d.%d.%d", v.Generation, v.Major, v.Minor) -} - -func (v Version) MarshalText() ([]byte, error) { - return []byte(v.String()), nil -} - -func (v Version) IsNewerThan(other Version) bool { - if v.Generation != other.Generation { - return v.Generation > other.Generation - } - if v.Major != other.Major { - return v.Major > other.Major - } - return v.Minor > other.Minor -} - -func (v Version) IsNewerThanMajor(other Version) bool { - if v.Generation != other.Generation { - return v.Generation > other.Generation - } - return v.Major > other.Major -} - -func (v Version) IsOlderThan(other Version) bool { - return !v.IsNewerThan(other) -} - -func (v Version) IsOlderThanMajor(other Version) bool { - if v.Generation != other.Generation { - return v.Generation < other.Generation - } - return v.Major < other.Major -} - -func (v Version) IsOlderMajorThan(other Version) bool { - return !v.IsNewerThanMajor(other) -} - -func (v Version) IsEqual(other Version) bool { - return v.Generation == other.Generation && v.Major == other.Major && v.Minor == other.Minor -} - -var ( - version = "unset" - currentVersion Version - lastVersion Version -) - -var versionRegex = regexp.MustCompile(`^v(\d+)\.(\d+)\.(\d+)(\-\w+)?$`) - -func ParseVersion(v string) (ver Version) { - if v == "" { - return ver - } - - if !versionRegex.MatchString(v) { // likely feature branch (e.g. feat/some-feature) - return ver - } - - v = strings.Split(v, "-")[0] - v = strings.TrimPrefix(v, "v") - parts := strings.Split(v, ".") - if len(parts) != 3 { - return ver - } - gen, err := strconv.Atoi(parts[0]) - if err != nil { - return ver - } - major, err := strconv.Atoi(parts[1]) - if err != nil { - return ver - } - minor, err := strconv.Atoi(parts[2]) - if err != nil { - return ver - } - return Ver(gen, major, minor) -}