Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit 5cba167

Browse files
authored
Merge pull request #367 from vdemeester/removing-nonused-err
Remove returned error in ID and IsRunning…
2 parents b6b9503 + 6d314a1 commit 5cba167

File tree

6 files changed

+31
-47
lines changed

6 files changed

+31
-47
lines changed

docker/container/container.go

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,8 @@ func (c *Container) Kill(ctx context.Context, signal string) error {
168168
}
169169

170170
// IsRunning returns the running state of the container.
171-
// FIXME(vdemeester): remove the nil error here
172-
func (c *Container) IsRunning(ctx context.Context) (bool, error) {
173-
return c.container.State.Running, nil
171+
func (c *Container) IsRunning(ctx context.Context) bool {
172+
return c.container.State.Running
174173
}
175174

176175
// Run creates, start and attach to the container based on the image name,
@@ -292,16 +291,6 @@ func (c *Container) Start(ctx context.Context) error {
292291
return nil
293292
}
294293

295-
// ID returns the container Id.
296-
func (c *Container) ID() (string, error) {
297-
return c.container.ID, nil
298-
}
299-
300-
// Name returns the container name.
301-
func (c *Container) Name() string {
302-
return c.container.Name
303-
}
304-
305294
// Restart restarts the container if existing, does nothing otherwise.
306295
func (c *Container) Restart(ctx context.Context, timeout int) error {
307296
timeoutDuration := time.Duration(timeout) * time.Second
@@ -355,6 +344,21 @@ func (c *Container) Networks() (map[string]*network.EndpointSettings, error) {
355344
return c.container.NetworkSettings.Networks, nil
356345
}
357346

347+
// ID returns the container Id.
348+
func (c *Container) ID() string {
349+
return c.container.ID
350+
}
351+
352+
// ShortID return the container Id in its short form
353+
func (c *Container) ShortID() string {
354+
return c.container.ID[:12]
355+
}
356+
357+
// Name returns the container name.
358+
func (c *Container) Name() string {
359+
return c.container.Name
360+
}
361+
358362
// Image returns the container image. Depending on the engine version its either
359363
// the complete id or the digest reference the image.
360364
func (c *Container) Image() string {

docker/service/convert.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,7 @@ func Convert(c *config.ServiceConfig, ctx project.Context, clientFactory compose
217217
}
218218
if len(containers) != 0 {
219219
container := containers[0]
220-
containerID, err := container.ID()
221-
if err != nil {
222-
return nil, nil, err
223-
}
220+
containerID := container.ID()
224221
networkMode = "container:" + containerID
225222
}
226223
// FIXME(vdemeester) log/warn in case of len(containers) == 0

docker/service/service.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,7 @@ func (s *Service) constructContainers(ctx context.Context, count int) ([]*contai
215215
return nil, err
216216
}
217217

218-
// FIXME(vdemeester) use property/method instead
219-
id, _ := c.ID()
218+
id := c.ID()
220219
logrus.Debugf("Created container %s: %v", id, c.Name())
221220

222221
result = append(result, c)
@@ -363,8 +362,7 @@ func (s *Service) connectContainerToNetworks(ctx context.Context, c *container.C
363362
// FIXME(vdemeester) implement alias checking (to not disconnect/reconnect for nothing)
364363
aliasPresent := false
365364
for _, alias := range existingNetwork.Aliases {
366-
// FIXME(vdemeester) use shortID instead of ID
367-
ID, _ := c.ID()
365+
ID := c.ShortID()
368366
if alias == ID {
369367
aliasPresent = true
370368
}
@@ -386,15 +384,15 @@ func (s *Service) connectContainerToNetworks(ctx context.Context, c *container.C
386384

387385
// NetworkDisconnect disconnects the container from the specified network
388386
func (s *Service) NetworkDisconnect(ctx context.Context, c *container.Container, net *yaml.Network, oneOff bool) error {
389-
containerID, _ := c.ID()
387+
containerID := c.ID()
390388
client := s.clientFactory.Create(s)
391389
return client.NetworkDisconnect(ctx, net.RealName, containerID, true)
392390
}
393391

394392
// NetworkConnect connects the container to the specified network
395393
// FIXME(vdemeester) will be refactor with Container refactoring
396394
func (s *Service) NetworkConnect(ctx context.Context, c *container.Container, net *yaml.Network, oneOff bool) error {
397-
containerID, _ := c.ID()
395+
containerID := c.ID()
398396
client := s.clientFactory.Create(s)
399397
internalLinks, err := s.getLinks()
400398
if err != nil {
@@ -452,7 +450,7 @@ func (s *Service) recreateIfNeeded(ctx context.Context, c *container.Container,
452450

453451
func (s *Service) recreate(ctx context.Context, c *container.Container) (*container.Container, error) {
454452
name := c.Name()
455-
id, _ := c.ID()
453+
id := c.ID()
456454
newName := fmt.Sprintf("%s_%s", name, id[:12])
457455
logrus.Debugf("Renaming %s => %s", name, newName)
458456
if err := c.Rename(ctx, newName); err != nil {
@@ -464,7 +462,7 @@ func (s *Service) recreate(ctx context.Context, c *container.Container) (*contai
464462
if err != nil {
465463
return nil, err
466464
}
467-
newID, _ := newContainer.ID()
465+
newID := newContainer.ID()
468466
logrus.Debugf("Created replacement container %s", newID)
469467
if err := c.Remove(ctx, false); err != nil {
470468
logrus.Errorf("Failed to remove old container %s", c.Name())
@@ -549,7 +547,7 @@ func (s *Service) Kill(ctx context.Context, signal string) error {
549547
// Delete implements Service.Delete. It removes any containers related to the service.
550548
func (s *Service) Delete(ctx context.Context, options options.Delete) error {
551549
return s.collectContainersAndDo(ctx, func(c *container.Container) error {
552-
running, _ := c.IsRunning(ctx)
550+
running := c.IsRunning(ctx)
553551
if !running || options.RemoveRunning {
554552
return c.Remove(ctx, options.RemoveVolume)
555553
}

docker/service/service_create.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,7 @@ func addIpc(config *containertypes.HostConfig, service project.Service, containe
155155
return nil, fmt.Errorf("Failed to find container for IPC %v", ipc)
156156
}
157157

158-
id, err := containers[0].ID()
159-
if err != nil {
160-
return nil, err
161-
}
162-
158+
id := containers[0].ID()
163159
config.IpcMode = containertypes.IpcMode("container:" + id)
164160
return config, nil
165161
}
@@ -169,11 +165,7 @@ func addNetNs(config *containertypes.HostConfig, service project.Service, contai
169165
return nil, fmt.Errorf("Failed to find container for networks ns %v", networkMode)
170166
}
171167

172-
id, err := containers[0].ID()
173-
if err != nil {
174-
return nil, err
175-
}
176-
168+
id := containers[0].ID()
177169
config.NetworkMode = containertypes.NetworkMode("container:" + id)
178170
return config, nil
179171
}

project/container.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66

77
// Container defines what a libcompose container provides.
88
type Container interface {
9-
ID() (string, error)
9+
ID() string
1010
Name() string
1111
Port(ctx context.Context, port string) (string, error)
12-
IsRunning(ctx context.Context) (bool, error)
12+
IsRunning(ctx context.Context) bool
1313
}

project/project_containers.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55

66
"golang.org/x/net/context"
77

8-
log "github.com/Sirupsen/logrus"
98
"github.com/docker/libcompose/project/events"
109
)
1110

@@ -21,10 +20,7 @@ func (p *Project) Containers(ctx context.Context, filter Filter, services ...str
2120
}
2221

2322
for _, container := range serviceContainers {
24-
running, innerErr := container.IsRunning(ctx)
25-
if innerErr != nil {
26-
log.Error(innerErr)
27-
}
23+
running := container.IsRunning(ctx)
2824
switch filter.State {
2925
case Running:
3026
if !running {
@@ -40,10 +36,7 @@ func (p *Project) Containers(ctx context.Context, filter Filter, services ...str
4036
// Invalid state filter
4137
return fmt.Errorf("Invalid container filter: %s", filter.State)
4238
}
43-
containerID, innerErr := container.ID()
44-
if innerErr != nil {
45-
log.Error(innerErr)
46-
}
39+
containerID := container.ID()
4740
containers = append(containers, containerID)
4841
}
4942
return nil

0 commit comments

Comments
 (0)