diff --git a/cmd/podman/farm/update.go b/cmd/podman/farm/update.go new file mode 100644 index 0000000000..aa559c5b2c --- /dev/null +++ b/cmd/podman/farm/update.go @@ -0,0 +1,109 @@ +package farm + +import ( + "errors" + "fmt" + + "github.com/containers/common/pkg/completion" + "github.com/containers/common/pkg/config" + "github.com/containers/podman/v4/cmd/podman/common" + "github.com/containers/podman/v4/cmd/podman/registry" + "github.com/spf13/cobra" +) + +var ( + farmUpdateDescription = `Update an existing farm by adding a connection, removing a connection, or changing it to the default farm.` + updateCommand = &cobra.Command{ + Use: "update [options] FARM", + Short: "Update an existing farm", + Long: farmUpdateDescription, + RunE: farmUpdate, + Args: cobra.ExactArgs(1), + ValidArgsFunction: common.AutoCompleteFarms, + Example: `podman farm update --add con1 farm1 + podman update --remove con2 farm2 + podman update --default farm3`, + } + + // Temporary struct to hold cli values. + updateOpts = struct { + Add []string + Remove []string + Default bool + }{} +) + +func init() { + registry.Commands = append(registry.Commands, registry.CliCommand{ + Command: updateCommand, + Parent: farmCmd, + }) + flags := updateCommand.Flags() + + addFlagName := "add" + flags.StringSliceVarP(&updateOpts.Add, addFlagName, "a", nil, "add system connection(s) to farm") + _ = updateCommand.RegisterFlagCompletionFunc(addFlagName, completion.AutocompleteNone) + removeFlagName := "remove" + flags.StringSliceVarP(&updateOpts.Remove, removeFlagName, "r", nil, "remove system connection(s) from farm") + _ = updateCommand.RegisterFlagCompletionFunc(removeFlagName, completion.AutocompleteNone) + defaultFlagName := "default" + flags.BoolVarP(&updateOpts.Default, defaultFlagName, "d", false, "set the given farm as the default farm") +} + +func farmUpdate(cmd *cobra.Command, args []string) error { + farmName := args[0] + + defChanged := cmd.Flags().Changed("default") + + if len(updateOpts.Add) == 0 && len(updateOpts.Remove) == 0 && !defChanged { + return fmt.Errorf("nothing to update for farm %q, please use the --add, --remove, or --default flags to update a farm", farmName) + } + + cfg, err := config.ReadCustomConfig() + if err != nil { + return err + } + + if len(cfg.Farms.List) == 0 { + return errors.New("no farms are created at this time, there is nothing to update") + } + + if defChanged { + // Change the default to the given farm if --default=true + if updateOpts.Default { + cfg.Farms.Default = farmName + } else { + // if --default=false, user doesn't want any farms to be default so clear the DefaultFarm + cfg.Farms.Default = "" + } + } + + if val, ok := cfg.Farms.List[farmName]; ok { + cMap := make(map[string]int) + for _, c := range val { + cMap[c] = 0 + } + + for _, cRemove := range updateOpts.Remove { + delete(cMap, cRemove) + } + + for _, cAdd := range updateOpts.Add { + if _, ok := cMap[cAdd]; !ok { + cMap[cAdd] = 0 + } + } + + updatedConnections := []string{} + for k := range cMap { + updatedConnections = append(updatedConnections, k) + } + cfg.Farms.List[farmName] = updatedConnections + } + + if err := cfg.Write(); err != nil { + return err + } + fmt.Printf("Farm %q updated\n", farmName) + return nil +} diff --git a/docs/source/markdown/podman-farm-update.1.md b/docs/source/markdown/podman-farm-update.1.md new file mode 100644 index 0000000000..32fe19519a --- /dev/null +++ b/docs/source/markdown/podman-farm-update.1.md @@ -0,0 +1,39 @@ +% podman-farm-update 1 + +## NAME +podman\-farm\-update - Update an existing farm + +## SYNOPSIS +**podman farm update** [*options*] *name* + +## DESCRIPTION +Update a farm by either adding connections to it, removing connections from it, or setting it as the new +default farm. + +## OPTIONS + +#### **--add**, **-a** + +Add new connections to an existing farm. Multiple connections can be added at once. + +#### **--default**, **-d** + +Set the current farm as the default. + +#### **--remove**, **-r** + +Remove one or more connections from an existing farm. + +## EXAMPLE +``` +$ podman farm update --add f35,f38 farm1 + +$ podman farm update --remove f35 farm1 + +$ podman farm update --default farm2 +``` +## 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