Skip to content

Commit 80511d2

Browse files
authored
Merge pull request #203 from abhi/crictl_ps
Adding --latest, --last, --no-trunc to crictl ps
2 parents adc934a + 3102bc1 commit 80511d2

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

cmd/crictl/container.go

+40-3
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,18 @@ var listContainersCommand = cli.Command{
279279
Name: "all, a",
280280
Usage: "Show all containers",
281281
},
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+
},
282294
},
283295
Action: func(context *cli.Context) error {
284296
if err := getRuntimeClient(context); err != nil {
@@ -294,6 +306,9 @@ var listContainersCommand = cli.Command{
294306
quiet: context.Bool("quiet"),
295307
output: context.String("output"),
296308
all: context.Bool("all"),
309+
latest: context.Bool("latest"),
310+
last: context.Int("last"),
311+
noTrunc: context.Bool("no-trunc"),
297312
}
298313

299314
for _, l := range context.StringSlice("label") {
@@ -551,7 +566,8 @@ func ListContainers(client pb.RuntimeServiceClient, opts listOptions) error {
551566
if !opts.verbose && !opts.quiet {
552567
fmt.Fprintln(w, "CONTAINER ID\tIMAGE\tCREATED\tSTATE\tNAME\tATTEMPT")
553568
}
554-
for _, c := range r.GetContainers() {
569+
containersList := getContainersList(r.GetContainers(), opts)
570+
for _, c := range containersList {
555571
if opts.quiet {
556572
fmt.Printf("%s\n", c.Id)
557573
continue
@@ -560,9 +576,12 @@ func ListContainers(client pb.RuntimeServiceClient, opts listOptions) error {
560576
createdAt := time.Unix(0, c.CreatedAt)
561577
ctm := units.HumanDuration(time.Now().UTC().Sub(createdAt)) + " ago"
562578
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+
}
564583
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)
566585
continue
567586
}
568587

@@ -597,3 +616,21 @@ func ListContainers(client pb.RuntimeServiceClient, opts listOptions) error {
597616
w.Flush()
598617
return nil
599618
}
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+
}

cmd/crictl/util.go

+6
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ type listOptions struct {
5858
output string
5959
// all containers
6060
all bool
61+
// latest container
62+
latest bool
63+
// last n containers
64+
last int
65+
// out with truncating the id
66+
noTrunc bool
6167
}
6268

6369
type execOptions struct {

0 commit comments

Comments
 (0)