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

     1  package pods
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/containers/libpod/cmd/podmanV2/parse"
     8  	"github.com/containers/libpod/cmd/podmanV2/registry"
     9  	"github.com/containers/libpod/cmd/podmanV2/utils"
    10  	"github.com/containers/libpod/pkg/domain/entities"
    11  	"github.com/spf13/cobra"
    12  )
    13  
    14  var (
    15  	podRestartDescription = `The pod ID or name can be used.
    16  
    17    All of the containers within each of the specified pods will be restarted. If a container in a pod is not currently running it will be started.`
    18  	restartCommand = &cobra.Command{
    19  		Use:   "restart [flags] POD [POD...]",
    20  		Short: "Restart one or more pods",
    21  		Long:  podRestartDescription,
    22  		RunE:  restart,
    23  		Args: func(cmd *cobra.Command, args []string) error {
    24  			return parse.CheckAllLatestAndCIDFile(cmd, args, false, false)
    25  		},
    26  		Example: `podman pod restart podID1 podID2
    27    podman pod restart --latest
    28    podman pod restart --all`,
    29  	}
    30  )
    31  
    32  var (
    33  	restartOptions = entities.PodRestartOptions{}
    34  )
    35  
    36  func init() {
    37  	registry.Commands = append(registry.Commands, registry.CliCommand{
    38  		Mode:    []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
    39  		Command: restartCommand,
    40  		Parent:  podCmd,
    41  	})
    42  
    43  	flags := restartCommand.Flags()
    44  	flags.BoolVarP(&restartOptions.All, "all", "a", false, "Restart all running pods")
    45  	flags.BoolVarP(&restartOptions.Latest, "latest", "l", false, "Restart the latest pod podman is aware of")
    46  	if registry.IsRemote() {
    47  		_ = flags.MarkHidden("latest")
    48  	}
    49  }
    50  
    51  func restart(cmd *cobra.Command, args []string) error {
    52  	var (
    53  		errs utils.OutputErrors
    54  	)
    55  	responses, err := registry.ContainerEngine().PodRestart(context.Background(), args, restartOptions)
    56  	if err != nil {
    57  		return err
    58  	}
    59  	// in the cli, first we print out all the successful attempts
    60  	for _, r := range responses {
    61  		if len(r.Errs) == 0 {
    62  			fmt.Println(r.Id)
    63  		} else {
    64  			errs = append(errs, r.Errs...)
    65  		}
    66  	}
    67  	return errs.PrintErrors()
    68  }