From f1b62c089ff4d33dfecae4a56f5d3a53accbee24 Mon Sep 17 00:00:00 2001 From: davidmdm Date: Thu, 27 May 2021 11:22:41 -0400 Subject: [PATCH] implemented rm flag for start command --- src/cmd/start/start.go | 22 +++++++++++++++++++--- src/internal/context.go | 10 +++++++++- src/internal/docker/cli.go | 4 ++++ 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/cmd/start/start.go b/src/cmd/start/start.go index db35911..c3eb4d7 100644 --- a/src/cmd/start/start.go +++ b/src/cmd/start/start.go @@ -14,7 +14,9 @@ import ( // New creates a cobra command func New() *cobra.Command { - return &cobra.Command{ + options := Options{Remove: new(bool)} + + cmd := &cobra.Command{ Use: "start", Short: "Starts container", Args: cobra.RangeArgs(0, 1), @@ -23,12 +25,23 @@ func New() *cobra.Command { if len(args) == 1 { name = args[0] } - return run(cmd.Context(), name) + if !cmd.Flag("rm").Changed { + options.Remove = nil + } + return run(cmd.Context(), name, options) }, } + + cmd.Flags().BoolVar(options.Remove, "rm", false, "removes container if true after exit") + + return cmd } -func run(ctx context.Context, name string) error { +type Options struct { + Remove *bool +} + +func run(ctx context.Context, name string, options Options) error { contexts, err := yey.LoadContexts() if err != nil { return err @@ -46,6 +59,9 @@ func run(ctx context.Context, name string) error { if err != nil { return fmt.Errorf("failed to get context with name %q: %w", name, err) } + if options.Remove != nil { + yeyContext.Remove = options.Remove + } shortImageName, err := docker.GetShortImageName(yeyContext.Image) if err != nil { diff --git a/src/internal/context.go b/src/internal/context.go index 9a32f9a..70e8015 100644 --- a/src/internal/context.go +++ b/src/internal/context.go @@ -11,7 +11,7 @@ import ( // Context represents execution configuration for some docker container type Context struct { Name string `yaml:",omitempty"` - Remove bool + Remove *bool Image string Env map[string]string Mounts map[string]string @@ -22,6 +22,10 @@ type Context struct { // Clone returns a deep-copy of this context func (c Context) Clone() Context { clone := c + if clone.Remove != nil { + value := *clone.Remove + clone.Remove = &value + } clone.Env = make(map[string]string) for key, value := range c.Env { clone.Env[key] = value @@ -36,6 +40,10 @@ func (c Context) Clone() Context { // Merge creates a deep-copy of this context and copies values from given source context on top of it func (c Context) Merge(source Context) Context { merged := c.Clone() + if source.Remove != nil { + value := *source.Remove + merged.Remove = &value + } if source.Image != "" { merged.Image = source.Image } diff --git a/src/internal/docker/cli.go b/src/internal/docker/cli.go index aa9b540..d096561 100644 --- a/src/internal/docker/cli.go +++ b/src/internal/docker/cli.go @@ -92,6 +92,10 @@ func runContainer(ctx context.Context, yeyCtx yey.Context, containerName string) ) } + if yeyCtx.Remove != nil && *yeyCtx.Remove { + args = append(args, "--rm") + } + args = append(args, yeyCtx.Image) args = append(args, yeyCtx.Cmd...)