cli/command/image: deprecate NewHistoryFormat, HistoryWrite

It's part of the presentation logic of the cli, and only used internally.
We can consider providing utilities for these, but better as part of
separate packages.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2025-08-21 14:01:22 +02:00
parent e3903a1ac8
commit 15cf4fa912
3 changed files with 22 additions and 8 deletions

View File

@ -20,7 +20,14 @@ const (
)
// NewHistoryFormat returns a format for rendering an HistoryContext
//
// Deprecated: this function was only used internally and will be removed in the next release.
func NewHistoryFormat(source string, quiet bool, human bool) formatter.Format {
return newHistoryFormat(source, quiet, human)
}
// newHistoryFormat returns a format for rendering a historyContext.
func newHistoryFormat(source string, quiet bool, human bool) formatter.Format {
if source == formatter.TableFormatKey {
switch {
case quiet:
@ -36,10 +43,17 @@ func NewHistoryFormat(source string, quiet bool, human bool) formatter.Format {
}
// HistoryWrite writes the context
func HistoryWrite(ctx formatter.Context, human bool, histories []image.HistoryResponseItem) error {
//
// Deprecated: this function was only used internally and will be removed in the next release.
func HistoryWrite(fmtCtx formatter.Context, human bool, histories []image.HistoryResponseItem) error {
return historyWrite(fmtCtx, human, histories)
}
// historyWrite writes the context
func historyWrite(fmtCtx formatter.Context, human bool, histories []image.HistoryResponseItem) error {
render := func(format func(subContext formatter.SubContext) error) error {
for _, history := range histories {
historyCtx := &historyContext{trunc: ctx.Trunc, h: history, human: human}
historyCtx := &historyContext{trunc: fmtCtx.Trunc, h: history, human: human}
if err := format(historyCtx); err != nil {
return err
}
@ -55,7 +69,7 @@ func HistoryWrite(ctx formatter.Context, human bool, histories []image.HistoryRe
"Size": formatter.SizeHeader,
"Comment": commentHeader,
}
return ctx.Write(historyCtx, render)
return fmtCtx.Write(historyCtx, render)
}
type historyContext struct {

View File

@ -237,7 +237,7 @@ imageID6 17 years ago /bin/bash echo 183MB
}{
{
formatter.Context{
Format: NewHistoryFormat("table", false, true),
Format: newHistoryFormat("table", false, true),
Trunc: true,
Output: out,
},
@ -245,7 +245,7 @@ imageID6 17 years ago /bin/bash echo 183MB
},
{
formatter.Context{
Format: NewHistoryFormat("table", false, true),
Format: newHistoryFormat("table", false, true),
Trunc: false,
Output: out,
},
@ -255,7 +255,7 @@ imageID6 17 years ago /bin/bash echo 183MB
for _, tc := range cases {
t.Run(string(tc.context.Format), func(t *testing.T) {
err := HistoryWrite(tc.context, true, histories)
err := historyWrite(tc.context, true, histories)
assert.NilError(t, err)
assert.Equal(t, out.String(), tc.expected)
// Clean buffer

View File

@ -77,8 +77,8 @@ func runHistory(ctx context.Context, dockerCli command.Cli, opts historyOptions)
historyCtx := formatter.Context{
Output: dockerCli.Out(),
Format: NewHistoryFormat(format, opts.quiet, opts.human),
Format: newHistoryFormat(format, opts.quiet, opts.human),
Trunc: !opts.noTrunc,
}
return HistoryWrite(historyCtx, opts.human, history)
return historyWrite(historyCtx, opts.human, history)
}