Skip to content

Commit

Permalink
Merge pull request #598 from Junnplus/ps-latest
Browse files Browse the repository at this point in the history
Add latest/last flag for nerdctl ps
  • Loading branch information
AkihiroSuda authored Dec 8, 2021
2 parents e6956be + 6165b5c commit 540dbd4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -545,8 +545,10 @@ Flags:
- :whale: `--format='{{json .}}'`: JSON
- :nerd_face: `--format=wide`: Wide table
- :nerd_face: `--format=json`: Alias of `--format='{{json .}}'`
- :whale: `-n, --last`: Show n last created containers (includes all states)
- :whale: `-l, --latest`: Show the latest created container (includes all states)

Unimplemented `docker ps` flags: `--filter`, `--last`, `--size`
Unimplemented `docker ps` flags: `--filter`, `--size`

### :whale: :blue_square: nerdctl inspect
Display detailed information on one or more containers.
Expand Down
38 changes: 32 additions & 6 deletions cmd/nerdctl/ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"fmt"
"io"
"os"
"sort"
"strings"
"text/tabwriter"
"text/template"
Expand All @@ -46,6 +47,8 @@ func newPsCommand() *cobra.Command {
SilenceErrors: true,
}
psCommand.Flags().BoolP("all", "a", false, "Show all containers (default shows just running)")
psCommand.Flags().IntP("last", "n", -1, "Show n last created containers (includes all states)")
psCommand.Flags().BoolP("latest", "l", false, "Show the latest created container (includes all states)")
psCommand.Flags().Bool("no-trunc", false, "Don't truncate output")
psCommand.Flags().BoolP("quiet", "q", false, "Only display container IDs")
// Alias "-f" is reserved for "--filter"
Expand All @@ -62,11 +65,37 @@ func psAction(cmd *cobra.Command, args []string) error {
return err
}
defer cancel()
all, err := cmd.Flags().GetBool("all")
if err != nil {
return err
}
latest, err := cmd.Flags().GetBool("latest")
if err != nil {
return err
}
lastN, err := cmd.Flags().GetInt("last")
if err != nil {
return err
}
if lastN == -1 && latest {
lastN = 1
}
containers, err := client.Containers(ctx)
if err != nil {
return err
}
return printContainers(ctx, cmd, containers)
if lastN > 0 {
all = true
sort.Slice(containers, func(i, j int) bool {
infoI, _ := containers[i].Info(ctx, containerd.WithoutRefreshedMetadata)
infoJ, _ := containers[j].Info(ctx, containerd.WithoutRefreshedMetadata)
return infoI.CreatedAt.After(infoJ.CreatedAt)
})
if lastN < len(containers) {
containers = containers[:lastN]
}
}
return printContainers(ctx, cmd, containers, all)
}

type containerPrintable struct {
Expand All @@ -82,17 +111,14 @@ type containerPrintable struct {
// TODO: "Labels", "LocalVolumes", "Mounts", "Networks", "RunningFor", "Size", "State"
}

func printContainers(ctx context.Context, cmd *cobra.Command, containers []containerd.Container) error {
func printContainers(ctx context.Context, cmd *cobra.Command, containers []containerd.Container, all bool) error {
noTrunc, err := cmd.Flags().GetBool("no-trunc")
if err != nil {
return err
}
var wide bool
trunc := !noTrunc
all, err := cmd.Flags().GetBool("all")
if err != nil {
return err
}

quiet, err := cmd.Flags().GetBool("quiet")
if err != nil {
return err
Expand Down

0 comments on commit 540dbd4

Please sign in to comment.