2016-08-23 19:37:37 -04:00
|
|
|
package system
|
|
|
|
|
|
|
|
import (
|
2017-03-02 07:05:48 -05:00
|
|
|
"errors"
|
|
|
|
|
2016-08-23 19:37:37 -04:00
|
|
|
"github.com/docker/docker/cli"
|
|
|
|
"github.com/docker/docker/cli/command"
|
|
|
|
"github.com/docker/docker/cli/command/formatter"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
type diskUsageOptions struct {
|
|
|
|
verbose bool
|
2017-03-02 07:05:48 -05:00
|
|
|
format string
|
2016-08-23 19:37:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDiskUsageCommand creates a new cobra.Command for `docker df`
|
|
|
|
func NewDiskUsageCommand(dockerCli *command.DockerCli) *cobra.Command {
|
|
|
|
var opts diskUsageOptions
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "df [OPTIONS]",
|
|
|
|
Short: "Show docker disk usage",
|
2016-10-08 06:38:25 -04:00
|
|
|
Args: cli.NoArgs,
|
2016-08-23 19:37:37 -04:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
return runDiskUsage(dockerCli, opts)
|
|
|
|
},
|
2016-11-02 20:43:32 -04:00
|
|
|
Tags: map[string]string{"version": "1.25"},
|
2016-08-23 19:37:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
|
|
|
|
|
|
|
flags.BoolVarP(&opts.verbose, "verbose", "v", false, "Show detailed information on space usage")
|
2017-03-02 07:05:48 -05:00
|
|
|
flags.StringVar(&opts.format, "format", "", "Pretty-print images using a Go template")
|
2016-08-23 19:37:37 -04:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func runDiskUsage(dockerCli *command.DockerCli, opts diskUsageOptions) error {
|
2017-03-02 07:05:48 -05:00
|
|
|
if opts.verbose && len(opts.format) != 0 {
|
|
|
|
return errors.New("the verbose and the format options conflict")
|
|
|
|
}
|
|
|
|
|
2016-08-23 19:37:37 -04:00
|
|
|
du, err := dockerCli.Client().DiskUsage(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-03-02 07:05:48 -05:00
|
|
|
format := opts.format
|
|
|
|
if len(format) == 0 {
|
|
|
|
format = formatter.TableFormatKey
|
|
|
|
}
|
|
|
|
|
2016-08-23 19:37:37 -04:00
|
|
|
duCtx := formatter.DiskUsageContext{
|
|
|
|
Context: formatter.Context{
|
|
|
|
Output: dockerCli.Out(),
|
2017-03-02 07:05:48 -05:00
|
|
|
Format: formatter.NewDiskUsageFormat(format),
|
2016-08-23 19:37:37 -04:00
|
|
|
},
|
|
|
|
LayersSize: du.LayersSize,
|
|
|
|
Images: du.Images,
|
|
|
|
Containers: du.Containers,
|
|
|
|
Volumes: du.Volumes,
|
|
|
|
Verbose: opts.verbose,
|
|
|
|
}
|
|
|
|
|
2017-03-02 07:05:48 -05:00
|
|
|
return duCtx.Write()
|
2016-08-23 19:37:37 -04:00
|
|
|
}
|