Files
docker-cli/cli/command/image/history.go
Alano Terblanche e66a1456d3 Unexport image commands
This patch deprecates exported image commands and moves the
implementation details to an unexported function.

Commands that are affected include:

- image.NewBuildCommand
- image.NewPullCommand
- image.NewPushCommand
- image.NewImagesCommand
- image.NewImageCommand
- image.NewHistoryCommand
- image.NewImportCommand
- image.NewLoadCommand
- image.NewRemoveCommand
- image.NewSaveCommand
- image.NewTagCommand
- image.NewPruneCommand

Signed-off-by: Alano Terblanche <18033717+Benehiko@users.noreply.github.com>
2025-08-19 14:25:15 +02:00

92 lines
2.6 KiB
Go

package image
import (
"context"
"github.com/containerd/platforms"
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/completion"
"github.com/docker/cli/cli/command/formatter"
flagsHelper "github.com/docker/cli/cli/flags"
"github.com/moby/moby/client"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
type historyOptions struct {
image string
platform string
human bool
quiet bool
noTrunc bool
format string
}
// NewHistoryCommand creates a new `docker history` command
//
// Deprecated: Do not import commands directly. They will be removed in a future release.
func NewHistoryCommand(dockerCLI command.Cli) *cobra.Command {
return newHistoryCommand(dockerCLI)
}
// newHistoryCommand creates a new `docker history` command
func newHistoryCommand(dockerCLI command.Cli) *cobra.Command {
var opts historyOptions
cmd := &cobra.Command{
Use: "history [OPTIONS] IMAGE",
Short: "Show the history of an image",
Args: cli.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
opts.image = args[0]
return runHistory(cmd.Context(), dockerCLI, opts)
},
ValidArgsFunction: completion.ImageNames(dockerCLI, 1),
Annotations: map[string]string{
"aliases": "docker image history, docker history",
},
}
flags := cmd.Flags()
flags.BoolVarP(&opts.human, "human", "H", true, "Print sizes and dates in human readable format")
flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Only show image IDs")
flags.BoolVar(&opts.noTrunc, "no-trunc", false, "Don't truncate output")
flags.StringVar(&opts.format, "format", "", flagsHelper.FormatHelp)
flags.StringVar(&opts.platform, "platform", "", `Show history for the given platform. Formatted as "os[/arch[/variant]]" (e.g., "linux/amd64")`)
_ = flags.SetAnnotation("platform", "version", []string{"1.48"})
_ = cmd.RegisterFlagCompletionFunc("platform", completion.Platforms)
return cmd
}
func runHistory(ctx context.Context, dockerCli command.Cli, opts historyOptions) error {
var options []client.ImageHistoryOption
if opts.platform != "" {
p, err := platforms.Parse(opts.platform)
if err != nil {
return errors.Wrap(err, "invalid platform")
}
options = append(options, client.ImageHistoryWithPlatform(p))
}
history, err := dockerCli.Client().ImageHistory(ctx, opts.image, options...)
if err != nil {
return err
}
format := opts.format
if len(format) == 0 {
format = formatter.TableFormatKey
}
historyCtx := formatter.Context{
Output: dockerCli.Out(),
Format: NewHistoryFormat(format, opts.quiet, opts.human),
Trunc: !opts.noTrunc,
}
return HistoryWrite(historyCtx, opts.human, history)
}