Files
ops-assistant/internal/core/ops/service.go
T

94 lines
2.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 ops
import (
"fmt"
"strings"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"ops-assistant/internal/core/command"
coremodule "ops-assistant/internal/core/module"
"ops-assistant/internal/core/registry"
)
type Service struct {
dbPath string
baseDir string
registry *registry.Registry
db *gorm.DB
}
func NewService(dbPath, baseDir string, reg *registry.Registry) *Service {
db, _ := gorm.Open(sqlite.Open(dbPath), &gorm.Config{})
return &Service{dbPath: dbPath, baseDir: baseDir, registry: reg, db: db}
}
func (s *Service) Handle(userID int64, text string) (bool, string) {
if !strings.HasPrefix(strings.TrimSpace(text), "/") {
return false, ""
}
cmd, _, err := command.ParseWithInputs(text)
if err != nil {
return false, ""
}
// 通用帮助
if cmd.Module == "help" || cmd.Name == "/help" || cmd.Name == "/start" {
return true, s.helpText()
}
if cmd.Module == "ops" && (len(cmd.Args) == 0 || cmd.Args[0] == "help") {
return true, s.helpText()
}
if cmd.Module == "ops" && len(cmd.Args) > 0 && cmd.Args[0] == "modules" {
return true, s.modulesStatusText()
}
if cmd.Module != "" && cmd.Module != "ops" && cmd.Module != "rate" && s.db != nil {
if !coremodule.IsEnabled(s.db, cmd.Module) {
return true, fmt.Sprintf("[ERR_FEATURE_DISABLED] 模块未启用: %s(开关: enable_module_%s)", cmd.Module, cmd.Module)
}
}
out, handled, err := s.registry.Handle(userID, cmd)
if !handled {
return false, ""
}
if err != nil {
return true, "❌ OPS 执行失败: " + err.Error()
}
return true, out
}
func (s *Service) helpText() string {
lines := []string{
"🛠️ OPS 帮助(模块入口):",
"- /ops modules (查看模块启用状态)",
"- /cpa (CPA 使用统计/备份/恢复)",
"- /cf (Cloudflare 管理)",
"- /mail (邮箱状态)",
"- /cfdav (CFDAV 用户管理)",
"- /sub (订阅管理)",
"- /rate 138sgd to cny (实时汇率转换)",
"提示:发送 /cpa /cf /mail /cfdav /sub 获取各模块完整指令清单。",
}
return strings.Join(lines, "\n")
}
func (s *Service) modulesStatusText() string {
mods := s.registry.ListModules()
if len(mods) == 0 {
return "暂无已注册模块"
}
lines := []string{"🧩 模块状态:"}
for _, m := range mods {
enabled := false
if s.db != nil {
enabled = coremodule.IsEnabled(s.db, m)
}
state := "disabled"
if enabled {
state = "enabled"
}
lines = append(lines, fmt.Sprintf("- %s: %s", m, state))
}
lines = append(lines, "\n可用命令:/ops modules")
return strings.Join(lines, "\n")
}