Skip to content

Commit

Permalink
Merge pull request #176 from miaoyq/fixes-173
Browse files Browse the repository at this point in the history
`ps`, `images` and `sandboxes` print table name when there are no items
  • Loading branch information
Random-Liu authored Nov 2, 2017
2 parents c88d957 + d170228 commit 044a560
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 24 deletions.
9 changes: 3 additions & 6 deletions cmd/crictl/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,9 +543,10 @@ func ListContainers(client pb.RuntimeServiceClient, opts listOptions) error {
return outputYAML(r.Containers)
}

// output in table format by default.
printHeader := true
w := tabwriter.NewWriter(os.Stdout, 20, 1, 3, ' ', 0)
if !opts.verbose && !opts.quiet {
fmt.Fprintln(w, "CONTAINER ID\tCREATED\tSTATE\tNAME")
}
for _, c := range r.GetContainers() {
if opts.quiet {
fmt.Printf("%s\n", c.Id)
Expand All @@ -555,10 +556,6 @@ 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 {
if printHeader {
printHeader = false
fmt.Fprintln(w, "CONTAINER ID\tCREATED\tSTATE\tNAME")
}
fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", c.Id, ctm, c.State, c.GetMetadata().GetName())
continue
}
Expand Down
22 changes: 11 additions & 11 deletions cmd/crictl/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,22 +132,22 @@ var listImageCommand = cli.Command{
// output in table format by default.
w := tabwriter.NewWriter(os.Stdout, 20, 1, 3, ' ', 0)
verbose := context.Bool("verbose")
printHeader := true
showDigest := context.Bool("digests")
quiet := context.Bool("quiet")
if !verbose && !quiet {
if showDigest {
fmt.Fprintln(w, "IMAGE\tTAG\tDIGEST\tIMAGE ID\tSIZE")
} else {
fmt.Fprintln(w, "IMAGE\tTAG\tIMAGE ID\tSIZE")
}
}
for _, image := range r.Images {
if context.Bool("quiet") {
if quiet {
fmt.Printf("%s\n", image.Id)
continue
}

if !verbose {
showDigest := context.Bool("digests")
if printHeader {
printHeader = false
if showDigest {
fmt.Fprintln(w, "IMAGE\tTAG\tDIGEST\tIMAGE ID\tSIZE")
} else {
fmt.Fprintln(w, "IMAGE\tTAG\tIMAGE ID\tSIZE")
}
}
imageName, repoDigest := normalizeRepoDigest(image.RepoDigests)
repoTagPairs := normalizeRepoTagPair(image.RepoTags, imageName)
size := units.HumanSizeWithPrecision(float64(image.GetSize_()), 3)
Expand Down
10 changes: 3 additions & 7 deletions cmd/crictl/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,20 +355,16 @@ func ListPodSandboxes(client pb.RuntimeServiceClient, opts listOptions) error {
return outputYAML(r.Items)
}

// output in table format by default.
printHeader := true
w := tabwriter.NewWriter(os.Stdout, 20, 1, 3, ' ', 0)
if !opts.verbose && !opts.quiet {
fmt.Fprintln(w, "SANDBOX ID\tNAME\tSTATE")
}
for _, pod := range r.Items {
if opts.quiet {
fmt.Printf("%s\n", pod.Id)
continue
}
if !opts.verbose {
if printHeader {
printHeader = false
fmt.Fprintln(w, "SANDBOX ID\tNAME\tSTATE")
}

fmt.Fprintf(w, "%s\t%s\t%s\n", pod.Id, pod.Metadata.Name, pod.State)
continue
}
Expand Down

0 comments on commit 044a560

Please sign in to comment.