[cmd]重构配置读取,支持传入参数
This commit is contained in:
parent
60a64a63ce
commit
47602c5838
|
|
@ -16,7 +16,8 @@ import (
|
|||
"golang.org/x/term"
|
||||
)
|
||||
|
||||
var SafeCmd = &cobra.Command{
|
||||
func NewSafeCmd() *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "safe",
|
||||
Short: "交互式安全确认,将可疑对象加入白名单",
|
||||
Long: "查看当前的可疑文件和进程列表,并选择将其移入白名单。",
|
||||
|
|
@ -29,6 +30,8 @@ var SafeCmd = &cobra.Command{
|
|||
|
||||
interactiveSafe(cfg)
|
||||
},
|
||||
}
|
||||
return cmd
|
||||
}
|
||||
|
||||
func readKeyWithESC() (string, error) {
|
||||
|
|
|
|||
|
|
@ -20,16 +20,17 @@ import (
|
|||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
var StartCmd = &cobra.Command{
|
||||
func NewStartCmd() *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "start",
|
||||
Short: "启动系统监控守护服务",
|
||||
Long: "sysmonitord start 命令用于启动系统监控守护服务,首次启动会进行全量扫描建立白名单。",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
logger.Log.Info("正在启动系统监控守护服务...")
|
||||
|
||||
cfg, err := config.LoadConfig("./config.yaml")
|
||||
if err != nil {
|
||||
logger.Log.Error("加载配置文件失败", zap.Error(err))
|
||||
cfg, ok := cmd.Context().Value("config").(*config.Config)
|
||||
if !ok {
|
||||
logger.Log.Error("无法获取配置")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
|
|
@ -176,4 +177,7 @@ var StartCmd = &cobra.Command{
|
|||
}
|
||||
|
||||
},
|
||||
}
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,7 +10,8 @@ import (
|
|||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var StatusCmd = &cobra.Command{
|
||||
func NewStatusCmd() *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "status",
|
||||
Short: "显示系统状态",
|
||||
Long: "显示Sysmonitod的当前状态",
|
||||
|
|
@ -24,6 +25,8 @@ var StatusCmd = &cobra.Command{
|
|||
|
||||
printStatus(cfg)
|
||||
},
|
||||
}
|
||||
return cmd
|
||||
}
|
||||
|
||||
func printStatus(cfg *config.Config) {
|
||||
|
|
|
|||
|
|
@ -7,11 +7,14 @@ import (
|
|||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var VersionCmd = &cobra.Command{
|
||||
func NewVersionCmd() *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "version",
|
||||
Short: "显示 sysmonitord 的版本信息",
|
||||
Long: "sysmonitord version 命令用于显示当前 sysmonitord 的版本、Git 提交信息和构建时间。",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
fmt.Println(version.Info())
|
||||
},
|
||||
}
|
||||
return cmd
|
||||
}
|
||||
|
|
|
|||
53
main.go
53
main.go
|
|
@ -1,6 +1,8 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"sysmonitord/cmd/safe"
|
||||
"sysmonitord/cmd/start"
|
||||
|
|
@ -13,39 +15,44 @@ import (
|
|||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func getConfigPath() string {
|
||||
if _, err := os.Stat("./config.yaml"); err == nil {
|
||||
return "./config.yaml"
|
||||
}
|
||||
|
||||
if _, err := os.Stat("/etc/sysmonitord/config.yaml"); err == nil {
|
||||
return "/etc/sysmonitord/config.yaml"
|
||||
}
|
||||
|
||||
return "./config.yaml"
|
||||
}
|
||||
var (
|
||||
cfgFile string
|
||||
cfg *config.Config
|
||||
)
|
||||
|
||||
func main() {
|
||||
logger.InitLogger()
|
||||
defer logger.Sync()
|
||||
|
||||
cfg, err := config.LoadConfig(getConfigPath())
|
||||
if err != nil {
|
||||
logger.Log.Error("加载配置文件失败", zap.Error(err))
|
||||
os.Exit(1)
|
||||
} else {
|
||||
logger.SetLogLevel(cfg.Log.Level)
|
||||
}
|
||||
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "sysmonitord",
|
||||
Short: "Sysmonitord 是一个 Linux 系统安全监控工具",
|
||||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||
if cfgFile == "" {
|
||||
if _, err := os.Stat("./config.yaml"); err == nil {
|
||||
cfgFile = "./config.yaml"
|
||||
} else if _, err := os.Stat("/etc/sysmonitord/config.yaml"); err == nil {
|
||||
cfgFile = "/etc/sysmonitord/config.yaml"
|
||||
}
|
||||
}
|
||||
|
||||
rootCmd.AddCommand(start.StartCmd)
|
||||
rootCmd.AddCommand(version.VersionCmd)
|
||||
rootCmd.AddCommand(status.StatusCmd)
|
||||
rootCmd.AddCommand(safe.SafeCmd)
|
||||
cfg, err := config.LoadConfig(cfgFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("加载配置文件失败: %w", err)
|
||||
}
|
||||
|
||||
ctx := context.WithValue(cmd.Context(), "config", cfg)
|
||||
cmd.SetContext(ctx)
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "配置文件路径 (默认: ./config.yaml 或 /etc/sysmonitord/config.yaml)")
|
||||
|
||||
rootCmd.AddCommand(start.NewStartCmd())
|
||||
rootCmd.AddCommand(version.NewVersionCmd())
|
||||
rootCmd.AddCommand(status.NewStatusCmd())
|
||||
rootCmd.AddCommand(safe.NewSafeCmd())
|
||||
|
||||
if err := rootCmd.Execute(); err != nil {
|
||||
logger.Log.Error("命令执行失败", zap.Error(err))
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user