Clarify terminology around functions which use and create snapshots for containers. Signed-off-by: Derek McGowan <derek@mcgstyle.net>
		
			
				
	
	
		
			52 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			52 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
package main
 | 
						|
 | 
						|
import (
 | 
						|
	"fmt"
 | 
						|
 | 
						|
	"github.com/containerd/containerd"
 | 
						|
	"github.com/urfave/cli"
 | 
						|
)
 | 
						|
 | 
						|
var deleteCommand = cli.Command{
 | 
						|
	Name:      "delete",
 | 
						|
	Usage:     "delete an existing container",
 | 
						|
	ArgsUsage: "CONTAINER",
 | 
						|
	Flags: []cli.Flag{
 | 
						|
		cli.BoolFlag{
 | 
						|
			Name:  "keep-snapshot",
 | 
						|
			Usage: "do not clean up snapshot with container",
 | 
						|
		},
 | 
						|
	},
 | 
						|
	Action: func(context *cli.Context) error {
 | 
						|
		ctx, cancel := appContext(context)
 | 
						|
		defer cancel()
 | 
						|
		client, err := newClient(context)
 | 
						|
		if err != nil {
 | 
						|
			return err
 | 
						|
		}
 | 
						|
		deleteOpts := []containerd.DeleteOpts{}
 | 
						|
		if !context.Bool("keep-snapshot") {
 | 
						|
			deleteOpts = append(deleteOpts, containerd.WithSnapshotCleanup)
 | 
						|
		}
 | 
						|
		container, err := client.LoadContainer(ctx, context.Args().First())
 | 
						|
		if err != nil {
 | 
						|
			return err
 | 
						|
		}
 | 
						|
		task, err := container.Task(ctx, nil)
 | 
						|
		if err != nil {
 | 
						|
			return container.Delete(ctx, deleteOpts...)
 | 
						|
		}
 | 
						|
		status, err := task.Status(ctx)
 | 
						|
		if err != nil {
 | 
						|
			return err
 | 
						|
		}
 | 
						|
		if status == containerd.Stopped || status == containerd.Created {
 | 
						|
			if _, err := task.Delete(ctx); err != nil {
 | 
						|
				return err
 | 
						|
			}
 | 
						|
			return container.Delete(ctx, deleteOpts...)
 | 
						|
		}
 | 
						|
		return fmt.Errorf("cannot delete a non stopped container: %v", status)
 | 
						|
	},
 | 
						|
}
 |