feat(controller): enhance command handlers to accept source parameter for improved message formatting
This commit is contained in:
@@ -10,16 +10,16 @@ import (
|
||||
"nukumizu-backend/internal/template"
|
||||
)
|
||||
|
||||
func handleHelp() (string, error) {
|
||||
func handleHelp(cmd Command) (string, error) {
|
||||
cfg := config.GetConfig()
|
||||
params := template.BuildBotInitializationMsgParams()
|
||||
return template.Render(cfg.ControllerMessage.BotHelp, params), nil
|
||||
return template.Render(cfg.ControllerMessage.BotHelp, params, cmd.Source), nil
|
||||
}
|
||||
|
||||
func handleList() (string, error) {
|
||||
func handleList(cmd Command) (string, error) {
|
||||
cfg := config.GetConfig()
|
||||
params := template.BuildParamsFromServerList()
|
||||
return template.Render(cfg.ControllerMessage.ServerList, params), nil
|
||||
return template.Render(cfg.ControllerMessage.ServerList, params, cmd.Source), nil
|
||||
}
|
||||
|
||||
func handleStatus(cmd Command) (string, error) {
|
||||
@@ -130,7 +130,7 @@ func handleRun(cmd Command) (string, error) {
|
||||
|
||||
cfg := config.GetConfig()
|
||||
params := template.BuildParamsFromExecResult(uuidArg, uuidArg, command, formatTaskResults(results))
|
||||
return template.Render(cfg.ControllerMessage.ServerExecuteResult, params), nil
|
||||
return template.Render(cfg.ControllerMessage.ServerExecuteResult, params, cmd.Source), nil
|
||||
}
|
||||
|
||||
func handleInfo(cmd Command) (string, error) {
|
||||
@@ -180,7 +180,7 @@ func handleInfo(cmd Command) (string, error) {
|
||||
func telegram_handleStart() (string, error) {
|
||||
cfg := config.GetConfig()
|
||||
params := template.BuildBotInitializationMsgParams()
|
||||
return template.Render(cfg.ControllerMessage.Tg_BotStart, params), nil
|
||||
return template.Render(cfg.ControllerMessage.Tg_BotStart, params, "telegram"), nil
|
||||
}
|
||||
|
||||
func handleGetIP(cmd Command) (string, error) {
|
||||
|
||||
@@ -51,9 +51,9 @@ func (m *Manager) RouteCommand(cmd Command) (string, error) {
|
||||
}
|
||||
switch cmd.Command {
|
||||
case "help":
|
||||
return handleHelp()
|
||||
return handleHelp(cmd)
|
||||
case "list":
|
||||
return handleList()
|
||||
return handleList(cmd)
|
||||
case "status":
|
||||
return handleStatus(cmd)
|
||||
case "shutdown":
|
||||
|
||||
@@ -456,9 +456,9 @@ func (t *Tracker) GetOnlineServers() []string {
|
||||
for uuid := range t.onlineSet {
|
||||
name := t.uuidToName[uuid]
|
||||
if name != "" {
|
||||
result = append(result, fmt.Sprintf("- %s (%s)", name, uuid))
|
||||
result = append(result, fmt.Sprintf("- %s (`%s`)", name, uuid))
|
||||
} else {
|
||||
result = append(result, fmt.Sprintf("- %s", uuid))
|
||||
result = append(result, fmt.Sprintf("- `%s`", uuid))
|
||||
}
|
||||
}
|
||||
return result
|
||||
@@ -478,9 +478,9 @@ func (t *Tracker) GetOfflineServers() []string {
|
||||
for uuid := range offlineSet {
|
||||
name := t.uuidToName[uuid]
|
||||
if name != "" {
|
||||
result = append(result, fmt.Sprintf("- %s (%s)", name, uuid))
|
||||
result = append(result, fmt.Sprintf("- %s (`%s`)", name, uuid))
|
||||
} else {
|
||||
result = append(result, fmt.Sprintf("- %s", uuid))
|
||||
result = append(result, fmt.Sprintf("- `%s`", uuid))
|
||||
}
|
||||
}
|
||||
return result
|
||||
|
||||
@@ -99,9 +99,29 @@ func BuildParamsFromExecResult(serverName, serverUUID, command, result string) P
|
||||
// - {{ softwareBuildTime }} — software build time
|
||||
// - {{ softwareDeveloper }} — software developer
|
||||
// - {{ softwareDescription }} — software description
|
||||
func Render(tmpl string, params Params) string {
|
||||
func Render(tmpl string, params Params, source ...string) string {
|
||||
result := tmpl
|
||||
|
||||
if len(source) > 0 && source[0] == "telegram" {
|
||||
// Telegram requires special formatting for code blocks and inline code.
|
||||
result = strings.ReplaceAll(result, "{{ time }}", "**" + params.Time + "**")
|
||||
result = strings.ReplaceAll(result, "{{ serverName }}", "**" + params.ServerName + "**")
|
||||
result = strings.ReplaceAll(result, "{{ serverUUID }}", "`" + params.ServerUUID + "`")
|
||||
result = strings.ReplaceAll(result, "{{ upStatus }}", "**" + params.UpStatus + "**")
|
||||
result = strings.ReplaceAll(result, "{{ event }}", "**" + params.Event + "**")
|
||||
result = strings.ReplaceAll(result, "{{ message }}", "`" + params.Message + "`")
|
||||
result = strings.ReplaceAll(result, "{{ command }}", "`" + params.Command + "`")
|
||||
result = strings.ReplaceAll(result, "{{ result }}", "```bash\n" + params.Result + "\n```")
|
||||
result = strings.ReplaceAll(result, "{{ list.onlineServers }}", params.OnlineServers)
|
||||
result = strings.ReplaceAll(result, "{{ list.offlineServers }}", params.OfflineServers)
|
||||
result = strings.ReplaceAll(result, "{{ softwareVersion }}", params.SoftwareVersion)
|
||||
result = strings.ReplaceAll(result, "{{ softwareBuildVer }}", fmt.Sprintf("%d", params.SoftwareBuildVer))
|
||||
result = strings.ReplaceAll(result, "{{ softwareCommitHash }}", params.SoftwareCommitHash)
|
||||
result = strings.ReplaceAll(result, "{{ softwareBuildType }}", params.SoftwareBuildType)
|
||||
result = strings.ReplaceAll(result, "{{ softwareBuildTime }}", params.SoftwareBuildTime)
|
||||
result = strings.ReplaceAll(result, "{{ softwareDeveloper }}", params.SoftwareDeveloper)
|
||||
result = strings.ReplaceAll(result, "{{ softwareDescription }}", params.SoftwareDescription)
|
||||
} else {
|
||||
result = strings.ReplaceAll(result, "{{ time }}", params.Time)
|
||||
result = strings.ReplaceAll(result, "{{ serverName }}", params.ServerName)
|
||||
result = strings.ReplaceAll(result, "{{ serverUUID }}", params.ServerUUID)
|
||||
@@ -119,16 +139,16 @@ func Render(tmpl string, params Params) string {
|
||||
result = strings.ReplaceAll(result, "{{ softwareBuildTime }}", params.SoftwareBuildTime)
|
||||
result = strings.ReplaceAll(result, "{{ softwareDeveloper }}", params.SoftwareDeveloper)
|
||||
result = strings.ReplaceAll(result, "{{ softwareDescription }}", params.SoftwareDescription)
|
||||
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// FormatServerListEntry formats a single server entry for list display.
|
||||
func FormatServerListEntry(name, uuid string) string {
|
||||
if name == "" {
|
||||
return fmt.Sprintf("- %s", uuid)
|
||||
return fmt.Sprintf("- `%s`", uuid)
|
||||
}
|
||||
return fmt.Sprintf("- %s (%s)", name, uuid)
|
||||
return fmt.Sprintf("- %s (`%s`)", name, uuid)
|
||||
}
|
||||
|
||||
// JoinServerListEntries joins formatted server entries with newlines.
|
||||
|
||||
Reference in New Issue
Block a user