Files
spider/cmd/root.go
toom1996 d8f667e391 update
2026-09-13 00:52:09 +08:00

64 lines
1.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package cmd
import (
"fmt"
"log"
"os"
"github.com/spf13/cobra"
"my-spiders/internal/config"
"my-spiders/internal/ingest"
)
var (
IngestClient *ingest.Client // 入库管线客户端(enabled 时非 nil)
cfgFile string
debug bool // 命令行 debug 标志
rootCmd = &cobra.Command{
Use: "spider-cli",
Short: "自动化多平台爬虫集成系统",
}
)
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func init() {
// 支持通过 --config 参数指定其他的配置文件路径
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "配置文件路径 (默认是 ./config.yaml)")
// 新增全局 -d / --debug 参数
rootCmd.PersistentFlags().BoolVarP(&debug, "debug", "d", false, "调试模式 (仅输出不入库)")
cobra.OnInitialize(initConfig)
}
func initConfig() {
// 1. 初始化配置文件
if err := config.InitConfig(cfgFile); err != nil {
log.Fatalf("[致命错误] %v", err)
}
// 命令行 -d 参数优先于 YAML 配置
if debug {
config.GlobalConfig.App.Debug = true
}
// 2. 入库管线:enabled 时构建 HMAC 上报客户端,并用后台盐值初始化 hashid 编码。
// spider 不再直连数据库,所有落库经后端 ingest 接口完成。
ic := config.GlobalConfig.Ingest
if ic.Enabled && ic.Endpoint != "" {
ingest.InitHashID(ic.HashIDSecret)
IngestClient = ingest.NewClient(ic.Endpoint, ic.Secret)
log.Printf("[Info] 入库管线已启用 -> %s", ic.Endpoint)
} else {
IngestClient = nil
}
}