Skip to content
This repository has been archived by the owner on Jun 16, 2021. It is now read-only.

Commit

Permalink
Merge pull request #507 from CpuID/cpuid_pr497_take2
Browse files Browse the repository at this point in the history
parse >2.x docker-compose.yml versions (the basics)
  • Loading branch information
vdemeester authored Aug 21, 2018
2 parents 31876c9 + 4748e8f commit a86fb3c
Showing 1 changed file with 37 additions and 4 deletions.
41 changes: 37 additions & 4 deletions config/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
"bufio"
"bytes"
"fmt"
"strconv"
"strings"

"reflect"

"github.com/docker/docker/pkg/urlutil"
"github.com/docker/libcompose/utils"
composeYaml "github.com/docker/libcompose/yaml"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)

Expand All @@ -25,14 +27,37 @@ var (
}
)

// CreateConfig unmarshals bytes to config and creates config based on version
func getComposeMajorVersion(version string) (int, error) {
if version == "" {
return 1, nil
}
parts := strings.Split(version, ".")
if len(parts) == 1 {
return strconv.Atoi(version)
} else if len(parts) == 2 {
return strconv.Atoi(parts[0])
} else {
return -1, fmt.Errorf("Invalid version string, expected single integer or dot delimited int.int. Got: %s", version)
}
}

// CreateConfig unmarshals bytes of a YAML manifest file and returns a new
// Config. Initialize any defaults that can't be parsed (but are optional)
// across various file formats. Most of these can remain unused.
//
// This function only handles parsing YAML in the general case. Any other file
// format validation should be handled by the caller.
func CreateConfig(bytes []byte) (*Config, error) {
var config Config
if err := yaml.Unmarshal(bytes, &config); err != nil {
return nil, err
}

if config.Version != "2" {
major, err := getComposeMajorVersion(config.Version)
if err != nil {
return nil, err
}
if major < 2 {
var baseRawServices RawServiceMap
if err := yaml.Unmarshal(bytes, &baseRawServices); err != nil {
return nil, err
Expand Down Expand Up @@ -102,14 +127,22 @@ func Merge(existingServices *ServiceConfigs, environmentLookup EnvironmentLookup
}
}

major, err := getComposeMajorVersion(config.Version)
if err != nil {
return "", nil, nil, nil, err
}

var serviceConfigs map[string]*ServiceConfig
if config.Version == "2" {
switch major {
case 3:
logrus.Fatal("Note: Compose file version 3 is not yet implemented")
case 2:
var err error
serviceConfigs, err = MergeServicesV2(existingServices, environmentLookup, resourceLookup, file, baseRawServices, options)
if err != nil {
return "", nil, nil, nil, err
}
} else {
default:
serviceConfigsV1, err := MergeServicesV1(existingServices, environmentLookup, resourceLookup, file, baseRawServices, options)
if err != nil {
return "", nil, nil, nil, err
Expand Down

0 comments on commit a86fb3c

Please sign in to comment.