refactor bug report response to use embedded template for report formatting

This commit is contained in:
Flavio Fois
2026-03-18 09:40:10 +01:00
parent f0181aafba
commit 15ef46d413
2 changed files with 47 additions and 18 deletions

View File

@@ -4,6 +4,7 @@ import (
"archive/zip" "archive/zip"
"bytes" "bytes"
"database/sql" "database/sql"
"embed"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
@@ -11,6 +12,7 @@ import (
"log" "log"
"net/http" "net/http"
"strings" "strings"
"text/template"
"github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
@@ -18,6 +20,13 @@ import (
"emly-api-go/internal/models" "emly-api-go/internal/models"
) )
//go:embed templates/report.txt.tmpl
var reportTemplateFS embed.FS
var reportTmpl = template.Must(
template.ParseFS(reportTemplateFS, "templates/report.txt.tmpl"),
)
var fileRoles = []struct { var fileRoles = []struct {
field string field string
role models.FileRole role models.FileRole
@@ -212,27 +221,25 @@ func GetBugReportZipById(db *sqlx.DB) http.HandlerFunc {
return return
} }
reportText := fmt.Sprintf( var sysInfoStr string
"Bug Report #%d\n========================\n\nName: %s\nEmail: %s\nHostname: %s\nOS User: %s\nHWID: %s\nIP: %s\nStatus: %s\nCreated: %s\nUpdated: %s\n\nDescription:\n------------\n%s\n",
report.ID,
report.Name,
report.Email,
report.Hostname,
report.OsUser,
report.HWID,
report.SubmitterIP,
report.Status,
report.CreatedAt.UTC().Format("2006-01-02T15:04:05.000Z"),
report.UpdatedAt.UTC().Format("2006-01-02T15:04:05.000Z"),
report.Description,
)
if len(report.SystemInfo) > 0 && string(report.SystemInfo) != "null" { if len(report.SystemInfo) > 0 && string(report.SystemInfo) != "null" {
pretty, err := json.MarshalIndent(report.SystemInfo, "", " ") if pretty, err := json.MarshalIndent(report.SystemInfo, "", " "); err == nil {
if err == nil { sysInfoStr = string(pretty)
reportText += fmt.Sprintf("\nSystem Info:\n------------\n%s\n", string(pretty))
} }
} }
tmplData := struct {
models.BugReport
CreatedAt string
UpdatedAt string
SystemInfo string
}{
BugReport: report,
CreatedAt: report.CreatedAt.UTC().Format("2006-01-02T15:04:05.000Z"),
UpdatedAt: report.UpdatedAt.UTC().Format("2006-01-02T15:04:05.000Z"),
SystemInfo: sysInfoStr,
}
var buf bytes.Buffer var buf bytes.Buffer
zw := zip.NewWriter(&buf) zw := zip.NewWriter(&buf)
@@ -241,7 +248,7 @@ func GetBugReportZipById(db *sqlx.DB) http.HandlerFunc {
jsonError(w, http.StatusInternalServerError, err.Error()) jsonError(w, http.StatusInternalServerError, err.Error())
return return
} }
if _, err = rf.Write([]byte(reportText)); err != nil { if err = reportTmpl.Execute(rf, tmplData); err != nil {
jsonError(w, http.StatusInternalServerError, err.Error()) jsonError(w, http.StatusInternalServerError, err.Error())
return return
} }

View File

@@ -0,0 +1,22 @@
EMLy Bug Report #{{ .ID }}
========================
Name: {{ .Name }}
Email: {{ .Email }}
Hostname: {{ .Hostname }}
OS User: {{ .OsUser }}
HWID: {{ .HWID }}
IP: {{ .SubmitterIP }}
Status: {{ .Status }}
Created: {{ .CreatedAt }}
Updated: {{ .UpdatedAt }}
Description:
------------
{{ .Description }}
{{ if .SystemInfo }}
System Info:
------------
{{ .SystemInfo }}
{{ end }}