Browse code

Re-implement --before and --since as options for --filter

* This commit will mark --before and --since as deprecated, but leave their behavior
unchanged until they are removed, then re-implement them as options for --filter.

* And update the related docs.

* Update the integration tests.

Fixes issue #17716

Signed-off-by: Wen Cheng Ma <wenchma@cn.ibm.com>

Wen Cheng Ma authored on 2015/11/05 16:08:00
Showing 6 changed files
... ...
@@ -28,10 +28,10 @@ func (cli *DockerCli) CmdPs(args ...string) error {
28 28
 		size     = cmd.Bool([]string{"s", "-size"}, false, "Display total file sizes")
29 29
 		all      = cmd.Bool([]string{"a", "-all"}, false, "Show all containers (default shows just running)")
30 30
 		noTrunc  = cmd.Bool([]string{"-no-trunc"}, false, "Don't truncate output")
31
-		nLatest  = cmd.Bool([]string{"l", "-latest"}, false, "Show the latest created container, include non-running")
32
-		since    = cmd.String([]string{"-since"}, "", "Show created since Id or Name, include non-running")
33
-		before   = cmd.String([]string{"-before"}, "", "Show only container created before Id or Name")
34
-		last     = cmd.Int([]string{"n"}, -1, "Show n last created containers, include non-running")
31
+		nLatest  = cmd.Bool([]string{"l", "-latest"}, false, "Show the latest created container (includes all states)")
32
+		since    = cmd.String([]string{"#-since"}, "", "Show containers created since Id or Name (includes all states)")
33
+		before   = cmd.String([]string{"#-before"}, "", "Only show containers created before Id or Name")
34
+		last     = cmd.Int([]string{"n"}, -1, "Show n last created containers (includes all states)")
35 35
 		format   = cmd.String([]string{"-format"}, "", "Pretty-print containers using a Go template")
36 36
 		flFilter = opts.NewListOpts(nil)
37 37
 	)
... ...
@@ -71,10 +71,12 @@ type listContext struct {
71 71
 	filters filters.Args
72 72
 	// exitAllowed is a list of exit codes allowed to filter with
73 73
 	exitAllowed []int
74
-	// beforeContainer is a filter to ignore containers that appear before the one given
75
-	beforeContainer *Container
76
-	// sinceContainer is a filter to stop the filtering when the iterator arrive to the given container
77
-	sinceContainer *Container
74
+	// beforeFilter is a filter to ignore containers that appear before the one given
75
+	// this is used for --filter=before= and --before=, the latter is deprecated.
76
+	beforeFilter *Container
77
+	// sinceFilter is a filter to stop the filtering when the iterator arrive to the given container
78
+	// this is used for --filter=since= and --since=, the latter is deprecated.
79
+	sinceFilter *Container
78 80
 	// ContainersConfig is the filters set by the user
79 81
 	*ContainersConfig
80 82
 }
... ...
@@ -155,6 +157,25 @@ func (daemon *Daemon) foldFilter(config *ContainersConfig) (*listContext, error)
155 155
 		}
156 156
 	}
157 157
 
158
+	var beforeContFilter, sinceContFilter *Container
159
+	if i, ok := psFilters["before"]; ok {
160
+		for _, value := range i {
161
+			beforeContFilter, err = daemon.Get(value)
162
+			if err != nil {
163
+				return nil, err
164
+			}
165
+		}
166
+	}
167
+
168
+	if i, ok := psFilters["since"]; ok {
169
+		for _, value := range i {
170
+			sinceContFilter, err = daemon.Get(value)
171
+			if err != nil {
172
+				return nil, err
173
+			}
174
+		}
175
+	}
176
+
158 177
 	imagesFilter := map[string]bool{}
159 178
 	var ancestorFilter bool
160 179
 	if ancestors, ok := psFilters["ancestor"]; ok {
... ...
@@ -183,16 +204,15 @@ func (daemon *Daemon) foldFilter(config *ContainersConfig) (*listContext, error)
183 183
 		return nil
184 184
 	}, 1)
185 185
 
186
-	var beforeCont, sinceCont *Container
187 186
 	if config.Before != "" {
188
-		beforeCont, err = daemon.Get(config.Before)
187
+		beforeContFilter, err = daemon.Get(config.Before)
189 188
 		if err != nil {
190 189
 			return nil, err
191 190
 		}
192 191
 	}
193 192
 
194 193
 	if config.Since != "" {
195
-		sinceCont, err = daemon.Get(config.Since)
194
+		sinceContFilter, err = daemon.Get(config.Since)
196 195
 		if err != nil {
197 196
 			return nil, err
198 197
 		}
... ...
@@ -204,8 +224,8 @@ func (daemon *Daemon) foldFilter(config *ContainersConfig) (*listContext, error)
204 204
 		names:            names,
205 205
 		images:           imagesFilter,
206 206
 		exitAllowed:      filtExited,
207
-		beforeContainer:  beforeCont,
208
-		sinceContainer:   sinceCont,
207
+		beforeFilter:     beforeContFilter,
208
+		sinceFilter:      sinceContFilter,
209 209
 		ContainersConfig: config,
210 210
 	}, nil
211 211
 }
... ...
@@ -214,7 +234,7 @@ func (daemon *Daemon) foldFilter(config *ContainersConfig) (*listContext, error)
214 214
 // It also decides if the iteration should be stopped or not.
215 215
 func includeContainerInList(container *Container, ctx *listContext) iterationAction {
216 216
 	// Do not include container if it's stopped and we're not filters
217
-	if !container.Running && !ctx.All && ctx.Limit <= 0 && ctx.beforeContainer == nil && ctx.sinceContainer == nil {
217
+	if !container.Running && !ctx.All && ctx.Limit <= 0 && ctx.beforeFilter == nil && ctx.sinceFilter == nil {
218 218
 		return excludeContainer
219 219
 	}
220 220
 
... ...
@@ -240,25 +260,25 @@ func includeContainerInList(container *Container, ctx *listContext) iterationAct
240 240
 
241 241
 	// Do not include container if it's in the list before the filter container.
242 242
 	// Set the filter container to nil to include the rest of containers after this one.
243
-	if ctx.beforeContainer != nil {
244
-		if container.ID == ctx.beforeContainer.ID {
245
-			ctx.beforeContainer = nil
243
+	if ctx.beforeFilter != nil {
244
+		if container.ID == ctx.beforeFilter.ID {
245
+			ctx.beforeFilter = nil
246 246
 		}
247 247
 		return excludeContainer
248 248
 	}
249 249
 
250
-	// Stop iteration when the index is over the limit
251
-	if ctx.Limit > 0 && ctx.idx == ctx.Limit {
252
-		return stopIteration
253
-	}
254
-
255 250
 	// Stop interation when the container arrives to the filter container
256
-	if ctx.sinceContainer != nil {
257
-		if container.ID == ctx.sinceContainer.ID {
251
+	if ctx.sinceFilter != nil {
252
+		if container.ID == ctx.sinceFilter.ID {
258 253
 			return stopIteration
259 254
 		}
260 255
 	}
261 256
 
257
+	// Stop iteration when the index is over the limit
258
+	if ctx.Limit > 0 && ctx.idx == ctx.Limit {
259
+		return stopIteration
260
+	}
261
+
262 262
 	// Do not include container if its exit code is not in the filter
263 263
 	if len(ctx.exitAllowed) > 0 {
264 264
 		shouldSkip := true
... ...
@@ -12,6 +12,15 @@ parent = "mn_use_docker"
12 12
 
13 13
 The following list of features are deprecated.
14 14
 
15
+### Docker ps 'before' and 'since' options
16
+
17
+**Deprecated In Release: [v1.10.0](https://github.com/docker/docker/releases/tag/v1.10.0)**
18
+
19
+**Target For Removal In Release: v1.12**
20
+
21
+The `docker ps --before` and `docker ps --since` options are deprecated.
22
+Use `docker ps --filter=before=...` and `docker ps --filter=since=...` instead.
23
+
15 24
 ### Command line short variant options
16 25
 **Deprecated In Release: v1.9**
17 26
 
... ...
@@ -15,16 +15,14 @@ parent = "smn_cli"
15 15
     List containers
16 16
 
17 17
       -a, --all=false       Show all containers (default shows just running)
18
-      --before=""           Show only container created before Id or Name
19 18
       -f, --filter=[]       Filter output based on conditions provided
20 19
       --format=[]           Pretty-print containers using a Go template
21 20
       --help=false          Print usage
22
-      -l, --latest=false    Show the latest created container, include non-running
23
-      -n=-1                 Show n last created containers, include non-running
21
+      -l, --latest=false    Show the latest created container (includes all states)
22
+      -n=-1                 Show n last created containers (includes all states)
24 23
       --no-trunc=false      Don't truncate output
25 24
       -q, --quiet=false     Only display numeric IDs
26 25
       -s, --size=false      Display total file sizes
27
-      --since=""            Show created since Id or Name, include non-running
28 26
 
29 27
 Running `docker ps --no-trunc` showing 2 linked containers.
30 28
 
... ...
@@ -58,52 +58,53 @@ func (s *DockerSuite) TestPsListContainersBase(c *check.C) {
58 58
 	out, _ = dockerCmd(c, "ps", "-n=2")
59 59
 	c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("LIMIT: Container list is not in the correct order: \n%s", out))
60 60
 
61
-	// since
62
-	out, _ = dockerCmd(c, "ps", "--since", firstID, "-a")
61
+	// filter since
62
+	out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-a")
63 63
 	expected = []string{fourthID, thirdID, secondID}
64 64
 	c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE & ALL: Container list is not in the correct order: \n%s", out))
65 65
 
66
-	out, _ = dockerCmd(c, "ps", "--since", firstID)
66
+	out, _ = dockerCmd(c, "ps", "-f", "since="+firstID)
67 67
 	c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE: Container list is not in the correct order: \n%s", out))
68 68
 
69
-	// before
70
-	out, _ = dockerCmd(c, "ps", "--before", thirdID, "-a")
69
+	// filter before
70
+	out, _ = dockerCmd(c, "ps", "-f", "before="+thirdID, "-a")
71 71
 	expected = []string{secondID, firstID}
72 72
 	c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("BEFORE & ALL: Container list is not in the correct order: \n%s", out))
73 73
 
74
-	out, _ = dockerCmd(c, "ps", "--before", thirdID)
74
+	out, _ = dockerCmd(c, "ps", "-f", "before="+thirdID)
75 75
 	c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("BEFORE: Container list is not in the correct order: \n%s", out))
76 76
 
77
-	// since & before
78
-	out, _ = dockerCmd(c, "ps", "--since", firstID, "--before", fourthID, "-a")
77
+	// filter since & before
78
+	out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-f", "before="+fourthID, "-a")
79 79
 	expected = []string{thirdID, secondID}
80 80
 	c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE, BEFORE & ALL: Container list is not in the correct order: \n%s", out))
81 81
 
82
-	out, _ = dockerCmd(c, "ps", "--since", firstID, "--before", fourthID)
82
+	out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-f", "before="+fourthID)
83 83
 	c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE, BEFORE: Container list is not in the correct order: \n%s", out))
84 84
 
85
-	// since & limit
86
-	out, _ = dockerCmd(c, "ps", "--since", firstID, "-n=2", "-a")
85
+	// filter since & limit
86
+	out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-n=2", "-a")
87 87
 	expected = []string{fourthID, thirdID}
88 88
 
89 89
 	c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE, LIMIT & ALL: Container list is not in the correct order: \n%s", out))
90 90
 
91
-	out, _ = dockerCmd(c, "ps", "--since", firstID, "-n=2")
91
+	out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-n=2")
92 92
 	c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE, LIMIT: Container list is not in the correct order: \n%s", out))
93 93
 
94
-	// before & limit
95
-	out, _ = dockerCmd(c, "ps", "--before", fourthID, "-n=1", "-a")
94
+	// filter before & limit
95
+	out, _ = dockerCmd(c, "ps", "-f", "before="+fourthID, "-n=1", "-a")
96 96
 	expected = []string{thirdID}
97 97
 	c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("BEFORE, LIMIT & ALL: Container list is not in the correct order: \n%s", out))
98 98
 
99
-	out, _ = dockerCmd(c, "ps", "--before", fourthID, "-n=1")
99
+	out, _ = dockerCmd(c, "ps", "-f", "before="+fourthID, "-n=1")
100 100
 	c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("BEFORE, LIMIT: Container list is not in the correct order: \n%s", out))
101 101
 
102
-	out, _ = dockerCmd(c, "ps", "--since", firstID, "--before", fourthID, "-n=1", "-a")
102
+	// filter since & filter before & limit
103
+	out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-f", "before="+fourthID, "-n=1", "-a")
103 104
 	expected = []string{thirdID}
104 105
 	c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE, BEFORE, LIMIT & ALL: Container list is not in the correct order: \n%s", out))
105 106
 
106
-	out, _ = dockerCmd(c, "ps", "--since", firstID, "--before", fourthID, "-n=1")
107
+	out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-f", "before="+fourthID, "-n=1")
107 108
 	c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE, BEFORE, LIMIT: Container list is not in the correct order: \n%s", out))
108 109
 
109 110
 }
... ...
@@ -7,7 +7,6 @@ docker-ps - List containers
7 7
 # SYNOPSIS
8 8
 **docker ps**
9 9
 [**-a**|**--all**[=*false*]]
10
-[**--before**[=*BEFORE*]]
11 10
 [**-f**|**--filter**[=*[]*]]
12 11
 [**--format**=*"TEMPLATE"*]
13 12
 [**--help**]
... ...
@@ -16,7 +15,6 @@ docker-ps - List containers
16 16
 [**--no-trunc**[=*false*]]
17 17
 [**-q**|**--quiet**[=*false*]]
18 18
 [**-s**|**--size**[=*false*]]
19
-[**--since**[=*SINCE*]]
20 19
 
21 20
 # DESCRIPTION
22 21
 
... ...
@@ -27,9 +25,6 @@ the running containers.
27 27
 **-a**, **--all**=*true*|*false*
28 28
    Show all containers. Only running containers are shown by default. The default is *false*.
29 29
 
30
-**--before**=""
31
-   Show only containers created before Id or Name, including non-running containers.
32
-
33 30
 **-f**, **--filter**=[]
34 31
    Provide filter values. Valid filters:
35 32
                           exited=<int> - containers with exit code of <int>
... ...
@@ -37,6 +32,8 @@ the running containers.
37 37
                           status=(created|restarting|running|paused|exited)
38 38
                           name=<string> - container's name
39 39
                           id=<ID> - container's ID
40
+                          before=(<container-name>|<container-id>)
41
+                          since=(<container-name>|<container-id>)
40 42
                           ancestor=(<image-name>[:tag]|<image-id>|<image@digest>) - filters containers that were
41 43
                           created from the given image or a descendant.
42 44
 
... ...
@@ -58,10 +55,10 @@ the running containers.
58 58
   Print usage statement
59 59
 
60 60
 **-l**, **--latest**=*true*|*false*
61
-   Show only the latest created container, include non-running ones. The default is *false*.
61
+   Show only the latest created container (includes all states). The default is *false*.
62 62
 
63 63
 **-n**=*-1*
64
-   Show n last created containers, include non-running ones.
64
+   Show n last created containers (includes all states).
65 65
 
66 66
 **--no-trunc**=*true*|*false*
67 67
    Don't truncate output. The default is *false*.
... ...
@@ -72,9 +69,6 @@ the running containers.
72 72
 **-s**, **--size**=*true*|*false*
73 73
    Display total file sizes. The default is *false*.
74 74
 
75
-**--since**=""
76
-   Show only containers created since Id or Name, include non-running ones.
77
-
78 75
 # EXAMPLES
79 76
 # Display all containers, including non-running
80 77