Skip to content

Commit

Permalink
Merge pull request containers#6022 from vrothberg/enable-inspect-tests
Browse files Browse the repository at this point in the history
enable inspect tests
  • Loading branch information
openshift-merge-robot authored Apr 29, 2020
2 parents 62a4bef + 8700c2f commit 3e912f7
Show file tree
Hide file tree
Showing 12 changed files with 214 additions and 220 deletions.
18 changes: 0 additions & 18 deletions cmd/podman/common/inspect.go

This file was deleted.

53 changes: 5 additions & 48 deletions cmd/podman/containers/inspect.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
package containers

import (
"context"
"fmt"
"os"
"strings"
"text/template"

"github.com/containers/libpod/cmd/podman/common"
"github.com/containers/libpod/cmd/podman/inspect"
"github.com/containers/libpod/cmd/podman/registry"

"github.com/containers/libpod/pkg/domain/entities"
"github.com/spf13/cobra"
)
Expand All @@ -20,7 +13,7 @@ var (
Use: "inspect [flags] CONTAINER",
Short: "Display the configuration of a container",
Long: `Displays the low-level information on a container identified by name or ID.`,
RunE: inspect,
RunE: inspectExec,
Example: `podman container inspect myCtr
podman container inspect -l --format '{{.Id}} {{.Config.Labels}}'`,
}
Expand All @@ -33,45 +26,9 @@ func init() {
Command: inspectCmd,
Parent: containerCmd,
})
inspectOpts = common.AddInspectFlagSet(inspectCmd)
flags := inspectCmd.Flags()

if !registry.IsRemote() {
flags.BoolVarP(&inspectOpts.Latest, "latest", "l", false, "Act on the latest container podman is aware of")
}

}

func inspect(cmd *cobra.Command, args []string) error {
responses, err := registry.ContainerEngine().ContainerInspect(context.Background(), args, *inspectOpts)
if err != nil {
return err
}
if inspectOpts.Format == "" {
b, err := json.MarshalIndent(responses, "", " ")
if err != nil {
return err
}
fmt.Println(string(b))
return nil
}
format := inspectOpts.Format
if !strings.HasSuffix(format, "\n") {
format += "\n"
}
tmpl, err := template.New("inspect").Parse(format)
if err != nil {
return err
}
for _, i := range responses {
if err := tmpl.Execute(os.Stdout, i); err != nil {
return err
}
}
return nil
inspectOpts = inspect.AddInspectFlagSet(inspectCmd)
}

func Inspect(cmd *cobra.Command, args []string, options *entities.InspectOptions) error {
inspectOpts = options
return inspect(cmd, args)
func inspectExec(cmd *cobra.Command, args []string) error {
return inspect.Inspect(args, *inspectOpts)
}
92 changes: 8 additions & 84 deletions cmd/podman/images/inspect.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
package images

import (
"context"
"fmt"
"os"
"strings"
"text/tabwriter"
"text/template"

"github.com/containers/buildah/pkg/formats"
"github.com/containers/libpod/cmd/podman/common"
"github.com/containers/libpod/cmd/podman/inspect"
"github.com/containers/libpod/cmd/podman/registry"
"github.com/containers/libpod/pkg/domain/entities"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

Expand All @@ -21,8 +12,8 @@ var (
inspectCmd = &cobra.Command{
Use: "inspect [flags] IMAGE",
Short: "Display the configuration of an image",
Long: `Displays the low-level information on an image identified by name or ID.`,
RunE: inspect,
Long: `Displays the low-level information of an image identified by name or ID.`,
RunE: inspectExec,
Example: `podman inspect alpine
podman inspect --format "imageId: {{.Id}} size: {{.Size}}" alpine
podman inspect --format "image: {{.ImageName}} driver: {{.Driver}}" myctr`,
Expand All @@ -36,78 +27,11 @@ func init() {
Command: inspectCmd,
Parent: imageCmd,
})
inspectOpts = common.AddInspectFlagSet(inspectCmd)
}

func inspect(cmd *cobra.Command, args []string) error {
if inspectOpts.Size {
return fmt.Errorf("--size can only be used for containers")
}
if inspectOpts.Latest {
return fmt.Errorf("--latest can only be used for containers")
}
if len(args) == 0 {
return errors.Errorf("image name must be specified: podman image inspect [options [...]] name")
}

results, err := registry.ImageEngine().Inspect(context.Background(), args, *inspectOpts)
if err != nil {
return err
}

if len(results.Images) > 0 {
if inspectOpts.Format == "" {
buf, err := json.MarshalIndent(results.Images, "", " ")
if err != nil {
return err
}
fmt.Println(string(buf))

for id, e := range results.Errors {
fmt.Fprintf(os.Stderr, "%s: %s\n", id, e.Error())
}
return nil
}
row := inspectFormat(inspectOpts.Format)
format := "{{range . }}" + row + "{{end}}"
tmpl, err := template.New("inspect").Parse(format)
if err != nil {
return err
}

w := tabwriter.NewWriter(os.Stdout, 8, 2, 2, ' ', 0)
defer func() { _ = w.Flush() }()
err = tmpl.Execute(w, results.Images)
if err != nil {
return err
}
}

var lastErr error
for id, e := range results.Errors {
if lastErr != nil {
fmt.Fprintf(os.Stderr, "%s: %s\n", id, lastErr.Error())
}
lastErr = e
}
return lastErr
}

func inspectFormat(row string) string {
r := strings.NewReplacer("{{.Id}}", formats.IDString,
".Src", ".Source",
".Dst", ".Destination",
".ImageID", ".Image",
)
row = r.Replace(row)

if !strings.HasSuffix(row, "\n") {
row += "\n"
}
return row
inspectOpts = inspect.AddInspectFlagSet(inspectCmd)
flags := inspectCmd.Flags()
_ = flags.MarkHidden("latest") // Shared with container-inspect but not wanted here.
}

func Inspect(cmd *cobra.Command, args []string, options *entities.InspectOptions) error {
inspectOpts = options
return inspect(cmd, args)
func inspectExec(cmd *cobra.Command, args []string) error {
return inspect.Inspect(args, *inspectOpts)
}
42 changes: 10 additions & 32 deletions cmd/podman/inspect.go
Original file line number Diff line number Diff line change
@@ -1,58 +1,36 @@
package main

import (
"fmt"

"github.com/containers/libpod/cmd/podman/common"
"github.com/containers/libpod/cmd/podman/containers"
"github.com/containers/libpod/cmd/podman/images"
"github.com/containers/libpod/cmd/podman/inspect"
"github.com/containers/libpod/cmd/podman/registry"
"github.com/containers/libpod/pkg/domain/entities"
"github.com/spf13/cobra"
)

// Inspect is one of the outlier commands in that it operates on images/containers/...

var (
inspectOpts *entities.InspectOptions

// Command: podman _inspect_ Object_ID
inspectCmd = &cobra.Command{
Use: "inspect [flags] {CONTAINER_ID | IMAGE_ID}",
Short: "Display the configuration of object denoted by ID",
Long: "Displays the low-level information on an object identified by name or ID",
TraverseChildren: true,
RunE: inspect,
Example: `podman inspect alpine
podman inspect --format "imageId: {{.Id}} size: {{.Size}}" alpine`,
RunE: inspectExec,
Example: `podman inspect fedora
podman inspect --type image fedora
podman inspect CtrID ImgID
podman inspect --format "imageId: {{.Id}} size: {{.Size}}" fedora`,
}
inspectOpts *entities.InspectOptions
)

func init() {
registry.Commands = append(registry.Commands, registry.CliCommand{
Mode: []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
Command: inspectCmd,
})
inspectOpts = common.AddInspectFlagSet(inspectCmd)
flags := inspectCmd.Flags()
flags.StringVarP(&inspectOpts.Type, "type", "t", "", "Return JSON for specified type, (image or container) (default \"all\")")
if !registry.IsRemote() {
flags.BoolVarP(&inspectOpts.Latest, "latest", "l", false, "Act on the latest container podman is aware of (containers only)")
}
inspectOpts = inspect.AddInspectFlagSet(inspectCmd)
}

func inspect(cmd *cobra.Command, args []string) error {
switch inspectOpts.Type {
case "image":
return images.Inspect(cmd, args, inspectOpts)
case "container":
return containers.Inspect(cmd, args, inspectOpts)
case "":
if err := images.Inspect(cmd, args, inspectOpts); err == nil {
return nil
}
return containers.Inspect(cmd, args, inspectOpts)
default:
return fmt.Errorf("invalid type %q is must be 'container' or 'image'", inspectOpts.Type)
}
func inspectExec(cmd *cobra.Command, args []string) error {
return inspect.Inspect(args, *inspectOpts)
}
Loading

0 comments on commit 3e912f7

Please sign in to comment.