mirror of https://github.com/docker/compose.git
refactor(cmd/compose/run): remove redundant `len` check
From the Go specification [1]: "1. For a nil slice, the number of iterations is 0." `len` returns 0 if the slice is nil [2]. Therefore, checking `len(v) > 0` before a loop is unnecessary. [1]: https://go.dev/ref/spec#For_range [2]: https://pkg.go.dev/builtin#len Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
This commit is contained in:
parent
b8773ad1c5
commit
6c345b3755
|
@ -80,11 +80,10 @@ func (options runOptions) apply(project *types.Project) error {
|
||||||
|
|
||||||
target.Tty = !options.noTty
|
target.Tty = !options.noTty
|
||||||
target.StdinOpen = options.interactive
|
target.StdinOpen = options.interactive
|
||||||
|
|
||||||
|
// --service-ports and --publish are incompatible
|
||||||
if !options.servicePorts {
|
if !options.servicePorts {
|
||||||
target.Ports = []types.ServicePortConfig{}
|
target.Ports = []types.ServicePortConfig{}
|
||||||
}
|
|
||||||
if len(options.publish) > 0 {
|
|
||||||
target.Ports = []types.ServicePortConfig{}
|
|
||||||
for _, p := range options.publish {
|
for _, p := range options.publish {
|
||||||
config, err := types.ParsePortConfig(p)
|
config, err := types.ParsePortConfig(p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -93,14 +92,13 @@ func (options runOptions) apply(project *types.Project) error {
|
||||||
target.Ports = append(target.Ports, config...)
|
target.Ports = append(target.Ports, config...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(options.volumes) > 0 {
|
|
||||||
for _, v := range options.volumes {
|
for _, v := range options.volumes {
|
||||||
volume, err := loader.ParseVolume(v)
|
volume, err := loader.ParseVolume(v)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
|
||||||
target.Volumes = append(target.Volumes, volume)
|
|
||||||
}
|
}
|
||||||
|
target.Volumes = append(target.Volumes, volume)
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, s := range project.Services {
|
for i, s := range project.Services {
|
||||||
|
|
|
@ -96,10 +96,18 @@ func TestLocalComposeRun(t *testing.T) {
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("compose run --publish", func(t *testing.T) {
|
t.Run("compose run --publish", func(t *testing.T) {
|
||||||
c.RunDockerComposeCmd(t, "-f", "./fixtures/run-test/compose.yaml", "run", "--publish", "8081:80", "-d", "back",
|
c.RunDockerComposeCmd(t, "-f", "./fixtures/run-test/ports.yaml", "run", "--publish", "8081:80", "-d", "back",
|
||||||
"/bin/sh", "-c", "sleep 1")
|
"/bin/sh", "-c", "sleep 1")
|
||||||
res := c.RunDockerCmd(t, "ps")
|
res := c.RunDockerCmd(t, "ps")
|
||||||
assert.Assert(t, strings.Contains(res.Stdout(), "8081->80/tcp"), res.Stdout())
|
assert.Assert(t, strings.Contains(res.Stdout(), "8081->80/tcp"), res.Stdout())
|
||||||
|
assert.Assert(t, !strings.Contains(res.Stdout(), "8082->80/tcp"), res.Stdout())
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("compose run --service-ports", func(t *testing.T) {
|
||||||
|
c.RunDockerComposeCmd(t, "-f", "./fixtures/run-test/ports.yaml", "run", "--service-ports", "-d", "back",
|
||||||
|
"/bin/sh", "-c", "sleep 1")
|
||||||
|
res := c.RunDockerCmd(t, "ps")
|
||||||
|
assert.Assert(t, strings.Contains(res.Stdout(), "8082->80/tcp"), res.Stdout())
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("compose run orphan", func(t *testing.T) {
|
t.Run("compose run orphan", func(t *testing.T) {
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
version: '3.8'
|
||||||
|
services:
|
||||||
|
back:
|
||||||
|
image: alpine
|
||||||
|
ports:
|
||||||
|
- 8082:80
|
Loading…
Reference in New Issue