Skip to content
This repository was archived by the owner on Jan 16, 2022. It is now read-only.

Commit f351c7c

Browse files
author
Kris Hicks
committed
Add cmd/yaml-patch
1 parent afde0dd commit f351c7c

4 files changed

Lines changed: 118 additions & 0 deletions

File tree

‎cmd/yaml-patch/file_flag.go‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
// Thanks, Concourse!
4+
5+
import (
6+
"fmt"
7+
"os"
8+
"path/filepath"
9+
)
10+
11+
type FileFlag string
12+
13+
func (f *FileFlag) UnmarshalFlag(value string) error {
14+
stat, err := os.Stat(value)
15+
if err != nil {
16+
return err
17+
}
18+
19+
if stat.IsDir() {
20+
return fmt.Errorf("path '%s' is a directory, not a file", value)
21+
}
22+
23+
abs, err := filepath.Abs(value)
24+
if err != nil {
25+
return err
26+
}
27+
28+
*f = FileFlag(abs)
29+
30+
return nil
31+
}
32+
33+
func (f FileFlag) Path() string {
34+
return string(f)
35+
}

‎cmd/yaml-patch/main.go‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"log"
7+
"os"
8+
9+
flags "github.com/jessevdk/go-flags"
10+
yamlpatch "github.com/krishicks/yaml-patch"
11+
)
12+
13+
type Opts struct {
14+
OpsFiles []FileFlag `long:"ops-file" short:"o" value-name:"PATH" description:"Path to file with one or more operations"`
15+
}
16+
17+
func main() {
18+
var opts Opts
19+
_, err := flags.Parse(&opts)
20+
if err != nil {
21+
log.Fatalf("error: %s\n", err)
22+
}
23+
24+
var patches []yamlpatch.Patch
25+
for _, opsFile := range opts.OpsFiles {
26+
bs, err := ioutil.ReadFile(opsFile.Path())
27+
if err != nil {
28+
log.Fatalf("error reading opsfile: %s", err)
29+
}
30+
patch, err := yamlpatch.DecodePatch(bs)
31+
if err != nil {
32+
log.Fatalf("error decoding opsfile: %s", err)
33+
}
34+
patches = append(patches, patch)
35+
}
36+
37+
doc, err := ioutil.ReadAll(os.Stdin)
38+
if err != nil {
39+
log.Fatalf("error reading from stdin: %s", err)
40+
}
41+
42+
mdoc := doc
43+
for _, patch := range patches {
44+
mdoc, err = patch.Apply(mdoc)
45+
if err != nil {
46+
log.Fatalf("error applying patch: %s", err)
47+
}
48+
}
49+
50+
fmt.Printf("%s", mdoc)
51+
}

‎cmd/yaml-patch/main_suite_test.go‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package main_test
2+
3+
import (
4+
. "github.com/onsi/ginkgo"
5+
. "github.com/onsi/gomega"
6+
"github.com/onsi/gomega/gexec"
7+
8+
"testing"
9+
)
10+
11+
var _ = AfterSuite(func() {
12+
gexec.CleanupBuildArtifacts()
13+
})
14+
15+
func TestMain(t *testing.T) {
16+
RegisterFailHandler(Fail)
17+
RunSpecs(t, "Main Suite")
18+
}

‎cmd/yaml-patch/main_test.go‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package main_test
2+
3+
import (
4+
. "github.com/onsi/ginkgo"
5+
. "github.com/onsi/gomega"
6+
"github.com/onsi/gomega/gexec"
7+
)
8+
9+
var _ = Describe("yaml-patch", func() {
10+
It("builds", func() {
11+
_, err := gexec.Build("github.com/krishicks/yaml-patch/cmd/yaml-patch")
12+
Expect(err).NotTo(HaveOccurred())
13+
})
14+
})

0 commit comments

Comments
 (0)