Add compose file path to ls command

docker compose ls will not include config files section in result

Signed-off-by: Vedant Koditkar <vedant.koditkar@outlook.com>
This commit is contained in:
Vedant Koditkar 2022-02-14 20:25:47 +05:30 committed by Nicolas De loof
parent 02f78d2893
commit fb3f9e270f
3 changed files with 43 additions and 13 deletions

View File

@ -92,22 +92,24 @@ func runList(ctx context.Context, backend api.Service, opts lsOptions) error {
view := viewFromStackList(stackList) view := viewFromStackList(stackList)
return formatter.Print(view, opts.Format, os.Stdout, func(w io.Writer) { return formatter.Print(view, opts.Format, os.Stdout, func(w io.Writer) {
for _, stack := range view { for _, stack := range view {
_, _ = fmt.Fprintf(w, "%s\t%s\n", stack.Name, stack.Status) _, _ = fmt.Fprintf(w, "%s\t%s\t%s\n", stack.Name, stack.Status, stack.ConfigFiles)
} }
}, "NAME", "STATUS") }, "NAME", "STATUS", "CONFIG FILES")
} }
type stackView struct { type stackView struct {
Name string Name string
Status string Status string
ConfigFiles string
} }
func viewFromStackList(stackList []api.Stack) []stackView { func viewFromStackList(stackList []api.Stack) []stackView {
retList := make([]stackView, len(stackList)) retList := make([]stackView, len(stackList))
for i, s := range stackList { for i, s := range stackList {
retList[i] = stackView{ retList[i] = stackView{
Name: s.Name, Name: s.Name,
Status: strings.TrimSpace(fmt.Sprintf("%s %s", s.Status, s.Reason)), Status: strings.TrimSpace(fmt.Sprintf("%s %s", s.Status, s.Reason)),
ConfigFiles: s.ConfigFiles,
} }
} }
return retList return retList

View File

@ -404,10 +404,11 @@ const (
// Stack holds the name and state of a compose application/stack // Stack holds the name and state of a compose application/stack
type Stack struct { type Stack struct {
ID string ID string
Name string Name string
Status string Status string
Reason string ConfigFiles string
Reason string
} }
// LogConsumer is a callback to process log messages from services // LogConsumer is a callback to process log messages from services

View File

@ -20,8 +20,10 @@ import (
"context" "context"
"fmt" "fmt"
"sort" "sort"
"strings"
"github.com/docker/compose/v2/pkg/api" "github.com/docker/compose/v2/pkg/api"
"github.com/docker/compose/v2/pkg/utils"
moby "github.com/docker/docker/api/types" moby "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters" "github.com/docker/docker/api/types/filters"
@ -46,15 +48,40 @@ func containersToStacks(containers []moby.Container) ([]api.Stack, error) {
} }
var projects []api.Stack var projects []api.Stack
for _, project := range keys { for _, project := range keys {
configFiles, err := combinedConfigFiles(containersByLabel[project])
if err != nil {
return nil, err
}
projects = append(projects, api.Stack{ projects = append(projects, api.Stack{
ID: project, ID: project,
Name: project, Name: project,
Status: combinedStatus(containerToState(containersByLabel[project])), Status: combinedStatus(containerToState(containersByLabel[project])),
ConfigFiles: configFiles,
}) })
} }
return projects, nil return projects, nil
} }
func combinedConfigFiles(containers []moby.Container) (string, error) {
configFiles := []string{}
for _, c := range containers {
files, ok := c.Labels[api.ConfigFilesLabel]
if !ok {
return "", fmt.Errorf("No label %q set on container %q of compose project", api.ConfigFilesLabel, c.ID)
}
for _, f := range strings.Split(files, ",") {
if !utils.StringContains(configFiles, f) {
configFiles = append(configFiles, f)
}
}
}
return strings.Join(configFiles, ","), nil
}
func containerToState(containers []moby.Container) []string { func containerToState(containers []moby.Container) []string {
statuses := []string{} statuses := []string{}
for _, c := range containers { for _, c := range containers {