46 lines
1.0 KiB
Go
46 lines
1.0 KiB
Go
package cmd
|
|
|
|
import (
|
|
"os"
|
|
|
|
"github.com/evil7/hostsync/core"
|
|
"github.com/evil7/hostsync/utils"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
// disableCmd 禁用命令
|
|
var disableCmd = &cobra.Command{
|
|
Use: "disable <blockName>",
|
|
Short: "禁用指定块",
|
|
Long: `禁用指定的配置块,其中的域名记录将被注释停用但不删除。
|
|
|
|
示例:
|
|
hostsync disable github # 禁用 github 块`,
|
|
Args: cobra.ExactArgs(1),
|
|
Run: runDisable,
|
|
}
|
|
|
|
func runDisable(cmd *cobra.Command, args []string) {
|
|
utils.CheckAdmin()
|
|
|
|
blockName := args[0]
|
|
if !utils.ValidateBlockName(blockName) {
|
|
utils.LogError("无效的块名称: %s", blockName)
|
|
utils.LogInfo("块名称只能包含字母、数字、下划线和连字符")
|
|
os.Exit(1)
|
|
}
|
|
|
|
hm := core.NewHostsManager()
|
|
if err := hm.Load(); err != nil {
|
|
utils.LogError("加载hosts文件失败: %v", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
if err := hm.DisableBlock(blockName); err != nil {
|
|
utils.LogError("禁用块失败: %v", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
utils.LogSuccess("已禁用块: %s", blockName)
|
|
}
|