github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/cmd/podmanV2/images/exists.go (about)

     1  package images
     2  
     3  import (
     4  	"os"
     5  
     6  	"github.com/containers/libpod/cmd/podmanV2/registry"
     7  	"github.com/containers/libpod/pkg/domain/entities"
     8  	"github.com/spf13/cobra"
     9  )
    10  
    11  var (
    12  	existsCmd = &cobra.Command{
    13  		Use:   "exists IMAGE",
    14  		Short: "Check if an image exists in local storage",
    15  		Long:  `If the named image exists in local storage, podman image exists exits with 0, otherwise the exit code will be 1.`,
    16  		Args:  cobra.ExactArgs(1),
    17  		RunE:  exists,
    18  		Example: `podman image exists ID
    19    podman image exists IMAGE && podman pull IMAGE`,
    20  	}
    21  )
    22  
    23  func init() {
    24  	registry.Commands = append(registry.Commands, registry.CliCommand{
    25  		Mode:    []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
    26  		Command: existsCmd,
    27  		Parent:  imageCmd,
    28  	})
    29  }
    30  
    31  func exists(cmd *cobra.Command, args []string) error {
    32  	found, err := registry.ImageEngine().Exists(registry.GetContext(), args[0])
    33  	if err != nil {
    34  		return err
    35  	}
    36  	if !found.Value {
    37  		os.Exit(1)
    38  	}
    39  	return nil
    40  }