From 4b34268e77f7bee40f340028e162010d011c2525 Mon Sep 17 00:00:00 2001 From: Remco Kranenburg Date: Thu, 19 Dec 2024 22:57:15 +0100 Subject: [PATCH] Add `--prune` for up command Signed-off-by: Remco Kranenburg --- cmd/compose/up.go | 6 ++++++ pkg/api/api.go | 1 + pkg/compose/up.go | 1 + 3 files changed, 8 insertions(+) diff --git a/cmd/compose/up.go b/cmd/compose/up.go index e41a9549bdf..4ec399c9a06 100644 --- a/cmd/compose/up.go +++ b/cmd/compose/up.go @@ -57,6 +57,7 @@ type upOptions struct { wait bool waitTimeout int watch bool + prune bool navigationMenu bool navigationMenuChanged bool } @@ -170,6 +171,7 @@ func upCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service) *c flags.BoolVar(&up.wait, "wait", false, "Wait for services to be running|healthy. Implies detached mode.") flags.IntVar(&up.waitTimeout, "wait-timeout", 0, "Maximum duration in seconds to wait for the project to be running|healthy") flags.BoolVarP(&up.watch, "watch", "w", false, "Watch source code and rebuild/refresh containers when files are updated.") + flags.BoolVar(&up.prune, "prune", false, "Prune dangling images on rebuild") flags.BoolVar(&up.navigationMenu, "menu", false, "Enable interactive shortcuts when running attached. Incompatible with --detach. Can also be enable/disable by setting COMPOSE_MENU environment var.") return upCmd @@ -207,6 +209,9 @@ func validateFlags(up *upOptions, create *createOptions) error { if create.noBuild && up.watch { return fmt.Errorf("--no-build and --watch are incompatible") } + if !up.watch && up.prune { + return fmt.Errorf("--prune can only be used with --watch") + } return nil } @@ -310,6 +315,7 @@ func runUp( Wait: upOptions.wait, WaitTimeout: timeout, Watch: upOptions.watch, + Prune: upOptions.prune, Services: services, NavigationMenu: upOptions.navigationMenu && ui.Mode != "plain", }, diff --git a/pkg/api/api.go b/pkg/api/api.go index 8d2d1280328..53a34d45e9d 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -229,6 +229,7 @@ type StartOptions struct { // Services passed in the command line to be started Services []string Watch bool + Prune bool NavigationMenu bool } diff --git a/pkg/compose/up.go b/pkg/compose/up.go index 8283bf0cbc4..f0dd0be726f 100644 --- a/pkg/compose/up.go +++ b/pkg/compose/up.go @@ -162,6 +162,7 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options return s.watch(ctx, doneCh, project, options.Start.Services, api.WatchOptions{ Build: &buildOpts, LogTo: options.Start.Attach, + Prune: options.Start.Prune, }) }) }