package handler import ( "fmt" "github.com/spf13/viper" "git.kplus.net.ua/yevhen/resource-scraper/pkg/service" "git.kplus.net.ua/yevhen/resource-scraper/types/constant" ) // consoleCommandHandler визначає тип для функцій-обробників консольних команд. // The error it returns decides the process exit code, so cron can tell a failed // run from a successful one. type consoleCommandHandler func() (string, error) type Handler struct { services *service.Service // consoleCommands зберігає мапу назв команд до відповідних функцій-обробників. consoleCommands map[string]consoleCommandHandler } func New(services *service.Service) *Handler { h := &Handler{services: services} // Ініціалізація мапи команд. h.consoleCommands = map[string]consoleCommandHandler{ "console": h.Console, "info": h.Info, "metal-archives": h.MetalArchives, "prescene": h.Prescene, "rutracker": h.Rutracker, "stb": h.STB, "srrdb": h.Srrdb, //"web": h.Web, } return h } // InitConsole runs the command selected by --scope-enable. The text is returned // even together with an error, so the caller can print what happened before it // reports the failure. func (h *Handler) InitConsole() (string, error) { commandName := viper.GetString(constant.FlagScopeEnable) cmdFunc, ok := h.consoleCommands[commandName] if !ok { return "", fmt.Errorf("unknown console command %q", commandName) } result, err := cmdFunc() return commandName + " launched, " + result + "\n", err } func (h *Handler) InitRoutes() (string, error) { //TODO: return "i am the web initiator", nil }