replace.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package transform
  14. import (
  15. "fmt"
  16. "gopkg.in/yaml.v3"
  17. )
  18. // ReplaceExtendsFile changes value for service.extends.file in input yaml stream, preserving formatting
  19. func ReplaceExtendsFile(in []byte, service string, value string) ([]byte, error) {
  20. var doc yaml.Node
  21. err := yaml.Unmarshal(in, &doc)
  22. if err != nil {
  23. return nil, err
  24. }
  25. if doc.Kind != yaml.DocumentNode {
  26. return nil, fmt.Errorf("expected document kind %v, got %v", yaml.DocumentNode, doc.Kind)
  27. }
  28. root := doc.Content[0]
  29. if root.Kind != yaml.MappingNode {
  30. return nil, fmt.Errorf("expected document root to be a mapping, got %v", root.Kind)
  31. }
  32. services, err := getMapping(root, "services")
  33. if err != nil {
  34. return nil, err
  35. }
  36. target, err := getMapping(services, service)
  37. if err != nil {
  38. return nil, err
  39. }
  40. extends, err := getMapping(target, "extends")
  41. if err != nil {
  42. return nil, err
  43. }
  44. file, err := getMapping(extends, "file")
  45. if err != nil {
  46. return nil, err
  47. }
  48. // we've found target `file` yaml node. Let's replace value in stream at node position
  49. return replace(in, file.Line, file.Column, value), nil
  50. }
  51. func getMapping(root *yaml.Node, key string) (*yaml.Node, error) {
  52. var node *yaml.Node
  53. l := len(root.Content)
  54. for i := 0; i < l; i += 2 {
  55. k := root.Content[i]
  56. if k.Kind != yaml.ScalarNode || k.Tag != "!!str" {
  57. return nil, fmt.Errorf("expected mapping key to be a string, got %v %v", root.Kind, k.Tag)
  58. }
  59. if k.Value == key {
  60. node = root.Content[i+1]
  61. return node, nil
  62. }
  63. }
  64. return nil, fmt.Errorf("key %v not found", key)
  65. }
  66. // replace changes yaml node value in stream at position, preserving content
  67. func replace(in []byte, line int, column int, value string) []byte {
  68. var out []byte
  69. l := 1
  70. pos := 0
  71. for _, b := range in {
  72. if b == '\n' {
  73. l++
  74. if l == line {
  75. break
  76. }
  77. }
  78. pos++
  79. }
  80. pos += column
  81. out = append(out, in[0:pos]...)
  82. out = append(out, []byte(value)...)
  83. for ; pos < len(in); pos++ {
  84. if in[pos] == '\n' {
  85. break
  86. }
  87. }
  88. out = append(out, in[pos:]...)
  89. return out
  90. }