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
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
2022-03-30 09:27:25 -04:00
|
|
|
"github.com/docker/cli/cli/command/completion"
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli/command/formatter"
|
2021-03-09 18:45:56 -05:00
|
|
|
flagsHelper "github.com/docker/cli/cli/flags"
|
2021-08-09 13:12:30 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
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 {
|
2023-09-09 18:27:44 -04:00
|
|
|
return runDiskUsage(cmd.Context(), dockerCli, opts)
|
2016-08-23 19:37:37 -04:00
|
|
|
},
|
2022-03-30 09:27:25 -04:00
|
|
|
Annotations: map[string]string{"version": "1.25"},
|
|
|
|
ValidArgsFunction: completion.NoComplete,
|
2016-08-23 19:37:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
|
|
|
|
|
|
|
flags.BoolVarP(&opts.verbose, "verbose", "v", false, "Show detailed information on space usage")
|
2021-03-09 18:45:56 -05:00
|
|
|
flags.StringVar(&opts.format, "format", "", flagsHelper.FormatHelp)
|
2016-08-23 19:37:37 -04:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2023-09-09 18:27:44 -04:00
|
|
|
func runDiskUsage(ctx context.Context, dockerCli command.Cli, opts diskUsageOptions) error {
|
2021-08-09 13:12:30 -04:00
|
|
|
// TODO expose types.DiskUsageOptions.Types as flag on the command-line and/or as separate commands (docker container df / docker container usage)
|
2023-09-09 18:27:44 -04:00
|
|
|
du, err := dockerCli.Client().DiskUsage(ctx, types.DiskUsageOptions{})
|
2016-08-23 19:37:37 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-03-02 07:05:48 -05:00
|
|
|
format := opts.format
|
|
|
|
if len(format) == 0 {
|
|
|
|
format = formatter.TableFormatKey
|
|
|
|
}
|
|
|
|
|
2018-08-31 18:14:36 -04:00
|
|
|
var bsz int64
|
|
|
|
for _, bc := range du.BuildCache {
|
|
|
|
if !bc.Shared {
|
|
|
|
bsz += bc.Size
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-23 19:37:37 -04:00
|
|
|
duCtx := formatter.DiskUsageContext{
|
|
|
|
Context: formatter.Context{
|
|
|
|
Output: dockerCli.Out(),
|
2018-09-13 13:47:40 -04:00
|
|
|
Format: formatter.NewDiskUsageFormat(format, opts.verbose),
|
2016-08-23 19:37:37 -04:00
|
|
|
},
|
2017-05-15 17:14:31 -04:00
|
|
|
LayersSize: du.LayersSize,
|
2018-08-31 18:14:36 -04:00
|
|
|
BuilderSize: bsz,
|
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
|
|
|
}
|