patcher.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package yamlpatch
  2. import (
  3. "bytes"
  4. "io"
  5. "os"
  6. "github.com/pkg/errors"
  7. log "github.com/sirupsen/logrus"
  8. "gopkg.in/yaml.v2"
  9. )
  10. type Patcher struct {
  11. BaseFilePath string
  12. PatchFilePath string
  13. quiet bool
  14. }
  15. func NewPatcher(filePath string, suffix string) *Patcher {
  16. return &Patcher{
  17. BaseFilePath: filePath,
  18. PatchFilePath: filePath + suffix,
  19. quiet: false,
  20. }
  21. }
  22. // SetQuiet sets the quiet flag, which will log as DEBUG_LEVEL instead of INFO
  23. func (p *Patcher) SetQuiet(quiet bool) {
  24. p.quiet = quiet
  25. }
  26. // read a single YAML file, check for errors (the merge package doesn't) then return the content as bytes.
  27. func readYAML(filePath string) ([]byte, error) {
  28. var content []byte
  29. var err error
  30. if content, err = os.ReadFile(filePath); err != nil {
  31. return nil, errors.Wrap(err, "while reading yaml file")
  32. }
  33. var yamlMap map[interface{}]interface{}
  34. if err = yaml.Unmarshal(content, &yamlMap); err != nil {
  35. return nil, errors.Wrap(err, filePath)
  36. }
  37. return content, nil
  38. }
  39. // MergedPatchContent reads a YAML file and, if it exists, its patch file,
  40. // then merges them and returns it serialized.
  41. func (p *Patcher) MergedPatchContent() ([]byte, error) {
  42. var err error
  43. var base []byte
  44. base, err = readYAML(p.BaseFilePath)
  45. if err != nil {
  46. return nil, err
  47. }
  48. var over []byte
  49. over, err = readYAML(p.PatchFilePath)
  50. if errors.Is(err, os.ErrNotExist) {
  51. return base, nil
  52. }
  53. if err != nil {
  54. return nil, err
  55. }
  56. logf := log.Infof
  57. if p.quiet {
  58. logf = log.Debugf
  59. }
  60. logf("Patching yaml: '%s' with '%s'", p.BaseFilePath, p.PatchFilePath)
  61. var patched *bytes.Buffer
  62. // strict mode true, will raise errors for duplicate map keys and
  63. // overriding with a different type
  64. patched, err = YAML([][]byte{base, over}, true)
  65. if err != nil {
  66. return nil, err
  67. }
  68. return patched.Bytes(), nil
  69. }
  70. // read multiple YAML documents inside a file, and writes them to a buffer
  71. // separated by the appropriate '---' terminators.
  72. func decodeDocuments(file *os.File, buf *bytes.Buffer, finalDashes bool) error {
  73. var (
  74. err error
  75. docBytes []byte
  76. )
  77. dec := yaml.NewDecoder(file)
  78. dec.SetStrict(true)
  79. dashTerminator := false
  80. for {
  81. yml := make(map[interface{}]interface{})
  82. err = dec.Decode(&yml)
  83. if err != nil {
  84. if errors.Is(err, io.EOF) {
  85. break
  86. }
  87. return errors.Wrapf(err, "while decoding %s", file.Name())
  88. }
  89. docBytes, err = yaml.Marshal(&yml)
  90. if err != nil {
  91. return errors.Wrapf(err, "while marshaling %s", file.Name())
  92. }
  93. if dashTerminator {
  94. buf.Write([]byte("---\n"))
  95. }
  96. buf.Write(docBytes)
  97. dashTerminator = true
  98. }
  99. if dashTerminator && finalDashes {
  100. buf.Write([]byte("---\n"))
  101. }
  102. return nil
  103. }
  104. // PrependedPatchContent collates the base .yaml file with the .yaml.patch, by putting
  105. // the content of the patch BEFORE the base document. The result is a multi-document
  106. // YAML in all cases, even if the base and patch files are single documents.
  107. func (p *Patcher) PrependedPatchContent() ([]byte, error) {
  108. var (
  109. result bytes.Buffer
  110. patchFile *os.File
  111. baseFile *os.File
  112. err error
  113. )
  114. patchFile, err = os.Open(p.PatchFilePath)
  115. // optional file, ignore if it does not exist
  116. if err != nil && !errors.Is(err, os.ErrNotExist) {
  117. return nil, errors.Wrapf(err, "while opening %s", p.PatchFilePath)
  118. }
  119. if patchFile != nil {
  120. if err = decodeDocuments(patchFile, &result, true); err != nil {
  121. return nil, err
  122. }
  123. logf := log.Infof
  124. if p.quiet {
  125. logf = log.Debugf
  126. }
  127. logf("Prepending yaml: '%s' with '%s'", p.BaseFilePath, p.PatchFilePath)
  128. }
  129. baseFile, err = os.Open(p.BaseFilePath)
  130. if err != nil {
  131. return nil, errors.Wrapf(err, "while opening %s", p.BaseFilePath)
  132. }
  133. if err = decodeDocuments(baseFile, &result, false); err != nil {
  134. return nil, err
  135. }
  136. return result.Bytes(), nil
  137. }