@@ -279,6 +279,18 @@ var listContainersCommand = cli.Command{
279
279
Name : "all, a" ,
280
280
Usage : "Show all containers" ,
281
281
},
282
+ cli.BoolFlag {
283
+ Name : "latest, l" ,
284
+ Usage : "Show recently created container" ,
285
+ },
286
+ cli.IntFlag {
287
+ Name : "last, n" ,
288
+ Usage : "Show last n recently created containers" ,
289
+ },
290
+ cli.BoolFlag {
291
+ Name : "no-trunc" ,
292
+ Usage : "Show output without truncating the ID" ,
293
+ },
282
294
},
283
295
Action : func (context * cli.Context ) error {
284
296
if err := getRuntimeClient (context ); err != nil {
@@ -294,6 +306,9 @@ var listContainersCommand = cli.Command{
294
306
quiet : context .Bool ("quiet" ),
295
307
output : context .String ("output" ),
296
308
all : context .Bool ("all" ),
309
+ latest : context .Bool ("latest" ),
310
+ last : context .Int ("last" ),
311
+ noTrunc : context .Bool ("no-trunc" ),
297
312
}
298
313
299
314
for _ , l := range context .StringSlice ("label" ) {
@@ -551,7 +566,8 @@ func ListContainers(client pb.RuntimeServiceClient, opts listOptions) error {
551
566
if ! opts .verbose && ! opts .quiet {
552
567
fmt .Fprintln (w , "CONTAINER ID\t IMAGE\t CREATED\t STATE\t NAME\t ATTEMPT" )
553
568
}
554
- for _ , c := range r .GetContainers () {
569
+ containersList := getContainersList (r .GetContainers (), opts )
570
+ for _ , c := range containersList {
555
571
if opts .quiet {
556
572
fmt .Printf ("%s\n " , c .Id )
557
573
continue
@@ -560,9 +576,12 @@ func ListContainers(client pb.RuntimeServiceClient, opts listOptions) error {
560
576
createdAt := time .Unix (0 , c .CreatedAt )
561
577
ctm := units .HumanDuration (time .Now ().UTC ().Sub (createdAt )) + " ago"
562
578
if ! opts .verbose {
563
- truncatedID := strings .TrimPrefix (c .Id , "" )[:truncatedIDLen ]
579
+ id := c .Id
580
+ if ! opts .noTrunc {
581
+ id = strings .TrimPrefix (c .Id , "" )[:truncatedIDLen ]
582
+ }
564
583
fmt .Fprintf (w , "%s\t %s\t %s\t %s\t %s\t %d\n " ,
565
- truncatedID , c .Image .Image , ctm , c .State , c .Metadata .Name , c .Metadata .Attempt )
584
+ id , c .Image .Image , ctm , c .State , c .Metadata .Name , c .Metadata .Attempt )
566
585
continue
567
586
}
568
587
@@ -597,3 +616,21 @@ func ListContainers(client pb.RuntimeServiceClient, opts listOptions) error {
597
616
w .Flush ()
598
617
return nil
599
618
}
619
+
620
+ func getContainersList (containersList []* pb.Container , opts listOptions ) []* pb.Container {
621
+ n := len (containersList )
622
+ if opts .latest {
623
+ n = 1
624
+ }
625
+ if opts .last > 0 {
626
+ n = opts .last
627
+ }
628
+ n = func (a , b int ) int {
629
+ if a < b {
630
+ return a
631
+ }
632
+ return b
633
+ }(n , len (containersList ))
634
+
635
+ return containersList [len (containersList )- n :]
636
+ }
0 commit comments