1
0
mirror of https://github.com/StackExchange/dnscontrol.git synced 2024-05-11 05:55:12 +00:00

get-zones output should work as input into preview (#688)

* Add tests for get-zones
* fix CAA, SSHFP, TLSA and other bugs
* New format for get-zones: "djs" which is js but uses "disco commas"
* Print diffs using github.com/andreyvit/diff

Co-authored-by: Tom Limoncelli <tlimoncelli@stackoverflow.com>
This commit is contained in:
Tom Limoncelli
2020-03-10 11:32:47 -04:00
committed by GitHub
parent 67e78f7e15
commit 4edf360854
30 changed files with 4993 additions and 37 deletions

24
vendor/github.com/andreyvit/diff/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,24 @@
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so
# Folders
_obj
_test
# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out
*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*
_testmain.go
*.exe
*.test
*.prof

21
vendor/github.com/andreyvit/diff/LICENSE generated vendored Normal file
View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2017 Andrey Tarantsov
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

28
vendor/github.com/andreyvit/diff/README.md generated vendored Normal file
View File

@@ -0,0 +1,28 @@
# diff
Quick'n'easy string diffing functions for Golang based on [github.com/sergi/go-diff](https://github.com/sergi/go-diff). Mainly for diffing strings in tests.
See [the docs on GoDoc](https://godoc.org/github.com/andreyvit/diff).
Get it:
go get -u github.com/andreyvit/diff
Example:
import (
"strings"
"testing"
"github.com/andreyvit/diff"
)
const expected = `
...
`
func TestFoo(t *testing.T) {
actual := Foo(...)
if a, e := strings.TrimSpace(actual), strings.TrimSpace(expected); a != e {
t.Errorf("Result not as expected:\n%v", diff.LineDiff(e, a))
}
}

128
vendor/github.com/andreyvit/diff/diff.go generated vendored Normal file
View File

@@ -0,0 +1,128 @@
package diff
import (
"bytes"
"strings"
"github.com/sergi/go-diff/diffmatchpatch"
)
func diff(a, b string) []diffmatchpatch.Diff {
dmp := diffmatchpatch.New()
diffs := dmp.DiffMain(a, b, true)
if len(diffs) > 2 {
diffs = dmp.DiffCleanupSemantic(diffs)
diffs = dmp.DiffCleanupEfficiency(diffs)
}
return diffs
}
// CharacterDiff returns an inline diff between the two strings, using (++added++) and (~~deleted~~) markup.
func CharacterDiff(a, b string) string {
return diffsToString(diff(a, b))
}
func diffsToString(diffs []diffmatchpatch.Diff) string {
var buff bytes.Buffer
for _, diff := range diffs {
text := diff.Text
switch diff.Type {
case diffmatchpatch.DiffInsert:
buff.WriteString("(++")
buff.WriteString(text)
buff.WriteString("++)")
case diffmatchpatch.DiffDelete:
buff.WriteString("(~~")
buff.WriteString(text)
buff.WriteString("~~)")
case diffmatchpatch.DiffEqual:
buff.WriteString(text)
}
}
return buff.String()
}
// LineDiff returns a normal linewise diff between the two given strings.
func LineDiff(a, b string) string {
return strings.Join(LineDiffAsLines(a, b), "\n")
}
// LineDiffAsLines returns the lines of a linewise diff between the two given strings.
func LineDiffAsLines(a, b string) []string {
return diffsToPatchLines(diff(a, b))
}
type patchBuilder struct {
output []string
oldLines []string
newLines []string
newLineBuffer bytes.Buffer
oldLineBuffer bytes.Buffer
}
func (b *patchBuilder) AddCharacters(text string, op diffmatchpatch.Operation) {
if op == diffmatchpatch.DiffInsert || op == diffmatchpatch.DiffEqual {
b.newLineBuffer.WriteString(text)
}
if op == diffmatchpatch.DiffDelete || op == diffmatchpatch.DiffEqual {
b.oldLineBuffer.WriteString(text)
}
}
func (b *patchBuilder) AddNewline(op diffmatchpatch.Operation) {
oldLine := b.oldLineBuffer.String()
newLine := b.newLineBuffer.String()
if op == diffmatchpatch.DiffEqual && (oldLine == newLine) {
b.FlushChunk()
b.output = append(b.output, " "+newLine)
b.oldLineBuffer.Reset()
b.newLineBuffer.Reset()
} else {
if op == diffmatchpatch.DiffDelete || op == diffmatchpatch.DiffEqual {
b.oldLines = append(b.oldLines, "-"+oldLine)
b.oldLineBuffer.Reset()
}
if op == diffmatchpatch.DiffInsert || op == diffmatchpatch.DiffEqual {
b.newLines = append(b.newLines, "+"+newLine)
b.newLineBuffer.Reset()
}
}
}
func (b *patchBuilder) FlushChunk() {
if b.oldLines != nil {
b.output = append(b.output, b.oldLines...)
b.oldLines = nil
}
if b.newLines != nil {
b.output = append(b.output, b.newLines...)
b.newLines = nil
}
}
func (b *patchBuilder) Flush() {
if b.oldLineBuffer.Len() > 0 && b.newLineBuffer.Len() > 0 {
b.AddNewline(diffmatchpatch.DiffEqual)
} else if b.oldLineBuffer.Len() > 0 {
b.AddNewline(diffmatchpatch.DiffDelete)
} else if b.newLineBuffer.Len() > 0 {
b.AddNewline(diffmatchpatch.DiffInsert)
}
b.FlushChunk()
}
func diffsToPatchLines(diffs []diffmatchpatch.Diff) []string {
b := new(patchBuilder)
b.output = make([]string, 0, len(diffs))
for _, diff := range diffs {
lines := strings.Split(diff.Text, "\n")
for idx, line := range lines {
if idx > 0 {
b.AddNewline(diff.Type)
}
b.AddCharacters(line, diff.Type)
}
}
b.Flush()
return b.output
}

2
vendor/github.com/andreyvit/diff/doc.go generated vendored Normal file
View File

@@ -0,0 +1,2 @@
// diff provides quick and easy string diffing functions based on github.com/sergi/go-diff, mainly for diffing strings in tests
package diff

19
vendor/github.com/andreyvit/diff/trim.go generated vendored Normal file
View File

@@ -0,0 +1,19 @@
package diff
import (
"strings"
)
// TrimLines applies TrimSpace to each string in the given array.
func TrimLines(input []string) []string {
result := make([]string, 0, len(input))
for _, el := range input {
result = append(result, strings.TrimSpace(el))
}
return result
}
// TrimLinesInString applies TrimSpace to each line in the given string, and returns the new trimmed string. Empty lines are not removed.
func TrimLinesInString(input string) string {
return strings.Join(TrimLines(strings.Split(strings.TrimSpace(input), "\n")), "\n")
}