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
|
|
|
|
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-09-08 13:11:39 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type historyOptions struct {
|
|
|
|
image string
|
|
|
|
|
|
|
|
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]
|
|
|
|
return runHistory(dockerCli, opts)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
2017-02-12 14:22:01 -05:00
|
|
|
flags.StringVar(&opts.format, "format", "", "Pretty-print images using a Go template")
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2017-03-30 20:21:14 -04:00
|
|
|
func runHistory(dockerCli command.Cli, opts historyOptions) error {
|
2016-09-08 13:11:39 -04:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
history, err := dockerCli.Client().ImageHistory(ctx, opts.image)
|
|
|
|
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
|
|
|
}
|