github.com/xeptore/docker-cli@v20.10.14+incompatible/cli/command/system/df.go (about) 1 package system 2 3 import ( 4 "context" 5 6 "github.com/docker/cli/cli" 7 "github.com/docker/cli/cli/command" 8 "github.com/docker/cli/cli/command/formatter" 9 "github.com/spf13/cobra" 10 ) 11 12 type diskUsageOptions struct { 13 verbose bool 14 format string 15 } 16 17 // newDiskUsageCommand creates a new cobra.Command for `docker df` 18 func newDiskUsageCommand(dockerCli command.Cli) *cobra.Command { 19 var opts diskUsageOptions 20 21 cmd := &cobra.Command{ 22 Use: "df [OPTIONS]", 23 Short: "Show docker disk usage", 24 Args: cli.NoArgs, 25 RunE: func(cmd *cobra.Command, args []string) error { 26 return runDiskUsage(dockerCli, opts) 27 }, 28 Annotations: map[string]string{"version": "1.25"}, 29 } 30 31 flags := cmd.Flags() 32 33 flags.BoolVarP(&opts.verbose, "verbose", "v", false, "Show detailed information on space usage") 34 flags.StringVar(&opts.format, "format", "", "Pretty-print images using a Go template") 35 36 return cmd 37 } 38 39 func runDiskUsage(dockerCli command.Cli, opts diskUsageOptions) error { 40 du, err := dockerCli.Client().DiskUsage(context.Background()) 41 if err != nil { 42 return err 43 } 44 45 format := opts.format 46 if len(format) == 0 { 47 format = formatter.TableFormatKey 48 } 49 50 var bsz int64 51 for _, bc := range du.BuildCache { 52 if !bc.Shared { 53 bsz += bc.Size 54 } 55 } 56 57 duCtx := formatter.DiskUsageContext{ 58 Context: formatter.Context{ 59 Output: dockerCli.Out(), 60 Format: formatter.NewDiskUsageFormat(format, opts.verbose), 61 }, 62 LayersSize: du.LayersSize, 63 BuilderSize: bsz, 64 BuildCache: du.BuildCache, 65 Images: du.Images, 66 Containers: du.Containers, 67 Volumes: du.Volumes, 68 Verbose: opts.verbose, 69 } 70 71 return duCtx.Write() 72 }