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

     1  package main
     2  
     3  import (
     4  	"os"
     5  
     6  	"github.com/containers/libpod/cmd/podman/cliconfig"
     7  	"github.com/containers/libpod/libpod/define"
     8  	"github.com/containers/libpod/libpod/image"
     9  	"github.com/containers/libpod/pkg/adapter"
    10  	"github.com/pkg/errors"
    11  	"github.com/spf13/cobra"
    12  )
    13  
    14  var (
    15  	imageExistsCommand     cliconfig.ImageExistsValues
    16  	containerExistsCommand cliconfig.ContainerExistsValues
    17  	podExistsCommand       cliconfig.PodExistsValues
    18  
    19  	imageExistsDescription = `If the named image exists in local storage, podman image exists exits with 0, otherwise the exit code will be 1.`
    20  
    21  	containerExistsDescription = `If the named container exists in local storage, podman container exists exits with 0, otherwise the exit code will be 1.`
    22  
    23  	podExistsDescription = `If the named pod exists in local storage, podman pod exists exits with 0, otherwise the exit code will be 1.`
    24  
    25  	_imageExistsCommand = &cobra.Command{
    26  		Use:   "exists IMAGE",
    27  		Short: "Check if an image exists in local storage",
    28  		Long:  imageExistsDescription,
    29  		RunE: func(cmd *cobra.Command, args []string) error {
    30  			imageExistsCommand.InputArgs = args
    31  			imageExistsCommand.GlobalFlags = MainGlobalOpts
    32  			imageExistsCommand.Remote = remoteclient
    33  			return imageExistsCmd(&imageExistsCommand)
    34  		},
    35  		Example: `podman image exists imageID
    36    podman image exists alpine || podman pull alpine`,
    37  	}
    38  
    39  	_containerExistsCommand = &cobra.Command{
    40  		Use:   "exists CONTAINER",
    41  		Short: "Check if a container exists in local storage",
    42  		Long:  containerExistsDescription,
    43  		RunE: func(cmd *cobra.Command, args []string) error {
    44  			containerExistsCommand.InputArgs = args
    45  			containerExistsCommand.GlobalFlags = MainGlobalOpts
    46  			containerExistsCommand.Remote = remoteclient
    47  			return containerExistsCmd(&containerExistsCommand)
    48  
    49  		},
    50  		Example: `podman container exists containerID
    51    podman container exists myctr || podman run --name myctr [etc...]`,
    52  	}
    53  
    54  	_podExistsCommand = &cobra.Command{
    55  		Use:   "exists POD",
    56  		Short: "Check if a pod exists in local storage",
    57  		Long:  podExistsDescription,
    58  		RunE: func(cmd *cobra.Command, args []string) error {
    59  			podExistsCommand.InputArgs = args
    60  			podExistsCommand.GlobalFlags = MainGlobalOpts
    61  			podExistsCommand.Remote = remoteclient
    62  			return podExistsCmd(&podExistsCommand)
    63  		},
    64  		Example: `podman pod exists podID
    65    podman pod exists mypod || podman pod create --name mypod`,
    66  	}
    67  )
    68  
    69  func init() {
    70  	imageExistsCommand.Command = _imageExistsCommand
    71  	imageExistsCommand.DisableFlagsInUseLine = true
    72  	imageExistsCommand.SetHelpTemplate(HelpTemplate())
    73  	imageExistsCommand.SetUsageTemplate(UsageTemplate())
    74  	containerExistsCommand.Command = _containerExistsCommand
    75  	containerExistsCommand.DisableFlagsInUseLine = true
    76  	containerExistsCommand.SetHelpTemplate(HelpTemplate())
    77  	containerExistsCommand.SetUsageTemplate(UsageTemplate())
    78  	podExistsCommand.Command = _podExistsCommand
    79  	podExistsCommand.DisableFlagsInUseLine = true
    80  	podExistsCommand.SetHelpTemplate(HelpTemplate())
    81  	podExistsCommand.SetUsageTemplate(UsageTemplate())
    82  }
    83  
    84  func imageExistsCmd(c *cliconfig.ImageExistsValues) error {
    85  	args := c.InputArgs
    86  	if len(args) > 1 || len(args) < 1 {
    87  		return errors.New("you may only check for the existence of one image at a time")
    88  	}
    89  	runtime, err := adapter.GetRuntime(getContext(), &c.PodmanCommand)
    90  	if err != nil {
    91  		return errors.Wrapf(err, "could not get runtime")
    92  	}
    93  	defer runtime.DeferredShutdown(false)
    94  	if _, err := runtime.NewImageFromLocal(args[0]); err != nil {
    95  		//TODO we need to ask about having varlink defined errors exposed
    96  		//so we can reuse them
    97  		if errors.Cause(err) == image.ErrNoSuchImage || err.Error() == "io.podman.ImageNotFound" {
    98  			os.Exit(1)
    99  		}
   100  		return err
   101  	}
   102  	return nil
   103  }
   104  
   105  func containerExistsCmd(c *cliconfig.ContainerExistsValues) error {
   106  	args := c.InputArgs
   107  	if len(args) > 1 || len(args) < 1 {
   108  		return errors.New("you may only check for the existence of one container at a time")
   109  	}
   110  	runtime, err := adapter.GetRuntimeNoStore(getContext(), &c.PodmanCommand)
   111  	if err != nil {
   112  		return errors.Wrapf(err, "could not get runtime")
   113  	}
   114  	defer runtime.DeferredShutdown(false)
   115  	if _, err := runtime.LookupContainer(args[0]); err != nil {
   116  		if errors.Cause(err) == define.ErrNoSuchCtr || err.Error() == "io.podman.ContainerNotFound" {
   117  			os.Exit(1)
   118  		}
   119  		return err
   120  	}
   121  	return nil
   122  }
   123  
   124  func podExistsCmd(c *cliconfig.PodExistsValues) error {
   125  	args := c.InputArgs
   126  	if len(args) > 1 || len(args) < 1 {
   127  		return errors.New("you may only check for the existence of one pod at a time")
   128  	}
   129  	runtime, err := adapter.GetRuntimeNoStore(getContext(), &c.PodmanCommand)
   130  	if err != nil {
   131  		return errors.Wrapf(err, "could not get runtime")
   132  	}
   133  	defer runtime.DeferredShutdown(false)
   134  
   135  	if _, err := runtime.LookupPod(args[0]); err != nil {
   136  		if errors.Cause(err) == define.ErrNoSuchPod || err.Error() == "io.podman.PodNotFound" {
   137  			os.Exit(1)
   138  		}
   139  		return err
   140  	}
   141  	return nil
   142  }