2018-10-23 11:05:44 -04:00
|
|
|
package container
|
2017-03-06 15:45:12 -05:00
|
|
|
|
|
|
|
import (
|
2018-10-23 11:05:44 -04:00
|
|
|
"github.com/docker/cli/cli/command/formatter"
|
2017-03-06 15:45:12 -05:00
|
|
|
"github.com/docker/docker/api/types/container"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultDiffTableFormat = "table {{.Type}}\t{{.Path}}"
|
|
|
|
|
|
|
|
changeTypeHeader = "CHANGE TYPE"
|
|
|
|
pathHeader = "PATH"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewDiffFormat returns a format for use with a diff Context
|
2018-10-23 11:05:44 -04:00
|
|
|
func NewDiffFormat(source string) formatter.Format {
|
2023-11-20 07:54:53 -05:00
|
|
|
if source == formatter.TableFormatKey {
|
2017-03-06 15:45:12 -05:00
|
|
|
return defaultDiffTableFormat
|
|
|
|
}
|
2018-10-23 11:05:44 -04:00
|
|
|
return formatter.Format(source)
|
2017-03-06 15:45:12 -05:00
|
|
|
}
|
|
|
|
|
2018-10-23 11:05:44 -04:00
|
|
|
// DiffFormatWrite writes formatted diff using the Context
|
2023-04-28 21:51:11 -04:00
|
|
|
func DiffFormatWrite(ctx formatter.Context, changes []container.FilesystemChange) error {
|
2018-10-23 11:05:44 -04:00
|
|
|
render := func(format func(subContext formatter.SubContext) error) error {
|
2017-03-06 15:45:12 -05:00
|
|
|
for _, change := range changes {
|
|
|
|
if err := format(&diffContext{c: change}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return ctx.Write(newDiffContext(), render)
|
|
|
|
}
|
|
|
|
|
|
|
|
type diffContext struct {
|
2018-10-23 11:05:44 -04:00
|
|
|
formatter.HeaderContext
|
2023-04-28 21:51:11 -04:00
|
|
|
c container.FilesystemChange
|
2017-03-06 15:45:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func newDiffContext() *diffContext {
|
|
|
|
diffCtx := diffContext{}
|
2018-10-23 11:05:44 -04:00
|
|
|
diffCtx.Header = formatter.SubHeaderContext{
|
2017-03-06 15:45:12 -05:00
|
|
|
"Type": changeTypeHeader,
|
|
|
|
"Path": pathHeader,
|
|
|
|
}
|
|
|
|
return &diffCtx
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *diffContext) MarshalJSON() ([]byte, error) {
|
2018-10-23 11:05:44 -04:00
|
|
|
return formatter.MarshalJSON(d)
|
2017-03-06 15:45:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *diffContext) Type() string {
|
2023-04-28 21:51:11 -04:00
|
|
|
return d.c.Kind.String()
|
2017-03-06 15:45:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *diffContext) Path() string {
|
|
|
|
return d.c.Path
|
|
|
|
}
|