Skip to content

Commit

Permalink
implemented rm flag for start command
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmdm committed May 27, 2021
1 parent 03befb0 commit f1b62c0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
22 changes: 19 additions & 3 deletions src/cmd/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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
Expand All @@ -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 {
Expand Down
10 changes: 9 additions & 1 deletion src/internal/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
}
Expand Down
4 changes: 4 additions & 0 deletions src/internal/docker/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)

Expand Down

0 comments on commit f1b62c0

Please sign in to comment.