This commit is contained in:
1 parent
9bdf866a87
commit
5faadcd6eb
41 files changed
+1994
-59
No files matched your search
@@ -2,6 +2,7 @@ package config
|
||||
|
||||
import (
|
||||
"encoding/csv"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -10,11 +11,15 @@ import (
|
||||
func exportAuditCSV(events []ConfigAuditEvent) ([]byte, error) {
|
||||
var builder strings.Builder
|
||||
writer := csv.NewWriter(&builder)
|
||||
if err := writer.Write([]string{"id", "operation", "section", "actor_id", "actor_role", "from_generation", "to_generation", "target_generation", "changed_fields", "confirmation_method", "result", "created_at"}); err != nil {
|
||||
if err := writer.Write([]string{"id", "operation", "section", "actor_id", "actor_role", "from_generation", "to_generation", "target_generation", "changed_fields", "confirmation_method", "result", "created_at", "actor_name", "changes"}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, event := range events {
|
||||
if err := writer.Write([]string{
|
||||
changes, err := json.Marshal(event.Changes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cells := []string{
|
||||
event.ID,
|
||||
string(event.Operation),
|
||||
string(event.Section),
|
||||
@@ -27,7 +32,15 @@ func exportAuditCSV(events []ConfigAuditEvent) ([]byte, error) {
|
||||
event.ConfirmationMethod,
|
||||
event.Result,
|
||||
event.CreatedAt.UTC().Format(time.RFC3339Nano),
|
||||
}); err != nil {
|
||||
event.ActorName,
|
||||
string(changes),
|
||||
}
|
||||
for index, cell := range cells {
|
||||
if strings.HasPrefix(cell, "=") || strings.HasPrefix(cell, "+") || strings.HasPrefix(cell, "-") || strings.HasPrefix(cell, "@") || strings.HasPrefix(cell, "\t") || strings.HasPrefix(cell, "\r") {
|
||||
cells[index] = "'" + cell
|
||||
}
|
||||
}
|
||||
if err := writer.Write(cells); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
@@ -37,3 +50,8 @@ func exportAuditCSV(events []ConfigAuditEvent) ([]byte, error) {
|
||||
}
|
||||
return []byte(builder.String()), nil
|
||||
}
|
||||
|
||||
// ExportConfigAuditCSV 让文件、内存和数据库导出共享字段及表格公式保护。
|
||||
func ExportConfigAuditCSV(events []ConfigAuditEvent) ([]byte, error) {
|
||||
return exportAuditCSV(events)
|
||||
}
|
||||
Reference in new issue
Block a user