-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #100 from BenTheElder/modules-v5
add module v5
- Loading branch information
Showing
12 changed files
with
2,626 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
github.com/jessevdk/go-flags v1.4.0 h1:4IU2WS7AumrZ/40jfhf4QVDMsQwqA7VEHozFRrGARJA= | ||
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= | ||
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= | ||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package main | ||
|
||
// Borrowed from Concourse: https://github.com/concourse/atc/blob/master/atccmd/file_flag.go | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
// FileFlag is a flag for passing a path to a file on disk. The file is | ||
// expected to be a file, not a directory, that actually exists. | ||
type FileFlag string | ||
|
||
// UnmarshalFlag implements go-flag's Unmarshaler interface | ||
func (f *FileFlag) UnmarshalFlag(value string) error { | ||
stat, err := os.Stat(value) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if stat.IsDir() { | ||
return fmt.Errorf("path '%s' is a directory, not a file", value) | ||
} | ||
|
||
abs, err := filepath.Abs(value) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
*f = FileFlag(abs) | ||
|
||
return nil | ||
} | ||
|
||
// Path is the path to the file | ||
func (f FileFlag) Path() string { | ||
return string(f) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"os" | ||
|
||
jsonpatch "github.com/evanphx/json-patch/v5" | ||
flags "github.com/jessevdk/go-flags" | ||
) | ||
|
||
type opts struct { | ||
PatchFilePaths []FileFlag `long:"patch-file" short:"p" value-name:"PATH" description:"Path to file with one or more operations"` | ||
} | ||
|
||
func main() { | ||
var o opts | ||
_, err := flags.Parse(&o) | ||
if err != nil { | ||
log.Fatalf("error: %s\n", err) | ||
} | ||
|
||
patches := make([]jsonpatch.Patch, len(o.PatchFilePaths)) | ||
|
||
for i, patchFilePath := range o.PatchFilePaths { | ||
var bs []byte | ||
bs, err = ioutil.ReadFile(patchFilePath.Path()) | ||
if err != nil { | ||
log.Fatalf("error reading patch file: %s", err) | ||
} | ||
|
||
var patch jsonpatch.Patch | ||
patch, err = jsonpatch.DecodePatch(bs) | ||
if err != nil { | ||
log.Fatalf("error decoding patch file: %s", err) | ||
} | ||
|
||
patches[i] = patch | ||
} | ||
|
||
doc, err := ioutil.ReadAll(os.Stdin) | ||
if err != nil { | ||
log.Fatalf("error reading from stdin: %s", err) | ||
} | ||
|
||
mdoc := doc | ||
for _, patch := range patches { | ||
mdoc, err = patch.Apply(mdoc) | ||
if err != nil { | ||
log.Fatalf("error applying patch: %s", err) | ||
} | ||
} | ||
|
||
fmt.Printf("%s", mdoc) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package jsonpatch | ||
|
||
import "fmt" | ||
|
||
// AccumulatedCopySizeError is an error type returned when the accumulated size | ||
// increase caused by copy operations in a patch operation has exceeded the | ||
// limit. | ||
type AccumulatedCopySizeError struct { | ||
limit int64 | ||
accumulated int64 | ||
} | ||
|
||
// NewAccumulatedCopySizeError returns an AccumulatedCopySizeError. | ||
func NewAccumulatedCopySizeError(l, a int64) *AccumulatedCopySizeError { | ||
return &AccumulatedCopySizeError{limit: l, accumulated: a} | ||
} | ||
|
||
// Error implements the error interface. | ||
func (a *AccumulatedCopySizeError) Error() string { | ||
return fmt.Sprintf("Unable to complete the copy, the accumulated size increase of copy is %d, exceeding the limit %d", a.accumulated, a.limit) | ||
} | ||
|
||
// ArraySizeError is an error type returned when the array size has exceeded | ||
// the limit. | ||
type ArraySizeError struct { | ||
limit int | ||
size int | ||
} | ||
|
||
// NewArraySizeError returns an ArraySizeError. | ||
func NewArraySizeError(l, s int) *ArraySizeError { | ||
return &ArraySizeError{limit: l, size: s} | ||
} | ||
|
||
// Error implements the error interface. | ||
func (a *ArraySizeError) Error() string { | ||
return fmt.Sprintf("Unable to create array of size %d, limit is %d", a.size, a.limit) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module github.com/evanphx/json-patch/v5 | ||
|
||
go 1.12 | ||
|
||
require ( | ||
github.com/jessevdk/go-flags v1.4.0 | ||
github.com/pkg/errors v0.8.1 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
github.com/jessevdk/go-flags v1.4.0 h1:4IU2WS7AumrZ/40jfhf4QVDMsQwqA7VEHozFRrGARJA= | ||
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= | ||
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= | ||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= |
Oops, something went wrong.