Browse Source

更新配置文件检查和重启标志处理

新增了配置文件检查命令,并调整了重启标志的处理方式。删除了不再使用的导入,并在多个地方统一使用新的重启标志 `RestartFlagComplete`。
SongZihuan 1 week ago
parent
commit
6b845b7cda

+ 2 - 0
src/cmd/catv1/main.go

@@ -7,6 +7,7 @@ package main
 import (
 	"github.com/SongZihuan/BackendServerTemplate/src/cmd/globalmain"
 	"github.com/SongZihuan/BackendServerTemplate/src/cmdparser/root"
+	"github.com/SongZihuan/BackendServerTemplate/src/cmdparser/subcmd"
 	_ "github.com/SongZihuan/BackendServerTemplate/src/global"
 	"github.com/SongZihuan/BackendServerTemplate/src/logger"
 	catv1 "github.com/SongZihuan/BackendServerTemplate/src/mainfunc/cat/v1"
@@ -32,6 +33,7 @@ func main() {
 		false,
 		catv1.MainV1)
 
+	subcmd.AddSubCMDOfRoot(cmd)
 	cmd.Flags().StringVarP(&catv1.InputConfigFilePath, "config", "c", catv1.InputConfigFilePath, "the file path of the configuration file")
 	cmd.Flags().StringVarP(&catv1.OutputConfigFilePath, "output-config", "o", catv1.OutputConfigFilePath, "the file path of the output configuration file")
 

+ 2 - 0
src/cmd/lionv1/main.go

@@ -6,6 +6,7 @@ package main
 
 import (
 	"github.com/SongZihuan/BackendServerTemplate/src/cmdparser/root"
+	"github.com/SongZihuan/BackendServerTemplate/src/cmdparser/subcmd"
 	_ "github.com/SongZihuan/BackendServerTemplate/src/global"
 	"github.com/SongZihuan/BackendServerTemplate/src/logger"
 	lionv1 "github.com/SongZihuan/BackendServerTemplate/src/mainfunc/lion/v1"
@@ -21,6 +22,7 @@ func main() {
 		true,
 		lionv1.MainV1)
 
+	subcmd.AddSubCMDOfRoot(cmd)
 	cmd.Flags().StringVarP(&lionv1.InputConfigFilePath, "config", "c", lionv1.InputConfigFilePath, "the file path of the configuration file")
 	cmd.Flags().StringVarP(&lionv1.OutputConfigFilePath, "output-config", "o", lionv1.OutputConfigFilePath, "the file path of the output configuration file")
 

+ 8 - 0
src/cmd/restart/flags.go

@@ -0,0 +1,8 @@
+// Copyright 2025 BackendServerTemplate Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package restart
+
+const RestartFlag = "restart"
+const RestartFlagComplete = "--" + RestartFlag

+ 2 - 0
src/cmd/tigerv1/main.go

@@ -6,6 +6,7 @@ package main
 
 import (
 	"github.com/SongZihuan/BackendServerTemplate/src/cmdparser/root"
+	"github.com/SongZihuan/BackendServerTemplate/src/cmdparser/subcmd"
 	_ "github.com/SongZihuan/BackendServerTemplate/src/global"
 	"github.com/SongZihuan/BackendServerTemplate/src/logger"
 	tigerv1 "github.com/SongZihuan/BackendServerTemplate/src/mainfunc/tiger/v1"
@@ -21,6 +22,7 @@ func main() {
 		true,
 		tigerv1.MainV1)
 
+	subcmd.AddSubCMDOfRoot(cmd)
 	cmd.Flags().StringVarP(&tigerv1.InputConfigFilePath, "config", "c", tigerv1.InputConfigFilePath, "the file path of the configuration file")
 	cmd.Flags().StringVarP(&tigerv1.OutputConfigFilePath, "output-config", "o", tigerv1.OutputConfigFilePath, "the file path of the output configuration file")
 

+ 33 - 0
src/cmdparser/check/cmd.go

@@ -0,0 +1,33 @@
+// Copyright 2025 BackendServerTemplate Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package check
+
+import (
+	"github.com/SongZihuan/BackendServerTemplate/src/cmd/globalmain"
+	checkv1 "github.com/SongZihuan/BackendServerTemplate/src/mainfunc/check/v1"
+	"github.com/spf13/cobra"
+)
+
+var CMD = &cobra.Command{
+	Use:     "check",
+	Aliases: []string{"config"},
+	Short:   "Check the correctness of the configuration file",
+	RunE: func(cmd *cobra.Command, args []string) error {
+		cmd.SilenceUsage = true
+		cmd.SilenceErrors = true
+
+		err := globalmain.PreRun(false)
+		if err != nil {
+			return err
+		}
+
+		return checkv1.MainV1(cmd, args)
+	},
+}
+
+func init() {
+	CMD.Flags().StringVarP(&checkv1.InputConfigFilePath, "config", "c", checkv1.InputConfigFilePath, "the file path of the configuration file")
+	CMD.Flags().StringVarP(&checkv1.OutputConfigFilePath, "output-config", "o", checkv1.OutputConfigFilePath, "the file path of the output configuration file")
+}

+ 2 - 9
src/cmdparser/root/root.go

@@ -8,9 +8,6 @@ import (
 	"fmt"
 	"github.com/SongZihuan/BackendServerTemplate/src/cmd/globalmain"
 	"github.com/SongZihuan/BackendServerTemplate/src/cmd/restart"
-	"github.com/SongZihuan/BackendServerTemplate/src/cmdparser/license"
-	"github.com/SongZihuan/BackendServerTemplate/src/cmdparser/report"
-	"github.com/SongZihuan/BackendServerTemplate/src/cmdparser/version"
 	"github.com/SongZihuan/BackendServerTemplate/src/global"
 	"github.com/SongZihuan/BackendServerTemplate/src/utils/cleanstringutils"
 	"github.com/SongZihuan/BackendServerTemplate/src/utils/consoleutils"
@@ -28,9 +25,6 @@ var isRestart bool = false
 var consoleMode string = ConsoleModeNormal
 var hasConsole bool = false
 
-const restartFlag = "restart"
-const RestartFlag = "--" + restartFlag
-
 func GetRootCMD(shortDescribe string, longDescribe string, reload *bool, _hasConsole bool, action func(cmd *cobra.Command, args []string) error) *cobra.Command {
 	hasConsole = _hasConsole && consoleutils.HasConsoleWindow()
 
@@ -70,7 +64,7 @@ func GetRootCMD(shortDescribe string, longDescribe string, reload *bool, _hasCon
 					return fmt.Errorf("`auto-reload` can only be enabled when `console-mode` is `normal`")
 				}
 
-				if cmd.Flags().Changed(restartFlag) {
+				if cmd.Flags().Changed(restart.RestartFlag) {
 					if isRestart {
 						err := restart.FromRestart()
 						if err != nil {
@@ -110,7 +104,6 @@ func GetRootCMD(shortDescribe string, longDescribe string, reload *bool, _hasCon
 		},
 	}
 
-	cmd.AddCommand(version.CMD, license.CMD, report.CMD)
 	cmd.PersistentFlags().StringVarP(&name, "name", "n", global.Name, "the program display name")
 
 	if _hasConsole {
@@ -119,7 +112,7 @@ func GetRootCMD(shortDescribe string, longDescribe string, reload *bool, _hasCon
 
 	if reload != nil {
 		cmd.Flags().BoolVar(reload, "auto-reload", false, "auto reload config file when the file changed")
-		cmd.Flags().BoolVar(&isRestart, restartFlag, false, "restart mode, note: DO NOT SET THIS FLAG unless you know your purpose clearly.")
+		cmd.Flags().BoolVar(&isRestart, restart.RestartFlag, false, "restart mode, note: DO NOT SET THIS FLAG unless you know your purpose clearly.")
 	}
 
 	return cmd

+ 17 - 0
src/cmdparser/subcmd/subcmd.go

@@ -0,0 +1,17 @@
+// Copyright 2025 BackendServerTemplate Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package subcmd
+
+import (
+	"github.com/SongZihuan/BackendServerTemplate/src/cmdparser/check"
+	"github.com/SongZihuan/BackendServerTemplate/src/cmdparser/license"
+	"github.com/SongZihuan/BackendServerTemplate/src/cmdparser/report"
+	"github.com/SongZihuan/BackendServerTemplate/src/cmdparser/version"
+	"github.com/spf13/cobra"
+)
+
+func AddSubCMDOfRoot(cmd *cobra.Command) {
+	cmd.AddCommand(version.CMD, license.CMD, report.CMD, check.CMD)
+}

+ 1 - 2
src/config/configparser/json.go

@@ -8,7 +8,6 @@ import (
 	"encoding/json"
 	"errors"
 	"github.com/SongZihuan/BackendServerTemplate/src/cmd/restart"
-	"github.com/SongZihuan/BackendServerTemplate/src/cmdparser/root"
 	"github.com/SongZihuan/BackendServerTemplate/src/config/configerror"
 	"github.com/SongZihuan/BackendServerTemplate/src/logger"
 	"github.com/SongZihuan/BackendServerTemplate/src/utils/envutils"
@@ -49,7 +48,7 @@ func NewJsonProvider(opt *NewProviderOption) *JsonProvider {
 		p.viper.OnConfigChange(func(e fsnotify.Event) {
 			logger.Infof("config change")
 
-			err := restart.RestartProgram(root.RestartFlag)
+			err := restart.RestartProgram(restart.RestartFlagComplete)
 			if err != nil {
 				logger.Errorf("restart program error: %s", err.Error())
 			}

+ 1 - 2
src/config/configparser/yaml.go

@@ -7,7 +7,6 @@ package configparser
 import (
 	"errors"
 	"github.com/SongZihuan/BackendServerTemplate/src/cmd/restart"
-	"github.com/SongZihuan/BackendServerTemplate/src/cmdparser/root"
 	"github.com/SongZihuan/BackendServerTemplate/src/config/configerror"
 	"github.com/SongZihuan/BackendServerTemplate/src/logger"
 	"github.com/SongZihuan/BackendServerTemplate/src/utils/envutils"
@@ -63,7 +62,7 @@ func (y *YamlProvider) reloadEvent(e fsnotify.Event) {
 	}
 
 	logger.Infof("config change")
-	err := restart.RestartProgram(root.RestartFlag)
+	err := restart.RestartProgram(restart.RestartFlagComplete)
 	if err != nil {
 		logger.Errorf("restart program error: %s", err.Error())
 		y.reloadLock.Unlock()

+ 38 - 0
src/mainfunc/check/v1/main.go

@@ -0,0 +1,38 @@
+// Copyright 2025 BackendServerTemplate Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package v1
+
+import (
+	"fmt"
+	"github.com/SongZihuan/BackendServerTemplate/src/config"
+	"github.com/SongZihuan/BackendServerTemplate/src/config/configparser"
+	"github.com/SongZihuan/BackendServerTemplate/src/utils/exitutils"
+	"github.com/spf13/cobra"
+)
+
+var InputConfigFilePath string = "config.yaml"
+var OutputConfigFilePath string = ""
+
+func MainV1(cmd *cobra.Command, args []string) (exitCode error) {
+	var err error
+
+	configProvider, err := configparser.NewProvider(InputConfigFilePath, &configparser.NewProviderOption{
+		AutoReload: false,
+	})
+	if err != nil {
+		return exitutils.SuccessExitSimple(fmt.Sprintf("Error: config file check failed: %s!", err.Error()))
+	}
+
+	err = config.InitConfig(&config.ConfigOption{
+		ConfigFilePath: InputConfigFilePath,
+		OutputFilePath: OutputConfigFilePath,
+		Provider:       configProvider,
+	})
+	if err != nil {
+		return exitutils.SuccessExitSimple(fmt.Sprintf("Error: config file check failed: %s!", err.Error()))
+	}
+
+	return exitutils.SuccessExitSimple("Info: config file check ok!")
+}