Add podman farm remove command

Podman farm remove allows users to remove one or more
existing farms.

Signed-off-by: Urvashi Mohnani <umohnani@redhat.com>
This commit is contained in:
Urvashi Mohnani
2023-07-26 09:44:34 -04:00
parent 59a8140f1f
commit 44a704dfcf
2 changed files with 129 additions and 0 deletions

99
cmd/podman/farm/remove.go Normal file
View File

@ -0,0 +1,99 @@
package farm
import (
"errors"
"fmt"
"github.com/containers/common/pkg/config"
"github.com/containers/podman/v4/cmd/podman/common"
"github.com/containers/podman/v4/cmd/podman/registry"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var (
farmRmDescription = `Remove one or more existing farms.`
rmCommand = &cobra.Command{
Use: "remove [options] [FARM...]",
Aliases: []string{"rm"},
Short: "Remove one or more farms",
Long: farmRmDescription,
RunE: rm,
ValidArgsFunction: common.AutoCompleteFarms,
Example: `podman farm rm myfarm1 myfarm2
podman farm rm --all`,
}
// Temporary struct to hold cli values.
rmOpts = struct {
All bool
}{}
)
func init() {
registry.Commands = append(registry.Commands, registry.CliCommand{
Command: rmCommand,
Parent: farmCmd,
})
flags := rmCommand.Flags()
flags.BoolVarP(&rmOpts.All, "all", "a", false, "Remove all farms")
}
func rm(cmd *cobra.Command, args []string) error {
cfg, err := config.ReadCustomConfig()
if err != nil {
return err
}
if rmOpts.All {
cfg.Farms.List = make(map[string][]string)
cfg.Farms.Default = ""
if err := cfg.Write(); err != nil {
return err
}
fmt.Println("All farms have been deleted")
return nil
}
// If the --all is not set, we require at least one arg
if len(args) == 0 {
return errors.New("requires at lease 1 arg(s), received 0")
}
if len(cfg.Farms.List) == 0 {
return errors.New("no existing farms; nothing to remove")
}
deletedFarms := []string{}
for _, k := range args {
if _, ok := cfg.Farms.List[k]; !ok {
logrus.Warnf("farm %q doesn't exists; nothing to remove", k)
continue
}
delete(cfg.Farms.List, k)
deletedFarms = append(deletedFarms, k)
if k == cfg.Farms.Default {
cfg.Farms.Default = ""
}
}
// Return error if none of the given farms were deleted
if len(deletedFarms) == 0 {
return fmt.Errorf("failed to delete farms %q", args)
}
// Set a new default farm if the current default farm has been removed
if cfg.Farms.Default == "" && cfg.Farms.List != nil {
for k := range cfg.Farms.List {
cfg.Farms.Default = k
break
}
}
if err := cfg.Write(); err != nil {
return err
}
for _, k := range deletedFarms {
fmt.Printf("Farm %q deleted\n", k)
}
return nil
}

View File

@ -0,0 +1,30 @@
% podman-farm-remove 1
## NAME
podman\-farm\-remove - Delete one or more farms
## SYNOPSIS
**podman farm remove** [*options*] *name*
**podman farm rm** [*options*] *name*
## DESCRIPTION
Delete one or more farms.
## OPTIONS
#### **--all**, **-a**
Remove all farms.
## EXAMPLE
```
$ podman farm remove farm1
$ podman farm rm --all
```
## SEE ALSO
**[podman(1)](podman.1.md)**, **[podman-farm(1)](podman-farm.1.md)**
## HISTORY
July 2023, Originally compiled by Urvashi Mohnani (umohnani at redhat dot com)s