Skip to content

Commit fb3f9e2

Browse files
KoditkarVedantndeloof
authored andcommitted
Add compose file path to ls command
docker compose ls will not include config files section in result Signed-off-by: Vedant Koditkar <[email protected]>
1 parent 02f78d2 commit fb3f9e2

File tree

3 files changed

+43
-13
lines changed

3 files changed

+43
-13
lines changed

cmd/compose/list.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,22 +92,24 @@ func runList(ctx context.Context, backend api.Service, opts lsOptions) error {
9292
view := viewFromStackList(stackList)
9393
return formatter.Print(view, opts.Format, os.Stdout, func(w io.Writer) {
9494
for _, stack := range view {
95-
_, _ = fmt.Fprintf(w, "%s\t%s\n", stack.Name, stack.Status)
95+
_, _ = fmt.Fprintf(w, "%s\t%s\t%s\n", stack.Name, stack.Status, stack.ConfigFiles)
9696
}
97-
}, "NAME", "STATUS")
97+
}, "NAME", "STATUS", "CONFIG FILES")
9898
}
9999

100100
type stackView struct {
101-
Name string
102-
Status string
101+
Name string
102+
Status string
103+
ConfigFiles string
103104
}
104105

105106
func viewFromStackList(stackList []api.Stack) []stackView {
106107
retList := make([]stackView, len(stackList))
107108
for i, s := range stackList {
108109
retList[i] = stackView{
109-
Name: s.Name,
110-
Status: strings.TrimSpace(fmt.Sprintf("%s %s", s.Status, s.Reason)),
110+
Name: s.Name,
111+
Status: strings.TrimSpace(fmt.Sprintf("%s %s", s.Status, s.Reason)),
112+
ConfigFiles: s.ConfigFiles,
111113
}
112114
}
113115
return retList

pkg/api/api.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -404,10 +404,11 @@ const (
404404

405405
// Stack holds the name and state of a compose application/stack
406406
type Stack struct {
407-
ID string
408-
Name string
409-
Status string
410-
Reason string
407+
ID string
408+
Name string
409+
Status string
410+
ConfigFiles string
411+
Reason string
411412
}
412413

413414
// LogConsumer is a callback to process log messages from services

pkg/compose/ls.go

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ import (
2020
"context"
2121
"fmt"
2222
"sort"
23+
"strings"
2324

2425
"github.com/docker/compose/v2/pkg/api"
26+
"github.com/docker/compose/v2/pkg/utils"
2527

2628
moby "github.com/docker/docker/api/types"
2729
"github.com/docker/docker/api/types/filters"
@@ -46,15 +48,40 @@ func containersToStacks(containers []moby.Container) ([]api.Stack, error) {
4648
}
4749
var projects []api.Stack
4850
for _, project := range keys {
51+
configFiles, err := combinedConfigFiles(containersByLabel[project])
52+
if err != nil {
53+
return nil, err
54+
}
55+
4956
projects = append(projects, api.Stack{
50-
ID: project,
51-
Name: project,
52-
Status: combinedStatus(containerToState(containersByLabel[project])),
57+
ID: project,
58+
Name: project,
59+
Status: combinedStatus(containerToState(containersByLabel[project])),
60+
ConfigFiles: configFiles,
5361
})
5462
}
5563
return projects, nil
5664
}
5765

66+
func combinedConfigFiles(containers []moby.Container) (string, error) {
67+
configFiles := []string{}
68+
69+
for _, c := range containers {
70+
files, ok := c.Labels[api.ConfigFilesLabel]
71+
if !ok {
72+
return "", fmt.Errorf("No label %q set on container %q of compose project", api.ConfigFilesLabel, c.ID)
73+
}
74+
75+
for _, f := range strings.Split(files, ",") {
76+
if !utils.StringContains(configFiles, f) {
77+
configFiles = append(configFiles, f)
78+
}
79+
}
80+
}
81+
82+
return strings.Join(configFiles, ","), nil
83+
}
84+
5885
func containerToState(containers []moby.Container) []string {
5986
statuses := []string{}
6087
for _, c := range containers {

0 commit comments

Comments
 (0)