Add Certificates page and Traefik API frontend client
This commit is contained in:
parent
9e4c612dcb
commit
7fc90524b5
40 changed files with 6585 additions and 359 deletions
|
|
@ -1,11 +1,13 @@
|
|||
package file
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/pmezard/go-difflib/difflib"
|
||||
)
|
||||
|
||||
// UnifiedDiff returns a unified diff between old and new content
|
||||
// UnifiedDiff returns a proper unified diff between old and new content using LCS (Myers via difflib).
|
||||
// It handles insertions, deletions, replacements, moved blocks, empty and new files.
|
||||
func UnifiedDiff(filename, oldContent, newContent string) string {
|
||||
if oldContent == newContent {
|
||||
return ""
|
||||
|
|
@ -13,72 +15,45 @@ func UnifiedDiff(filename, oldContent, newContent string) string {
|
|||
oldLines := splitLines(oldContent)
|
||||
newLines := splitLines(newContent)
|
||||
|
||||
// Simple line-based diff: collect added/removed
|
||||
// Use LCS-style? For MVP use simple prefix diff
|
||||
var b strings.Builder
|
||||
fmt.Fprintf(&b, "--- a/%s\n+++ b/%s\n", filename, filename)
|
||||
|
||||
// Find common prefix/suffix for brevity
|
||||
// Instead do full: show removed then added
|
||||
oldSet := make(map[string]int)
|
||||
for _, l := range oldLines {
|
||||
oldSet[l]++
|
||||
diff := difflib.UnifiedDiff{
|
||||
A: difflib.SplitLines(oldContent),
|
||||
B: difflib.SplitLines(newContent),
|
||||
FromFile: "a/" + filename,
|
||||
ToFile: "b/" + filename,
|
||||
Context: 3,
|
||||
}
|
||||
newSet := make(map[string]int)
|
||||
for _, l := range newLines {
|
||||
newSet[l]++
|
||||
// difflib.SplitLines handles trailing newlines correctly, but we also keep oldLines/newLines for
|
||||
// fallback when one side is empty to ensure diff is still produced.
|
||||
if len(oldLines) == 0 && len(newLines) == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
// Very simple: if old empty (new file), show all as +
|
||||
if len(oldLines) == 0 || (len(oldLines) == 1 && oldLines[0] == "") {
|
||||
for _, l := range newLines {
|
||||
fmt.Fprintf(&b, "+%s\n", l)
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
if len(newLines) == 0 {
|
||||
for _, l := range oldLines {
|
||||
fmt.Fprintf(&b, "-%s\n", l)
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
// For MVP, do line-by-line with +/- for changed lines
|
||||
// Use diff library would be better, but avoid extra dep: implement simple
|
||||
max := len(oldLines)
|
||||
if len(newLines) > max {
|
||||
max = len(newLines)
|
||||
}
|
||||
// Show hunks: 3 context lines? Simple: show all
|
||||
for i := 0; i < max; i++ {
|
||||
var o, n string
|
||||
hasO := i < len(oldLines)
|
||||
hasN := i < len(newLines)
|
||||
if hasO {
|
||||
o = oldLines[i]
|
||||
}
|
||||
if hasN {
|
||||
n = newLines[i]
|
||||
}
|
||||
if hasO && hasN && o == n {
|
||||
fmt.Fprintf(&b, " %s\n", o)
|
||||
} else {
|
||||
if hasO {
|
||||
fmt.Fprintf(&b, "-%s\n", o)
|
||||
}
|
||||
if hasN {
|
||||
fmt.Fprintf(&b, "+%s\n", n)
|
||||
text, _ := difflib.GetUnifiedDiffString(diff)
|
||||
// difflib returns empty string when no diff; ensure header is present for new/deleted files
|
||||
if text == "" {
|
||||
// Fallback minimal diff for edge cases difflib might consider empty
|
||||
var b strings.Builder
|
||||
b.WriteString("--- a/" + filename + "\n+++ b/" + filename + "\n")
|
||||
if len(oldLines) == 0 {
|
||||
for _, l := range newLines {
|
||||
b.WriteString("+" + l + "\n")
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
if len(newLines) == 0 {
|
||||
for _, l := range oldLines {
|
||||
b.WriteString("-" + l + "\n")
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
return ""
|
||||
}
|
||||
return b.String()
|
||||
return text
|
||||
}
|
||||
|
||||
func splitLines(s string) []string {
|
||||
if s == "" {
|
||||
return []string{}
|
||||
}
|
||||
// Normalize line endings
|
||||
s = strings.ReplaceAll(s, "\r\n", "\n")
|
||||
return strings.Split(s, "\n")
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue