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

Commit cfb957a

Browse files
committed
parse >2 docker-compose.yml versions (the basics)
Signed-off-by: Nathan Sullivan <[email protected]>
1 parent 31876c9 commit cfb957a

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

config/merge.go

Lines changed: 27 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,27 @@ var (
2527
}
2628
)
2729

28-
// CreateConfig unmarshals bytes to config and creates config based on version
30+
func getComposeMajorVersion(version string) (int, error) {
31+
return strconv.Atoi(strings.Split(version, ".")[0])
32+
}
33+
34+
// CreateConfig unmarshals bytes of a YAML manifest file and returns a new
35+
// Config. Initialize any defaults that can't be parsed (but are optional)
36+
// across various file formats. Most of these can remain unused.
37+
//
38+
// This function only handles parsing YAML in the general case. Any other file
39+
// format validation should be handled by the caller.
2940
func CreateConfig(bytes []byte) (*Config, error) {
3041
var config Config
3142
if err := yaml.Unmarshal(bytes, &config); err != nil {
3243
return nil, err
3344
}
3445

35-
if config.Version != "2" {
46+
major, err := getComposeMajorVersion(config.Version)
47+
if err != nil {
48+
return nil, err
49+
}
50+
if major < 2 {
3651
var baseRawServices RawServiceMap
3752
if err := yaml.Unmarshal(bytes, &baseRawServices); err != nil {
3853
return nil, err
@@ -102,14 +117,22 @@ func Merge(existingServices *ServiceConfigs, environmentLookup EnvironmentLookup
102117
}
103118
}
104119

120+
major, err := getComposeMajorVersion(config.Version)
121+
if err != nil {
122+
return "", nil, nil, nil, err
123+
}
124+
105125
var serviceConfigs map[string]*ServiceConfig
106-
if config.Version == "2" {
126+
switch major {
127+
case 3:
128+
logrus.Fatal("Note: Compose file version 3 is not yet implemented")
129+
case 2:
107130
var err error
108131
serviceConfigs, err = MergeServicesV2(existingServices, environmentLookup, resourceLookup, file, baseRawServices, options)
109132
if err != nil {
110133
return "", nil, nil, nil, err
111134
}
112-
} else {
135+
default:
113136
serviceConfigsV1, err := MergeServicesV1(existingServices, environmentLookup, resourceLookup, file, baseRawServices, options)
114137
if err != nil {
115138
return "", nil, nil, nil, err

0 commit comments

Comments
 (0)