system connection remove: use Args function to validate

Using the ExactArgs(1) function is better because we have less
duplication of the error text and the ValidArgsFunction uses that to
suggest shell completion. The command before this commit would suggest
connection names even if there was already one arg on the cli set.

However because there is the --all option we still must exclude that
first.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
This commit is contained in:
Paul Holzinger
2024-07-18 14:42:24 +02:00
parent eecdba4009
commit 85f4f89810

View File

@ -1,7 +1,6 @@
package connection
import (
"errors"
"slices"
"github.com/containers/common/pkg/config"
@ -14,10 +13,16 @@ import (
var (
// Skip creating engines since this command will obtain connection information to said engines.
rmCmd = &cobra.Command{
Use: "remove [options] NAME",
Aliases: []string{"rm"},
Long: `Delete named destination from podman configuration`,
Short: "Delete named destination",
Use: "remove [options] NAME",
Aliases: []string{"rm"},
Long: `Delete named destination from podman configuration`,
Short: "Delete named destination",
Args: func(cmd *cobra.Command, args []string) error {
if rmOpts.All {
return nil
}
return cobra.ExactArgs(1)(cmd, args)
},
ValidArgsFunction: common.AutocompleteSystemConnections,
RunE: rm,
Example: `podman system connection remove devl
@ -60,10 +65,6 @@ func rm(cmd *cobra.Command, args []string) error {
return nil
}
if len(args) != 1 {
return errors.New("accepts 1 arg(s), received 0")
}
delete(cfg.Connection.Connections, args[0])
if cfg.Connection.Default == args[0] {
cfg.Connection.Default = ""