Skip to content

A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.

feat(plugin): Extracting the main function to answercmd pkg

LinkinStar3ccdc8670e9e
diff --git a/cmd/answer/main.go b/cmd/answer/main.go
index 626eb2b2..a1dee485 100644
--- a/cmd/answer/main.go
+++ b/cmd/answer/main.go
@@ -1,71 +1,12 @@
package main
import (
- "os"
- "time"
-
- "github.com/answerdev/answer/internal/base/conf"
- "github.com/answerdev/answer/internal/base/constant"
- "github.com/answerdev/answer/internal/base/cron"
- "github.com/answerdev/answer/internal/cli"
- "github.com/answerdev/answer/internal/schema"
- "github.com/gin-gonic/gin"
- "github.com/segmentfault/pacman"
- "github.com/segmentfault/pacman/contrib/log/zap"
- "github.com/segmentfault/pacman/contrib/server/http"
- "github.com/segmentfault/pacman/log"
-)
-
-// go build -ldflags "-X main.Version=x.y.z"
-var (
- // Name is the name of the project
- Name = "answer"
- // Version is the version of the project
- Version = "0.0.0"
- // Revision is the git short commit revision number
- Revision = ""
- // Time is the build time of the project
- Time = ""
- // log level
- logLevel = os.Getenv("LOG_LEVEL")
- // log path
- logPath = os.Getenv("LOG_PATH")
+ answercmd "github.com/answerdev/answer/cmd"
)
// @securityDefinitions.apikey ApiKeyAuth
// @in header
// @name Authorization
func main() {
- log.SetLogger(zap.NewLogger(
- log.ParseLevel(logLevel), zap.WithName("answer"), zap.WithPath(logPath), zap.WithCallerFullPath()))
- Execute()
-}
-
-func runApp() {
- c, err := conf.ReadConfig(cli.GetConfigFilePath())
- if err != nil {
- panic(err)
- }
- conf.GetPathIgnoreList()
- app, cleanup, err := initApplication(
- c.Debug, c.Server, c.Data.Database, c.Data.Cache, c.I18n, c.Swaggerui, c.ServiceConfig, log.GetLogger())
- if err != nil {
- panic(err)
- }
- constant.Version = Version
- schema.AppStartTime = time.Now()
-
- defer cleanup()
- if err := app.Run(); err != nil {
- panic(err)
- }
-}
-
-func newApplication(serverConf *conf.Server, server *gin.Engine, manager *cron.ScheduledTaskManager) *pacman.Application {
- manager.Run()
- return pacman.NewApp(
- pacman.WithName(Name),
- pacman.WithVersion(Version),
- pacman.WithServer(http.NewServer(server, serverConf.HTTP.Addr)),
- )
+ answercmd.Main()
}
diff --git a/cmd/answer/command.go b/cmd/command.go
similarity index 98%
rename from cmd/answer/command.go
rename to cmd/command.go
index 1748f4b7..c109cc9e 100644
--- a/cmd/answer/command.go
+++ b/cmd/command.go
@@ -1,4 +1,4 @@
-package main
+package answercmd
import (
"fmt"
@@ -163,7 +163,7 @@ To run answer, use:
)
// Execute adds all child commands to the root command and sets flags appropriately.
-// This is called by main.main(). It only needs to happen once to the rootCmd.
+// This is called by main(). It only needs to happen once to the rootCmd.
func Execute() {
err := rootCmd.Execute()
if err != nil {
diff --git a/cmd/main.go b/cmd/main.go
new file mode 100644
index 00000000..75bcea7f
--- /dev/null
+++ b/cmd/main.go
@@ -0,0 +1,72 @@
+package answercmd
+
+import (
+ "os"
+ "time"
+
+ "github.com/answerdev/answer/internal/base/conf"
+ "github.com/answerdev/answer/internal/base/constant"
+ "github.com/answerdev/answer/internal/base/cron"
+ "github.com/answerdev/answer/internal/cli"
+ "github.com/answerdev/answer/internal/schema"
+ "github.com/gin-gonic/gin"
+ "github.com/segmentfault/pacman"
+ "github.com/segmentfault/pacman/contrib/log/zap"
+ "github.com/segmentfault/pacman/contrib/server/http"
+ "github.com/segmentfault/pacman/log"
+)
+
+// go build -ldflags "-X answercmd.Version=x.y.z"
+var (
+ // Name is the name of the project
+ Name = "answer"
+ // Version is the version of the project
+ Version = "0.0.0"
+ // Revision is the git short commit revision number
+ Revision = ""
+ // Time is the build time of the project
+ Time = ""
+ // log level
+ logLevel = os.Getenv("LOG_LEVEL")
+ // log path
+ logPath = os.Getenv("LOG_PATH")
+)
+
+// Main
+// @securityDefinitions.apikey ApiKeyAuth
+// @in header
+// @name Authorization
+func Main() {
+ log.SetLogger(zap.NewLogger(
+ log.ParseLevel(logLevel), zap.WithName("answer"), zap.WithPath(logPath), zap.WithCallerFullPath()))
+ Execute()
+}
+
+func runApp() {
+ c, err := conf.ReadConfig(cli.GetConfigFilePath())
+ if err != nil {
+ panic(err)
+ }
+ conf.GetPathIgnoreList()
+ app, cleanup, err := initApplication(
+ c.Debug, c.Server, c.Data.Database, c.Data.Cache, c.I18n, c.Swaggerui, c.ServiceConfig, log.GetLogger())
+ if err != nil {
+ panic(err)
+ }
+ constant.Version = Version
+ schema.AppStartTime = time.Now()
+
+ defer cleanup()
+ if err := app.Run(); err != nil {
+ panic(err)
+ }
+}
+
+func newApplication(serverConf *conf.Server, server *gin.Engine, manager *cron.ScheduledTaskManager) *pacman.Application {
+ manager.Run()
+ return pacman.NewApp(
+ pacman.WithName(Name),
+ pacman.WithVersion(Version),
+ pacman.WithServer(http.NewServer(server, serverConf.HTTP.Addr)),
+ )
+}
diff --git a/cmd/answer/wire.go b/cmd/wire.go
similarity index 98%
rename from cmd/answer/wire.go
rename to cmd/wire.go
index 90d85635..e8357abd 100644
--- a/cmd/answer/wire.go
+++ b/cmd/wire.go
@@ -3,7 +3,7 @@
// The build tag makes sure the stub is not built in the final build.
-package main
+package answercmd
import (
"github.com/answerdev/answer/internal/base/conf"
diff --git a/cmd/answer/wire_gen.go b/cmd/wire_gen.go
similarity index 99%
rename from cmd/answer/wire_gen.go
rename to cmd/wire_gen.go
index 1df8e810..79d1fb32 100644
--- a/cmd/answer/wire_gen.go
+++ b/cmd/wire_gen.go
@@ -4,7 +4,7 @@
//go:build !wireinject
// +build !wireinject
-package main
+package answercmd
import (
"github.com/answerdev/answer/internal/base/conf"