MCPcopy
hub / github.com/docker/compose / ReplaceExtendsFile

Function ReplaceExtendsFile

pkg/compose/transform/replace.go:26–62  ·  view source on GitHub ↗

ReplaceExtendsFile changes value for service.extends.file in input yaml stream, preserving formatting

(in []byte, service string, value string)

Source from the content-addressed store, hash-verified

24
25// ReplaceExtendsFile changes value for service.extends.file in input yaml stream, preserving formatting
26func ReplaceExtendsFile(in []byte, service string, value string) ([]byte, error) {
27 var doc yaml.Node
28 err := yaml.Unmarshal(in, &doc)
29 if err != nil {
30 return nil, err
31 }
32 if doc.Kind != yaml.DocumentNode {
33 return nil, fmt.Errorf("expected document kind %v, got %v", yaml.DocumentNode, doc.Kind)
34 }
35 root := doc.Content[0]
36 if root.Kind != yaml.MappingNode {
37 return nil, fmt.Errorf("expected document root to be a mapping, got %v", root.Kind)
38 }
39
40 services, err := getMapping(root, "services")
41 if err != nil {
42 return nil, err
43 }
44
45 target, err := getMapping(services, service)
46 if err != nil {
47 return nil, err
48 }
49
50 extends, err := getMapping(target, "extends")
51 if err != nil {
52 return nil, err
53 }
54
55 file, err := getMapping(extends, "file")
56 if err != nil {
57 return nil, err
58 }
59
60 // we've found target `file` yaml node. Let's replace value in stream at node position
61 return replace(in, file.Line, file.Column, value), nil
62}
63
64// ReplaceEnvFile changes value for service.extends.env_file in input yaml stream, preserving formatting
65func ReplaceEnvFile(in []byte, service string, i int, value string) ([]byte, error) {

Callers 2

processFileFunction · 0.92
TestReplaceFunction · 0.85

Calls 2

getMappingFunction · 0.85
replaceFunction · 0.85

Tested by 1

TestReplaceFunction · 0.68