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

Commit 6d314a1

Browse files
committed
Remove returned error in ID and IsRunning…
… for containers. It wasn't needed anymore. Signed-off-by: Vincent Demeester <vincent@sbr.pm>
1 parent a5702b9 commit 6d314a1

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
@@ -212,10 +212,7 @@ func Convert(c *config.ServiceConfig, ctx project.Context, clientFactory compose
212212
}
213213
if len(containers) != 0 {
214214
container := containers[0]
215-
containerID, err := container.ID()
216-
if err != nil {
217-
return nil, nil, err
218-
}
215+
containerID := container.ID()
219216
networkMode = "container:" + containerID
220217
}
221218
// 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
@@ -232,8 +232,7 @@ func (s *Service) constructContainers(ctx context.Context, count int) ([]*contai
232232
return nil, err
233233
}
234234

235-
// FIXME(vdemeester) use property/method instead
236-
id, _ := c.ID()
235+
id := c.ID()
237236
logrus.Debugf("Created container %s: %v", id, c.Name())
238237

239238
result = append(result, c)
@@ -380,8 +379,7 @@ func (s *Service) connectContainerToNetworks(ctx context.Context, c *container.C
380379
// FIXME(vdemeester) implement alias checking (to not disconnect/reconnect for nothing)
381380
aliasPresent := false
382381
for _, alias := range existingNetwork.Aliases {
383-
// FIXME(vdemeester) use shortID instead of ID
384-
ID, _ := c.ID()
382+
ID := c.ShortID()
385383
if alias == ID {
386384
aliasPresent = true
387385
}
@@ -403,15 +401,15 @@ func (s *Service) connectContainerToNetworks(ctx context.Context, c *container.C
403401

404402
// NetworkDisconnect disconnects the container from the specified network
405403
func (s *Service) NetworkDisconnect(ctx context.Context, c *container.Container, net *yaml.Network, oneOff bool) error {
406-
containerID, _ := c.ID()
404+
containerID := c.ID()
407405
client := s.clientFactory.Create(s)
408406
return client.NetworkDisconnect(ctx, net.RealName, containerID, true)
409407
}
410408

411409
// NetworkConnect connects the container to the specified network
412410
// FIXME(vdemeester) will be refactor with Container refactoring
413411
func (s *Service) NetworkConnect(ctx context.Context, c *container.Container, net *yaml.Network, oneOff bool) error {
414-
containerID, _ := c.ID()
412+
containerID := c.ID()
415413
client := s.clientFactory.Create(s)
416414
internalLinks, err := s.getLinks()
417415
if err != nil {
@@ -469,7 +467,7 @@ func (s *Service) recreateIfNeeded(ctx context.Context, c *container.Container,
469467

470468
func (s *Service) recreate(ctx context.Context, c *container.Container) (*container.Container, error) {
471469
name := c.Name()
472-
id, _ := c.ID()
470+
id := c.ID()
473471
newName := fmt.Sprintf("%s_%s", name, id[:12])
474472
logrus.Debugf("Renaming %s => %s", name, newName)
475473
if err := c.Rename(ctx, newName); err != nil {
@@ -481,7 +479,7 @@ func (s *Service) recreate(ctx context.Context, c *container.Container) (*contai
481479
if err != nil {
482480
return nil, err
483481
}
484-
newID, _ := newContainer.ID()
482+
newID := newContainer.ID()
485483
logrus.Debugf("Created replacement container %s", newID)
486484
if err := c.Remove(ctx, false); err != nil {
487485
logrus.Errorf("Failed to remove old container %s", c.Name())
@@ -566,7 +564,7 @@ func (s *Service) Kill(ctx context.Context, signal string) error {
566564
// Delete implements Service.Delete. It removes any containers related to the service.
567565
func (s *Service) Delete(ctx context.Context, options options.Delete) error {
568566
return s.collectContainersAndDo(ctx, func(c *container.Container) error {
569-
running, _ := c.IsRunning(ctx)
567+
running := c.IsRunning(ctx)
570568
if !running || options.RemoveRunning {
571569
return c.Remove(ctx, options.RemoveVolume)
572570
}

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)