From c5d3950e77a1602978c86313cdf8231094b9d632 Mon Sep 17 00:00:00 2001 From: pauhull <22707808+pauhull@users.noreply.github.com> Date: Thu, 7 Mar 2024 16:23:17 +0100 Subject: [PATCH 1/9] feat: add better error messages for missing positional arguments --- internal/cmd/all/all.go | 3 +- internal/cmd/base/delete.go | 2 +- internal/cmd/base/describe.go | 2 +- internal/cmd/base/labels.go | 4 +-- internal/cmd/base/set_rdns.go | 2 +- internal/cmd/base/update.go | 2 +- internal/cmd/certificate/certificate.go | 3 +- internal/cmd/certificate/create.go | 2 +- internal/cmd/completion/completion.go | 3 +- internal/cmd/context/active.go | 3 +- internal/cmd/context/context.go | 3 +- internal/cmd/context/create.go | 3 +- internal/cmd/context/delete.go | 3 +- internal/cmd/context/list.go | 2 +- internal/cmd/context/use.go | 3 +- internal/cmd/datacenter/datacenter.go | 3 +- internal/cmd/firewall/add_rule.go | 3 +- internal/cmd/firewall/apply_to_resource.go | 3 +- internal/cmd/firewall/create.go | 2 +- internal/cmd/firewall/delete_rule.go | 3 +- internal/cmd/firewall/firewall.go | 2 +- internal/cmd/firewall/remove_from_resource.go | 3 +- internal/cmd/firewall/replace_rules.go | 3 +- internal/cmd/floatingip/assign.go | 3 +- internal/cmd/floatingip/create.go | 2 +- internal/cmd/floatingip/disable_protection.go | 3 +- internal/cmd/floatingip/enable_protection.go | 3 +- internal/cmd/floatingip/floatingip.go | 2 +- internal/cmd/floatingip/unassign.go | 3 +- internal/cmd/image/disable_protection.go | 3 +- internal/cmd/image/enable_protection.go | 3 +- internal/cmd/image/image.go | 2 +- internal/cmd/iso/iso.go | 3 +- internal/cmd/loadbalancer/add_service.go | 3 +- internal/cmd/loadbalancer/add_target.go | 2 +- .../cmd/loadbalancer/attach_to_network.go | 3 +- internal/cmd/loadbalancer/change_algorithm.go | 3 +- internal/cmd/loadbalancer/change_type.go | 3 +- internal/cmd/loadbalancer/create.go | 2 +- .../cmd/loadbalancer/detach_from_network.go | 3 +- .../cmd/loadbalancer/disable_protection.go | 3 +- .../loadbalancer/disable_public_interface.go | 3 +- .../cmd/loadbalancer/enable_protection.go | 3 +- .../loadbalancer/enable_public_interface.go | 3 +- internal/cmd/loadbalancer/load_balancer.go | 2 +- internal/cmd/loadbalancer/metrics.go | 2 +- internal/cmd/loadbalancer/remove_target.go | 2 +- internal/cmd/loadbalancer/update_service.go | 3 +- .../loadbalancertype/load_balancer_type.go | 3 +- internal/cmd/location/location.go | 3 +- internal/cmd/network/add_route.go | 3 +- internal/cmd/network/add_subnet.go | 3 +- internal/cmd/network/change_ip_range.go | 3 +- internal/cmd/network/create.go | 2 +- internal/cmd/network/disable_protection.go | 3 +- internal/cmd/network/enable_protection.go | 3 +- .../cmd/network/expose_routes_to_vswitch.go | 3 +- internal/cmd/network/network.go | 2 +- internal/cmd/network/remove_route.go | 3 +- internal/cmd/network/remove_subnet.go | 3 +- internal/cmd/placementgroup/placementgroup.go | 3 +- internal/cmd/primaryip/assign.go | 3 +- internal/cmd/primaryip/create.go | 2 +- internal/cmd/primaryip/disable_protection.go | 3 +- internal/cmd/primaryip/enable_protection.go | 3 +- internal/cmd/primaryip/primaryip.go | 2 +- internal/cmd/primaryip/unassign.go | 3 +- internal/cmd/server/add_to_placement_group.go | 3 +- internal/cmd/server/attach_iso.go | 3 +- internal/cmd/server/attach_to_network.go | 3 +- internal/cmd/server/change_alias_ips.go | 3 +- internal/cmd/server/change_type.go | 3 +- internal/cmd/server/create_image.go | 3 +- internal/cmd/server/detach_from_network.go | 3 +- internal/cmd/server/detach_iso.go | 3 +- internal/cmd/server/disable_backup.go | 3 +- internal/cmd/server/disable_protection.go | 3 +- internal/cmd/server/disable_rescue.go | 3 +- internal/cmd/server/enable_backup.go | 3 +- internal/cmd/server/enable_protection.go | 3 +- internal/cmd/server/enable_rescue.go | 3 +- internal/cmd/server/ip.go | 3 +- internal/cmd/server/metrics.go | 2 +- internal/cmd/server/poweroff.go | 3 +- internal/cmd/server/poweron.go | 3 +- internal/cmd/server/reboot.go | 3 +- internal/cmd/server/rebuild.go | 3 +- .../cmd/server/remove_from_placement_group.go | 3 +- internal/cmd/server/request_console.go | 2 +- internal/cmd/server/reset.go | 3 +- internal/cmd/server/reset_password.go | 3 +- internal/cmd/server/server.go | 2 +- internal/cmd/server/shutdown.go | 3 +- internal/cmd/server/ssh.go | 3 +- internal/cmd/servertype/server_type.go | 3 +- internal/cmd/sshkey/create.go | 2 +- internal/cmd/sshkey/sshkey.go | 3 +- internal/cmd/util/validation.go | 36 +++++++++++++++++++ internal/cmd/version/version.go | 3 +- internal/cmd/volume/attach.go | 3 +- internal/cmd/volume/create.go | 2 +- internal/cmd/volume/detach.go | 3 +- internal/cmd/volume/disable_protection.go | 3 +- internal/cmd/volume/enable_protection.go | 3 +- internal/cmd/volume/resize.go | 3 +- internal/cmd/volume/volume.go | 2 +- 106 files changed, 220 insertions(+), 106 deletions(-) create mode 100644 internal/cmd/util/validation.go diff --git a/internal/cmd/all/all.go b/internal/cmd/all/all.go index 08e11b91..87182076 100644 --- a/internal/cmd/all/all.go +++ b/internal/cmd/all/all.go @@ -3,6 +3,7 @@ package all import ( "github.com/spf13/cobra" + "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" ) @@ -10,7 +11,7 @@ func NewCommand(s state.State) *cobra.Command { cmd := &cobra.Command{ Use: "all", Short: "Commands that apply to all resources", - Args: cobra.NoArgs, + Args: util.Validate, TraverseChildren: true, DisableFlagsInUseLine: true, } diff --git a/internal/cmd/base/delete.go b/internal/cmd/base/delete.go index 1f215805..4b2af6ee 100644 --- a/internal/cmd/base/delete.go +++ b/internal/cmd/base/delete.go @@ -34,7 +34,7 @@ func (dc *DeleteCmd) CobraCommand(s state.State) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete %s<%s>", opts, strings.ToLower(dc.ResourceNameSingular)), Short: dc.ShortDescription, - Args: cobra.ExactArgs(1), + Args: util.Validate, ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(dc.NameSuggestions(s.Client()))), TraverseChildren: true, DisableFlagsInUseLine: true, diff --git a/internal/cmd/base/describe.go b/internal/cmd/base/describe.go index 0e3b7bcd..b3ec5106 100644 --- a/internal/cmd/base/describe.go +++ b/internal/cmd/base/describe.go @@ -35,7 +35,7 @@ func (dc *DescribeCmd) CobraCommand(s state.State) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe [options] <%s>", strings.ToLower(dc.ResourceNameSingular)), Short: dc.ShortDescription, - Args: cobra.ExactArgs(1), + Args: util.Validate, ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(dc.NameSuggestions(s.Client()))), TraverseChildren: true, DisableFlagsInUseLine: true, diff --git a/internal/cmd/base/labels.go b/internal/cmd/base/labels.go index a3aa4dbd..4f3b7db7 100644 --- a/internal/cmd/base/labels.go +++ b/internal/cmd/base/labels.go @@ -29,7 +29,7 @@ func (lc *LabelCmds) AddCobraCommand(s state.State) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("add-label [--overwrite] <%s>