fixing crashes

PR-136-crash-fixes
Jesse Duffield 5 years ago
parent 93748467eb
commit c435185fed

@ -200,11 +200,7 @@ func (c *DockerCommand) RefreshContainersAndServices() error {
currentServices := c.Services currentServices := c.Services
containers, err := c.GetContainers() var err error
if err != nil {
return err
}
var services []*Service var services []*Service
// we only need to get these services once because they won't change in the runtime of the program // we only need to get these services once because they won't change in the runtime of the program
if currentServices != nil { if currentServices != nil {
@ -216,10 +212,12 @@ func (c *DockerCommand) RefreshContainersAndServices() error {
} }
} }
var displayContainers = containers var containers []*Container
var displayContainers []*Container
if c.InDockerComposeProject { if c.InDockerComposeProject {
var standaloneContainers, err = c.assignContainersToServices(containers, services) var standaloneContainers []*Container
standaloneContainers, containers, err = c.assignContainersToServices(services)
if err != nil { if err != nil {
return err return err
} }
@ -227,6 +225,12 @@ func (c *DockerCommand) RefreshContainersAndServices() error {
if !c.Config.UserConfig.Gui.ShowAllContainers { if !c.Config.UserConfig.Gui.ShowAllContainers {
displayContainers = standaloneContainers displayContainers = standaloneContainers
} }
} else {
containers, err = c.GetContainers()
if err != nil {
return err
}
displayContainers = containers
} }
// sort services first by whether they have a linked container, and second by alphabetical order // sort services first by whether they have a linked container, and second by alphabetical order
@ -249,12 +253,17 @@ func (c *DockerCommand) RefreshContainersAndServices() error {
return nil return nil
} }
func (c *DockerCommand) assignContainersToServices(containers []*Container, services []*Service) ([]*Container, error) { func (c *DockerCommand) assignContainersToServices(services []*Service) ([]*Container, []*Container, error) {
// Get a list of the Container IDs for this Docker Compose project. // Get a list of the Container IDs for this Docker Compose project.
composeCommand := c.Config.UserConfig.CommandTemplates.DockerCompose composeCommand := c.Config.UserConfig.CommandTemplates.DockerCompose
output, err := c.OSCommand.RunCommandWithOutput(fmt.Sprintf("%s ps -q", composeCommand)) output, err := c.OSCommand.RunCommandWithOutput(fmt.Sprintf("%s ps -q", composeCommand))
if err != nil { if err != nil {
return nil, err return nil, nil, err
}
containers, err := c.GetContainers()
if err != nil {
return nil, nil, err
} }
// Create an index for Container IDs to make lookups less expensive. // Create an index for Container IDs to make lookups less expensive.
@ -273,9 +282,14 @@ func (c *DockerCommand) assignContainersToServices(containers []*Container, serv
// Walk the list of Containers, categorizing them as either associated with a // Walk the list of Containers, categorizing them as either associated with a
// Service or as standalone. // Service or as standalone.
serviceContainerCount := len(serviceContainerIds)
standaloneContainerCount := len(containers) - serviceContainerCount // source of truth is the containers variable because it's the fastest, and the latest
standaloneContainers := make([]*Container, 0, standaloneContainerCount) // we don't want to compare the lengths, we want to compare the members of each group and see which ones are actually standalone.
// serviceContainerCount := len(serviceContainerIds)
// standaloneContainerCount := len(containers) - serviceContainerCount
// standaloneContainers := make([]*Container, 0, standaloneContainerCount)
standaloneContainers := []*Container{}
for _, container := range containers { for _, container := range containers {
if !container.OneOff && serviceContainerIds[container.ID] { if !container.OneOff && serviceContainerIds[container.ID] {
service := serviceMap[container.ServiceName] service := serviceMap[container.ServiceName]
@ -285,7 +299,7 @@ func (c *DockerCommand) assignContainersToServices(containers []*Container, serv
} }
} }
return standaloneContainers, nil return standaloneContainers, containers, nil
} }
// filterOutExited filters out the exited containers if c.ShowExited is false // filterOutExited filters out the exited containers if c.ShowExited is false

Loading…
Cancel
Save