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

     1  package main
     2  
     3  import (
     4  	"bufio"
     5  	"fmt"
     6  	"os"
     7  	"strings"
     8  
     9  	"github.com/containers/libpod/cmd/podman/cliconfig"
    10  	"github.com/containers/libpod/pkg/adapter"
    11  	"github.com/pkg/errors"
    12  	"github.com/spf13/cobra"
    13  )
    14  
    15  var (
    16  	systemResetCommand     cliconfig.SystemResetValues
    17  	systemResetDescription = `Reset podman storage back to default state"
    18  
    19    All containers will be stopped and removed, and all images, volumes and container content will be removed.
    20  `
    21  	_systemResetCommand = &cobra.Command{
    22  		Use:   "reset",
    23  		Args:  noSubArgs,
    24  		Short: "Reset podman storage",
    25  		Long:  systemResetDescription,
    26  		RunE: func(cmd *cobra.Command, args []string) error {
    27  			systemResetCommand.InputArgs = args
    28  			systemResetCommand.GlobalFlags = MainGlobalOpts
    29  			systemResetCommand.Remote = remoteclient
    30  			return systemResetCmd(&systemResetCommand)
    31  		},
    32  	}
    33  )
    34  
    35  func init() {
    36  	systemResetCommand.Command = _systemResetCommand
    37  	flags := systemResetCommand.Flags()
    38  	flags.BoolVarP(&systemResetCommand.Force, "force", "f", false, "Do not prompt for confirmation")
    39  
    40  	systemResetCommand.SetHelpTemplate(HelpTemplate())
    41  	systemResetCommand.SetUsageTemplate(UsageTemplate())
    42  }
    43  
    44  func systemResetCmd(c *cliconfig.SystemResetValues) error {
    45  	// Prompt for confirmation if --force is not set
    46  	if !c.Force {
    47  		reader := bufio.NewReader(os.Stdin)
    48  		fmt.Print(`
    49  WARNING! This will remove:
    50          - all containers
    51          - all pods
    52          - all images
    53          - all build cache
    54  Are you sure you want to continue? [y/N] `)
    55  		answer, err := reader.ReadString('\n')
    56  		if err != nil {
    57  			return errors.Wrapf(err, "error reading input")
    58  		}
    59  		if strings.ToLower(answer)[0] != 'y' {
    60  			return nil
    61  		}
    62  	}
    63  
    64  	runtime, err := adapter.GetRuntime(getContext(), &c.PodmanCommand)
    65  	if err != nil {
    66  		return errors.Wrapf(err, "error creating libpod runtime")
    67  	}
    68  	// No shutdown, since storage will be destroyed when command completes
    69  
    70  	return runtime.Reset()
    71  }