github.com/grahambrereton-form3/tilt@v0.10.18/internal/cli/docker_prune.go (about)

     1  package cli
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"time"
     7  
     8  	"github.com/spf13/cobra"
     9  
    10  	"github.com/windmilleng/tilt/pkg/logger"
    11  	"github.com/windmilleng/tilt/pkg/model"
    12  
    13  	"github.com/windmilleng/tilt/internal/docker"
    14  	"github.com/windmilleng/tilt/internal/tiltfile"
    15  
    16  	"github.com/windmilleng/tilt/internal/analytics"
    17  	"github.com/windmilleng/tilt/internal/engine/dockerprune"
    18  )
    19  
    20  type dockerPruneCmd struct {
    21  	fileName string
    22  }
    23  
    24  type dpDeps struct {
    25  	dCli docker.Client
    26  	tfl  tiltfile.TiltfileLoader
    27  }
    28  
    29  func newDPDeps(dCli docker.Client, tfl tiltfile.TiltfileLoader) dpDeps {
    30  	return dpDeps{
    31  		dCli: dCli,
    32  		tfl:  tfl,
    33  	}
    34  }
    35  
    36  func (c *dockerPruneCmd) register() *cobra.Command {
    37  	cmd := &cobra.Command{
    38  		Use:   "docker-prune",
    39  		Short: "run docker prune as Tilt does",
    40  	}
    41  
    42  	cmd.Flags().StringVar(&c.fileName, "file", tiltfile.FileName, "Path to Tiltfile")
    43  
    44  	return cmd
    45  }
    46  
    47  func (c *dockerPruneCmd) run(ctx context.Context, args []string) error {
    48  	a := analytics.Get(ctx)
    49  	a.Incr("cmd.dockerPrune", nil)
    50  	a.IncrIfUnopted("analytics.up.optdefault")
    51  	defer a.Flush(time.Second)
    52  
    53  	// (Most relevant output from dockerpruner is at the `debug` level)
    54  	l := logger.NewLogger(logger.DebugLvl, os.Stdout)
    55  	ctx = logger.WithLogger(ctx, l)
    56  
    57  	deps, err := wireDockerPrune(ctx, a)
    58  	if err != nil {
    59  		return err
    60  	}
    61  
    62  	tlr := deps.tfl.Load(ctx, c.fileName, nil)
    63  	if tlr.Error != nil {
    64  		return tlr.Error
    65  	}
    66  
    67  	imgSelectors := model.RefSelectorsForManifests(tlr.Manifests)
    68  
    69  	dp := dockerprune.NewDockerPruner(deps.dCli)
    70  
    71  	// TODO: print the commands being run
    72  	dp.Prune(ctx, tlr.DockerPruneSettings.MaxAge, imgSelectors)
    73  
    74  	return nil
    75  }