mirror of
https://github.com/jesseduffield/lazydocker.git
synced 2026-07-25 08:31:03 +00:00
fix: merge declared services with ps command services
preferrebly uses the ones with container ids associated
This commit is contained in:
parent
54f6a445b2
commit
430eebeffe
1 changed files with 54 additions and 11 deletions
|
|
@ -32,6 +32,11 @@ const (
|
||||||
dockerHostEnvKey = "DOCKER_HOST"
|
dockerHostEnvKey = "DOCKER_HOST"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
declaredServicesMap = make(map[string]string)
|
||||||
|
declaredServicesMu sync.Mutex
|
||||||
|
)
|
||||||
|
|
||||||
// DockerCommand is our main docker interface
|
// DockerCommand is our main docker interface
|
||||||
type DockerCommand struct {
|
type DockerCommand struct {
|
||||||
Log *logrus.Entry
|
Log *logrus.Entry
|
||||||
|
|
@ -189,7 +194,7 @@ func (c *DockerCommand) RefreshContainersAndServices(currentServices []*Service,
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
|
||||||
services, err = c.GetServices()
|
services, err = c.GetServices()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
|
|
@ -271,6 +276,29 @@ func (c *DockerCommand) GetContainers(existingContainers []*Container) ([]*Conta
|
||||||
return ownContainers, nil
|
return ownContainers, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *DockerCommand) getDeclaredServices(composeCommand string) map[string]string {
|
||||||
|
declaredServicesMu.Lock()
|
||||||
|
defer declaredServicesMu.Unlock()
|
||||||
|
|
||||||
|
if len(declaredServicesMap) > 0 {
|
||||||
|
// We only need to fetch the declared services once
|
||||||
|
return declaredServicesMap
|
||||||
|
}
|
||||||
|
|
||||||
|
output, err := c.OSCommand.RunCommandWithOutput(fmt.Sprintf("%s config --services", composeCommand))
|
||||||
|
if err != nil {
|
||||||
|
c.Log.Error(fmt.Sprintf("Error fetching declared services: %v", err))
|
||||||
|
return declaredServicesMap
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, line := range utils.SplitLines(output) {
|
||||||
|
declaredServicesMap[line] = line
|
||||||
|
}
|
||||||
|
|
||||||
|
return declaredServicesMap
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// GetServices gets services
|
// GetServices gets services
|
||||||
func (c *DockerCommand) GetServices() ([]*Service, error) {
|
func (c *DockerCommand) GetServices() ([]*Service, error) {
|
||||||
if !c.InDockerComposeProject {
|
if !c.InDockerComposeProject {
|
||||||
|
|
@ -278,26 +306,41 @@ func (c *DockerCommand) GetServices() ([]*Service, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
composeCommand := c.Config.UserConfig.CommandTemplates.DockerCompose
|
composeCommand := c.Config.UserConfig.CommandTemplates.DockerCompose
|
||||||
output, err := c.OSCommand.RunCommandWithOutput(fmt.Sprintf("%s ps -a --format '{{.ID}} {{.Service}}'", composeCommand))
|
|
||||||
|
psServices, err := c.OSCommand.RunCommandWithOutput(fmt.Sprintf("%s ps -a --format '{{.ID}} {{.Service}}'", composeCommand))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
lines := utils.SplitLines(output)
|
c.getDeclaredServices(composeCommand)
|
||||||
services := make([]*Service, len(lines))
|
|
||||||
for i, str := range lines {
|
psServicesMap := make(map[string]string)
|
||||||
if str == "" {
|
for _, line := range utils.SplitLines(psServices) {
|
||||||
continue
|
serviceParams := strings.Split(line, " ")
|
||||||
|
psServicesMap[serviceParams[1]] = serviceParams[0]
|
||||||
}
|
}
|
||||||
serviceParams := strings.Split(str, " ")
|
|
||||||
|
merged := make(map[string]string)
|
||||||
|
for k, v := range declaredServicesMap {
|
||||||
|
merged[k] = v
|
||||||
|
}
|
||||||
|
for k, v := range psServicesMap {
|
||||||
|
// Overrides if we have access to the id
|
||||||
|
merged[k] = v
|
||||||
|
}
|
||||||
|
|
||||||
|
services := make([]*Service, len(merged))
|
||||||
|
i := 0
|
||||||
|
for id, service := range merged {
|
||||||
services[i] = &Service{
|
services[i] = &Service{
|
||||||
Name: serviceParams[1],
|
Name: id,
|
||||||
ID: serviceParams[0],
|
ID: service,
|
||||||
OSCommand: c.OSCommand,
|
OSCommand: c.OSCommand,
|
||||||
Log: c.Log,
|
Log: c.Log,
|
||||||
DockerCommand: c,
|
DockerCommand: c,
|
||||||
}
|
}
|
||||||
|
i++
|
||||||
}
|
}
|
||||||
|
|
||||||
return services, nil
|
return services, nil
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue