github.com/ali-iotechsys/cli@v20.10.0+incompatible/cli/command/image/history.go (about)

     1  package image
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/docker/cli/cli"
     7  	"github.com/docker/cli/cli/command"
     8  	"github.com/docker/cli/cli/command/formatter"
     9  	"github.com/spf13/cobra"
    10  )
    11  
    12  type historyOptions struct {
    13  	image string
    14  
    15  	human   bool
    16  	quiet   bool
    17  	noTrunc bool
    18  	format  string
    19  }
    20  
    21  // NewHistoryCommand creates a new `docker history` command
    22  func NewHistoryCommand(dockerCli command.Cli) *cobra.Command {
    23  	var opts historyOptions
    24  
    25  	cmd := &cobra.Command{
    26  		Use:   "history [OPTIONS] IMAGE",
    27  		Short: "Show the history of an image",
    28  		Args:  cli.ExactArgs(1),
    29  		RunE: func(cmd *cobra.Command, args []string) error {
    30  			opts.image = args[0]
    31  			return runHistory(dockerCli, opts)
    32  		},
    33  	}
    34  
    35  	flags := cmd.Flags()
    36  
    37  	flags.BoolVarP(&opts.human, "human", "H", true, "Print sizes and dates in human readable format")
    38  	flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Only show image IDs")
    39  	flags.BoolVar(&opts.noTrunc, "no-trunc", false, "Don't truncate output")
    40  	flags.StringVar(&opts.format, "format", "", "Pretty-print images using a Go template")
    41  
    42  	return cmd
    43  }
    44  
    45  func runHistory(dockerCli command.Cli, opts historyOptions) error {
    46  	ctx := context.Background()
    47  
    48  	history, err := dockerCli.Client().ImageHistory(ctx, opts.image)
    49  	if err != nil {
    50  		return err
    51  	}
    52  
    53  	format := opts.format
    54  	if len(format) == 0 {
    55  		format = formatter.TableFormatKey
    56  	}
    57  
    58  	historyCtx := formatter.Context{
    59  		Output: dockerCli.Out(),
    60  		Format: NewHistoryFormat(format, opts.quiet, opts.human),
    61  		Trunc:  !opts.noTrunc,
    62  	}
    63  	return HistoryWrite(historyCtx, opts.human, history)
    64  }