Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding --latest, --last, --no-trunc to crictl ps #203

Merged
merged 1 commit into from
Nov 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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