Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement patch generation, apply and tests #282

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added cmd/dyff/dyff
Binary file not shown.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/mitchellh/hashstructure v1.1.0
github.com/onsi/ginkgo/v2 v2.8.3
github.com/onsi/gomega v1.27.1
github.com/pkg/errors v0.9.1
github.com/sergi/go-diff v1.3.1
github.com/spf13/cobra v1.6.1
github.com/texttheater/golang-levenshtein v1.0.1
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ github.com/onsi/ginkgo/v2 v2.8.3/go.mod h1:6OaUA8BCi0aZfmzYT/q9AacwTzDpNbxILUT+T
github.com/onsi/gomega v1.27.1 h1:rfztXRbg6nv/5f+Raen9RcGoSecHIFgBBLQK3Wdj754=
github.com/onsi/gomega v1.27.1/go.mod h1:aHX5xOykVYzWOV4WqQy0sy8BQptgukenXpCXfadcIAw=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
Expand Down
136 changes: 136 additions & 0 deletions internal/cmd/applyPatch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// Copyright © 2019 The Homeport Team
//
// 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.

package cmd

import (
"bufio"
"bytes"
"fmt"
"github.com/gonvenience/ytbx"
"github.com/homeport/dyff/pkg/dyff"
"github.com/pkg/errors"
"github.com/spf13/cobra"
yamlv3 "gopkg.in/yaml.v3"
"io"
"os"
)

type applyPatchCmdOptions struct {
output string
indent uint
}

var applyPatchCmdSettings applyPatchCmdOptions

// yamlCmd represents the yaml command
var applyPatchCmd = &cobra.Command{
Use: "apply-patch [flags] <patch-file-location> <input-file or '-'>",
Aliases: []string{"apply", "ap"},
Args: cobra.MinimumNArgs(2),
Short: "Apply a YAML patch produced by 'between' to a file (use '-' for stdin)",
Long: `Applies a YAML patch created by the 'between' command (see between help for details) to a YAML file or stdin and writes to an output file or stdout.`,

RunE: func(cmd *cobra.Command, args []string) error {
patchb, err := os.ReadFile(args[0])
if err != nil {
return errors.Wrap(err, "error reading patch file")
}

var patch []dyff.PatchOp
err = yamlv3.Unmarshal(patchb, &patch)
if err != nil {
return errors.Wrap(err, "error unmarshaling patch (is patch file corrupted?)")
}

var input []byte
if args[1] == "-" {
in, err := io.ReadAll(os.Stdin)
if err != nil {
return errors.Wrap(err, "error reading from stdin")
}
input = in
} else {
in, err := os.ReadFile(args[1])
if err != nil {
return errors.Wrap(err, "error reading input file")
}
input = in
}

inputdocs, err := ytbx.LoadYAMLDocuments(input)
if err != nil {
return errors.Wrap(err, "error unmarshaling input (is it valid YAML?)")
}

if len(inputdocs) == 0 {
return fmt.Errorf("no YAML documents found in input")
}

if len(inputdocs) > 1 {
fmt.Fprintf(os.Stderr, "warning: multiple yaml docs found in input, applying patch to the first doc only")
}

err = dyff.ApplyPatch(inputdocs[0], patch)
if err != nil {
return errors.Wrap(err, "error applying patch")
}

var b bytes.Buffer
bw := bufio.NewWriter(&b)
yenc := yamlv3.NewEncoder(bw)
yenc.SetIndent(int(applyPatchCmdSettings.indent))

err = yenc.Encode(inputdocs[0])
if err != nil {
return errors.Wrap(err, "error marshaling yaml output")
}
err = yenc.Close()
if err != nil {
return errors.Wrap(err, "error flushing yaml encoder")
}
err = bw.Flush()
if err != nil {
return errors.Wrap(err, "error flushing output buffer")
}

out := b.Bytes()

if applyPatchCmdSettings.output == "-" {
fmt.Printf("%s\v", out)
} else {
err := os.WriteFile(applyPatchCmdSettings.output, out, 0644)
if err != nil {
return errors.Wrap(err, "error writing output file")
}
}

return nil
},
}

func init() {
rootCmd.AddCommand(applyPatchCmd)

applyPatchCmd.Flags().SortFlags = false

applyPatchCmd.Flags().StringVarP(&applyPatchCmdSettings.output, "output", "o", "-", "output path (use - for stdout)")
applyPatchCmd.Flags().UintVarP(&applyPatchCmdSettings.indent, "indent", "i", 2, "YAML output indent size (in spaces)")
}
36 changes: 33 additions & 3 deletions internal/cmd/between.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@
package cmd

import (
"fmt"
"github.com/gonvenience/wrap"
"github.com/gonvenience/ytbx"
"github.com/spf13/cobra"

"github.com/homeport/dyff/pkg/dyff"
"github.com/spf13/cobra"
yamlv3 "gopkg.in/yaml.v3"
"os"
)

type betweenCmdOptions struct {
Expand All @@ -34,6 +36,8 @@ type betweenCmdOptions struct {
chroot string
chrootFrom string
chrootTo string
genPatch bool
patchfile string
}

var betweenCmdSettings betweenCmdOptions
Expand Down Expand Up @@ -109,7 +113,31 @@ types are: YAML (http://yaml.org/) and JSON (http://json.org/).
report = report.ExcludeRegexp(reportOptions.excludeRegexps...)
}

return writeReport(cmd, report)
if err := writeReport(cmd, report); err != nil {
return err
}

if betweenCmdSettings.genPatch {
p, err := dyff.GeneratePatch(&report)
if err != nil {
return wrap.Errorf(err, "error generating patch")
}
pm, err := yamlv3.Marshal(p)
if err != nil {
return wrap.Errorf(err, "error marshaling patch")
}

if betweenCmdSettings.patchfile == "-" {
fmt.Printf("%s\n", pm)
} else {
err := os.WriteFile(betweenCmdSettings.patchfile, pm, 0644)
if err != nil {
return wrap.Errorf(err, "error writing patch file")
}
}
}

return nil
},
}

Expand All @@ -126,4 +154,6 @@ func init() {
betweenCmd.Flags().StringVar(&betweenCmdSettings.chrootFrom, "chroot-of-from", "", "only change the root level of the from input file")
betweenCmd.Flags().StringVar(&betweenCmdSettings.chrootTo, "chroot-of-to", "", "only change the root level of the to input file")
betweenCmd.Flags().BoolVar(&betweenCmdSettings.translateListToDocuments, "chroot-list-to-documents", false, "in case the change root points to a list, treat this list as a set of documents and not as the list itself")
betweenCmd.Flags().BoolVar(&betweenCmdSettings.genPatch, "generate-patch", false, "generate patch")
betweenCmd.Flags().StringVar(&betweenCmdSettings.patchfile, "patch-file", "patch.txt", "patch output filename (use - for stdout)")
}
85 changes: 85 additions & 0 deletions internal/cmd/cmds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ package cmd_test

import (
"fmt"
dyff2 "github.com/homeport/dyff/pkg/dyff"
yamlv3 "gopkg.in/yaml.v3"
"os"
"strings"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -579,6 +582,88 @@ spec.replicas (Deployment/default/test)
Expect(err).ToNot(HaveOccurred())
Expect(out).To(BeEquivalentTo("\n"))
})

It("should create a patch file when requested", func() {
from := createTestFile(`{"list":[{"aaa":"bbb","name":"one"}]}`)
defer os.Remove(from)

to := createTestFile(`{"list":[{"aaa":"bbb","name":"two"}]}`)
defer os.Remove(to)

_, err := dyff("between", "--generate-patch", "--patch-file=patch.txt", from, to)
Expect(err).ToNot(HaveOccurred())
defer os.Remove("patch.txt")

out, err := os.ReadFile("patch.txt")
Expect(err).ToNot(HaveOccurred())

var p []dyff2.PatchOp
err = yamlv3.Unmarshal(out, &p)
Expect(err).ToNot(HaveOccurred())

Expect(p).To(HaveLen(2))
Expect(p[0].Op).To(Equal("remove"))
Expect(p[1].Op).To(Equal("add"))
})
})

Context("apply-patch command", func() {
It("should apply a patch successfully and write output to a file", func() {
type testdoc struct {
Foo string
}

from := createTestFile(`{"foo":"bar"}`)
defer os.Remove(from)

patch := createTestFile(`- op: replace
fromkind: scalar
tokind: scalar
path: /foo
tovalue: abc
fromvalue: bar`)
defer os.Remove(patch)

_, err := dyff("apply-patch", "--output=out.yml", patch, from)
Expect(err).ToNot(HaveOccurred())
defer os.Remove("out.yml")

d, err := os.ReadFile("out.yml")
Expect(err).ToNot(HaveOccurred())

var td testdoc
err = yamlv3.Unmarshal(d, &td)
Expect(err).ToNot(HaveOccurred())

Expect(td.Foo).To(Equal("abc"))
})

It("should apply a patch successfully and write output to stdout if requested", func() {
type testdoc struct {
Foo string
}

from := createTestFile(`{"foo":"bar"}`)
defer os.Remove(from)

patch := createTestFile(`- op: replace
fromkind: scalar
tokind: scalar
path: /foo
tovalue: abc
fromvalue: bar`)
defer os.Remove(patch)

out, err := dyff("apply-patch", "--output=-", patch, from)
Expect(err).ToNot(HaveOccurred())

var td testdoc
err = yamlv3.Unmarshal([]byte(strings.TrimSpace(out)), &td)
Expect(err).ToNot(HaveOccurred())

Expect(td.Foo).To(Equal("abc"))
})

})

Context("last-applied command", func() {
Expand Down
Loading