2016-08-23 19:37:37 -04:00
|
|
|
package system
|
|
|
|
|
|
|
|
import (
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2017-03-02 07:05:48 -05:00
|
|
|
"errors"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
|
|
|
"github.com/docker/cli/cli/command/formatter"
|
2016-08-23 19:37:37 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type diskUsageOptions struct {
|
|
|
|
verbose bool
|
2017-03-02 07:05:48 -05:00
|
|
|
format string
|
2016-08-23 19:37:37 -04:00
|
|
|
}
|
|
|
|
|
2017-07-18 09:26:45 -04:00
|
|
|
// newDiskUsageCommand creates a new cobra.Command for `docker df`
|
2017-10-11 12:18:27 -04:00
|
|
|
func newDiskUsageCommand(dockerCli command.Cli) *cobra.Command {
|
2016-08-23 19:37:37 -04:00
|
|
|
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)
|
|
|
|
},
|
2017-10-25 12:59:32 -04:00
|
|
|
Annotations: 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
|
|
|
|
}
|
|
|
|
|
2017-10-11 12:18:27 -04:00
|
|
|
func runDiskUsage(dockerCli command.Cli, 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
|
|
|
},
|
2017-05-15 17:14:31 -04:00
|
|
|
LayersSize: du.LayersSize,
|
|
|
|
BuilderSize: du.BuilderSize,
|
2018-05-18 14:51:35 -04:00
|
|
|
BuildCache: du.BuildCache,
|
2017-05-15 17:14:31 -04:00
|
|
|
Images: du.Images,
|
|
|
|
Containers: du.Containers,
|
|
|
|
Volumes: du.Volumes,
|
|
|
|
Verbose: opts.verbose,
|
2016-08-23 19:37:37 -04:00
|
|
|
}
|
|
|
|
|
2017-03-02 07:05:48 -05:00
|
|
|
return duCtx.Write()
|
2016-08-23 19:37:37 -04:00
|
|
|
}
|