File: main.go

package info (click to toggle)
golang-github-evanphx-json-patch 5.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 284 kB
  • sloc: makefile: 3
file content (56 lines) | stat: -rw-r--r-- 1,105 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"os"

	jsonpatch "github.com/evanphx/json-patch"
	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)
}