From e4850d9c48d06908a105361760e44d2acd401f5f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Gabriel=20F=C3=A9ron?= <g@leirbag.net>
Date: Fri, 9 Dec 2022 17:42:46 +0100
Subject: [PATCH] Add --include-deps to push command
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Gabriel Féron <g@leirbag.net>
---
 cmd/compose/pull.go                     | 27 ++++++++++++++++---------
 cmd/compose/push.go                     |  7 ++++++-
 docs/reference/compose_push.md          |  1 +
 docs/reference/docker_compose_push.yaml | 10 +++++++++
 4 files changed, 34 insertions(+), 11 deletions(-)

diff --git a/cmd/compose/pull.go b/cmd/compose/pull.go
index 48faa55dd..7a1fde24d 100644
--- a/cmd/compose/pull.go
+++ b/cmd/compose/pull.go
@@ -21,6 +21,7 @@ import (
 	"fmt"
 	"os"
 
+	"github.com/compose-spec/compose-go/types"
 	"github.com/morikuni/aec"
 	"github.com/spf13/cobra"
 
@@ -67,6 +68,21 @@ func pullCommand(p *projectOptions, backend api.Service) *cobra.Command {
 	return cmd
 }
 
+func FilterServices(project *types.Project, services []string) error {
+	enabled, err := project.GetServices(services...)
+	if err != nil {
+		return err
+	}
+	for _, s := range project.Services {
+		if !utils.StringContains(services, s.Name) {
+			project.DisabledServices = append(project.DisabledServices, s)
+		}
+	}
+	project.Services = enabled
+
+	return nil
+}
+
 func runPull(ctx context.Context, backend api.Service, opts pullOptions, services []string) error {
 	project, err := opts.toProject(services)
 	if err != nil {
@@ -74,16 +90,7 @@ func runPull(ctx context.Context, backend api.Service, opts pullOptions, service
 	}
 
 	if !opts.includeDeps {
-		enabled, err := project.GetServices(services...)
-		if err != nil {
-			return err
-		}
-		for _, s := range project.Services {
-			if !utils.StringContains(services, s.Name) {
-				project.DisabledServices = append(project.DisabledServices, s)
-			}
-		}
-		project.Services = enabled
+		FilterServices(project, services)
 	}
 
 	return backend.Pull(ctx, project, api.PullOptions{
diff --git a/cmd/compose/push.go b/cmd/compose/push.go
index 5f74ae7c5..4f915d003 100644
--- a/cmd/compose/push.go
+++ b/cmd/compose/push.go
@@ -27,7 +27,7 @@ import (
 type pushOptions struct {
 	*projectOptions
 	composeOptions
-
+	IncludeDeps    bool
 	Ignorefailures bool
 	Quiet          bool
 }
@@ -45,6 +45,7 @@ func pushCommand(p *projectOptions, backend api.Service) *cobra.Command {
 		ValidArgsFunction: completeServiceNames(p),
 	}
 	pushCmd.Flags().BoolVar(&opts.Ignorefailures, "ignore-push-failures", false, "Push what it can and ignores images with push failures")
+	pushCmd.Flags().BoolVar(&opts.IncludeDeps, "include-deps", false, "Also push images of services declared as dependencies")
 	pushCmd.Flags().BoolVarP(&opts.Quiet, "quiet", "q", false, "Push without printing progress information")
 
 	return pushCmd
@@ -56,6 +57,10 @@ func runPush(ctx context.Context, backend api.Service, opts pushOptions, service
 		return err
 	}
 
+	if !opts.IncludeDeps {
+		FilterServices(project, services)
+	}
+
 	return backend.Push(ctx, project, api.PushOptions{
 		IgnoreFailures: opts.Ignorefailures,
 		Quiet:          opts.Quiet,
diff --git a/docs/reference/compose_push.md b/docs/reference/compose_push.md
index 26a0de084..e5df84338 100644
--- a/docs/reference/compose_push.md
+++ b/docs/reference/compose_push.md
@@ -8,6 +8,7 @@ Push service images
 | Name | Type | Default | Description |
 | --- | --- | --- | --- |
 | `--ignore-push-failures` |  |  | Push what it can and ignores images with push failures |
+| `--include-deps` |  |  | Also push images of services declared as dependencies |
 | `-q`, `--quiet` |  |  | Push without printing progress information |
 
 
diff --git a/docs/reference/docker_compose_push.yaml b/docs/reference/docker_compose_push.yaml
index 72c757551..5efd080bd 100644
--- a/docs/reference/docker_compose_push.yaml
+++ b/docs/reference/docker_compose_push.yaml
@@ -33,6 +33,16 @@ options:
       experimentalcli: false
       kubernetes: false
       swarm: false
+    - option: include-deps
+      value_type: bool
+      default_value: "false"
+      description: Also push images of services declared as dependencies
+      deprecated: false
+      hidden: false
+      experimental: false
+      experimentalcli: false
+      kubernetes: false
+      swarm: false
     - option: quiet
       shorthand: q
       value_type: bool