Skip to content

Commit 3974f6f

Browse files
committed
Support -p flag and DRY up code
1 parent 697cd44 commit 3974f6f

4 files changed

Lines changed: 27 additions & 17 deletions

File tree

pkg/commands/docker.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,25 @@ func NewDockerCommand(log *logrus.Entry, osCommand *OSCommand, tr *i18n.Translat
154154
log.Warn(err.Error())
155155
}
156156

157+
// When the user passes -p outside of a compose directory, treat it as the
158+
// local project so the project/services panels still appear and filtering
159+
// is applied. Inside a compose dir, LocalProjectName is derived from
160+
// container labels later in RefreshContainersAndServices.
161+
if !dockerCommand.InDockerComposeProject && config.ProjectName != "" {
162+
dockerCommand.LocalProjectName = config.ProjectName
163+
}
164+
157165
return dockerCommand, nil
158166
}
159167

168+
// IsProjectScoped reports whether lazydocker should be scoped to a single
169+
// compose project — either because we're inside a compose directory or
170+
// because the user passed -p. When false, the project/services panels are
171+
// hidden and all containers are shown in a flat list.
172+
func (c *DockerCommand) IsProjectScoped() bool {
173+
return c.InDockerComposeProject || c.Config.ProjectName != ""
174+
}
175+
160176
func (c *DockerCommand) setDockerComposeCommand(config *config.AppConfig) {
161177
if config.UserConfig.CommandTemplates.DockerCompose != "docker compose" {
162178
return

pkg/gui/containers_panel.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,10 @@ func (gui *Gui) getContainersPanel() *panels.SideListPanel[*commands.Container]
8787
return false
8888
}
8989

90-
// Only apply project and standalone filtering when we are inside a
91-
// docker-compose project. Outside of a compose project all
92-
// containers are shown in a flat list regardless of which compose
93-
// project they belong to.
94-
if gui.DockerCommand.InDockerComposeProject {
90+
// When project-scoped, apply project and standalone filtering.
91+
// Otherwise all containers are shown in a flat list regardless
92+
// of which compose project they belong to.
93+
if gui.DockerCommand.IsProjectScoped() {
9594
// This check must be inside the InDockerComposeProject guard:
9695
// outside a compose project, services are still derived from
9796
// container labels, so compose-managed containers from other

pkg/gui/project_panel.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,7 @@ func (gui *Gui) getProjectPanel() *panels.SideListPanel[*commands.Project] {
5959
return gui.renderContainersAndServices()
6060
},
6161
Hide: func() bool {
62-
// Only show the project panel when we are inside a docker-compose
63-
// project directory. When launched outside of a compose project
64-
// there is no meaningful local project to display, so we hide the
65-
// panel and let the containers panel show all containers in a flat
66-
// list (matching the behaviour from before v0.25).
67-
return !gui.DockerCommand.InDockerComposeProject
62+
return !gui.DockerCommand.IsProjectScoped()
6863
},
6964
}
7065
}
@@ -195,6 +190,11 @@ func (gui *Gui) renderAllLogs(project *commands.Project) tasks.TaskFunc {
195190
}
196191

197192
func (gui *Gui) renderDockerComposeConfig(project *commands.Project) tasks.TaskFunc {
193+
if !gui.DockerCommand.InDockerComposeProject {
194+
return gui.NewSimpleRenderStringTask(func() string {
195+
return "Compose config is only available when launched from a docker-compose project directory"
196+
})
197+
}
198198
if project != nil && project.Name != gui.DockerCommand.LocalProjectName {
199199
return gui.NewSimpleRenderStringTask(func() string {
200200
return "Compose config is not available for non-local projects"

pkg/gui/services_panel.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,7 @@ func (gui *Gui) getServicesPanel() *panels.SideListPanel[*commands.Service] {
9090
return presentation.GetServiceDisplayStrings(&gui.Config.UserConfig.Gui, service)
9191
},
9292
Hide: func() bool {
93-
// Only show the services panel when we are inside a docker-compose
94-
// project directory. When launched outside of a compose project
95-
// there is no local project context, so the panel is hidden and
96-
// all containers are shown in a flat list (matching pre-v0.25
97-
// behaviour).
98-
return !gui.DockerCommand.InDockerComposeProject
93+
return !gui.DockerCommand.IsProjectScoped()
9994
},
10095
}
10196
}

0 commit comments

Comments
 (0)