@@ -4,13 +4,15 @@ import (
4
4
"bufio"
5
5
"bytes"
6
6
"fmt"
7
+ "strconv"
7
8
"strings"
8
9
9
10
"reflect"
10
11
11
12
"github.com/docker/docker/pkg/urlutil"
12
13
"github.com/docker/libcompose/utils"
13
14
composeYaml "github.com/docker/libcompose/yaml"
15
+ "github.com/sirupsen/logrus"
14
16
"gopkg.in/yaml.v2"
15
17
)
16
18
@@ -25,14 +27,37 @@ var (
25
27
}
26
28
)
27
29
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.
29
50
func CreateConfig (bytes []byte ) (* Config , error ) {
30
51
var config Config
31
52
if err := yaml .Unmarshal (bytes , & config ); err != nil {
32
53
return nil , err
33
54
}
34
55
35
- if config .Version != "2" {
56
+ major , err := getComposeMajorVersion (config .Version )
57
+ if err != nil {
58
+ return nil , err
59
+ }
60
+ if major < 2 {
36
61
var baseRawServices RawServiceMap
37
62
if err := yaml .Unmarshal (bytes , & baseRawServices ); err != nil {
38
63
return nil , err
@@ -102,14 +127,22 @@ func Merge(existingServices *ServiceConfigs, environmentLookup EnvironmentLookup
102
127
}
103
128
}
104
129
130
+ major , err := getComposeMajorVersion (config .Version )
131
+ if err != nil {
132
+ return "" , nil , nil , nil , err
133
+ }
134
+
105
135
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 :
107
140
var err error
108
141
serviceConfigs , err = MergeServicesV2 (existingServices , environmentLookup , resourceLookup , file , baseRawServices , options )
109
142
if err != nil {
110
143
return "" , nil , nil , nil , err
111
144
}
112
- } else {
145
+ default :
113
146
serviceConfigsV1 , err := MergeServicesV1 (existingServices , environmentLookup , resourceLookup , file , baseRawServices , options )
114
147
if err != nil {
115
148
return "" , nil , nil , nil , err
0 commit comments