Skip to content

Commit

Permalink
feat: add better error messages for missing positional arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
phm07 committed Mar 8, 2024
1 parent 54faaca commit a534ce4
Show file tree
Hide file tree
Showing 106 changed files with 220 additions and 106 deletions.
3 changes: 2 additions & 1 deletion internal/cmd/all/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package all
import (
"github.com/spf13/cobra"

"github.com/hetznercloud/cli/internal/cmd/util"
"github.com/hetznercloud/cli/internal/state"
)

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,
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/base/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/base/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/base/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (lc *LabelCmds) AddCobraCommand(s state.State) *cobra.Command {
cmd := &cobra.Command{
Use: fmt.Sprintf("add-label [--overwrite] <%s> <label>...", strings.ToLower(lc.ResourceNameSingular)),
Short: lc.ShortDescriptionAdd,
Args: cobra.MinimumNArgs(2),
Args: util.Validate,
ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(lc.NameSuggestions(s.Client()))),
TraverseChildren: true,
DisableFlagsInUseLine: true,
Expand Down Expand Up @@ -91,7 +91,7 @@ func (lc *LabelCmds) RemoveCobraCommand(s state.State) *cobra.Command {
cmd := &cobra.Command{
Use: fmt.Sprintf("remove-label <%s> (--all | <label>...)", strings.ToLower(lc.ResourceNameSingular)),
Short: lc.ShortDescriptionRemove,
Args: cobra.MinimumNArgs(1),
Args: util.Validate,
ValidArgsFunction: cmpl.SuggestArgs(
cmpl.SuggestCandidatesF(lc.NameSuggestions(s.Client())),
cmpl.SuggestCandidatesCtx(func(_ *cobra.Command, args []string) []string {
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/base/set_rdns.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (rc *SetRdnsCmd) CobraCommand(s state.State) *cobra.Command {
cmd := &cobra.Command{
Use: fmt.Sprintf("set-rdns [options] <%s> --hostname <hostname>", strings.ToLower(rc.ResourceNameSingular)),
Short: rc.ShortDescription,
Args: cobra.ExactArgs(1),
Args: util.Validate,
ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(rc.NameSuggestions(s.Client()))),
TraverseChildren: true,
DisableFlagsInUseLine: true,
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/base/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (uc *UpdateCmd) CobraCommand(s state.State) *cobra.Command {
cmd := &cobra.Command{
Use: fmt.Sprintf("update [options] <%s>", strings.ToLower(uc.ResourceNameSingular)),
Short: uc.ShortDescription,
Args: cobra.ExactArgs(1),
Args: util.Validate,
ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(uc.NameSuggestions(s.Client()))),
TraverseChildren: true,
DisableFlagsInUseLine: true,
Expand Down
3 changes: 2 additions & 1 deletion internal/cmd/certificate/certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package certificate
import (
"github.com/spf13/cobra"

"github.com/hetznercloud/cli/internal/cmd/util"
"github.com/hetznercloud/cli/internal/state"
)

func NewCommand(s state.State) *cobra.Command {
cmd := &cobra.Command{
Use: "certificate",
Short: "Manage certificates",
Args: cobra.NoArgs,
Args: util.Validate,
TraverseChildren: true,
DisableFlagsInUseLine: true,
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/certificate/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var CreateCmd = base.CreateCmd{
cmd := &cobra.Command{
Use: "create [options] --name <name> (--type managed | --type uploaded --cert-file <file> --key-file <file>)",
Short: "Create or upload a Certificate",
Args: cobra.ExactArgs(0),
Args: util.Validate,
}

cmd.Flags().String("name", "", "Certificate name (required)")
Expand Down
3 changes: 2 additions & 1 deletion internal/cmd/completion/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/spf13/cobra"

"github.com/hetznercloud/cli/internal/cmd/util"
"github.com/hetznercloud/cli/internal/state"
)

Expand Down Expand Up @@ -94,7 +95,7 @@ and source this file from your PowerShell profile.
PS> hcloud completion powershell > hcloud.ps1
`,
Args: cobra.ExactArgs(1),
Args: util.Validate,
ValidArgs: []string{"bash", "fish", "zsh", "powershell"},
DisableFlagsInUseLine: true,
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down
3 changes: 2 additions & 1 deletion internal/cmd/context/active.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import (

"github.com/spf13/cobra"

"github.com/hetznercloud/cli/internal/cmd/util"
"github.com/hetznercloud/cli/internal/state"
)

func newActiveCommand(s state.State) *cobra.Command {
cmd := &cobra.Command{
Use: "active",
Short: "Show active context",
Args: cobra.NoArgs,
Args: util.Validate,
TraverseChildren: true,
DisableFlagsInUseLine: true,
RunE: state.Wrap(s, runActive),
Expand Down
3 changes: 2 additions & 1 deletion internal/cmd/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package context
import (
"github.com/spf13/cobra"

"github.com/hetznercloud/cli/internal/cmd/util"
"github.com/hetznercloud/cli/internal/state"
)

func NewCommand(s state.State) *cobra.Command {
cmd := &cobra.Command{
Use: "context",
Short: "Manage contexts",
Args: cobra.NoArgs,
Args: util.Validate,
TraverseChildren: true,
DisableFlagsInUseLine: true,
}
Expand Down
3 changes: 2 additions & 1 deletion internal/cmd/context/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/spf13/cobra"
"golang.org/x/term"

"github.com/hetznercloud/cli/internal/cmd/util"
"github.com/hetznercloud/cli/internal/state"
"github.com/hetznercloud/cli/internal/state/config"
)
Expand All @@ -19,7 +20,7 @@ func newCreateCommand(s state.State) *cobra.Command {
cmd := &cobra.Command{
Use: "create <name>",
Short: "Create a new context",
Args: cobra.ExactArgs(1),
Args: util.Validate,
TraverseChildren: true,
DisableFlagsInUseLine: true,
RunE: state.Wrap(s, runCreate),
Expand Down
3 changes: 2 additions & 1 deletion internal/cmd/context/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/spf13/cobra"

"github.com/hetznercloud/cli/internal/cmd/cmpl"
"github.com/hetznercloud/cli/internal/cmd/util"
"github.com/hetznercloud/cli/internal/state"
"github.com/hetznercloud/cli/internal/state/config"
)
Expand All @@ -15,7 +16,7 @@ func newDeleteCommand(s state.State) *cobra.Command {
cmd := &cobra.Command{
Use: "delete <context>",
Short: "Delete a context",
Args: cobra.ExactArgs(1),
Args: util.Validate,
ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidates(config.ContextNames(s.Config())...)),
TraverseChildren: true,
DisableFlagsInUseLine: true,
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/context/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func newListCommand(s state.State) *cobra.Command {
"Displays a list of contexts.",
listTableOutput.Columns(),
),
Args: cobra.NoArgs,
Args: util.Validate,
TraverseChildren: true,
DisableFlagsInUseLine: true,
RunE: state.Wrap(s, runList),
Expand Down
3 changes: 2 additions & 1 deletion internal/cmd/context/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/spf13/cobra"

"github.com/hetznercloud/cli/internal/cmd/cmpl"
"github.com/hetznercloud/cli/internal/cmd/util"
"github.com/hetznercloud/cli/internal/state"
"github.com/hetznercloud/cli/internal/state/config"
)
Expand All @@ -15,7 +16,7 @@ func newUseCommand(s state.State) *cobra.Command {
cmd := &cobra.Command{
Use: "use <context>",
Short: "Use a context",
Args: cobra.ExactArgs(1),
Args: util.Validate,
ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidates(config.ContextNames(s.Config())...)),
TraverseChildren: true,
DisableFlagsInUseLine: true,
Expand Down
3 changes: 2 additions & 1 deletion internal/cmd/datacenter/datacenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package datacenter
import (
"github.com/spf13/cobra"

"github.com/hetznercloud/cli/internal/cmd/util"
"github.com/hetznercloud/cli/internal/state"
)

func NewCommand(s state.State) *cobra.Command {
cmd := &cobra.Command{
Use: "datacenter",
Short: "Manage datacenters",
Args: cobra.NoArgs,
Args: util.Validate,
TraverseChildren: true,
DisableFlagsInUseLine: true,
}
Expand Down
3 changes: 2 additions & 1 deletion internal/cmd/firewall/add_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/hetznercloud/cli/internal/cmd/base"
"github.com/hetznercloud/cli/internal/cmd/cmpl"
"github.com/hetznercloud/cli/internal/cmd/util"
"github.com/hetznercloud/cli/internal/hcapi2"
"github.com/hetznercloud/cli/internal/state"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
Expand All @@ -18,7 +19,7 @@ var AddRuleCmd = base.Cmd{
cmd := &cobra.Command{
Use: "add-rule [options] <firewall> (--direction in --source-ips <ips> | --direction out --destination-ips <ips>) --protocol <icmp|udp|tcp|esp|gre>",
Short: "Add a single rule to a firewall",
Args: cobra.ExactArgs(1),
Args: util.Validate,
ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(client.Firewall().Names)),
TraverseChildren: true,
DisableFlagsInUseLine: true,
Expand Down
3 changes: 2 additions & 1 deletion internal/cmd/firewall/apply_to_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/hetznercloud/cli/internal/cmd/base"
"github.com/hetznercloud/cli/internal/cmd/cmpl"
"github.com/hetznercloud/cli/internal/cmd/util"
"github.com/hetznercloud/cli/internal/hcapi2"
"github.com/hetznercloud/cli/internal/state"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
Expand All @@ -17,7 +18,7 @@ var ApplyToResourceCmd = base.Cmd{
cmd := &cobra.Command{
Use: "apply-to-resource <firewall> (--type server --server <server> | --type label_selector --label-selector <label-selector>)",
Short: "Applies a Firewall to a single resource",
Args: cobra.ExactArgs(1),
Args: util.Validate,
ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(client.Firewall().Names)),
TraverseChildren: true,
DisableFlagsInUseLine: true,
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/firewall/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var CreateCmd = base.CreateCmd{
cmd := &cobra.Command{
Use: "create [options] --name <name>",
Short: "Create a Firewall",
Args: cobra.NoArgs,
Args: util.Validate,
}
cmd.Flags().String("name", "", "Name")
cmd.MarkFlagRequired("name")
Expand Down
3 changes: 2 additions & 1 deletion internal/cmd/firewall/delete_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/hetznercloud/cli/internal/cmd/base"
"github.com/hetznercloud/cli/internal/cmd/cmpl"
"github.com/hetznercloud/cli/internal/cmd/util"
"github.com/hetznercloud/cli/internal/hcapi2"
"github.com/hetznercloud/cli/internal/state"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
Expand All @@ -19,7 +20,7 @@ var DeleteRuleCmd = base.Cmd{
cmd := &cobra.Command{
Use: "delete-rule [options] <firewall> (--direction in --source-ips <ips> | --direction out --destination-ips <ips>) --protocol <icmp|esp|gre|udp|tcp>",
Short: "Delete a single rule to a firewall",
Args: cobra.ExactArgs(1),
Args: util.Validate,
ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(client.Firewall().Names)),
TraverseChildren: true,
DisableFlagsInUseLine: true,
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/firewall/firewall.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func NewCommand(s state.State) *cobra.Command {
cmd := &cobra.Command{
Use: "firewall",
Short: "Manage Firewalls",
Args: cobra.NoArgs,
Args: util.Validate,
TraverseChildren: true,
DisableFlagsInUseLine: true,
}
Expand Down
3 changes: 2 additions & 1 deletion internal/cmd/firewall/remove_from_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/hetznercloud/cli/internal/cmd/base"
"github.com/hetznercloud/cli/internal/cmd/cmpl"
"github.com/hetznercloud/cli/internal/cmd/util"
"github.com/hetznercloud/cli/internal/hcapi2"
"github.com/hetznercloud/cli/internal/state"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
Expand All @@ -17,7 +18,7 @@ var RemoveFromResourceCmd = base.Cmd{
cmd := &cobra.Command{
Use: "remove-from-resource <firewall> (--type server --server <server> | --type label_selector --label-selector <label-selector>)",
Short: "Removes a Firewall from a single resource",
Args: cobra.ExactArgs(1),
Args: util.Validate,
ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(client.Firewall().Names)),
TraverseChildren: true,
DisableFlagsInUseLine: true,
Expand Down
3 changes: 2 additions & 1 deletion internal/cmd/firewall/replace_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/hetznercloud/cli/internal/cmd/base"
"github.com/hetznercloud/cli/internal/cmd/cmpl"
"github.com/hetznercloud/cli/internal/cmd/util"
"github.com/hetznercloud/cli/internal/hcapi2"
"github.com/hetznercloud/cli/internal/state"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
Expand All @@ -22,7 +23,7 @@ var ReplaceRulesCmd = base.Cmd{
cmd := &cobra.Command{
Use: "replace-rules <firewall> --rules-file <file>",
Short: "Replaces all rules from a Firewall from a file",
Args: cobra.ExactArgs(1),
Args: util.Validate,
ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(client.Firewall().Names)),
TraverseChildren: true,
DisableFlagsInUseLine: true,
Expand Down
3 changes: 2 additions & 1 deletion internal/cmd/floatingip/assign.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/hetznercloud/cli/internal/cmd/base"
"github.com/hetznercloud/cli/internal/cmd/cmpl"
"github.com/hetznercloud/cli/internal/cmd/util"
"github.com/hetznercloud/cli/internal/hcapi2"
"github.com/hetznercloud/cli/internal/state"
)
Expand All @@ -16,7 +17,7 @@ var AssignCmd = base.Cmd{
return &cobra.Command{
Use: "assign <floating-ip> <server>",
Short: "Assign a Floating IP to a server",
Args: cobra.ExactArgs(2),
Args: util.Validate,
ValidArgsFunction: cmpl.SuggestArgs(
cmpl.SuggestCandidatesF(client.FloatingIP().Names),
cmpl.SuggestCandidatesF(client.Server().Names),
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/floatingip/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var CreateCmd = base.CreateCmd{
cmd := &cobra.Command{
Use: "create [options] --type <ipv4|ipv6> (--home-location <location> | --server <server>)",
Short: "Create a Floating IP",
Args: cobra.NoArgs,
Args: util.Validate,
TraverseChildren: true,
DisableFlagsInUseLine: true,
}
Expand Down
3 changes: 2 additions & 1 deletion internal/cmd/floatingip/disable_protection.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/hetznercloud/cli/internal/cmd/base"
"github.com/hetznercloud/cli/internal/cmd/cmpl"
"github.com/hetznercloud/cli/internal/cmd/util"
"github.com/hetznercloud/cli/internal/hcapi2"
"github.com/hetznercloud/cli/internal/state"
)
Expand All @@ -16,7 +17,7 @@ var DisableProtectionCmd = base.Cmd{
return &cobra.Command{
Use: "disable-protection <floating-ip> <protection-level>...",
Short: "Disable resource protection for a Floating IP",
Args: cobra.MinimumNArgs(2),
Args: util.Validate,
ValidArgsFunction: cmpl.SuggestArgs(
cmpl.SuggestCandidatesF(client.FloatingIP().Names),
cmpl.SuggestCandidates("delete"),
Expand Down
3 changes: 2 additions & 1 deletion internal/cmd/floatingip/enable_protection.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/hetznercloud/cli/internal/cmd/base"
"github.com/hetznercloud/cli/internal/cmd/cmpl"
"github.com/hetznercloud/cli/internal/cmd/util"
"github.com/hetznercloud/cli/internal/hcapi2"
"github.com/hetznercloud/cli/internal/state"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
Expand Down Expand Up @@ -62,7 +63,7 @@ var EnableProtectionCmd = base.Cmd{
return &cobra.Command{
Use: "enable-protection <floating-ip> <protection-level>...",
Short: "Enable resource protection for a Floating IP",
Args: cobra.MinimumNArgs(2),
Args: util.Validate,
ValidArgsFunction: cmpl.SuggestArgs(
cmpl.SuggestCandidatesF(client.FloatingIP().Names),
cmpl.SuggestCandidates("delete"),
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/floatingip/floatingip.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func NewCommand(s state.State) *cobra.Command {
cmd := &cobra.Command{
Use: "floating-ip",
Short: "Manage Floating IPs",
Args: cobra.NoArgs,
Args: util.Validate,
TraverseChildren: true,
DisableFlagsInUseLine: true,
}
Expand Down
Loading

0 comments on commit a534ce4

Please sign in to comment.