replace.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. // ReplaceEnvFile changes value for service.extends.env_file in input yaml stream, preserving formatting
  52. func ReplaceEnvFile(in []byte, service string, i int, value string) ([]byte, error) {
  53. var doc yaml.Node
  54. err := yaml.Unmarshal(in, &doc)
  55. if err != nil {
  56. return nil, err
  57. }
  58. if doc.Kind != yaml.DocumentNode {
  59. return nil, fmt.Errorf("expected document kind %v, got %v", yaml.DocumentNode, doc.Kind)
  60. }
  61. root := doc.Content[0]
  62. if root.Kind != yaml.MappingNode {
  63. return nil, fmt.Errorf("expected document root to be a mapping, got %v", root.Kind)
  64. }
  65. services, err := getMapping(root, "services")
  66. if err != nil {
  67. return nil, err
  68. }
  69. target, err := getMapping(services, service)
  70. if err != nil {
  71. return nil, err
  72. }
  73. envFile, err := getMapping(target, "env_file")
  74. if err != nil {
  75. return nil, err
  76. }
  77. // env_file can be either a string, sequence of strings, or sequence of mappings with path attribute
  78. if envFile.Kind == yaml.SequenceNode {
  79. envFile = envFile.Content[i]
  80. if envFile.Kind == yaml.MappingNode {
  81. envFile, err = getMapping(envFile, "path")
  82. if err != nil {
  83. return nil, err
  84. }
  85. }
  86. return replace(in, envFile.Line, envFile.Column, value), nil
  87. } else {
  88. return replace(in, envFile.Line, envFile.Column, value), nil
  89. }
  90. }
  91. func getMapping(root *yaml.Node, key string) (*yaml.Node, error) {
  92. var node *yaml.Node
  93. l := len(root.Content)
  94. for i := 0; i < l; i += 2 {
  95. k := root.Content[i]
  96. if k.Kind != yaml.ScalarNode || k.Tag != "!!str" {
  97. return nil, fmt.Errorf("expected mapping key to be a string, got %v %v", root.Kind, k.Tag)
  98. }
  99. if k.Value == key {
  100. node = root.Content[i+1]
  101. return node, nil
  102. }
  103. }
  104. return nil, fmt.Errorf("key %v not found", key)
  105. }
  106. // replace changes yaml node value in stream at position, preserving content
  107. func replace(in []byte, line int, column int, value string) []byte {
  108. var out []byte
  109. l := 1
  110. pos := 0
  111. for _, b := range in {
  112. if b == '\n' {
  113. l++
  114. if l == line {
  115. break
  116. }
  117. }
  118. pos++
  119. }
  120. pos += column
  121. out = append(out, in[0:pos]...)
  122. out = append(out, []byte(value)...)
  123. for ; pos < len(in); pos++ {
  124. if in[pos] == '\n' {
  125. break
  126. }
  127. }
  128. out = append(out, in[pos:]...)
  129. return out
  130. }