Skip to content

Commit

Permalink
feat: use yaml.v3 to split documents
Browse files Browse the repository at this point in the history
  • Loading branch information
RichiCoder1 committed Apr 21, 2021
1 parent dd3c4f1 commit 3901e89
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 69 deletions.
27 changes: 24 additions & 3 deletions cmd/gitter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
"log"
"net"
Expand All @@ -39,6 +40,7 @@ import (
"github.com/jackc/pgx/v4/pgxpool"
"github.com/pkg/errors"
"gopkg.in/square/go-jose.v2/json"
yaml "gopkg.in/yaml.v3"
)

const (
Expand Down Expand Up @@ -233,9 +235,28 @@ func splitYAML(greps []git.GrepResult, dir string) map[string][][]byte {
log.Printf("failed to read CRD file: %s", res.FileName)
continue
}
// TODO(hasheddan): generalize this replacement
b = bytes.ReplaceAll(b, []byte("rw-rw----"), []byte("660"))
allCRDs[res.FileName] = bytes.Split(b, []byte("---"))

decoder := yaml.NewDecoder(bytes.NewReader(b))
var yamls [][]byte
for {
var node yaml.Node
err := decoder.Decode(&node)
if err == io.EOF {
break
}
if err != nil {
log.Printf("failed to decode part of CRD file: %s", res.FileName)
continue
}

doc, err := yaml.Marshal(node)
if err != nil {
log.Printf("failed to decode part of CRD file: %s", res.FileName)
continue
}
yamls = append(yamls, doc)
}
allCRDs[res.FileName] = yamls
}
return allCRDs
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ require (
google.golang.org/genproto v0.0.0-20201109203340-2640f1f9cdfb // indirect
google.golang.org/grpc v1.33.2 // indirect
gopkg.in/square/go-jose.v2 v2.2.2
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
k8s.io/apiextensions-apiserver v0.18.2
k8s.io/apimachinery v0.18.2
sigs.k8s.io/yaml v1.2.0
Expand Down
Loading

0 comments on commit 3901e89

Please sign in to comment.