github.1git.de/docker/cli@v26.1.3+incompatible/cli/command/container/formatter_diff.go (about)

     1  package container
     2  
     3  import (
     4  	"github.com/docker/cli/cli/command/formatter"
     5  	"github.com/docker/docker/api/types/container"
     6  )
     7  
     8  const (
     9  	defaultDiffTableFormat = "table {{.Type}}\t{{.Path}}"
    10  
    11  	changeTypeHeader = "CHANGE TYPE"
    12  	pathHeader       = "PATH"
    13  )
    14  
    15  // NewDiffFormat returns a format for use with a diff Context
    16  func NewDiffFormat(source string) formatter.Format {
    17  	if source == formatter.TableFormatKey {
    18  		return defaultDiffTableFormat
    19  	}
    20  	return formatter.Format(source)
    21  }
    22  
    23  // DiffFormatWrite writes formatted diff using the Context
    24  func DiffFormatWrite(ctx formatter.Context, changes []container.FilesystemChange) error {
    25  	render := func(format func(subContext formatter.SubContext) error) error {
    26  		for _, change := range changes {
    27  			if err := format(&diffContext{c: change}); err != nil {
    28  				return err
    29  			}
    30  		}
    31  		return nil
    32  	}
    33  	return ctx.Write(newDiffContext(), render)
    34  }
    35  
    36  type diffContext struct {
    37  	formatter.HeaderContext
    38  	c container.FilesystemChange
    39  }
    40  
    41  func newDiffContext() *diffContext {
    42  	diffCtx := diffContext{}
    43  	diffCtx.Header = formatter.SubHeaderContext{
    44  		"Type": changeTypeHeader,
    45  		"Path": pathHeader,
    46  	}
    47  	return &diffCtx
    48  }
    49  
    50  func (d *diffContext) MarshalJSON() ([]byte, error) {
    51  	return formatter.MarshalJSON(d)
    52  }
    53  
    54  func (d *diffContext) Type() string {
    55  	return d.c.Kind.String()
    56  }
    57  
    58  func (d *diffContext) Path() string {
    59  	return d.c.Path
    60  }