github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/cmd/podmanV2/images/inspect.go (about) 1 package images 2 3 import ( 4 "context" 5 "encoding/json" 6 "fmt" 7 "os" 8 "strings" 9 "text/tabwriter" 10 "text/template" 11 12 "github.com/containers/buildah/pkg/formats" 13 "github.com/containers/libpod/cmd/podmanV2/common" 14 "github.com/containers/libpod/cmd/podmanV2/registry" 15 "github.com/containers/libpod/pkg/domain/entities" 16 "github.com/pkg/errors" 17 "github.com/spf13/cobra" 18 ) 19 20 var ( 21 // Command: podman image _inspect_ 22 inspectCmd = &cobra.Command{ 23 Use: "inspect [flags] IMAGE", 24 Short: "Display the configuration of an image", 25 Long: `Displays the low-level information on an image identified by name or ID.`, 26 RunE: inspect, 27 Example: `podman image inspect alpine`, 28 } 29 inspectOpts *entities.InspectOptions 30 ) 31 32 func init() { 33 registry.Commands = append(registry.Commands, registry.CliCommand{ 34 Mode: []entities.EngineMode{entities.ABIMode, entities.TunnelMode}, 35 Command: inspectCmd, 36 Parent: imageCmd, 37 }) 38 inspectOpts = common.AddInspectFlagSet(inspectCmd) 39 } 40 41 func inspect(cmd *cobra.Command, args []string) error { 42 latestContainer := inspectOpts.Latest 43 44 if len(args) == 0 && !latestContainer { 45 return errors.Errorf("container or image name must be specified: podman inspect [options [...]] name") 46 } 47 48 if len(args) > 0 && latestContainer { 49 return errors.Errorf("you cannot provide additional arguments with --latest") 50 } 51 52 results, err := registry.ImageEngine().Inspect(context.Background(), args, *inspectOpts) 53 if err != nil { 54 return err 55 } 56 57 if len(results.Images) > 0 { 58 if inspectOpts.Format == "" { 59 buf, err := json.MarshalIndent(results.Images, "", " ") 60 if err != nil { 61 return err 62 } 63 fmt.Println(string(buf)) 64 65 for id, e := range results.Errors { 66 fmt.Fprintf(os.Stderr, "%s: %s\n", id, e.Error()) 67 } 68 return nil 69 } 70 row := inspectFormat(inspectOpts.Format) 71 format := "{{range . }}" + row + "{{end}}" 72 tmpl, err := template.New("inspect").Parse(format) 73 if err != nil { 74 return err 75 } 76 77 w := tabwriter.NewWriter(os.Stdout, 8, 2, 2, ' ', 0) 78 defer func() { _ = w.Flush() }() 79 err = tmpl.Execute(w, results.Images) 80 if err != nil { 81 return err 82 } 83 } 84 85 for id, e := range results.Errors { 86 fmt.Fprintf(os.Stderr, "%s: %s\n", id, e.Error()) 87 } 88 return nil 89 } 90 91 func inspectFormat(row string) string { 92 r := strings.NewReplacer("{{.Id}}", formats.IDString, 93 ".Src", ".Source", 94 ".Dst", ".Destination", 95 ".ImageID", ".Image", 96 ) 97 row = r.Replace(row) 98 99 if !strings.HasSuffix(row, "\n") { 100 row += "\n" 101 } 102 return row 103 } 104 105 func Inspect(cmd *cobra.Command, args []string, options *entities.InspectOptions) error { 106 inspectOpts = options 107 return inspect(cmd, args) 108 }