浏览代码

introduce --no-attach to ignore some service output

Signed-off-by: Nicolas De Loof <[email protected]>
Nicolas De Loof 2 年之前
父节点
当前提交
d5e4f00644
共有 4 个文件被更改,包括 31 次插入0 次删除
  1. 3 0
      cmd/compose/up.go
  2. 4 0
      docs/reference/compose_up.md
  3. 13 0
      docs/reference/docker_compose_up.yaml
  4. 11 0
      pkg/utils/slices.go

+ 3 - 0
cmd/compose/up.go

@@ -48,6 +48,7 @@ type upOptions struct {
 	noPrefix           bool
 	attachDependencies bool
 	attach             []string
+	noAttach           []string
 	timestamp          bool
 	wait               bool
 }
@@ -128,6 +129,7 @@ func upCommand(p *ProjectOptions, streams api.Streams, backend api.Service) *cob
 	flags.BoolVar(&up.attachDependencies, "attach-dependencies", false, "Attach to dependent containers.")
 	flags.BoolVar(&create.quietPull, "quiet-pull", false, "Pull without printing progress information.")
 	flags.StringArrayVar(&up.attach, "attach", []string{}, "Attach to service output.")
+	flags.StringArrayVar(&up.noAttach, "no-attach", []string{}, "Don't attach to specified service.")
 	flags.BoolVar(&up.wait, "wait", false, "Wait for services to be running|healthy. Implies detached mode.")
 
 	return upCmd
@@ -185,6 +187,7 @@ func runUp(ctx context.Context, streams api.Streams, backend api.Service, create
 	if len(attachTo) == 0 {
 		attachTo = project.ServiceNames()
 	}
+	attachTo = utils.RemoveAll(attachTo, upOptions.noAttach)
 
 	create := api.CreateOptions{
 		Services:             services,

+ 4 - 0
docs/reference/compose_up.md

@@ -15,6 +15,7 @@ Create and start containers
 | `-d`, `--detach`             |               |           | Detached mode: Run containers in the background                                                          |
 | `--exit-code-from`           | `string`      |           | Return the exit code of the selected service container. Implies --abort-on-container-exit                |
 | `--force-recreate`           |               |           | Recreate containers even if their configuration and image haven't changed.                               |
+| `--no-attach`                | `stringArray` |           | Don't attach to specified service.                                                                       |
 | `--no-build`                 |               |           | Don't build an image, even if it's missing.                                                              |
 | `--no-color`                 |               |           | Produce monochrome output.                                                                               |
 | `--no-deps`                  |               |           | Don't start linked services.                                                                             |
@@ -40,6 +41,9 @@ Builds, (re)creates, starts, and attaches to containers for a service.
 Unless they are already running, this command also starts any linked services.
 
 The `docker compose up` command aggregates the output of each container (like `docker compose logs --follow` does).
+One can optionally select a subset of services to attach to using `--attach` flag, or exclude some services using 
+`--no-attach` to prevent output to be flooded by some verbose services. 
+
 When the command exits, all containers are stopped. Running `docker compose up --detach` starts the containers in the
 background and leaves them running.
 

+ 13 - 0
docs/reference/docker_compose_up.yaml

@@ -6,6 +6,9 @@ long: |-
     Unless they are already running, this command also starts any linked services.
 
     The `docker compose up` command aggregates the output of each container (like `docker compose logs --follow` does).
+    One can optionally select a subset of services to attach to using `--attach` flag, or exclude some services using
+    `--no-attach` to prevent output to be flooded by some verbose services.
+
     When the command exits, all containers are stopped. Running `docker compose up --detach` starts the containers in the
     background and leaves them running.
 
@@ -104,6 +107,16 @@ options:
       experimentalcli: false
       kubernetes: false
       swarm: false
+    - option: no-attach
+      value_type: stringArray
+      default_value: '[]'
+      description: Don't attach to specified service.
+      deprecated: false
+      hidden: false
+      experimental: false
+      experimentalcli: false
+      kubernetes: false
+      swarm: false
     - option: no-build
       value_type: bool
       default_value: "false"

+ 11 - 0
pkg/utils/slices.go

@@ -28,3 +28,14 @@ func Contains[T any](origin []T, element T) bool {
 	}
 	return false
 }
+
+// RemoveAll removes all elements from origin slice
+func RemoveAll[T any](origin []T, elements []T) []T {
+	var filtered []T
+	for _, v := range origin {
+		if !Contains(elements, v) {
+			filtered = append(filtered, v)
+		}
+	}
+	return filtered
+}