2016-09-08 13:11:39 -04:00
|
|
|
package image
|
|
|
|
|
|
|
|
import (
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
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
|
|
|
|
func NewHistoryCommand(dockerCli *command.DockerCli) *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(dockerCli, opts)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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 numeric IDs")
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func runHistory(dockerCli *command.DockerCli, opts historyOptions) error {
|
|
|
|
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(),
|
|
|
|
Format: formatter.NewHistoryFormat(format, opts.quiet, opts.human),
|
|
|
|
Trunc: !opts.noTrunc,
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2017-02-12 14:22:01 -05:00
|
|
|
return formatter.HistoryWrite(historyCtx, opts.human, history)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|