From c52f8649a24f31a21a5d9c1651a61e91598b7615 Mon Sep 17 00:00:00 2001 From: appleboy Date: Sun, 16 Mar 2025 14:18:58 +0800 Subject: [PATCH] refactor: add hot reloading and improve logging functions - Add command to install `air` for hot reloading in `Makefile` - Add `dev` command to run the application with hot reload in `Makefile` - Add `vendor` command to tidy and verify module dependencies in `Makefile` - Update log synchronization method to use `log.Default().Sync()` in `cmd/cmd.go` - Change variadic parameter type from `interface{}` to `any` in logging functions - Remove `Sync` function from `pkg/log/log.go` ref: https://github.com/uber-go/zap/issues/880 Signed-off-by: appleboy --- Makefile | 21 ++++++++++++++++++++- cmd/cmd.go | 2 +- pkg/log/log.go | 18 +++++------------- 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/Makefile b/Makefile index c307b88..43628d0 100644 --- a/Makefile +++ b/Makefile @@ -3,4 +3,23 @@ EXECUTABLE := gitea-mcp .PHONY: build build: - $(GO) build -v -ldflags '-s -w' -o $(EXECUTABLE) \ No newline at end of file + $(GO) build -v -ldflags '-s -w' -o $(EXECUTABLE) + +## air: install air for hot reload +.PHONY: air +air: + @hash air > /dev/null 2>&1; if [ $$? -ne 0 ]; then \ + $(GO) install github.com/air-verse/air@latest; \ + fi + +## dev: run the application with hot reload +.PHONY: dev +dev: air + air --build.cmd "make build" --build.bin ./gitea-mcp + +## vendor: tidy and verify module dependencies +.PHONY: vendor +vendor: + @echo 'Tidying and verifying module dependencies...' + go mod tidy + go mod verify \ No newline at end of file diff --git a/cmd/cmd.go b/cmd/cmd.go index 491dbc3..299d0a6 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -55,7 +55,7 @@ func init() { } func Execute(version string) { - defer log.Sync() + defer log.Default().Sync() if err := operation.Run(transport, version); err != nil { log.Fatalf("Run Gitea MCP Server Error: %v", err) } diff --git a/pkg/log/log.go b/pkg/log/log.go index 7992346..20e414e 100644 --- a/pkg/log/log.go +++ b/pkg/log/log.go @@ -71,30 +71,22 @@ func Panic(msg string, fields ...zap.Field) { Default().Panic(msg, fields...) } -func Debugf(format string, args ...interface{}) { +func Debugf(format string, args ...any) { Default().Sugar().Debugf(format, args...) } -func Infof(format string, args ...interface{}) { +func Infof(format string, args ...any) { Default().Sugar().Infof(format, args...) } -func Warnf(format string, args ...interface{}) { +func Warnf(format string, args ...any) { Default().Sugar().Warnf(format, args...) } -func Errorf(format string, args ...interface{}) { +func Errorf(format string, args ...any) { Default().Sugar().Errorf(format, args...) } -func Fatalf(format string, args ...interface{}) { +func Fatalf(format string, args ...any) { Default().Sugar().Fatalf(format, args...) } - -func Sync() { - err := defaultLogger.Sync() - if err != nil { - Error(err.Error()) - } - Info("logger has been synced") -}