Fail fail when the ps format template is invalid.
Fixes error continuing execution when the parsing fails.
Signed-off-by: David Calavera <david.calavera@gmail.com>
(cherry picked from commit 3d3db0d4af
)
This commit is contained in:
parent
d3bbaa70cd
commit
e15f6fca3f
2 changed files with 22 additions and 5 deletions
|
@ -170,9 +170,11 @@ func customFormat(ctx Context, containers []types.Container) {
|
||||||
format += "\t{{.Size}}"
|
format += "\t{{.Size}}"
|
||||||
}
|
}
|
||||||
|
|
||||||
tmpl, err := template.New("ps template").Parse(format)
|
tmpl, err := template.New("").Parse(format)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
buffer.WriteString(fmt.Sprintf("Invalid `docker ps` format: %v\n", err))
|
buffer.WriteString(fmt.Sprintf("Template parsing error: %v\n", err))
|
||||||
|
buffer.WriteTo(ctx.Output)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, container := range containers {
|
for _, container := range containers {
|
||||||
|
@ -181,8 +183,9 @@ func customFormat(ctx Context, containers []types.Container) {
|
||||||
c: container,
|
c: container,
|
||||||
}
|
}
|
||||||
if err := tmpl.Execute(buffer, containerCtx); err != nil {
|
if err := tmpl.Execute(buffer, containerCtx); err != nil {
|
||||||
buffer = bytes.NewBufferString(fmt.Sprintf("Invalid `docker ps` format: %v\n", err))
|
buffer = bytes.NewBufferString(fmt.Sprintf("Template parsing error: %v\n", err))
|
||||||
break
|
buffer.WriteTo(ctx.Output)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
if table && len(header) == 0 {
|
if table && len(header) == 0 {
|
||||||
header = containerCtx.fullHeader()
|
header = containerCtx.fullHeader()
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package ps
|
package ps
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -10,7 +11,7 @@ import (
|
||||||
"github.com/docker/docker/pkg/stringid"
|
"github.com/docker/docker/pkg/stringid"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestContainerContextID(t *testing.T) {
|
func TestContainerPsContext(t *testing.T) {
|
||||||
containerId := stringid.GenerateRandomID()
|
containerId := stringid.GenerateRandomID()
|
||||||
unix := time.Now().Unix()
|
unix := time.Now().Unix()
|
||||||
|
|
||||||
|
@ -86,3 +87,16 @@ func TestContainerContextID(t *testing.T) {
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestContainerPsFormatError(t *testing.T) {
|
||||||
|
out := bytes.NewBufferString("")
|
||||||
|
ctx := Context{
|
||||||
|
Format: "{{InvalidFunction}}",
|
||||||
|
Output: out,
|
||||||
|
}
|
||||||
|
|
||||||
|
customFormat(ctx, make([]types.Container, 0))
|
||||||
|
if out.String() != "Template parsing error: template: :1: function \"InvalidFunction\" not defined\n" {
|
||||||
|
t.Fatalf("Expected format error, got `%v`\n", out.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue