2016-09-08 13:11:39 -04:00
|
|
|
package image
|
|
|
|
|
|
|
|
import (
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2024-08-08 06:44:24 -04:00
|
|
|
"github.com/containerd/platforms"
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
2024-07-03 11:42:29 -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"
|
2024-09-12 13:18:46 -04:00
|
|
|
"github.com/docker/docker/api/types/image"
|
2024-08-08 06:44:24 -04:00
|
|
|
"github.com/pkg/errors"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type historyOptions struct {
|
2024-08-08 06:44:24 -04:00
|
|
|
image string
|
|
|
|
platform string
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
human bool
|
|
|
|
quiet bool
|
|
|
|
noTrunc bool
|
2017-02-12 14:22:01 -05:00
|
|
|
format string
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewHistoryCommand creates a new `docker history` command
|
2017-03-30 20:21:14 -04:00
|
|
|
func NewHistoryCommand(dockerCli command.Cli) *cobra.Command {
|
2016-09-08 13:11:39 -04:00
|
|
|
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]
|
2023-09-09 18:27:44 -04:00
|
|
|
return runHistory(cmd.Context(), dockerCli, opts)
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
2024-07-03 11:42:29 -04:00
|
|
|
ValidArgsFunction: completion.ImageNames(dockerCli),
|
cli: use custom annotation for aliases
Cobra allows for aliases to be defined for a command, but only allows these
to be defined at the same level (for example, `docker image ls` as alias for
`docker image list`). Our CLI has some commands that are available both as a
top-level shorthand as well as `docker <object> <verb>` subcommands. For example,
`docker ps` is a shorthand for `docker container ps` / `docker container ls`.
This patch introduces a custom "aliases" annotation that can be used to print
all available aliases for a command. While this requires these aliases to be
defined manually, in practice the list of aliases rarely changes, so maintenance
should be minimal.
As a convention, we could consider the first command in this list to be the
canonical command, so that we can use this information to add redirects in
our documentation in future.
Before this patch:
docker images --help
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Options:
-a, --all Show all images (default hides intermediate images)
...
With this patch:
docker images --help
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Aliases:
docker image ls, docker image list, docker images
Options:
-a, --all Show all images (default hides intermediate images)
...
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-06-28 04:52:25 -04:00
|
|
|
Annotations: map[string]string{
|
|
|
|
"aliases": "docker image history, docker history",
|
|
|
|
},
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
|
|
|
|
|
|
|
flags.BoolVarP(&opts.human, "human", "H", true, "Print sizes and dates in human readable format")
|
2020-03-02 04:28:52 -05:00
|
|
|
flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Only show image IDs")
|
2016-09-08 13:11:39 -04:00
|
|
|
flags.BoolVar(&opts.noTrunc, "no-trunc", false, "Don't truncate output")
|
2021-03-09 18:45:56 -05:00
|
|
|
flags.StringVar(&opts.format, "format", "", flagsHelper.FormatHelp)
|
2024-08-08 06:44:24 -04:00
|
|
|
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"})
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2024-08-08 06:44:24 -04:00
|
|
|
_ = cmd.RegisterFlagCompletionFunc("platform", completion.Platforms)
|
2016-09-08 13:11:39 -04:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2023-09-09 18:27:44 -04:00
|
|
|
func runHistory(ctx context.Context, dockerCli command.Cli, opts historyOptions) error {
|
2024-08-08 06:44:24 -04:00
|
|
|
var options image.HistoryOptions
|
|
|
|
if opts.platform != "" {
|
|
|
|
p, err := platforms.Parse(opts.platform)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "invalid platform")
|
|
|
|
}
|
|
|
|
options.Platform = &p
|
|
|
|
}
|
|
|
|
|
|
|
|
history, err := dockerCli.Client().ImageHistory(ctx, opts.image, options)
|
2016-09-08 13:11:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-02-12 14:22:01 -05:00
|
|
|
format := opts.format
|
|
|
|
if len(format) == 0 {
|
|
|
|
format = formatter.TableFormatKey
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
2017-02-12 14:22:01 -05:00
|
|
|
historyCtx := formatter.Context{
|
|
|
|
Output: dockerCli.Out(),
|
2018-10-23 11:05:44 -04:00
|
|
|
Format: NewHistoryFormat(format, opts.quiet, opts.human),
|
2017-02-12 14:22:01 -05:00
|
|
|
Trunc: !opts.noTrunc,
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2018-10-23 11:05:44 -04:00
|
|
|
return HistoryWrite(historyCtx, opts.human, history)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|