From 85f4f89810b3211065e38f9d252e679948100c8c Mon Sep 17 00:00:00 2001 From: Paul Holzinger Date: Thu, 18 Jul 2024 14:42:24 +0200 Subject: [PATCH] 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 --- cmd/podman/system/connection/remove.go | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/cmd/podman/system/connection/remove.go b/cmd/podman/system/connection/remove.go index 225b1f038f..3398a7601f 100644 --- a/cmd/podman/system/connection/remove.go +++ b/cmd/podman/system/connection/remove.go @@ -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 = ""