Selaa lähdekoodia

Merge pull request #28340 from cpuguy83/28337_fix_template_opts_ps

Fix issue with missing fields for `ps` template
Victor Vieux 8 vuotta sitten
vanhempi
commit
8930aa0a86
2 muutettua tiedostoa jossa 32 lisäystä ja 7 poistoa
  1. 6 7
      cli/command/container/list.go
  2. 26 0
      cli/command/formatter/container_test.go

+ 6 - 7
cli/command/container/list.go

@@ -62,6 +62,12 @@ func newListCommand(dockerCli *command.DockerCli) *cobra.Command {
 type preProcessor struct {
 	types.Container
 	opts *types.ContainerListOptions
+
+	// Fields that need to exist so the template doesn't error out
+	// These are needed since they are available on the final object but are not
+	// fields in types.Container
+	// TODO(cpuguy83): this seems rather broken
+	Networks, CreatedAt, RunningFor bool
 }
 
 // Size sets the size option when called by a template execution.
@@ -70,13 +76,6 @@ func (p *preProcessor) Size() bool {
 	return true
 }
 
-// Networks does nothing but return true.
-// It is needed to avoid the template check to fail as this field
-// doesn't exist in `types.Container`
-func (p *preProcessor) Networks() bool {
-	return true
-}
-
 func buildContainerListOptions(opts *psOptions) (*types.ContainerListOptions, error) {
 	options := &types.ContainerListOptions{
 		All:     opts.all,

+ 26 - 0
cli/command/formatter/container_test.go

@@ -370,3 +370,29 @@ func TestContainerContextWriteJSONField(t *testing.T) {
 		assert.Equal(t, s, containers[i].ID)
 	}
 }
+
+func TestContainerBackCompat(t *testing.T) {
+	containers := []types.Container{types.Container{ID: "brewhaha"}}
+	cases := []string{
+		"ID",
+		"Names",
+		"Image",
+		"Command",
+		"CreatedAt",
+		"RunningFor",
+		"Ports",
+		"Status",
+		"Size",
+		"Labels",
+		"Mounts",
+	}
+	buf := bytes.NewBuffer(nil)
+	for _, c := range cases {
+		ctx := Context{Format: Format(fmt.Sprintf("{{ .%s }}", c)), Output: buf}
+		if err := ContainerWrite(ctx, containers); err != nil {
+			t.Log("could not render template for field '%s': %v", c, err)
+			t.Fail()
+		}
+		buf.Reset()
+	}
+}