-
Notifications
You must be signed in to change notification settings - Fork 119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix: use container name consistent with Docker Compose API #633
Changes from 2 commits
906267f
3904cfb
dc8dd27
35ba5cf
8a0f337
a9710fe
201fd2c
27c58be
42416ef
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ import ( | |
"strings" | ||
"time" | ||
|
||
"github.com/Masterminds/semver" | ||
"github.com/pkg/errors" | ||
"gopkg.in/yaml.v3" | ||
|
||
|
@@ -25,6 +26,7 @@ import ( | |
// Project represents a Docker Compose project. | ||
type Project struct { | ||
name string | ||
dcMajorVersion string | ||
composeFilePaths []string | ||
} | ||
|
||
|
@@ -141,8 +143,9 @@ func NewProject(name string, paths ...string) (*Project, error) { | |
} | ||
|
||
c := Project{ | ||
name, | ||
paths, | ||
name: name, | ||
dcMajorVersion: "", | ||
composeFilePaths: paths, | ||
} | ||
|
||
return &c, nil | ||
|
@@ -340,7 +343,38 @@ func (p *Project) runDockerComposeCmd(opts dockerComposeOptions) error { | |
return cmd.Run() | ||
} | ||
|
||
func (p *Project) dockerComposeVersion() (*semver.Version, error) { | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: empty line (cane be removed) |
||
var b bytes.Buffer | ||
|
||
args := []string{ | ||
"version", | ||
"--short", | ||
} | ||
if err := p.runDockerComposeCmd(dockerComposeOptions{args: args, stdout: &b}); err != nil { | ||
return nil, errors.Wrap(err, "running Docker Compose version command failed") | ||
} | ||
dcVersion := b.String() | ||
ver, err := semver.NewVersion(dcVersion) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "docker compose version not a valid semver %s", dcVersion) | ||
mtojek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
return ver, nil | ||
} | ||
|
||
// ContainerName method the container name for the service. | ||
func (p *Project) ContainerName(serviceName string) string { | ||
return fmt.Sprintf("%s_%s_1", p.name, serviceName) | ||
if p.dcMajorVersion == "" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be cleaner to keep the
Then, the logic determining
|
||
ver, err := p.dockerComposeVersion() | ||
if err != nil { | ||
logger.Errorf("Unable to determine docker-compose version: %v. Defaulting to 1.x", err) | ||
p.dcMajorVersion = "1" | ||
} else { | ||
p.dcMajorVersion = strconv.FormatInt(ver.Major(), 10) | ||
} | ||
} | ||
if p.dcMajorVersion == "1" { | ||
return fmt.Sprintf("%s_%s_1", p.name, serviceName) | ||
} | ||
return fmt.Sprintf("%s-%s-1", p.name, serviceName) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As suggested in the other comment,
dcMajorVersion
can be replaced withdockerComposeV1
(bool).Then in the
NewProject
, you can evaluate its value.BTW I hope that the
Project
isn't created somewhere just with{}
.