forked from moby/moby
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request moby#31599 from ripcurld0/diff_cmd_format
Use formatter in docker diff
- Loading branch information
Showing
3 changed files
with
136 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package formatter | ||
|
||
import ( | ||
"github.com/docker/docker/api/types/container" | ||
"github.com/docker/docker/pkg/archive" | ||
) | ||
|
||
const ( | ||
defaultDiffTableFormat = "table {{.Type}}\t{{.Path}}" | ||
|
||
changeTypeHeader = "CHANGE TYPE" | ||
pathHeader = "PATH" | ||
) | ||
|
||
// NewDiffFormat returns a format for use with a diff Context | ||
func NewDiffFormat(source string) Format { | ||
switch source { | ||
case TableFormatKey: | ||
return defaultDiffTableFormat | ||
} | ||
return Format(source) | ||
} | ||
|
||
// DiffWrite writes formatted diff using the Context | ||
func DiffWrite(ctx Context, changes []container.ContainerChangeResponseItem) error { | ||
|
||
render := func(format func(subContext subContext) error) error { | ||
for _, change := range changes { | ||
if err := format(&diffContext{c: change}); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
return ctx.Write(newDiffContext(), render) | ||
} | ||
|
||
type diffContext struct { | ||
HeaderContext | ||
c container.ContainerChangeResponseItem | ||
} | ||
|
||
func newDiffContext() *diffContext { | ||
diffCtx := diffContext{} | ||
diffCtx.header = map[string]string{ | ||
"Type": changeTypeHeader, | ||
"Path": pathHeader, | ||
} | ||
return &diffCtx | ||
} | ||
|
||
func (d *diffContext) MarshalJSON() ([]byte, error) { | ||
return marshalJSON(d) | ||
} | ||
|
||
func (d *diffContext) Type() string { | ||
var kind string | ||
switch d.c.Kind { | ||
case archive.ChangeModify: | ||
kind = "C" | ||
case archive.ChangeAdd: | ||
kind = "A" | ||
case archive.ChangeDelete: | ||
kind = "D" | ||
} | ||
return kind | ||
|
||
} | ||
|
||
func (d *diffContext) Path() string { | ||
return d.c.Path | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package formatter | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/docker/docker/api/types/container" | ||
"github.com/docker/docker/pkg/archive" | ||
"github.com/docker/docker/pkg/testutil/assert" | ||
) | ||
|
||
func TestDiffContextFormatWrite(t *testing.T) { | ||
// Check default output format (verbose and non-verbose mode) for table headers | ||
cases := []struct { | ||
context Context | ||
expected string | ||
}{ | ||
{ | ||
Context{Format: NewDiffFormat("table")}, | ||
`CHANGE TYPE PATH | ||
C /var/log/app.log | ||
A /usr/app/app.js | ||
D /usr/app/old_app.js | ||
`, | ||
}, | ||
{ | ||
Context{Format: NewDiffFormat("table {{.Path}}")}, | ||
`PATH | ||
/var/log/app.log | ||
/usr/app/app.js | ||
/usr/app/old_app.js | ||
`, | ||
}, | ||
{ | ||
Context{Format: NewDiffFormat("{{.Type}}: {{.Path}}")}, | ||
`C: /var/log/app.log | ||
A: /usr/app/app.js | ||
D: /usr/app/old_app.js | ||
`, | ||
}, | ||
} | ||
|
||
diffs := []container.ContainerChangeResponseItem{ | ||
{archive.ChangeModify, "/var/log/app.log"}, | ||
{archive.ChangeAdd, "/usr/app/app.js"}, | ||
{archive.ChangeDelete, "/usr/app/old_app.js"}, | ||
} | ||
|
||
for _, testcase := range cases { | ||
out := bytes.NewBufferString("") | ||
testcase.context.Output = out | ||
err := DiffWrite(testcase.context, diffs) | ||
if err != nil { | ||
assert.Error(t, err, testcase.expected) | ||
} else { | ||
assert.Equal(t, out.String(), testcase.expected) | ||
} | ||
} | ||
} |