refactor bug report response to use embedded template for report formatting
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"archive/zip"
|
||||
"bytes"
|
||||
"database/sql"
|
||||
"embed"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -11,6 +12,7 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/jmoiron/sqlx"
|
||||
@@ -18,6 +20,13 @@ import (
|
||||
"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 {
|
||||
field string
|
||||
role models.FileRole
|
||||
@@ -212,27 +221,25 @@ func GetBugReportZipById(db *sqlx.DB) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
reportText := fmt.Sprintf(
|
||||
"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,
|
||||
)
|
||||
var sysInfoStr string
|
||||
if len(report.SystemInfo) > 0 && string(report.SystemInfo) != "null" {
|
||||
pretty, err := json.MarshalIndent(report.SystemInfo, "", " ")
|
||||
if err == nil {
|
||||
reportText += fmt.Sprintf("\nSystem Info:\n------------\n%s\n", string(pretty))
|
||||
if pretty, err := json.MarshalIndent(report.SystemInfo, "", " "); err == nil {
|
||||
sysInfoStr = 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
|
||||
zw := zip.NewWriter(&buf)
|
||||
|
||||
@@ -241,7 +248,7 @@ func GetBugReportZipById(db *sqlx.DB) http.HandlerFunc {
|
||||
jsonError(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
if _, err = rf.Write([]byte(reportText)); err != nil {
|
||||
if err = reportTmpl.Execute(rf, tmplData); err != nil {
|
||||
jsonError(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
22
internal/handlers/templates/report.txt.tmpl
Normal file
22
internal/handlers/templates/report.txt.tmpl
Normal 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 }}
|
||||
|
||||
Reference in New Issue
Block a user