Skip to content

Commit

Permalink
Merge pull request #17 from izumin5210/docker-compose_v1.21.x
Browse files Browse the repository at this point in the history
Check docker-compose version for normalizing container names correctly
  • Loading branch information
creasty authored May 8, 2018
2 parents 53be326 + 3b4d6de commit 762010d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
37 changes: 35 additions & 2 deletions docker/util.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
package docker

import (
"os/exec"
"regexp"
"strings"

"github.com/Masterminds/semver"
)

func init() {
if doesAllowDashAndUnderscore() {
// https://github.com/docker/compose/blob/1.21.0/compose/cli/command.py#L132
projectNormalizePattern = regexp.MustCompile(`[^-_a-z0-9]`)
} else {
// https://github.com/docker/compose/blob/f55c9d42013e8fbb5285bc402d8248a846485217/compose/cli/command.py#L105
projectNormalizePattern = regexp.MustCompile(`[^a-z0-9]`)
}
}

var (
// https://github.com/docker/compose/blob/f55c9d42013e8fbb5285bc402d8248a846485217/compose/cli/command.py#L105
projectNormalizePattern = regexp.MustCompile(`[^a-z0-9]`)
projectNormalizePattern *regexp.Regexp
)

// NormalizeProjectName normalizes a project name
Expand All @@ -16,3 +28,24 @@ func NormalizeProjectName(str string) string {
str = projectNormalizePattern.ReplaceAllString(str, "")
return str
}

func doesAllowDashAndUnderscore() (allowed bool) {
allowed = true

data, err := exec.Command("docker-compose", "version", "--short").Output()
if err != nil {
return
}

currVer, err := semver.NewVersion(strings.TrimRight(string(data), "\n"))
if err != nil {
return
}

allowedVer, err := semver.NewVersion("1.21.0")
if err != nil {
return
}

return currVer.Equal(allowedVer) || currVer.GreaterThan(allowedVer)
}
6 changes: 4 additions & 2 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ import:
- package: github.com/ghodss/yaml
- package: github.com/k0kubun/pp
version: ^2.3.0
- package: github.com/Masterminds/semver
version: ^1.4.2

0 comments on commit 762010d

Please sign in to comment.