133 lines
3.8 KiB
Go
133 lines
3.8 KiB
Go
package cmd
|
||
|
||
import (
|
||
"fmt"
|
||
"os"
|
||
"runtime"
|
||
|
||
"github.com/evil7/hostsync/config"
|
||
"github.com/evil7/hostsync/utils"
|
||
"github.com/spf13/cobra"
|
||
)
|
||
|
||
var (
|
||
debugMode bool
|
||
version = "1.0.2"
|
||
)
|
||
|
||
// rootCmd 根命令
|
||
var rootCmd = &cobra.Command{
|
||
Use: "hostsync",
|
||
Short: "一个强大的命令行 Hosts 文件管理工具",
|
||
Long: `HostSync 是一个强大的命令行 Hosts 文件管理工具,支持分块管理、智能 DNS 解析和定时自动更新。
|
||
|
||
功能特点:
|
||
- 🎯 按名称分块管理 Hosts 配置
|
||
- 🌐 支持多种 DNS 解析方式(DNS、DoH、预设服务器)
|
||
- 🔄 一键自动更新记录
|
||
- ⏰ 按任务定时自动更新块
|
||
- 📝 智能格式化与清理
|
||
- 🔧 后台服务模式运行`,
|
||
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
||
// 设置调试模式
|
||
utils.SetDebugMode(debugMode)
|
||
},
|
||
}
|
||
|
||
// Execute 执行根命令
|
||
func Execute() {
|
||
if err := rootCmd.Execute(); err != nil {
|
||
fmt.Fprintln(os.Stderr, err)
|
||
os.Exit(1)
|
||
}
|
||
}
|
||
|
||
func init() {
|
||
// 全局标志
|
||
rootCmd.PersistentFlags().BoolVar(&debugMode, "debug", false, "启用调试模式")
|
||
// 添加子命令
|
||
rootCmd.AddCommand(versionCmd)
|
||
rootCmd.AddCommand(listCmd)
|
||
rootCmd.AddCommand(enableCmd)
|
||
rootCmd.AddCommand(disableCmd)
|
||
rootCmd.AddCommand(addCmd)
|
||
rootCmd.AddCommand(removeCmd)
|
||
rootCmd.AddCommand(updateCmd)
|
||
rootCmd.AddCommand(formatCmd)
|
||
rootCmd.AddCommand(serverCmd)
|
||
rootCmd.AddCommand(cronCmd)
|
||
rootCmd.AddCommand(initCmd)
|
||
rootCmd.AddCommand(serviceCmd)
|
||
rootCmd.AddCommand(logCmd)
|
||
}
|
||
|
||
// versionCmd 版本命令
|
||
var versionCmd = &cobra.Command{
|
||
Use: "version",
|
||
Short: "显示版本信息",
|
||
Run: func(cmd *cobra.Command, args []string) {
|
||
showVersionInfo()
|
||
},
|
||
}
|
||
|
||
// showVersionInfo 显示详细的版本和系统信息
|
||
func showVersionInfo() {
|
||
utils.LogResult("程序版本: v%s\n", version)
|
||
utils.LogResult("Go 版本: %s\n", runtime.Version())
|
||
utils.LogResult("系统架构: %s/%s\n", runtime.GOOS, runtime.GOARCH)
|
||
utils.LogResult("编译器: %s\n", runtime.Compiler)
|
||
|
||
// 显示配置信息
|
||
if config.AppConfig != nil {
|
||
utils.LogResult("\n配置信息:\n")
|
||
utils.LogResult(" Hosts 路径: %s\n", config.AppConfig.HostsPath)
|
||
utils.LogResult(" 日志级别: %s\n", config.AppConfig.LogLevel)
|
||
utils.LogResult(" 日志路径: %s\n", config.AppConfig.LogPath)
|
||
utils.LogResult(" 备份数量: %d 个\n", config.AppConfig.BackupCount)
|
||
utils.LogResult(" DNS 超时: %d ms\n", config.AppConfig.DNSTimeout)
|
||
utils.LogResult(" 最大并发: %d\n", config.AppConfig.MaxConcurrent)
|
||
// 显示DNS服务器数量
|
||
if len(config.DNSServers) > 0 {
|
||
utils.LogResult(" 可用DNS服务器: %d 个\n", len(config.DNSServers))
|
||
}
|
||
|
||
utils.LogResult("\n文件路径:\n")
|
||
if config.ConfigPath != "" {
|
||
utils.LogResult(" 配置文件: %s\n", config.ConfigPath)
|
||
}
|
||
if config.ServersPath != "" {
|
||
utils.LogResult(" 服务器配置: %s\n", config.ServersPath)
|
||
}
|
||
} else {
|
||
utils.LogResult("\n配置信息: 未初始化\n")
|
||
}
|
||
|
||
// 显示运行时信息
|
||
utils.LogResult("\n运行时信息:\n")
|
||
utils.LogResult(" CPU 核心数: %d\n", runtime.NumCPU())
|
||
utils.LogResult(" Goroutines: %d\n", runtime.NumGoroutine())
|
||
// 显示可执行文件信息
|
||
utils.LogResult("\n程序信息:\n")
|
||
if execPath, err := os.Executable(); err == nil {
|
||
if stat, err := os.Stat(execPath); err == nil {
|
||
utils.LogResult(" 可执行文件: %s\n", execPath)
|
||
utils.LogResult(" 文件大小: %s\n", formatFileSizeSimple(stat.Size()))
|
||
utils.LogResult(" 修改时间: %s\n", stat.ModTime().Format("2006-01-02 15:04:05"))
|
||
}
|
||
}
|
||
}
|
||
|
||
// formatFileSizeSimple 简单的文件大小格式化函数
|
||
func formatFileSizeSimple(size int64) string {
|
||
const unit = 1024
|
||
if size < unit {
|
||
return fmt.Sprintf("%d B", size)
|
||
}
|
||
div, exp := int64(unit), 0
|
||
for n := size / unit; n >= unit; n /= unit {
|
||
div *= unit
|
||
exp++
|
||
}
|
||
return fmt.Sprintf("%.1f %cB", float64(size)/float64(div), "KMGTPE"[exp])
|
||
}
|