Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit a86fb3c

Browse files
authored
Merge pull request #507 from CpuID/cpuid_pr497_take2
parse >2.x docker-compose.yml versions (the basics)
2 parents 31876c9 + 4748e8f commit a86fb3c

File tree

1 file changed

+37
-4
lines changed

1 file changed

+37
-4
lines changed

config/merge.go

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ import (
44
"bufio"
55
"bytes"
66
"fmt"
7+
"strconv"
78
"strings"
89

910
"reflect"
1011

1112
"github.com/docker/docker/pkg/urlutil"
1213
"github.com/docker/libcompose/utils"
1314
composeYaml "github.com/docker/libcompose/yaml"
15+
"github.com/sirupsen/logrus"
1416
"gopkg.in/yaml.v2"
1517
)
1618

@@ -25,14 +27,37 @@ var (
2527
}
2628
)
2729

28-
// CreateConfig unmarshals bytes to config and creates config based on version
30+
func getComposeMajorVersion(version string) (int, error) {
31+
if version == "" {
32+
return 1, nil
33+
}
34+
parts := strings.Split(version, ".")
35+
if len(parts) == 1 {
36+
return strconv.Atoi(version)
37+
} else if len(parts) == 2 {
38+
return strconv.Atoi(parts[0])
39+
} else {
40+
return -1, fmt.Errorf("Invalid version string, expected single integer or dot delimited int.int. Got: %s", version)
41+
}
42+
}
43+
44+
// CreateConfig unmarshals bytes of a YAML manifest file and returns a new
45+
// Config. Initialize any defaults that can't be parsed (but are optional)
46+
// across various file formats. Most of these can remain unused.
47+
//
48+
// This function only handles parsing YAML in the general case. Any other file
49+
// format validation should be handled by the caller.
2950
func CreateConfig(bytes []byte) (*Config, error) {
3051
var config Config
3152
if err := yaml.Unmarshal(bytes, &config); err != nil {
3253
return nil, err
3354
}
3455

35-
if config.Version != "2" {
56+
major, err := getComposeMajorVersion(config.Version)
57+
if err != nil {
58+
return nil, err
59+
}
60+
if major < 2 {
3661
var baseRawServices RawServiceMap
3762
if err := yaml.Unmarshal(bytes, &baseRawServices); err != nil {
3863
return nil, err
@@ -102,14 +127,22 @@ func Merge(existingServices *ServiceConfigs, environmentLookup EnvironmentLookup
102127
}
103128
}
104129

130+
major, err := getComposeMajorVersion(config.Version)
131+
if err != nil {
132+
return "", nil, nil, nil, err
133+
}
134+
105135
var serviceConfigs map[string]*ServiceConfig
106-
if config.Version == "2" {
136+
switch major {
137+
case 3:
138+
logrus.Fatal("Note: Compose file version 3 is not yet implemented")
139+
case 2:
107140
var err error
108141
serviceConfigs, err = MergeServicesV2(existingServices, environmentLookup, resourceLookup, file, baseRawServices, options)
109142
if err != nil {
110143
return "", nil, nil, nil, err
111144
}
112-
} else {
145+
default:
113146
serviceConfigsV1, err := MergeServicesV1(existingServices, environmentLookup, resourceLookup, file, baseRawServices, options)
114147
if err != nil {
115148
return "", nil, nil, nil, err

0 commit comments

Comments
 (0)