Skip to content

Commit

Permalink
fix: refactor lib for TUI integration (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
vdbulcke committed Mar 11, 2023
1 parent 9a3dcd7 commit 6afa26e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 28 deletions.
12 changes: 6 additions & 6 deletions src/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ type Config struct {
}

type Patch struct {
Source string `yaml:"source" validate:"required"`
Destination string `yaml:"destination" validate:"required"`
JSONPatch string `yaml:"json_patch" validate:"required"`
Source string `yaml:"source" json:"source" validate:"required"`
Destination string `yaml:"destination" json:"destination" validate:"required"`
JSONPatch string `yaml:"json_patch" json:"json_patch" validate:"required"`

SourceNotExists string `yaml:"source_not_exist" default:"fail" validate:"required,oneof=fail continue"`
Tags []string `yaml:"tags" `
DecodedPatch jsonpatch.Patch
SourceNotExists string `yaml:"source_not_exist" json:"source_not_exist" default:"fail" validate:"required,oneof=fail continue"`
Tags []string `yaml:"tags" json:"tags" `
DecodedPatch jsonpatch.Patch `yaml:"-" json:"-" `
}

func (p *Patch) UnmarshalYAML(unmarshal func(interface{}) error) error {
Expand Down
37 changes: 26 additions & 11 deletions src/patcher/main.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
package patcher

import (
"bytes"

"github.com/vdbulcke/json-patcher/src/config"
"github.com/vdbulcke/json-patcher/src/logger"
"go.uber.org/zap"
)

type Options struct {
skipTags string
SkipTags string
debug bool
allowUnescapedHTML bool
AllowUnescapedHTML bool
}

func NewOptions(skipTags string, allowUnescapedHTML, debug bool) *Options {

return &Options{
skipTags: skipTags,
allowUnescapedHTML: allowUnescapedHTML,
SkipTags: skipTags,
AllowUnescapedHTML: allowUnescapedHTML,
debug: debug,
}
}
Expand All @@ -29,7 +31,7 @@ func Apply(cfg *config.Config, opts *Options) error {
for _, p := range cfg.Patches {

// handle skips
r, err := skip(p, opts.skipTags)
r, err := Skip(p, opts)
if err != nil {
logger.Debug("error processing", zap.String("source", p.Source), zap.String("destination", p.Destination))
return err
Expand All @@ -42,7 +44,14 @@ func Apply(cfg *config.Config, opts *Options) error {

logger.Debug("processing", zap.String("source", p.Source), zap.String("destination", p.Destination))

err = Patch(p, opts)
// execute patch
mdoc, err := Patch(p, opts)
if err != nil {
return err
}

// write patch
err = WriteDestination(p.Destination, mdoc, opts)
if err != nil {
return err
}
Expand All @@ -53,19 +62,25 @@ func Apply(cfg *config.Config, opts *Options) error {

}

// Patch a source into a destination
func Patch(patch *config.Patch, opts *Options) error {
// Patch a source
func Patch(patch *config.Patch, opts *Options) ([]byte, error) {

doc, err := readSource(patch.Source, opts)
if err != nil {
return err
return nil, err
}

mdoc, err := patch.DecodedPatch.Apply(doc)
if err != nil {
return err
return nil, err
}

return writeDestination(patch.Destination, mdoc, opts)
// revert HTML escape
if opts.AllowUnescapedHTML {
mdoc = bytes.Replace(mdoc, []byte("\\u0026"), []byte("&"), -1)
mdoc = bytes.Replace(mdoc, []byte("\\u003c"), []byte("<"), -1)
mdoc = bytes.Replace(mdoc, []byte("\\u003e"), []byte(">"), -1)
}

return mdoc, nil
}
14 changes: 3 additions & 11 deletions src/patcher/utils.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package patcher

import (
"bytes"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -40,14 +39,7 @@ func readSource(source string, opts *Options) ([]byte, error) {
}

// writeDestination write bytes to file or STDOUT
func writeDestination(destination string, data []byte, opts *Options) error {

// revert HTML escape
if opts.allowUnescapedHTML {
data = bytes.Replace(data, []byte("\\u0026"), []byte("&"), -1)
data = bytes.Replace(data, []byte("\\u003c"), []byte("<"), -1)
data = bytes.Replace(data, []byte("\\u003e"), []byte(">"), -1)
}
func WriteDestination(destination string, data []byte, opts *Options) error {

var writer *os.File
if destination == config.STDOUT {
Expand Down Expand Up @@ -83,10 +75,10 @@ func writeDestination(destination string, data []byte, opts *Options) error {
return nil
}

func skip(patch *config.Patch, skipTags string) (*SkipReason, error) {
func Skip(patch *config.Patch, opts *Options) (*SkipReason, error) {

// handle skip tags
for _, t := range strings.Split(skipTags, ",") {
for _, t := range strings.Split(opts.SkipTags, ",") {

// for each skipTags if at least one
// is matching a patch tag then skip
Expand Down

0 comments on commit 6afa26e

Please sign in to comment.