Skip to content

Commit

Permalink
Merge pull request #203 from abhi/crictl_ps
Browse files Browse the repository at this point in the history
Adding --latest, --last, --no-trunc to crictl ps
  • Loading branch information
Random-Liu authored Nov 28, 2017
2 parents adc934a + 3102bc1 commit 80511d2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
43 changes: 40 additions & 3 deletions cmd/crictl/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,18 @@ var listContainersCommand = cli.Command{
Name: "all, a",
Usage: "Show all containers",
},
cli.BoolFlag{
Name: "latest, l",
Usage: "Show recently created container",
},
cli.IntFlag{
Name: "last, n",
Usage: "Show last n recently created containers",
},
cli.BoolFlag{
Name: "no-trunc",
Usage: "Show output without truncating the ID",
},
},
Action: func(context *cli.Context) error {
if err := getRuntimeClient(context); err != nil {
Expand All @@ -294,6 +306,9 @@ var listContainersCommand = cli.Command{
quiet: context.Bool("quiet"),
output: context.String("output"),
all: context.Bool("all"),
latest: context.Bool("latest"),
last: context.Int("last"),
noTrunc: context.Bool("no-trunc"),
}

for _, l := range context.StringSlice("label") {
Expand Down Expand Up @@ -551,7 +566,8 @@ func ListContainers(client pb.RuntimeServiceClient, opts listOptions) error {
if !opts.verbose && !opts.quiet {
fmt.Fprintln(w, "CONTAINER ID\tIMAGE\tCREATED\tSTATE\tNAME\tATTEMPT")
}
for _, c := range r.GetContainers() {
containersList := getContainersList(r.GetContainers(), opts)
for _, c := range containersList {
if opts.quiet {
fmt.Printf("%s\n", c.Id)
continue
Expand All @@ -560,9 +576,12 @@ func ListContainers(client pb.RuntimeServiceClient, opts listOptions) error {
createdAt := time.Unix(0, c.CreatedAt)
ctm := units.HumanDuration(time.Now().UTC().Sub(createdAt)) + " ago"
if !opts.verbose {
truncatedID := strings.TrimPrefix(c.Id, "")[:truncatedIDLen]
id := c.Id
if !opts.noTrunc {
id = strings.TrimPrefix(c.Id, "")[:truncatedIDLen]
}
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%d\n",
truncatedID, c.Image.Image, ctm, c.State, c.Metadata.Name, c.Metadata.Attempt)
id, c.Image.Image, ctm, c.State, c.Metadata.Name, c.Metadata.Attempt)
continue
}

Expand Down Expand Up @@ -597,3 +616,21 @@ func ListContainers(client pb.RuntimeServiceClient, opts listOptions) error {
w.Flush()
return nil
}

func getContainersList(containersList []*pb.Container, opts listOptions) []*pb.Container {
n := len(containersList)
if opts.latest {
n = 1
}
if opts.last > 0 {
n = opts.last
}
n = func(a, b int) int {
if a < b {
return a
}
return b
}(n, len(containersList))

return containersList[len(containersList)-n:]
}
6 changes: 6 additions & 0 deletions cmd/crictl/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ type listOptions struct {
output string
// all containers
all bool
// latest container
latest bool
// last n containers
last int
// out with truncating the id
noTrunc bool
}

type execOptions struct {
Expand Down

0 comments on commit 80511d2

Please sign in to comment.