diff --git a/internal/cmd/certificate/certificate.go b/internal/cmd/certificate/certificate.go index 5288a8be..550d3c45 100644 --- a/internal/cmd/certificate/certificate.go +++ b/internal/cmd/certificate/certificate.go @@ -17,12 +17,12 @@ func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command { } cmd.AddCommand( ListCmd.CobraCommand(cli.Context, client, cli), - newCreateCommand(cli), - updateCmd.CobraCommand(cli.Context, client, cli), - labelCmds.AddCobraCommand(cli.Context, client, cli), - labelCmds.RemoveCobraCommand(cli.Context, client, cli), - deleteCmd.CobraCommand(cli.Context, client, cli, cli), - describeCmd.CobraCommand(cli.Context, client, cli), + CreateCmd.CobraCommand(cli.Context, client, cli, cli), + UpdateCmd.CobraCommand(cli.Context, client, cli), + LabelCmds.AddCobraCommand(cli.Context, client, cli), + LabelCmds.RemoveCobraCommand(cli.Context, client, cli), + DeleteCmd.CobraCommand(cli.Context, client, cli, cli), + DescribeCmd.CobraCommand(cli.Context, client, cli), ) return cmd diff --git a/internal/cmd/certificate/create.go b/internal/cmd/certificate/create.go index 64851d60..d48ac745 100644 --- a/internal/cmd/certificate/create.go +++ b/internal/cmd/certificate/create.go @@ -1,66 +1,65 @@ package certificate import ( + "context" "fmt" - "io/ioutil" + "os" "github.com/spf13/cobra" + "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" ) -func newCreateCommand(cli *state.State) *cobra.Command { - cmd := &cobra.Command{ - Use: "create [FLAGS]", - Short: "Create or upload a Certificate", - Args: cobra.NoArgs, - TraverseChildren: true, - DisableFlagsInUseLine: true, - PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runCreate), - } - - cmd.Flags().String("name", "", "Certificate name (required)") - cmd.MarkFlagRequired("name") - - cmd.Flags().StringP("type", "t", string(hcloud.CertificateTypeUploaded), - fmt.Sprintf("Type of certificate to create. Valid choices: %v, %v", - hcloud.CertificateTypeUploaded, hcloud.CertificateTypeManaged)) - cmd.RegisterFlagCompletionFunc( - "type", - cmpl.SuggestCandidates(string(hcloud.CertificateTypeUploaded), string(hcloud.CertificateTypeManaged)), - ) - - cmd.Flags().String("cert-file", "", "File containing the PEM encoded certificate (required if type is uploaded)") - cmd.Flags().String("key-file", "", - "File containing the PEM encoded private key for the certificate (required if type is uploaded)") - cmd.Flags().StringSlice("domain", nil, "One or more domains the certificate is valid for.") - - return cmd +var CreateCmd = base.Cmd{ + BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { + cmd := &cobra.Command{ + Use: "create [FLAGS]", + Short: "Create or upload a Certificate", + Args: cobra.ExactArgs(0), + } + + cmd.Flags().String("name", "", "Certificate name (required)") + cmd.MarkFlagRequired("name") + + cmd.Flags().StringP("type", "t", string(hcloud.CertificateTypeUploaded), + fmt.Sprintf("Type of certificate to create. Valid choices: %v, %v", + hcloud.CertificateTypeUploaded, hcloud.CertificateTypeManaged)) + cmd.RegisterFlagCompletionFunc( + "type", + cmpl.SuggestCandidates(string(hcloud.CertificateTypeUploaded), string(hcloud.CertificateTypeManaged)), + ) + + cmd.Flags().String("cert-file", "", "File containing the PEM encoded certificate (required if type is uploaded)") + cmd.Flags().String("key-file", "", + "File containing the PEM encoded private key for the certificate (required if type is uploaded)") + cmd.Flags().StringSlice("domain", nil, "One or more domains the certificate is valid for.") + + return cmd + }, + Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, strings []string) error { + certType, err := cmd.Flags().GetString("type") + if err != nil { + return err + } + switch hcloud.CertificateType(certType) { + case hcloud.CertificateTypeUploaded: + return createUploaded(ctx, client, cmd) + case hcloud.CertificateTypeManaged: + return createManaged(ctx, client, waiter, cmd) + default: + return createUploaded(ctx, client, cmd) + } + }, } -func runCreate(cli *state.State, cmd *cobra.Command, args []string) error { - certType, err := cmd.Flags().GetString("type") - if err != nil { - return err - } - switch hcloud.CertificateType(certType) { - case hcloud.CertificateTypeUploaded: - return createUploaded(cli, cmd, args) - case hcloud.CertificateTypeManaged: - return createManaged(cli, cmd, args) - default: - return createUploaded(cli, cmd, args) - } -} - -func createUploaded(cli *state.State, cmd *cobra.Command, args []string) error { +func createUploaded(ctx context.Context, client hcapi2.Client, cmd *cobra.Command) error { var ( - name string - + name string certFile, keyFile string certPEM, keyPEM []byte cert *hcloud.Certificate @@ -81,10 +80,10 @@ func createUploaded(cli *state.State, cmd *cobra.Command, args []string) error { return err } - if certPEM, err = ioutil.ReadFile(certFile); err != nil { + if certPEM, err = os.ReadFile(certFile); err != nil { return err } - if keyPEM, err = ioutil.ReadFile(keyFile); err != nil { + if keyPEM, err = os.ReadFile(keyFile); err != nil { return err } @@ -94,14 +93,14 @@ func createUploaded(cli *state.State, cmd *cobra.Command, args []string) error { Certificate: string(certPEM), PrivateKey: string(keyPEM), } - if cert, _, err = cli.Client().Certificate.Create(cli.Context, createOpts); err != nil { + if cert, _, err = client.Certificate().Create(ctx, createOpts); err != nil { return err } fmt.Printf("Certificate %d created\n", cert.ID) return nil } -func createManaged(cli *state.State, cmd *cobra.Command, args []string) error { +func createManaged(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command) error { var ( name string domains []string @@ -124,10 +123,10 @@ func createManaged(cli *state.State, cmd *cobra.Command, args []string) error { Type: hcloud.CertificateTypeManaged, DomainNames: domains, } - if res, _, err = cli.Client().Certificate.CreateCertificate(cli.Context, createOpts); err != nil { + if res, _, err = client.Certificate().CreateCertificate(ctx, createOpts); err != nil { return err } - if err := cli.ActionProgress(cli.Context, res.Action); err != nil { + if err := waiter.ActionProgress(ctx, res.Action); err != nil { return err } fmt.Printf("Certificate %d created\n", res.Certificate.ID) diff --git a/internal/cmd/certificate/delete.go b/internal/cmd/certificate/delete.go index 6b5e0353..a1d88bee 100644 --- a/internal/cmd/certificate/delete.go +++ b/internal/cmd/certificate/delete.go @@ -11,7 +11,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var deleteCmd = base.DeleteCmd{ +var DeleteCmd = base.DeleteCmd{ ResourceNameSingular: "certificate", ShortDescription: "Delete a certificate", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Firewall().Names }, diff --git a/internal/cmd/certificate/describe.go b/internal/cmd/certificate/describe.go index 71a33005..b286a6cb 100644 --- a/internal/cmd/certificate/describe.go +++ b/internal/cmd/certificate/describe.go @@ -13,7 +13,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var describeCmd = base.DescribeCmd{ +var DescribeCmd = base.DescribeCmd{ ResourceNameSingular: "certificate", ShortDescription: "Describe an certificate", JSONKeyGetByID: "certificate", diff --git a/internal/cmd/certificate/labels.go b/internal/cmd/certificate/labels.go index dbc2619d..7803ddc6 100644 --- a/internal/cmd/certificate/labels.go +++ b/internal/cmd/certificate/labels.go @@ -9,7 +9,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var labelCmds = base.LabelCmds{ +var LabelCmds = base.LabelCmds{ ResourceNameSingular: "certificate", ShortDescriptionAdd: "Add a label to an certificate", ShortDescriptionRemove: "Remove a label from an certificate", diff --git a/internal/cmd/certificate/update.go b/internal/cmd/certificate/update.go index 26331c80..a793b2d3 100644 --- a/internal/cmd/certificate/update.go +++ b/internal/cmd/certificate/update.go @@ -11,7 +11,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var updateCmd = base.UpdateCmd{ +var UpdateCmd = base.UpdateCmd{ ResourceNameSingular: "certificate", ShortDescription: "Update a certificate", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Firewall().Names }, diff --git a/internal/cmd/datacenter/datacenter.go b/internal/cmd/datacenter/datacenter.go index 19d66659..523da491 100644 --- a/internal/cmd/datacenter/datacenter.go +++ b/internal/cmd/datacenter/datacenter.go @@ -17,7 +17,7 @@ func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command { } cmd.AddCommand( ListCmd.CobraCommand(cli.Context, client, cli), - describeCmd.CobraCommand(cli.Context, client, cli), + DescribeCmd.CobraCommand(cli.Context, client, cli), ) return cmd } diff --git a/internal/cmd/datacenter/describe.go b/internal/cmd/datacenter/describe.go index 121c25ff..819cfb95 100644 --- a/internal/cmd/datacenter/describe.go +++ b/internal/cmd/datacenter/describe.go @@ -11,7 +11,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var describeCmd = base.DescribeCmd{ +var DescribeCmd = base.DescribeCmd{ ResourceNameSingular: "datacenter", ShortDescription: "Describe an datacenter", JSONKeyGetByID: "datacenter", diff --git a/internal/cmd/firewall/add_rule.go b/internal/cmd/firewall/add_rule.go index 8f5f02c5..f19d7b73 100644 --- a/internal/cmd/firewall/add_rule.go +++ b/internal/cmd/firewall/add_rule.go @@ -14,7 +14,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var AddRuleCommand = base.Cmd{ +var AddRuleCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { cmd := &cobra.Command{ Use: "add-rule FIREWALL FLAGS", diff --git a/internal/cmd/firewall/apply_to_resource.go b/internal/cmd/firewall/apply_to_resource.go index 4c71b47c..9b40d137 100644 --- a/internal/cmd/firewall/apply_to_resource.go +++ b/internal/cmd/firewall/apply_to_resource.go @@ -13,7 +13,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var ApplyToResourceCommand = base.Cmd{ +var ApplyToResourceCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { cmd := &cobra.Command{ Use: "apply-to-resource FIREWALL FLAGS", diff --git a/internal/cmd/firewall/create.go b/internal/cmd/firewall/create.go index 95072c47..e45824fc 100644 --- a/internal/cmd/firewall/create.go +++ b/internal/cmd/firewall/create.go @@ -1,6 +1,7 @@ package firewall import ( + "context" "encoding/json" "fmt" "io/ioutil" @@ -9,83 +10,81 @@ import ( "github.com/spf13/cobra" - "github.com/hetznercloud/cli/internal/cmd/util" + "github.com/hetznercloud/cli/internal/cmd/base" + "github.com/hetznercloud/cli/internal/hcapi2" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/v2/hcloud" "github.com/hetznercloud/hcloud-go/v2/hcloud/schema" ) -func newCreateCommand(cli *state.State) *cobra.Command { - cmd := &cobra.Command{ - Use: "create FLAGS", - Short: "Create a Firewall", - Args: cobra.NoArgs, - TraverseChildren: true, - DisableFlagsInUseLine: true, - PreRunE: util.ChainRunE(cli.EnsureToken), - RunE: cli.Wrap(runFirewallCreate), - } - cmd.Flags().String("name", "", "Name") - cmd.MarkFlagRequired("name") - - cmd.Flags().StringToString("label", nil, "User-defined labels ('key=value') (can be specified multiple times)") +var CreateCmd = base.Cmd{ + BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { + cmd := &cobra.Command{ + Use: "create FLAGS", + Short: "Create a Firewall", + Args: cobra.NoArgs, + } + cmd.Flags().String("name", "", "Name") + cmd.MarkFlagRequired("name") - cmd.Flags().String("rules-file", "", "JSON file containing your routes (use - to read from stdin). The structure of the file needs to be the same as within the API: https://docs.hetzner.cloud/#firewalls-get-a-firewall ") - return cmd -} + cmd.Flags().StringToString("label", nil, "User-defined labels ('key=value') (can be specified multiple times)") -func runFirewallCreate(cli *state.State, cmd *cobra.Command, args []string) error { - name, _ := cmd.Flags().GetString("name") - labels, _ := cmd.Flags().GetStringToString("label") + cmd.Flags().String("rules-file", "", "JSON file containing your routes (use - to read from stdin). The structure of the file needs to be the same as within the API: https://docs.hetzner.cloud/#firewalls-get-a-firewall ") + return cmd + }, + Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, strings []string) error { + name, _ := cmd.Flags().GetString("name") + labels, _ := cmd.Flags().GetStringToString("label") - opts := hcloud.FirewallCreateOpts{ - Name: name, - Labels: labels, - } + opts := hcloud.FirewallCreateOpts{ + Name: name, + Labels: labels, + } - rulesFile, _ := cmd.Flags().GetString("rules-file") + rulesFile, _ := cmd.Flags().GetString("rules-file") - if len(rulesFile) > 0 { - var data []byte - var err error - if rulesFile == "-" { - data, err = ioutil.ReadAll(os.Stdin) - } else { - data, err = ioutil.ReadFile(rulesFile) - } - if err != nil { - return err - } - var rules []schema.FirewallRule - err = json.Unmarshal(data, &rules) - if err != nil { - return err - } - for _, rule := range rules { - var sourceNets []net.IPNet - for i, sourceIP := range rule.SourceIPs { - _, sourceNet, err := net.ParseCIDR(sourceIP) - if err != nil { - return fmt.Errorf("invalid CIDR on index %d : %s", i, err) + if len(rulesFile) > 0 { + var data []byte + var err error + if rulesFile == "-" { + data, err = ioutil.ReadAll(os.Stdin) + } else { + data, err = ioutil.ReadFile(rulesFile) + } + if err != nil { + return err + } + var rules []schema.FirewallRule + err = json.Unmarshal(data, &rules) + if err != nil { + return err + } + for _, rule := range rules { + var sourceNets []net.IPNet + for i, sourceIP := range rule.SourceIPs { + _, sourceNet, err := net.ParseCIDR(sourceIP) + if err != nil { + return fmt.Errorf("invalid CIDR on index %d : %s", i, err) + } + sourceNets = append(sourceNets, *sourceNet) } - sourceNets = append(sourceNets, *sourceNet) + opts.Rules = append(opts.Rules, hcloud.FirewallRule{ + Direction: hcloud.FirewallRuleDirection(rule.Direction), + SourceIPs: sourceNets, + Protocol: hcloud.FirewallRuleProtocol(rule.Protocol), + Port: rule.Port, + Description: rule.Description, + }) } - opts.Rules = append(opts.Rules, hcloud.FirewallRule{ - Direction: hcloud.FirewallRuleDirection(rule.Direction), - SourceIPs: sourceNets, - Protocol: hcloud.FirewallRuleProtocol(rule.Protocol), - Port: rule.Port, - Description: rule.Description, - }) } - } - result, _, err := cli.Client().Firewall.Create(cli.Context, opts) - if err != nil { - return err - } + result, _, err := client.Firewall().Create(ctx, opts) + if err != nil { + return err + } - fmt.Printf("Firewall %d created\n", result.Firewall.ID) + fmt.Printf("Firewall %d created\n", result.Firewall.ID) - return nil + return nil + }, } diff --git a/internal/cmd/firewall/delete.go b/internal/cmd/firewall/delete.go index 97e863ad..b2f0edb3 100644 --- a/internal/cmd/firewall/delete.go +++ b/internal/cmd/firewall/delete.go @@ -11,7 +11,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var deleteCmd = base.DeleteCmd{ +var DeleteCmd = base.DeleteCmd{ ResourceNameSingular: "firewall", ShortDescription: "Delete a firewall", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Firewall().Names }, diff --git a/internal/cmd/firewall/delete_rule.go b/internal/cmd/firewall/delete_rule.go index 69df58af..e5c08447 100644 --- a/internal/cmd/firewall/delete_rule.go +++ b/internal/cmd/firewall/delete_rule.go @@ -15,7 +15,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var DeleteRuleCommand = base.Cmd{ +var DeleteRuleCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { cmd := &cobra.Command{ Use: "delete-rule FIREWALL FLAGS", diff --git a/internal/cmd/firewall/describe.go b/internal/cmd/firewall/describe.go index 8090ba6c..f76ea402 100644 --- a/internal/cmd/firewall/describe.go +++ b/internal/cmd/firewall/describe.go @@ -14,7 +14,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var describeCmd = base.DescribeCmd{ +var DescribeCmd = base.DescribeCmd{ ResourceNameSingular: "firewall", ShortDescription: "Describe an firewall", JSONKeyGetByID: "firewall", diff --git a/internal/cmd/firewall/firewall.go b/internal/cmd/firewall/firewall.go index a57e0273..c04b7f59 100644 --- a/internal/cmd/firewall/firewall.go +++ b/internal/cmd/firewall/firewall.go @@ -17,17 +17,17 @@ func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command { } cmd.AddCommand( ListCmd.CobraCommand(cli.Context, client, cli), - describeCmd.CobraCommand(cli.Context, client, cli), - newCreateCommand(cli), - updateCmd.CobraCommand(cli.Context, client, cli), - ReplaceRulesCommand.CobraCommand(cli.Context, client, cli, cli), - deleteCmd.CobraCommand(cli.Context, client, cli, cli), - AddRuleCommand.CobraCommand(cli.Context, client, cli, cli), - DeleteRuleCommand.CobraCommand(cli.Context, client, cli, cli), - ApplyToResourceCommand.CobraCommand(cli.Context, client, cli, cli), - RemoveFromResourceCommand.CobraCommand(cli.Context, client, cli, cli), - labelCmds.AddCobraCommand(cli.Context, client, cli), - labelCmds.RemoveCobraCommand(cli.Context, client, cli), + DescribeCmd.CobraCommand(cli.Context, client, cli), + CreateCmd.CobraCommand(cli.Context, client, cli, cli), + UpdateCmd.CobraCommand(cli.Context, client, cli), + ReplaceRulesCmd.CobraCommand(cli.Context, client, cli, cli), + DeleteCmd.CobraCommand(cli.Context, client, cli, cli), + AddRuleCmd.CobraCommand(cli.Context, client, cli, cli), + DeleteRuleCmd.CobraCommand(cli.Context, client, cli, cli), + ApplyToResourceCmd.CobraCommand(cli.Context, client, cli, cli), + RemoveFromResourceCmd.CobraCommand(cli.Context, client, cli, cli), + LabelCmds.AddCobraCommand(cli.Context, client, cli), + LabelCmds.RemoveCobraCommand(cli.Context, client, cli), ) return cmd } diff --git a/internal/cmd/firewall/labels.go b/internal/cmd/firewall/labels.go index f61aebb3..0df33515 100644 --- a/internal/cmd/firewall/labels.go +++ b/internal/cmd/firewall/labels.go @@ -9,7 +9,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var labelCmds = base.LabelCmds{ +var LabelCmds = base.LabelCmds{ ResourceNameSingular: "firewall", ShortDescriptionAdd: "Add a label to an firewall", ShortDescriptionRemove: "Remove a label from an firewall", diff --git a/internal/cmd/firewall/remove_from_resource.go b/internal/cmd/firewall/remove_from_resource.go index 3baff9af..df2a8a98 100644 --- a/internal/cmd/firewall/remove_from_resource.go +++ b/internal/cmd/firewall/remove_from_resource.go @@ -13,7 +13,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var RemoveFromResourceCommand = base.Cmd{ +var RemoveFromResourceCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { cmd := &cobra.Command{ Use: "remove-from-resource FIREWALL FLAGS", diff --git a/internal/cmd/firewall/replace_rules.go b/internal/cmd/firewall/replace_rules.go index cfdc166a..084396ab 100644 --- a/internal/cmd/firewall/replace_rules.go +++ b/internal/cmd/firewall/replace_rules.go @@ -18,7 +18,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud/schema" ) -var ReplaceRulesCommand = base.Cmd{ +var ReplaceRulesCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { cmd := &cobra.Command{ Use: "replace-rules FIREWALL FLAGS", diff --git a/internal/cmd/firewall/update.go b/internal/cmd/firewall/update.go index ce32cfdc..fc53a6a8 100644 --- a/internal/cmd/firewall/update.go +++ b/internal/cmd/firewall/update.go @@ -11,7 +11,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var updateCmd = base.UpdateCmd{ +var UpdateCmd = base.UpdateCmd{ ResourceNameSingular: "Firewall", ShortDescription: "Update a firewall", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Firewall().Names }, diff --git a/internal/cmd/floatingip/assign.go b/internal/cmd/floatingip/assign.go index 237cfc10..c341d2da 100644 --- a/internal/cmd/floatingip/assign.go +++ b/internal/cmd/floatingip/assign.go @@ -12,7 +12,7 @@ import ( "github.com/hetznercloud/cli/internal/state" ) -var AssignCommand = base.Cmd{ +var AssignCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { return &cobra.Command{ Use: "assign [FLAGS] FLOATINGIP SERVER", diff --git a/internal/cmd/floatingip/create.go b/internal/cmd/floatingip/create.go index bb941ac4..e9979fc2 100644 --- a/internal/cmd/floatingip/create.go +++ b/internal/cmd/floatingip/create.go @@ -14,7 +14,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var CreateCommand = base.Cmd{ +var CreateCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { cmd := &cobra.Command{ Use: "create FLAGS", diff --git a/internal/cmd/floatingip/delete.go b/internal/cmd/floatingip/delete.go index ef22f8a8..334b03a9 100644 --- a/internal/cmd/floatingip/delete.go +++ b/internal/cmd/floatingip/delete.go @@ -11,7 +11,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var deleteCmd = base.DeleteCmd{ +var DeleteCmd = base.DeleteCmd{ ResourceNameSingular: "Floating IP", ShortDescription: "Delete a Floating IP", NameSuggestions: func(c hcapi2.Client) func() []string { return c.FloatingIP().Names }, diff --git a/internal/cmd/floatingip/describe.go b/internal/cmd/floatingip/describe.go index 3ab48baa..a04d37e6 100644 --- a/internal/cmd/floatingip/describe.go +++ b/internal/cmd/floatingip/describe.go @@ -13,7 +13,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var describeCmd = base.DescribeCmd{ +var DescribeCmd = base.DescribeCmd{ ResourceNameSingular: "Floating IP", ShortDescription: "Describe an Floating IP", JSONKeyGetByID: "floating_ip", diff --git a/internal/cmd/floatingip/disable_protection.go b/internal/cmd/floatingip/disable_protection.go index db2ba4e3..2f507411 100644 --- a/internal/cmd/floatingip/disable_protection.go +++ b/internal/cmd/floatingip/disable_protection.go @@ -12,7 +12,7 @@ import ( "github.com/hetznercloud/cli/internal/state" ) -var DisableProtectionCommand = base.Cmd{ +var DisableProtectionCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { return &cobra.Command{ Use: "disable-protection [FLAGS] FLOATINGIP PROTECTIONLEVEL [PROTECTIONLEVEL...]", diff --git a/internal/cmd/floatingip/enable_protection.go b/internal/cmd/floatingip/enable_protection.go index 2956f780..816a6fe3 100644 --- a/internal/cmd/floatingip/enable_protection.go +++ b/internal/cmd/floatingip/enable_protection.go @@ -57,7 +57,7 @@ func changeProtection(ctx context.Context, client hcapi2.Client, waiter state.Ac return nil } -var EnableProtectionCommand = base.Cmd{ +var EnableProtectionCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { return &cobra.Command{ Use: "enable-protection [FLAGS] FLOATINGIP PROTECTIONLEVEL [PROTECTIONLEVEL...]", diff --git a/internal/cmd/floatingip/floatingip.go b/internal/cmd/floatingip/floatingip.go index b98876e1..464c38d0 100644 --- a/internal/cmd/floatingip/floatingip.go +++ b/internal/cmd/floatingip/floatingip.go @@ -16,18 +16,18 @@ func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command { DisableFlagsInUseLine: true, } cmd.AddCommand( - updateCmd.CobraCommand(cli.Context, client, cli), + UpdateCmd.CobraCommand(cli.Context, client, cli), ListCmd.CobraCommand(cli.Context, client, cli), - CreateCommand.CobraCommand(cli.Context, client, cli, cli), - describeCmd.CobraCommand(cli.Context, client, cli), - AssignCommand.CobraCommand(cli.Context, client, cli, cli), - UnassignCommand.CobraCommand(cli.Context, client, cli, cli), - deleteCmd.CobraCommand(cli.Context, client, cli, cli), - EnableProtectionCommand.CobraCommand(cli.Context, client, cli, cli), - DisableProtectionCommand.CobraCommand(cli.Context, client, cli, cli), - labelCmds.AddCobraCommand(cli.Context, client, cli), - labelCmds.RemoveCobraCommand(cli.Context, client, cli), - setRDNSCmd.CobraCommand(cli.Context, client, cli, cli), + CreateCmd.CobraCommand(cli.Context, client, cli, cli), + DescribeCmd.CobraCommand(cli.Context, client, cli), + AssignCmd.CobraCommand(cli.Context, client, cli, cli), + UnassignCmd.CobraCommand(cli.Context, client, cli, cli), + DeleteCmd.CobraCommand(cli.Context, client, cli, cli), + EnableProtectionCmd.CobraCommand(cli.Context, client, cli, cli), + DisableProtectionCmd.CobraCommand(cli.Context, client, cli, cli), + LabelCmds.AddCobraCommand(cli.Context, client, cli), + LabelCmds.RemoveCobraCommand(cli.Context, client, cli), + SetRDNSCmd.CobraCommand(cli.Context, client, cli, cli), ) return cmd } diff --git a/internal/cmd/floatingip/labels.go b/internal/cmd/floatingip/labels.go index b882744e..824e5ac5 100644 --- a/internal/cmd/floatingip/labels.go +++ b/internal/cmd/floatingip/labels.go @@ -9,7 +9,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var labelCmds = base.LabelCmds{ +var LabelCmds = base.LabelCmds{ ResourceNameSingular: "Floating IP", ShortDescriptionAdd: "Add a label to an Floating IP", ShortDescriptionRemove: "Remove a label from an Floating IP", diff --git a/internal/cmd/floatingip/set_rdns.go b/internal/cmd/floatingip/set_rdns.go index 9b8520c1..1cd1cf6e 100644 --- a/internal/cmd/floatingip/set_rdns.go +++ b/internal/cmd/floatingip/set_rdns.go @@ -11,7 +11,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var setRDNSCmd = base.SetRdnsCmd{ +var SetRDNSCmd = base.SetRdnsCmd{ ResourceNameSingular: "Floating IP", ShortDescription: "Change reverse DNS of a Floating IP", NameSuggestions: func(c hcapi2.Client) func() []string { return c.FloatingIP().Names }, diff --git a/internal/cmd/floatingip/unassign.go b/internal/cmd/floatingip/unassign.go index 67656281..03a33653 100644 --- a/internal/cmd/floatingip/unassign.go +++ b/internal/cmd/floatingip/unassign.go @@ -12,7 +12,7 @@ import ( "github.com/hetznercloud/cli/internal/state" ) -var UnassignCommand = base.Cmd{ +var UnassignCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { return &cobra.Command{ Use: "unassign [FLAGS] FLOATINGIP", diff --git a/internal/cmd/floatingip/update.go b/internal/cmd/floatingip/update.go index 5b1c1307..94078395 100644 --- a/internal/cmd/floatingip/update.go +++ b/internal/cmd/floatingip/update.go @@ -11,7 +11,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var updateCmd = base.UpdateCmd{ +var UpdateCmd = base.UpdateCmd{ ResourceNameSingular: "Floating IP", ShortDescription: "Update a Floating IP", NameSuggestions: func(c hcapi2.Client) func() []string { return c.FloatingIP().Names }, diff --git a/internal/cmd/image/delete.go b/internal/cmd/image/delete.go index f0fd5e94..3a19e52f 100644 --- a/internal/cmd/image/delete.go +++ b/internal/cmd/image/delete.go @@ -11,7 +11,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var deleteCmd = base.DeleteCmd{ +var DeleteCmd = base.DeleteCmd{ ResourceNameSingular: "image", ShortDescription: "Delete an image", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Image().Names }, diff --git a/internal/cmd/image/describe.go b/internal/cmd/image/describe.go index 996ad1d0..625b870b 100644 --- a/internal/cmd/image/describe.go +++ b/internal/cmd/image/describe.go @@ -15,7 +15,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var describeCmd = base.DescribeCmd{ +var DescribeCmd = base.DescribeCmd{ ResourceNameSingular: "image", ShortDescription: "Describe an image", JSONKeyGetByID: "image", diff --git a/internal/cmd/image/disable_protection.go b/internal/cmd/image/disable_protection.go index d3453726..6582c302 100644 --- a/internal/cmd/image/disable_protection.go +++ b/internal/cmd/image/disable_protection.go @@ -14,7 +14,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var DisableProtectionCommand = base.Cmd{ +var DisableProtectionCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { return &cobra.Command{ Use: "disable-protection [FLAGS] IMAGE PROTECTIONLEVEL [PROTECTIONLEVEL...]", diff --git a/internal/cmd/image/enable_protection.go b/internal/cmd/image/enable_protection.go index 5133077c..755cfd2f 100644 --- a/internal/cmd/image/enable_protection.go +++ b/internal/cmd/image/enable_protection.go @@ -59,7 +59,7 @@ func changeProtection(ctx context.Context, client hcapi2.Client, waiter state.Ac return nil } -var EnableProtectionCommand = base.Cmd{ +var EnableProtectionCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { return &cobra.Command{ diff --git a/internal/cmd/image/image.go b/internal/cmd/image/image.go index e5bf878a..a1c1214d 100644 --- a/internal/cmd/image/image.go +++ b/internal/cmd/image/image.go @@ -17,13 +17,13 @@ func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command { } cmd.AddCommand( ListCmd.CobraCommand(cli.Context, client, cli), - deleteCmd.CobraCommand(cli.Context, client, cli, cli), - describeCmd.CobraCommand(cli.Context, client, cli), - updateCmd.CobraCommand(cli.Context, client, cli), - EnableProtectionCommand.CobraCommand(cli.Context, client, cli, cli), - DisableProtectionCommand.CobraCommand(cli.Context, client, cli, cli), - labelCmds.AddCobraCommand(cli.Context, client, cli), - labelCmds.RemoveCobraCommand(cli.Context, client, cli), + DeleteCmd.CobraCommand(cli.Context, client, cli, cli), + DescribeCmd.CobraCommand(cli.Context, client, cli), + UpdateCmd.CobraCommand(cli.Context, client, cli), + EnableProtectionCmd.CobraCommand(cli.Context, client, cli, cli), + DisableProtectionCmd.CobraCommand(cli.Context, client, cli, cli), + LabelCmds.AddCobraCommand(cli.Context, client, cli), + LabelCmds.RemoveCobraCommand(cli.Context, client, cli), ) return cmd } diff --git a/internal/cmd/image/labels.go b/internal/cmd/image/labels.go index 6fdbcb50..7fcce19a 100644 --- a/internal/cmd/image/labels.go +++ b/internal/cmd/image/labels.go @@ -9,7 +9,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var labelCmds = base.LabelCmds{ +var LabelCmds = base.LabelCmds{ ResourceNameSingular: "image", ShortDescriptionAdd: "Add a label to an image", ShortDescriptionRemove: "Remove a label from an image", diff --git a/internal/cmd/image/update.go b/internal/cmd/image/update.go index 9a254d77..84d3c669 100644 --- a/internal/cmd/image/update.go +++ b/internal/cmd/image/update.go @@ -12,7 +12,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var updateCmd = base.UpdateCmd{ +var UpdateCmd = base.UpdateCmd{ ResourceNameSingular: "Image", ShortDescription: "Update an image", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Image().Names }, diff --git a/internal/cmd/loadbalancer/add_service.go b/internal/cmd/loadbalancer/add_service.go index 89ea8042..55d081c2 100644 --- a/internal/cmd/loadbalancer/add_service.go +++ b/internal/cmd/loadbalancer/add_service.go @@ -13,7 +13,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var AddServiceCommand = base.Cmd{ +var AddServiceCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { cmd := &cobra.Command{ Use: "add-service LOADBALANCER FLAGS", diff --git a/internal/cmd/loadbalancer/add_target.go b/internal/cmd/loadbalancer/add_target.go index 46b81f86..6798933e 100644 --- a/internal/cmd/loadbalancer/add_target.go +++ b/internal/cmd/loadbalancer/add_target.go @@ -15,7 +15,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var AddTargetCommand = base.Cmd{ +var AddTargetCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { cmd := &cobra.Command{ Use: "add-target LOADBALANCER FLAGS", diff --git a/internal/cmd/loadbalancer/attach_to_network.go b/internal/cmd/loadbalancer/attach_to_network.go index 0d798c69..3b640f89 100644 --- a/internal/cmd/loadbalancer/attach_to_network.go +++ b/internal/cmd/loadbalancer/attach_to_network.go @@ -13,7 +13,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var AttachToNetworkCommand = base.Cmd{ +var AttachToNetworkCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { cmd := &cobra.Command{ Use: "attach-to-network [FLAGS] LOADBALANCER", diff --git a/internal/cmd/loadbalancer/change_algorithm.go b/internal/cmd/loadbalancer/change_algorithm.go index d415841a..28d05d1f 100644 --- a/internal/cmd/loadbalancer/change_algorithm.go +++ b/internal/cmd/loadbalancer/change_algorithm.go @@ -13,7 +13,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var ChangeAlgorithmCommand = base.Cmd{ +var ChangeAlgorithmCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { cmd := &cobra.Command{ Use: "change-algorithm LOADBALANCER FLAGS", diff --git a/internal/cmd/loadbalancer/change_type.go b/internal/cmd/loadbalancer/change_type.go index a9ead445..ff276f17 100644 --- a/internal/cmd/loadbalancer/change_type.go +++ b/internal/cmd/loadbalancer/change_type.go @@ -13,7 +13,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var ChangeTypeCommand = base.Cmd{ +var ChangeTypeCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { return &cobra.Command{ Use: "change-type [FLAGS] LOADBALANCER LOADBALANCERTYPE", diff --git a/internal/cmd/loadbalancer/create.go b/internal/cmd/loadbalancer/create.go index 55d9ad75..9c6fb23a 100644 --- a/internal/cmd/loadbalancer/create.go +++ b/internal/cmd/loadbalancer/create.go @@ -13,7 +13,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var CreateCommand = base.Cmd{ +var CreateCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { cmd := &cobra.Command{ Use: "create [FLAGS]", diff --git a/internal/cmd/loadbalancer/delete.go b/internal/cmd/loadbalancer/delete.go index 0dd83f4f..cdf886fc 100644 --- a/internal/cmd/loadbalancer/delete.go +++ b/internal/cmd/loadbalancer/delete.go @@ -11,7 +11,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var deleteCmd = base.DeleteCmd{ +var DeleteCmd = base.DeleteCmd{ ResourceNameSingular: "Load Balancer", ShortDescription: "Delete a Load Balancer", NameSuggestions: func(c hcapi2.Client) func() []string { return c.LoadBalancer().Names }, diff --git a/internal/cmd/loadbalancer/delete_service.go b/internal/cmd/loadbalancer/delete_service.go index 848604fa..9fb18811 100644 --- a/internal/cmd/loadbalancer/delete_service.go +++ b/internal/cmd/loadbalancer/delete_service.go @@ -12,7 +12,7 @@ import ( "github.com/hetznercloud/cli/internal/state" ) -var DeleteServiceCommand = base.Cmd{ +var DeleteServiceCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { cmd := &cobra.Command{ Use: "delete-service [FLAGS] LOADBALANCER", diff --git a/internal/cmd/loadbalancer/detach_from_network.go b/internal/cmd/loadbalancer/detach_from_network.go index e03a0f10..08e63c64 100644 --- a/internal/cmd/loadbalancer/detach_from_network.go +++ b/internal/cmd/loadbalancer/detach_from_network.go @@ -13,7 +13,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var DetachFromNetworkCommand = base.Cmd{ +var DetachFromNetworkCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { cmd := &cobra.Command{ Use: "detach-from-network [FLAGS] LOADBALANCER", diff --git a/internal/cmd/loadbalancer/disable_protection.go b/internal/cmd/loadbalancer/disable_protection.go index 7bb00c65..fcb93570 100644 --- a/internal/cmd/loadbalancer/disable_protection.go +++ b/internal/cmd/loadbalancer/disable_protection.go @@ -12,7 +12,7 @@ import ( "github.com/hetznercloud/cli/internal/state" ) -var DisableProtectionCommand = base.Cmd{ +var DisableProtectionCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { return &cobra.Command{ Use: "disable-protection [FLAGS] LOADBALANCER PROTECTIONLEVEL [PROTECTIONLEVEL...]", diff --git a/internal/cmd/loadbalancer/disable_public_interface.go b/internal/cmd/loadbalancer/disable_public_interface.go index cbd5eda7..c2ff03e2 100644 --- a/internal/cmd/loadbalancer/disable_public_interface.go +++ b/internal/cmd/loadbalancer/disable_public_interface.go @@ -12,7 +12,7 @@ import ( "github.com/hetznercloud/cli/internal/state" ) -var DisablePublicInterfaceCommand = base.Cmd{ +var DisablePublicInterfaceCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { return &cobra.Command{ Use: "disable-public-interface [FLAGS] LOADBALANCER", diff --git a/internal/cmd/loadbalancer/enable_protection.go b/internal/cmd/loadbalancer/enable_protection.go index e374eccd..d57662eb 100644 --- a/internal/cmd/loadbalancer/enable_protection.go +++ b/internal/cmd/loadbalancer/enable_protection.go @@ -57,7 +57,7 @@ func changeProtection(ctx context.Context, client hcapi2.Client, waiter state.Ac return nil } -var EnableProtectionCommand = base.Cmd{ +var EnableProtectionCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { return &cobra.Command{ Use: "enable-protection [FLAGS] LOADBALANCER PROTECTIONLEVEL [PROTECTIONLEVEL...]", diff --git a/internal/cmd/loadbalancer/enable_public_interface.go b/internal/cmd/loadbalancer/enable_public_interface.go index 3d1ab4d2..9cb1737f 100644 --- a/internal/cmd/loadbalancer/enable_public_interface.go +++ b/internal/cmd/loadbalancer/enable_public_interface.go @@ -12,7 +12,7 @@ import ( "github.com/hetznercloud/cli/internal/state" ) -var EnablePublicInterfaceCommand = base.Cmd{ +var EnablePublicInterfaceCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { return &cobra.Command{ Use: "enable-public-interface [FLAGS] LOADBALANCER", diff --git a/internal/cmd/loadbalancer/labels.go b/internal/cmd/loadbalancer/labels.go index 621cd14d..d57fb71e 100644 --- a/internal/cmd/loadbalancer/labels.go +++ b/internal/cmd/loadbalancer/labels.go @@ -9,7 +9,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var labelCmds = base.LabelCmds{ +var LabelCmds = base.LabelCmds{ ResourceNameSingular: "Load Balancer", ShortDescriptionAdd: "Add a label to a Load Balancer", ShortDescriptionRemove: "Remove a label from a Load Balancer", diff --git a/internal/cmd/loadbalancer/load_balancer.go b/internal/cmd/loadbalancer/load_balancer.go index 53b4e446..d4d8bf57 100644 --- a/internal/cmd/loadbalancer/load_balancer.go +++ b/internal/cmd/loadbalancer/load_balancer.go @@ -17,28 +17,28 @@ func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command { DisableFlagsInUseLine: true, } cmd.AddCommand( - CreateCommand.CobraCommand(cli.Context, client, cli, cli), + CreateCmd.CobraCommand(cli.Context, client, cli, cli), ListCmd.CobraCommand(cli.Context, client, cli), DescribeCmd.CobraCommand(cli.Context, client, cli), - deleteCmd.CobraCommand(cli.Context, client, cli, cli), - updateCmd.CobraCommand(cli.Context, client, cli), - labelCmds.AddCobraCommand(cli.Context, client, cli), - labelCmds.RemoveCobraCommand(cli.Context, client, cli), - AddTargetCommand.CobraCommand(cli.Context, client, cli, cli), - RemoveTargetCommand.CobraCommand(cli.Context, client, cli, cli), - ChangeAlgorithmCommand.CobraCommand(cli.Context, client, cli, cli), - UpdateServiceCommand.CobraCommand(cli.Context, client, cli, cli), - DeleteServiceCommand.CobraCommand(cli.Context, client, cli, cli), - AddServiceCommand.CobraCommand(cli.Context, client, cli, cli), - EnableProtectionCommand.CobraCommand(cli.Context, client, cli, cli), - DisableProtectionCommand.CobraCommand(cli.Context, client, cli, cli), - AttachToNetworkCommand.CobraCommand(cli.Context, client, cli, cli), - DetachFromNetworkCommand.CobraCommand(cli.Context, client, cli, cli), - EnablePublicInterfaceCommand.CobraCommand(cli.Context, client, cli, cli), - DisablePublicInterfaceCommand.CobraCommand(cli.Context, client, cli, cli), - ChangeTypeCommand.CobraCommand(cli.Context, client, cli, cli), - MetricsCommand.CobraCommand(cli.Context, client, cli, cli), - setRDNSCmd.CobraCommand(cli.Context, client, cli, cli), + DeleteCmd.CobraCommand(cli.Context, client, cli, cli), + UpdateCmd.CobraCommand(cli.Context, client, cli), + LabelCmds.AddCobraCommand(cli.Context, client, cli), + LabelCmds.RemoveCobraCommand(cli.Context, client, cli), + AddTargetCmd.CobraCommand(cli.Context, client, cli, cli), + RemoveTargetCmd.CobraCommand(cli.Context, client, cli, cli), + ChangeAlgorithmCmd.CobraCommand(cli.Context, client, cli, cli), + UpdateServiceCmd.CobraCommand(cli.Context, client, cli, cli), + DeleteServiceCmd.CobraCommand(cli.Context, client, cli, cli), + AddServiceCmd.CobraCommand(cli.Context, client, cli, cli), + EnableProtectionCmd.CobraCommand(cli.Context, client, cli, cli), + DisableProtectionCmd.CobraCommand(cli.Context, client, cli, cli), + AttachToNetworkCmd.CobraCommand(cli.Context, client, cli, cli), + DetachFromNetworkCmd.CobraCommand(cli.Context, client, cli, cli), + EnablePublicInterfaceCmd.CobraCommand(cli.Context, client, cli, cli), + DisablePublicInterfaceCmd.CobraCommand(cli.Context, client, cli, cli), + ChangeTypeCmd.CobraCommand(cli.Context, client, cli, cli), + MetricsCmd.CobraCommand(cli.Context, client, cli, cli), + SetRDNSCmd.CobraCommand(cli.Context, client, cli, cli), ) return cmd } diff --git a/internal/cmd/loadbalancer/metrics.go b/internal/cmd/loadbalancer/metrics.go index d49c3825..f71e6a92 100644 --- a/internal/cmd/loadbalancer/metrics.go +++ b/internal/cmd/loadbalancer/metrics.go @@ -20,7 +20,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var MetricsCommand = base.Cmd{ +var MetricsCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { cmd := &cobra.Command{ Use: "metrics [FLAGS] LOADBALANCER", diff --git a/internal/cmd/loadbalancer/remove_target.go b/internal/cmd/loadbalancer/remove_target.go index 6cee6d6c..77ee7211 100644 --- a/internal/cmd/loadbalancer/remove_target.go +++ b/internal/cmd/loadbalancer/remove_target.go @@ -15,7 +15,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var RemoveTargetCommand = base.Cmd{ +var RemoveTargetCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { cmd := &cobra.Command{ Use: "remove-target LOADBALANCER FLAGS", diff --git a/internal/cmd/loadbalancer/set_rdns.go b/internal/cmd/loadbalancer/set_rdns.go index 927e8982..7c032fc2 100644 --- a/internal/cmd/loadbalancer/set_rdns.go +++ b/internal/cmd/loadbalancer/set_rdns.go @@ -11,7 +11,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var setRDNSCmd = base.SetRdnsCmd{ +var SetRDNSCmd = base.SetRdnsCmd{ ResourceNameSingular: "Load Balancer", ShortDescription: "Change reverse DNS of a Load Balancer", NameSuggestions: func(c hcapi2.Client) func() []string { return c.LoadBalancer().Names }, diff --git a/internal/cmd/loadbalancer/update.go b/internal/cmd/loadbalancer/update.go index 83a3230d..0137843f 100644 --- a/internal/cmd/loadbalancer/update.go +++ b/internal/cmd/loadbalancer/update.go @@ -11,7 +11,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var updateCmd = base.UpdateCmd{ +var UpdateCmd = base.UpdateCmd{ ResourceNameSingular: "Load Balancer", ShortDescription: "Update a Load Balancer", NameSuggestions: func(c hcapi2.Client) func() []string { return c.LoadBalancer().Names }, diff --git a/internal/cmd/loadbalancer/update_service.go b/internal/cmd/loadbalancer/update_service.go index ad782788..36105c52 100644 --- a/internal/cmd/loadbalancer/update_service.go +++ b/internal/cmd/loadbalancer/update_service.go @@ -14,7 +14,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var UpdateServiceCommand = base.Cmd{ +var UpdateServiceCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { cmd := &cobra.Command{ Use: "update-service LOADBALANCER FLAGS", diff --git a/internal/cmd/loadbalancertype/describe.go b/internal/cmd/loadbalancertype/describe.go index dcb738e8..6ae26057 100644 --- a/internal/cmd/loadbalancertype/describe.go +++ b/internal/cmd/loadbalancertype/describe.go @@ -11,7 +11,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var describeCmd = base.DescribeCmd{ +var DescribeCmd = base.DescribeCmd{ ResourceNameSingular: "Load Balancer Type", ShortDescription: "Describe a Load Balancer type", JSONKeyGetByID: "load_balancer_type", diff --git a/internal/cmd/loadbalancertype/load_balancer_type.go b/internal/cmd/loadbalancertype/load_balancer_type.go index bd138521..223544f1 100644 --- a/internal/cmd/loadbalancertype/load_balancer_type.go +++ b/internal/cmd/loadbalancertype/load_balancer_type.go @@ -16,7 +16,7 @@ func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command { DisableFlagsInUseLine: true, } cmd.AddCommand( - describeCmd.CobraCommand(cli.Context, client, cli), + DescribeCmd.CobraCommand(cli.Context, client, cli), ListCmd.CobraCommand(cli.Context, client, cli), ) return cmd diff --git a/internal/cmd/network/add_route.go b/internal/cmd/network/add_route.go index 51b4bded..aa347744 100644 --- a/internal/cmd/network/add_route.go +++ b/internal/cmd/network/add_route.go @@ -14,7 +14,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var AddRouteCommand = base.Cmd{ +var AddRouteCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { cmd := &cobra.Command{ Use: "add-route NETWORK FLAGS", diff --git a/internal/cmd/network/add_subnet.go b/internal/cmd/network/add_subnet.go index 5b42da70..b29b243b 100644 --- a/internal/cmd/network/add_subnet.go +++ b/internal/cmd/network/add_subnet.go @@ -14,7 +14,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var AddSubnetCommand = base.Cmd{ +var AddSubnetCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { cmd := &cobra.Command{ Use: "add-subnet NETWORK FLAGS", diff --git a/internal/cmd/network/change_ip_range.go b/internal/cmd/network/change_ip_range.go index be10273d..fd3031ff 100644 --- a/internal/cmd/network/change_ip_range.go +++ b/internal/cmd/network/change_ip_range.go @@ -14,7 +14,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var ChangeIPRangeCommand = base.Cmd{ +var ChangeIPRangeCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { cmd := &cobra.Command{ Use: "change-ip-range [FLAGS] NETWORK", diff --git a/internal/cmd/network/create.go b/internal/cmd/network/create.go index a126b326..0f499757 100644 --- a/internal/cmd/network/create.go +++ b/internal/cmd/network/create.go @@ -1,67 +1,66 @@ package network import ( + "context" "net" "github.com/spf13/cobra" + "github.com/hetznercloud/cli/internal/cmd/base" "github.com/hetznercloud/cli/internal/cmd/cmpl" "github.com/hetznercloud/cli/internal/hcapi2" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -func newCreateCommand(cli *state.State) *cobra.Command { - cmd := &cobra.Command{ - Use: "create [FLAGS]", - Short: "Create a network", - Args: cobra.NoArgs, - TraverseChildren: true, - DisableFlagsInUseLine: true, - PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runCreate), - } +var CreateCmd = base.Cmd{ + BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { + cmd := &cobra.Command{ + Use: "create [FLAGS]", + Short: "Create a network", + Args: cobra.NoArgs, + } - cmd.Flags().String("name", "", "Network name (required)") - cmd.MarkFlagRequired("name") + cmd.Flags().String("name", "", "Network name (required)") + cmd.MarkFlagRequired("name") - cmd.Flags().IPNet("ip-range", net.IPNet{}, "Network IP range (required)") - cmd.MarkFlagRequired("ip-range") + cmd.Flags().IPNet("ip-range", net.IPNet{}, "Network IP range (required)") + cmd.MarkFlagRequired("ip-range") - cmd.Flags().Bool("expose-routes-to-vswitch", false, "Expose routes from this network to the vSwitch connection. It only takes effect if a vSwitch connection is active.") + cmd.Flags().Bool("expose-routes-to-vswitch", false, "Expose routes from this network to the vSwitch connection. It only takes effect if a vSwitch connection is active.") - cmd.Flags().StringToString("label", nil, "User-defined labels ('key=value') (can be specified multiple times)") + cmd.Flags().StringToString("label", nil, "User-defined labels ('key=value') (can be specified multiple times)") - cmd.Flags().StringSlice("enable-protection", []string{}, "Enable protection (delete) (default: none)") - cmd.RegisterFlagCompletionFunc("enable-protection", cmpl.SuggestCandidates("delete")) - return cmd -} - -func runCreate(cli *state.State, cmd *cobra.Command, args []string) error { - name, _ := cmd.Flags().GetString("name") - ipRange, _ := cmd.Flags().GetIPNet("ip-range") - labels, _ := cmd.Flags().GetStringToString("label") - exposeRoutesToVSwitch, _ := cmd.Flags().GetBool("expose-routes-to-vswitch") - protection, _ := cmd.Flags().GetStringSlice("enable-protection") + cmd.Flags().StringSlice("enable-protection", []string{}, "Enable protection (delete) (default: none)") + cmd.RegisterFlagCompletionFunc("enable-protection", cmpl.SuggestCandidates("delete")) + return cmd + }, + Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + name, _ := cmd.Flags().GetString("name") + ipRange, _ := cmd.Flags().GetIPNet("ip-range") + labels, _ := cmd.Flags().GetStringToString("label") + exposeRoutesToVSwitch, _ := cmd.Flags().GetBool("expose-routes-to-vswitch") + protection, _ := cmd.Flags().GetStringSlice("enable-protection") - protectionOpts, err := getChangeProtectionOpts(true, protection) - if err != nil { - return err - } + protectionOpts, err := getChangeProtectionOpts(true, protection) + if err != nil { + return err + } - createOpts := hcloud.NetworkCreateOpts{ - Name: name, - IPRange: &ipRange, - Labels: labels, - ExposeRoutesToVSwitch: exposeRoutesToVSwitch, - } + createOpts := hcloud.NetworkCreateOpts{ + Name: name, + IPRange: &ipRange, + Labels: labels, + ExposeRoutesToVSwitch: exposeRoutesToVSwitch, + } - network, _, err := cli.Client().Network.Create(cli.Context, createOpts) - if err != nil { - return err - } + network, _, err := client.Network().Create(ctx, createOpts) + if err != nil { + return err + } - cmd.Printf("Network %d created\n", network.ID) + cmd.Printf("Network %d created\n", network.ID) - return changeProtection(cli.Context, hcapi2.NewClient(cli.Client()), cli, network, true, protectionOpts) + return changeProtection(ctx, client, waiter, network, true, protectionOpts) + }, } diff --git a/internal/cmd/network/delete.go b/internal/cmd/network/delete.go index 1d6ea107..40bb61b5 100644 --- a/internal/cmd/network/delete.go +++ b/internal/cmd/network/delete.go @@ -11,7 +11,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var deleteCmd = base.DeleteCmd{ +var DeleteCmd = base.DeleteCmd{ ResourceNameSingular: "Network", ShortDescription: "Delete a network", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Network().Names }, diff --git a/internal/cmd/network/disable_protection.go b/internal/cmd/network/disable_protection.go index 82884cb3..4773619c 100644 --- a/internal/cmd/network/disable_protection.go +++ b/internal/cmd/network/disable_protection.go @@ -12,7 +12,7 @@ import ( "github.com/hetznercloud/cli/internal/state" ) -var DisableProtectionCommand = base.Cmd{ +var DisableProtectionCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { return &cobra.Command{ Use: "disable-protection [FLAGS] NETWORK PROTECTIONLEVEL [PROTECTIONLEVEL...]", diff --git a/internal/cmd/network/enable_protection.go b/internal/cmd/network/enable_protection.go index 404bffdf..340965c2 100644 --- a/internal/cmd/network/enable_protection.go +++ b/internal/cmd/network/enable_protection.go @@ -57,7 +57,7 @@ func changeProtection(ctx context.Context, client hcapi2.Client, waiter state.Ac return nil } -var EnableProtectionCommand = base.Cmd{ +var EnableProtectionCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { return &cobra.Command{ Use: "enable-protection [FLAGS] NETWORK PROTECTIONLEVEL [PROTECTIONLEVEL...]", diff --git a/internal/cmd/network/expose-routes-to-vswitch.go b/internal/cmd/network/expose-routes-to-vswitch.go index be9b193e..3f357114 100644 --- a/internal/cmd/network/expose-routes-to-vswitch.go +++ b/internal/cmd/network/expose-routes-to-vswitch.go @@ -13,7 +13,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var ExposeRoutesToVSwitchCommand = base.Cmd{ +var ExposeRoutesToVSwitchCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { cmd := &cobra.Command{ Use: "expose-routes-to-vswitch [flags] network", diff --git a/internal/cmd/network/labels.go b/internal/cmd/network/labels.go index ffd452b3..0331d420 100644 --- a/internal/cmd/network/labels.go +++ b/internal/cmd/network/labels.go @@ -9,7 +9,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var labelCmds = base.LabelCmds{ +var LabelCmds = base.LabelCmds{ ResourceNameSingular: "Network", ShortDescriptionAdd: "Add a label to a Network", ShortDescriptionRemove: "Remove a label from a Network", diff --git a/internal/cmd/network/network.go b/internal/cmd/network/network.go index 5b43e138..6a2cb149 100644 --- a/internal/cmd/network/network.go +++ b/internal/cmd/network/network.go @@ -18,19 +18,19 @@ func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command { cmd.AddCommand( ListCmd.CobraCommand(cli.Context, client, cli), DescribeCmd.CobraCommand(cli.Context, client, cli), - newCreateCommand(cli), - updateCmd.CobraCommand(cli.Context, client, cli), - deleteCmd.CobraCommand(cli.Context, client, cli, cli), - ChangeIPRangeCommand.CobraCommand(cli.Context, client, cli, cli), - AddRouteCommand.CobraCommand(cli.Context, client, cli, cli), - RemoveRouteCommand.CobraCommand(cli.Context, client, cli, cli), - AddSubnetCommand.CobraCommand(cli.Context, client, cli, cli), - RemoveSubnetCommand.CobraCommand(cli.Context, client, cli, cli), - labelCmds.AddCobraCommand(cli.Context, client, cli), - labelCmds.RemoveCobraCommand(cli.Context, client, cli), - EnableProtectionCommand.CobraCommand(cli.Context, client, cli, cli), - DisableProtectionCommand.CobraCommand(cli.Context, client, cli, cli), - ExposeRoutesToVSwitchCommand.CobraCommand(cli.Context, client, cli, cli), + CreateCmd.CobraCommand(cli.Context, client, cli, cli), + UpdateCmd.CobraCommand(cli.Context, client, cli), + DeleteCmd.CobraCommand(cli.Context, client, cli, cli), + ChangeIPRangeCmd.CobraCommand(cli.Context, client, cli, cli), + AddRouteCmd.CobraCommand(cli.Context, client, cli, cli), + RemoveRouteCmd.CobraCommand(cli.Context, client, cli, cli), + AddSubnetCmd.CobraCommand(cli.Context, client, cli, cli), + RemoveSubnetCmd.CobraCommand(cli.Context, client, cli, cli), + LabelCmds.AddCobraCommand(cli.Context, client, cli), + LabelCmds.RemoveCobraCommand(cli.Context, client, cli), + EnableProtectionCmd.CobraCommand(cli.Context, client, cli, cli), + DisableProtectionCmd.CobraCommand(cli.Context, client, cli, cli), + ExposeRoutesToVSwitchCmd.CobraCommand(cli.Context, client, cli, cli), ) return cmd } diff --git a/internal/cmd/network/remove_route.go b/internal/cmd/network/remove_route.go index 65972cd7..5ede8af3 100644 --- a/internal/cmd/network/remove_route.go +++ b/internal/cmd/network/remove_route.go @@ -14,7 +14,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var RemoveRouteCommand = base.Cmd{ +var RemoveRouteCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { cmd := &cobra.Command{ Use: "remove-route NETWORK FLAGS", diff --git a/internal/cmd/network/remove_subnet.go b/internal/cmd/network/remove_subnet.go index e2064d88..af2c520a 100644 --- a/internal/cmd/network/remove_subnet.go +++ b/internal/cmd/network/remove_subnet.go @@ -14,7 +14,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var RemoveSubnetCommand = base.Cmd{ +var RemoveSubnetCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { cmd := &cobra.Command{ Use: "remove-subnet NETWORK FLAGS", diff --git a/internal/cmd/network/update.go b/internal/cmd/network/update.go index c00d5393..3f5015cb 100644 --- a/internal/cmd/network/update.go +++ b/internal/cmd/network/update.go @@ -11,7 +11,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var updateCmd = base.UpdateCmd{ +var UpdateCmd = base.UpdateCmd{ ResourceNameSingular: "Network", ShortDescription: "Update a Network.\n\nTo enable or disable exposing routes to the vSwitch connection you can use the subcommand \"hcloud network expose-routes-to-vswitch\".", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Network().Names }, diff --git a/internal/cmd/primaryip/delete.go b/internal/cmd/primaryip/delete.go index cb6203a9..aca42a98 100644 --- a/internal/cmd/primaryip/delete.go +++ b/internal/cmd/primaryip/delete.go @@ -11,7 +11,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var deleteCmd = base.DeleteCmd{ +var DeleteCmd = base.DeleteCmd{ ResourceNameSingular: "Primary IP", ShortDescription: "Delete a Primary IP", NameSuggestions: func(c hcapi2.Client) func() []string { return c.PrimaryIP().Names }, diff --git a/internal/cmd/primaryip/delete_test.go b/internal/cmd/primaryip/delete_test.go index 06766e75..1f35a576 100644 --- a/internal/cmd/primaryip/delete_test.go +++ b/internal/cmd/primaryip/delete_test.go @@ -15,7 +15,7 @@ func TestDelete(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := deleteCmd.CobraCommand(context.Background(), fx.Client, fx.TokenEnsurer, fx.ActionWaiter) + cmd := DeleteCmd.CobraCommand(context.Background(), fx.Client, fx.TokenEnsurer, fx.ActionWaiter) primaryip := &hcloud.PrimaryIP{ID: 13} fx.ExpectEnsureToken() fx.Client.PrimaryIPClient.EXPECT(). diff --git a/internal/cmd/primaryip/describe.go b/internal/cmd/primaryip/describe.go index 81c41cfd..cb55a7c6 100644 --- a/internal/cmd/primaryip/describe.go +++ b/internal/cmd/primaryip/describe.go @@ -13,7 +13,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var describeCmd = base.DescribeCmd{ +var DescribeCmd = base.DescribeCmd{ ResourceNameSingular: "Primary IP", ShortDescription: "Describe an Primary IP", JSONKeyGetByID: "primary_ip", diff --git a/internal/cmd/primaryip/describe_test.go b/internal/cmd/primaryip/describe_test.go index 85442743..50617aeb 100644 --- a/internal/cmd/primaryip/describe_test.go +++ b/internal/cmd/primaryip/describe_test.go @@ -19,7 +19,7 @@ func TestDescribe(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := describeCmd.CobraCommand(context.Background(), fx.Client, fx.TokenEnsurer) + cmd := DescribeCmd.CobraCommand(context.Background(), fx.Client, fx.TokenEnsurer) created := time.Date(1, time.Month(1), 1, 0, 0, 0, 0, time.UTC) ip := net.ParseIP("192.168.0.1") fx.ExpectEnsureToken() diff --git a/internal/cmd/primaryip/disable_protecion_test.go b/internal/cmd/primaryip/disable_protection_test.go similarity index 100% rename from internal/cmd/primaryip/disable_protecion_test.go rename to internal/cmd/primaryip/disable_protection_test.go diff --git a/internal/cmd/primaryip/enable_protecion_test.go b/internal/cmd/primaryip/enable_protection_test.go similarity index 100% rename from internal/cmd/primaryip/enable_protecion_test.go rename to internal/cmd/primaryip/enable_protection_test.go diff --git a/internal/cmd/primaryip/labels.go b/internal/cmd/primaryip/labels.go index 0d4febec..6a4a7eaa 100644 --- a/internal/cmd/primaryip/labels.go +++ b/internal/cmd/primaryip/labels.go @@ -9,7 +9,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var labelCmds = base.LabelCmds{ +var LabelCmds = base.LabelCmds{ ResourceNameSingular: "primary-ip", ShortDescriptionAdd: "Add a label to a Primary IP", ShortDescriptionRemove: "Remove a label from a Primary IP", diff --git a/internal/cmd/primaryip/primaryip.go b/internal/cmd/primaryip/primaryip.go index ecd6e661..ed80ea16 100644 --- a/internal/cmd/primaryip/primaryip.go +++ b/internal/cmd/primaryip/primaryip.go @@ -17,17 +17,17 @@ func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command { } cmd.AddCommand( ListCmd.CobraCommand(cli.Context, client, cli), - describeCmd.CobraCommand(cli.Context, client, cli), + DescribeCmd.CobraCommand(cli.Context, client, cli), CreateCmd.CobraCommand(cli.Context, client, cli, cli), updateCmd.CobraCommand(cli.Context, client, cli), - deleteCmd.CobraCommand(cli.Context, client, cli, cli), + DeleteCmd.CobraCommand(cli.Context, client, cli, cli), AssignCmd.CobraCommand(cli.Context, client, cli, cli), UnAssignCmd.CobraCommand(cli.Context, client, cli, cli), ChangeDNSCmd.CobraCommand(cli.Context, client, cli, cli), EnableProtectionCmd.CobraCommand(cli.Context, client, cli, cli), DisableProtectionCmd.CobraCommand(cli.Context, client, cli, cli), - labelCmds.AddCobraCommand(cli.Context, client, cli), - labelCmds.RemoveCobraCommand(cli.Context, client, cli), + LabelCmds.AddCobraCommand(cli.Context, client, cli), + LabelCmds.RemoveCobraCommand(cli.Context, client, cli), ) return cmd } diff --git a/internal/cmd/server/add_to_placement_group.go b/internal/cmd/server/add_to_placement_group.go index 76e30d6d..d3741493 100644 --- a/internal/cmd/server/add_to_placement_group.go +++ b/internal/cmd/server/add_to_placement_group.go @@ -12,7 +12,7 @@ import ( "github.com/hetznercloud/cli/internal/state" ) -var AddToPlacementGroupCommand = base.Cmd{ +var AddToPlacementGroupCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { cmd := &cobra.Command{ Use: "add-to-placement-group [FLAGS] SERVER", diff --git a/internal/cmd/server/add_to_placement_group_test.go b/internal/cmd/server/add_to_placement_group_test.go index fb2d5a24..b3c7b14a 100644 --- a/internal/cmd/server/add_to_placement_group_test.go +++ b/internal/cmd/server/add_to_placement_group_test.go @@ -16,7 +16,7 @@ func TestAddToPlacementGroup(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := server.AddToPlacementGroupCommand.CobraCommand( + cmd := server.AddToPlacementGroupCmd.CobraCommand( context.Background(), fx.Client, fx.TokenEnsurer, diff --git a/internal/cmd/server/attach_iso.go b/internal/cmd/server/attach_iso.go index a7929b9a..507648f5 100644 --- a/internal/cmd/server/attach_iso.go +++ b/internal/cmd/server/attach_iso.go @@ -13,7 +13,7 @@ import ( "github.com/hetznercloud/cli/internal/state" ) -var AttachISOCommand = base.Cmd{ +var AttachISOCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { return &cobra.Command{ Use: "attach-iso [FLAGS] SERVER ISO", diff --git a/internal/cmd/server/attach_to_network.go b/internal/cmd/server/attach_to_network.go index 92fbb79f..9d886a2d 100644 --- a/internal/cmd/server/attach_to_network.go +++ b/internal/cmd/server/attach_to_network.go @@ -14,7 +14,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var AttachToNetworkCommand = base.Cmd{ +var AttachToNetworkCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { cmd := &cobra.Command{ Use: "attach-to-network [FLAGS] SERVER", diff --git a/internal/cmd/server/change_alias_ips.go b/internal/cmd/server/change_alias_ips.go index 99fec75e..3a966d4c 100644 --- a/internal/cmd/server/change_alias_ips.go +++ b/internal/cmd/server/change_alias_ips.go @@ -14,7 +14,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var ChangeAliasIPsCommand = base.Cmd{ +var ChangeAliasIPsCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { cmd := &cobra.Command{ Use: "change-alias-ips [FLAGS] SERVER", diff --git a/internal/cmd/server/change_type.go b/internal/cmd/server/change_type.go index 23b77d66..e4b8a115 100644 --- a/internal/cmd/server/change_type.go +++ b/internal/cmd/server/change_type.go @@ -13,7 +13,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var ChangeTypeCommand = base.Cmd{ +var ChangeTypeCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { cmd := &cobra.Command{ Use: "change-type [FLAGS] SERVER SERVERTYPE", diff --git a/internal/cmd/server/create_image.go b/internal/cmd/server/create_image.go index 3856827b..dd63bab7 100644 --- a/internal/cmd/server/create_image.go +++ b/internal/cmd/server/create_image.go @@ -1,80 +1,72 @@ package server import ( + "context" "fmt" "github.com/spf13/cobra" + "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" ) -func newCreateImageCommand(cli *state.State) *cobra.Command { - cmd := &cobra.Command{ - Use: "create-image [FLAGS] SERVER", - Short: "Create an image from a server", - Args: cobra.ExactArgs(1), - TraverseChildren: true, - DisableFlagsInUseLine: true, - PreRunE: util.ChainRunE(validateCreateImage, cli.EnsureToken), - RunE: cli.Wrap(runCreateImage), - } - cmd.Flags().String("type", "", "Image type (required)") - cmd.RegisterFlagCompletionFunc("type", cmpl.SuggestCandidates("backup", "snapshot")) - cmd.MarkFlagRequired("type") - - cmd.Flags().String("description", "", "Image description") - - cmd.Flags().StringToString("label", nil, "User-defined labels ('key=value') (can be specified multiple times)") - - return cmd -} - -func validateCreateImage(cmd *cobra.Command, args []string) error { - imageType, _ := cmd.Flags().GetString("type") - switch hcloud.ImageType(imageType) { - case hcloud.ImageTypeBackup, hcloud.ImageTypeSnapshot: - break - default: - return fmt.Errorf("invalid image type: %v", imageType) - } - - return nil -} - -func runCreateImage(cli *state.State, cmd *cobra.Command, args []string) error { - idOrName := args[0] - server, _, err := cli.Client().Server.Get(cli.Context, idOrName) - if err != nil { - return err - } - if server == nil { - return fmt.Errorf("server not found: %s", idOrName) - } - - imageType, _ := cmd.Flags().GetString("type") - - description, _ := cmd.Flags().GetString("description") - - labels, _ := cmd.Flags().GetStringToString("label") - - opts := &hcloud.ServerCreateImageOpts{ - Type: hcloud.ImageType(imageType), - Description: hcloud.String(description), - Labels: labels, - } - result, _, err := cli.Client().Server.CreateImage(cli.Context, server, opts) - if err != nil { - return err - } - - if err := cli.ActionProgress(cli.Context, result.Action); err != nil { - return err - } - - fmt.Printf("Image %d created from server %d\n", result.Image.ID, server.ID) - - return nil +var CreateImageCmd = base.Cmd{ + BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { + cmd := &cobra.Command{ + Use: "create-image [FLAGS] SERVER", + Short: "Create an image from a server", + Args: cobra.ExactArgs(1), + } + cmd.Flags().String("type", "", "Image type (required)") + cmd.RegisterFlagCompletionFunc("type", cmpl.SuggestCandidates("backup", "snapshot")) + cmd.MarkFlagRequired("type") + + cmd.Flags().String("description", "", "Image description") + + cmd.Flags().StringToString("label", nil, "User-defined labels ('key=value') (can be specified multiple times)") + + return cmd + }, + Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + idOrName := args[0] + server, _, err := client.Server().Get(ctx, idOrName) + if err != nil { + return err + } + if server == nil { + return fmt.Errorf("server not found: %s", idOrName) + } + + imageType, _ := cmd.Flags().GetString("type") + description, _ := cmd.Flags().GetString("description") + labels, _ := cmd.Flags().GetStringToString("label") + + switch hcloud.ImageType(imageType) { + case hcloud.ImageTypeBackup, hcloud.ImageTypeSnapshot: + break + default: + return fmt.Errorf("invalid image type: %v", imageType) + } + + opts := &hcloud.ServerCreateImageOpts{ + Type: hcloud.ImageType(imageType), + Description: hcloud.Ptr(description), + Labels: labels, + } + result, _, err := client.Server().CreateImage(ctx, server, opts) + if err != nil { + return err + } + + if err := waiter.ActionProgress(ctx, result.Action); err != nil { + return err + } + + fmt.Printf("Image %d created from server %d\n", result.Image.ID, server.ID) + + return nil + }, } diff --git a/internal/cmd/server/delete.go b/internal/cmd/server/delete.go index c59487c7..dbd241b8 100644 --- a/internal/cmd/server/delete.go +++ b/internal/cmd/server/delete.go @@ -11,7 +11,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var deleteCmd = base.DeleteCmd{ +var DeleteCmd = base.DeleteCmd{ ResourceNameSingular: "Server", ShortDescription: "Delete a server", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Server().Names }, diff --git a/internal/cmd/server/describe.go b/internal/cmd/server/describe.go index 1aaa164e..a768314e 100644 --- a/internal/cmd/server/describe.go +++ b/internal/cmd/server/describe.go @@ -13,7 +13,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var describeCmd = base.DescribeCmd{ +var DescribeCmd = base.DescribeCmd{ ResourceNameSingular: "server", ShortDescription: "Describe a server", JSONKeyGetByID: "server", diff --git a/internal/cmd/server/detach_from_network.go b/internal/cmd/server/detach_from_network.go index a44b9f01..68b8a227 100644 --- a/internal/cmd/server/detach_from_network.go +++ b/internal/cmd/server/detach_from_network.go @@ -13,7 +13,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var DetachFromNetworkCommand = base.Cmd{ +var DetachFromNetworkCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { cmd := &cobra.Command{ Use: "detach-from-network [FLAGS] SERVER", diff --git a/internal/cmd/server/detach_iso.go b/internal/cmd/server/detach_iso.go index 228acd98..71d2f32a 100644 --- a/internal/cmd/server/detach_iso.go +++ b/internal/cmd/server/detach_iso.go @@ -12,7 +12,7 @@ import ( "github.com/hetznercloud/cli/internal/state" ) -var DetachISOCommand = base.Cmd{ +var DetachISOCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { return &cobra.Command{ Use: "detach-iso [FLAGS] SERVER", diff --git a/internal/cmd/server/disable_backup.go b/internal/cmd/server/disable_backup.go index cf2331fe..ece84312 100644 --- a/internal/cmd/server/disable_backup.go +++ b/internal/cmd/server/disable_backup.go @@ -12,7 +12,7 @@ import ( "github.com/hetznercloud/cli/internal/state" ) -var DisableBackupCommand = base.Cmd{ +var DisableBackupCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { return &cobra.Command{ Use: "disable-backup [FLAGS] SERVER", diff --git a/internal/cmd/server/disable_protection.go b/internal/cmd/server/disable_protection.go index daca2de3..3b3ce5d2 100644 --- a/internal/cmd/server/disable_protection.go +++ b/internal/cmd/server/disable_protection.go @@ -12,7 +12,7 @@ import ( "github.com/hetznercloud/cli/internal/state" ) -var DisableProtectionCommand = base.Cmd{ +var DisableProtectionCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { return &cobra.Command{ Use: "disable-protection [FLAGS] SERVER PROTECTIONLEVEL [PROTECTIONLEVEL...]", diff --git a/internal/cmd/server/disable_rescue.go b/internal/cmd/server/disable_rescue.go index 4b9e1da3..6175f9d4 100644 --- a/internal/cmd/server/disable_rescue.go +++ b/internal/cmd/server/disable_rescue.go @@ -12,7 +12,7 @@ import ( "github.com/hetznercloud/cli/internal/state" ) -var DisableRescueCommand = base.Cmd{ +var DisableRescueCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { return &cobra.Command{ Use: "disable-rescue [FLAGS] SERVER", diff --git a/internal/cmd/server/enable_backup.go b/internal/cmd/server/enable_backup.go index 4b4a9934..cf4c3faa 100644 --- a/internal/cmd/server/enable_backup.go +++ b/internal/cmd/server/enable_backup.go @@ -12,7 +12,7 @@ import ( "github.com/hetznercloud/cli/internal/state" ) -var EnableBackupCommand = base.Cmd{ +var EnableBackupCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { cmd := &cobra.Command{ Use: "enable-backup [FLAGS] SERVER", diff --git a/internal/cmd/server/enable_protection.go b/internal/cmd/server/enable_protection.go index 2f1137fc..31a9963f 100644 --- a/internal/cmd/server/enable_protection.go +++ b/internal/cmd/server/enable_protection.go @@ -59,7 +59,7 @@ func changeProtection(ctx context.Context, client hcapi2.Client, waiter state.Ac return nil } -var EnableProtectionCommand = base.Cmd{ +var EnableProtectionCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { return &cobra.Command{ Use: "enable-protection [FLAGS] SERVER PROTECTIONLEVEL [PROTECTIONLEVEL...]", diff --git a/internal/cmd/server/enable_rescue.go b/internal/cmd/server/enable_rescue.go index f78c8c46..49f6f316 100644 --- a/internal/cmd/server/enable_rescue.go +++ b/internal/cmd/server/enable_rescue.go @@ -13,7 +13,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var EnableRescueCommand = base.Cmd{ +var EnableRescueCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { cmd := &cobra.Command{ Use: "enable-rescue [FLAGS] SERVER", diff --git a/internal/cmd/server/ip.go b/internal/cmd/server/ip.go index 8e736f4e..c80b8840 100644 --- a/internal/cmd/server/ip.go +++ b/internal/cmd/server/ip.go @@ -12,7 +12,7 @@ import ( "github.com/hetznercloud/cli/internal/state" ) -var IPCommand = base.Cmd{ +var IPCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { cmd := &cobra.Command{ Use: "ip SERVER FLAGS", diff --git a/internal/cmd/server/labels.go b/internal/cmd/server/labels.go index 612a89f6..f684f929 100644 --- a/internal/cmd/server/labels.go +++ b/internal/cmd/server/labels.go @@ -9,7 +9,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var labelCmds = base.LabelCmds{ +var LabelCmds = base.LabelCmds{ ResourceNameSingular: "server", ShortDescriptionAdd: "Add a label to a server", ShortDescriptionRemove: "Remove a label from a server", diff --git a/internal/cmd/server/metrics.go b/internal/cmd/server/metrics.go index 461da12f..40b65567 100644 --- a/internal/cmd/server/metrics.go +++ b/internal/cmd/server/metrics.go @@ -20,7 +20,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var MetricsCommand = base.Cmd{ +var MetricsCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { cmd := &cobra.Command{ Use: "metrics [FLAGS] SERVER", diff --git a/internal/cmd/server/poweroff.go b/internal/cmd/server/poweroff.go index 16068366..1aad5983 100644 --- a/internal/cmd/server/poweroff.go +++ b/internal/cmd/server/poweroff.go @@ -12,7 +12,7 @@ import ( "github.com/hetznercloud/cli/internal/state" ) -var PoweroffCommand = base.Cmd{ +var PoweroffCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { return &cobra.Command{ Use: "poweroff [FLAGS] SERVER", diff --git a/internal/cmd/server/poweron.go b/internal/cmd/server/poweron.go index 7b5849a7..d363927b 100644 --- a/internal/cmd/server/poweron.go +++ b/internal/cmd/server/poweron.go @@ -12,7 +12,7 @@ import ( "github.com/hetznercloud/cli/internal/state" ) -var PoweronCommand = base.Cmd{ +var PoweronCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { return &cobra.Command{ Use: "poweron [FLAGS] SERVER", diff --git a/internal/cmd/server/reboot.go b/internal/cmd/server/reboot.go index ba8bb91e..8f60710f 100644 --- a/internal/cmd/server/reboot.go +++ b/internal/cmd/server/reboot.go @@ -12,7 +12,7 @@ import ( "github.com/hetznercloud/cli/internal/state" ) -var RebootCommand = base.Cmd{ +var RebootCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { return &cobra.Command{ Use: "reboot [FLAGS] SERVER", diff --git a/internal/cmd/server/rebuild.go b/internal/cmd/server/rebuild.go index 0931176c..0c46f963 100644 --- a/internal/cmd/server/rebuild.go +++ b/internal/cmd/server/rebuild.go @@ -14,7 +14,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var RebuildCommand = base.Cmd{ +var RebuildCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { cmd := &cobra.Command{ Use: "rebuild [FLAGS] SERVER", diff --git a/internal/cmd/server/remove_from_placement_group.go b/internal/cmd/server/remove_from_placement_group.go index 6d51cb11..561deaf2 100644 --- a/internal/cmd/server/remove_from_placement_group.go +++ b/internal/cmd/server/remove_from_placement_group.go @@ -12,7 +12,7 @@ import ( "github.com/hetznercloud/cli/internal/state" ) -var RemoveFromPlacementGroup = base.Cmd{ +var RemoveFromPlacementGroupCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { cmd := &cobra.Command{ Use: "remove-from-placement-group SERVER", diff --git a/internal/cmd/server/remove_from_placement_group_test.go b/internal/cmd/server/remove_from_placement_group_test.go index 3a2dc783..c5ccdf5b 100644 --- a/internal/cmd/server/remove_from_placement_group_test.go +++ b/internal/cmd/server/remove_from_placement_group_test.go @@ -16,7 +16,7 @@ func TestRemoveFromPlacementGroup(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := server.RemoveFromPlacementGroup.CobraCommand( + cmd := server.RemoveFromPlacementGroupCmd.CobraCommand( context.Background(), fx.Client, fx.TokenEnsurer, diff --git a/internal/cmd/server/request_console.go b/internal/cmd/server/request_console.go index f39aa570..76a79610 100644 --- a/internal/cmd/server/request_console.go +++ b/internal/cmd/server/request_console.go @@ -14,7 +14,7 @@ import ( "github.com/hetznercloud/cli/internal/state" ) -var RequestConsoleCommand = base.Cmd{ +var RequestConsoleCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { cmd := &cobra.Command{ Use: "request-console [FLAGS] SERVER", diff --git a/internal/cmd/server/reset.go b/internal/cmd/server/reset.go index 35777d27..194ac4fb 100644 --- a/internal/cmd/server/reset.go +++ b/internal/cmd/server/reset.go @@ -12,7 +12,7 @@ import ( "github.com/hetznercloud/cli/internal/state" ) -var ResetCommand = base.Cmd{ +var ResetCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { return &cobra.Command{ Use: "reset [FLAGS] SERVER", diff --git a/internal/cmd/server/reset_password.go b/internal/cmd/server/reset_password.go index d57015ea..31bc8143 100644 --- a/internal/cmd/server/reset_password.go +++ b/internal/cmd/server/reset_password.go @@ -12,7 +12,7 @@ import ( "github.com/hetznercloud/cli/internal/state" ) -var ResetPasswordCommand = base.Cmd{ +var ResetPasswordCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { return &cobra.Command{ Use: "reset-password [FLAGS] SERVER", diff --git a/internal/cmd/server/server.go b/internal/cmd/server/server.go index bd8306f1..e95b0e15 100644 --- a/internal/cmd/server/server.go +++ b/internal/cmd/server/server.go @@ -17,39 +17,39 @@ func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command { } cmd.AddCommand( ListCmd.CobraCommand(cli.Context, client, cli), - describeCmd.CobraCommand(cli.Context, client, cli), + DescribeCmd.CobraCommand(cli.Context, client, cli), CreateCmd.CobraCommand(cli.Context, client, cli, cli), - deleteCmd.CobraCommand(cli.Context, client, cli, cli), - RebootCommand.CobraCommand(cli.Context, client, cli, cli), - PoweronCommand.CobraCommand(cli.Context, client, cli, cli), - PoweroffCommand.CobraCommand(cli.Context, client, cli, cli), - ResetCommand.CobraCommand(cli.Context, client, cli, cli), - ShutdownCommand.CobraCommand(cli.Context, client, cli, cli), - newCreateImageCommand(cli), - ResetPasswordCommand.CobraCommand(cli.Context, client, cli, cli), - EnableRescueCommand.CobraCommand(cli.Context, client, cli, cli), - DisableRescueCommand.CobraCommand(cli.Context, client, cli, cli), - AttachISOCommand.CobraCommand(cli.Context, client, cli, cli), - DetachISOCommand.CobraCommand(cli.Context, client, cli, cli), - updateCmd.CobraCommand(cli.Context, client, cli), - ChangeTypeCommand.CobraCommand(cli.Context, client, cli, cli), - RebuildCommand.CobraCommand(cli.Context, client, cli, cli), - EnableBackupCommand.CobraCommand(cli.Context, client, cli, cli), - DisableBackupCommand.CobraCommand(cli.Context, client, cli, cli), - EnableProtectionCommand.CobraCommand(cli.Context, client, cli, cli), - DisableProtectionCommand.CobraCommand(cli.Context, client, cli, cli), - SSHCommand.CobraCommand(cli.Context, client, cli, cli), - labelCmds.AddCobraCommand(cli.Context, client, cli), - labelCmds.RemoveCobraCommand(cli.Context, client, cli), - setRDNSCmd.CobraCommand(cli.Context, client, cli, cli), - AttachToNetworkCommand.CobraCommand(cli.Context, client, cli, cli), - DetachFromNetworkCommand.CobraCommand(cli.Context, client, cli, cli), - ChangeAliasIPsCommand.CobraCommand(cli.Context, client, cli, cli), - IPCommand.CobraCommand(cli.Context, client, cli, cli), - RequestConsoleCommand.CobraCommand(cli.Context, client, cli, cli), - MetricsCommand.CobraCommand(cli.Context, client, cli, cli), - AddToPlacementGroupCommand.CobraCommand(cli.Context, client, cli, cli), - RemoveFromPlacementGroup.CobraCommand(cli.Context, client, cli, cli), + DeleteCmd.CobraCommand(cli.Context, client, cli, cli), + RebootCmd.CobraCommand(cli.Context, client, cli, cli), + PoweronCmd.CobraCommand(cli.Context, client, cli, cli), + PoweroffCmd.CobraCommand(cli.Context, client, cli, cli), + ResetCmd.CobraCommand(cli.Context, client, cli, cli), + ShutdownCmd.CobraCommand(cli.Context, client, cli, cli), + CreateImageCmd.CobraCommand(cli.Context, client, cli, cli), + ResetPasswordCmd.CobraCommand(cli.Context, client, cli, cli), + EnableRescueCmd.CobraCommand(cli.Context, client, cli, cli), + DisableRescueCmd.CobraCommand(cli.Context, client, cli, cli), + AttachISOCmd.CobraCommand(cli.Context, client, cli, cli), + DetachISOCmd.CobraCommand(cli.Context, client, cli, cli), + UpdateCmd.CobraCommand(cli.Context, client, cli), + ChangeTypeCmd.CobraCommand(cli.Context, client, cli, cli), + RebuildCmd.CobraCommand(cli.Context, client, cli, cli), + EnableBackupCmd.CobraCommand(cli.Context, client, cli, cli), + DisableBackupCmd.CobraCommand(cli.Context, client, cli, cli), + EnableProtectionCmd.CobraCommand(cli.Context, client, cli, cli), + DisableProtectionCmd.CobraCommand(cli.Context, client, cli, cli), + SSHCmd.CobraCommand(cli.Context, client, cli, cli), + LabelCmds.AddCobraCommand(cli.Context, client, cli), + LabelCmds.RemoveCobraCommand(cli.Context, client, cli), + SetRDNSCmd.CobraCommand(cli.Context, client, cli, cli), + AttachToNetworkCmd.CobraCommand(cli.Context, client, cli, cli), + DetachFromNetworkCmd.CobraCommand(cli.Context, client, cli, cli), + ChangeAliasIPsCmd.CobraCommand(cli.Context, client, cli, cli), + IPCmd.CobraCommand(cli.Context, client, cli, cli), + RequestConsoleCmd.CobraCommand(cli.Context, client, cli, cli), + MetricsCmd.CobraCommand(cli.Context, client, cli, cli), + AddToPlacementGroupCmd.CobraCommand(cli.Context, client, cli, cli), + RemoveFromPlacementGroupCmd.CobraCommand(cli.Context, client, cli, cli), ) return cmd } diff --git a/internal/cmd/server/set_rdns.go b/internal/cmd/server/set_rdns.go index f9165348..200ebae9 100644 --- a/internal/cmd/server/set_rdns.go +++ b/internal/cmd/server/set_rdns.go @@ -11,7 +11,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var setRDNSCmd = base.SetRdnsCmd{ +var SetRDNSCmd = base.SetRdnsCmd{ ResourceNameSingular: "Server", ShortDescription: "Change reverse DNS of a Server", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Server().Names }, diff --git a/internal/cmd/server/shutdown.go b/internal/cmd/server/shutdown.go index a4e43314..34d95973 100644 --- a/internal/cmd/server/shutdown.go +++ b/internal/cmd/server/shutdown.go @@ -15,7 +15,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var ShutdownCommand = base.Cmd{ +var ShutdownCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { const description = "Shuts down a Server gracefully by sending an ACPI shutdown request. " + diff --git a/internal/cmd/server/shutdown_test.go b/internal/cmd/server/shutdown_test.go index 4846f236..3ffd47dd 100644 --- a/internal/cmd/server/shutdown_test.go +++ b/internal/cmd/server/shutdown_test.go @@ -16,7 +16,7 @@ func TestShutdown(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := ShutdownCommand.CobraCommand( + cmd := ShutdownCmd.CobraCommand( context.Background(), fx.Client, fx.TokenEnsurer, @@ -52,7 +52,7 @@ func TestShutdownWait(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := ShutdownCommand.CobraCommand( + cmd := ShutdownCmd.CobraCommand( context.Background(), fx.Client, fx.TokenEnsurer, diff --git a/internal/cmd/server/ssh.go b/internal/cmd/server/ssh.go index 62fde0cb..7f30c36b 100644 --- a/internal/cmd/server/ssh.go +++ b/internal/cmd/server/ssh.go @@ -16,7 +16,7 @@ import ( "github.com/hetznercloud/cli/internal/state" ) -var SSHCommand = base.Cmd{ +var SSHCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { cmd := &cobra.Command{ Use: "ssh [FLAGS] SERVER [COMMAND...]", diff --git a/internal/cmd/server/update.go b/internal/cmd/server/update.go index 2c0c8433..8cb8f58b 100644 --- a/internal/cmd/server/update.go +++ b/internal/cmd/server/update.go @@ -11,7 +11,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var updateCmd = base.UpdateCmd{ +var UpdateCmd = base.UpdateCmd{ ResourceNameSingular: "Server", ShortDescription: "Update a Server", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Server().Names }, diff --git a/internal/cmd/servertype/describe.go b/internal/cmd/servertype/describe.go index b2d83bbd..3ca5c1fc 100644 --- a/internal/cmd/servertype/describe.go +++ b/internal/cmd/servertype/describe.go @@ -12,7 +12,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var describeCmd = base.DescribeCmd{ +var DescribeCmd = base.DescribeCmd{ ResourceNameSingular: "serverType", ShortDescription: "Describe a server type", JSONKeyGetByID: "server_type", diff --git a/internal/cmd/servertype/server_type.go b/internal/cmd/servertype/server_type.go index daa2cf20..ad802709 100644 --- a/internal/cmd/servertype/server_type.go +++ b/internal/cmd/servertype/server_type.go @@ -17,7 +17,7 @@ func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command { } cmd.AddCommand( ListCmd.CobraCommand(cli.Context, client, cli), - describeCmd.CobraCommand(cli.Context, client, cli), + DescribeCmd.CobraCommand(cli.Context, client, cli), ) return cmd } diff --git a/internal/cmd/sshkey/create.go b/internal/cmd/sshkey/create.go index 3848e26c..b4091e1e 100644 --- a/internal/cmd/sshkey/create.go +++ b/internal/cmd/sshkey/create.go @@ -1,82 +1,70 @@ package sshkey import ( - "errors" + "context" "fmt" - "io/ioutil" + "io" "os" "github.com/spf13/cobra" - "github.com/hetznercloud/cli/internal/cmd/util" + "github.com/hetznercloud/cli/internal/cmd/base" + "github.com/hetznercloud/cli/internal/hcapi2" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -func newCreateCommand(cli *state.State) *cobra.Command { - cmd := &cobra.Command{ - Use: "create FLAGS", - Short: "Create a SSH key", - Args: cobra.NoArgs, - TraverseChildren: true, - DisableFlagsInUseLine: true, - PreRunE: util.ChainRunE(validateCreate, cli.EnsureToken), - RunE: cli.Wrap(runCreate), - } - cmd.Flags().String("name", "", "Key name (required)") - cmd.Flags().String("public-key", "", "Public key") - cmd.Flags().String("public-key-from-file", "", "Path to file containing public key") - cmd.Flags().StringToString("label", nil, "User-defined labels ('key=value') (can be specified multiple times)") - return cmd -} - -func validateCreate(cmd *cobra.Command, args []string) error { - if name, _ := cmd.Flags().GetString("name"); name == "" { - return errors.New("flag --name is required") - } +var CreateCmd = base.Cmd{ + BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { + cmd := &cobra.Command{ + Use: "create FLAGS", + Short: "Create a SSH key", + Args: cobra.NoArgs, + } + cmd.Flags().String("name", "", "Key name (required)") + _ = cmd.MarkFlagRequired("name") - publicKey, _ := cmd.Flags().GetString("public-key") - publicKeyFile, _ := cmd.Flags().GetString("public-key-from-file") - if publicKey != "" && publicKeyFile != "" { - return errors.New("flags --public-key and --public-key-from-file are mutually exclusive") - } + cmd.Flags().String("public-key", "", "Public key") + cmd.Flags().String("public-key-from-file", "", "Path to file containing public key") + cmd.MarkFlagsMutuallyExclusive("public-key", "public-key-from-file") - return nil -} + cmd.Flags().StringToString("label", nil, "User-defined labels ('key=value') (can be specified multiple times)") + return cmd + }, + Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + name, _ := cmd.Flags().GetString("name") + publicKey, _ := cmd.Flags().GetString("public-key") + publicKeyFile, _ := cmd.Flags().GetString("public-key-from-file") + labels, _ := cmd.Flags().GetStringToString("label") -func runCreate(cli *state.State, cmd *cobra.Command, args []string) error { - name, _ := cmd.Flags().GetString("name") - publicKey, _ := cmd.Flags().GetString("public-key") - publicKeyFile, _ := cmd.Flags().GetString("public-key-from-file") - labels, _ := cmd.Flags().GetStringToString("label") + if publicKeyFile != "" { + var ( + data []byte + err error + ) + if publicKeyFile == "-" { + data, err = io.ReadAll(os.Stdin) + } else { + data, err = os.ReadFile(publicKeyFile) + } + if err != nil { + return err + } + publicKey = string(data) + } - if publicKeyFile != "" { - var ( - data []byte - err error - ) - if publicKeyFile == "-" { - data, err = ioutil.ReadAll(os.Stdin) - } else { - data, err = ioutil.ReadFile(publicKeyFile) + opts := hcloud.SSHKeyCreateOpts{ + Name: name, + PublicKey: publicKey, + Labels: labels, } + sshKey, _, err := client.SSHKey().Create(ctx, opts) if err != nil { return err } - publicKey = string(data) - } - - opts := hcloud.SSHKeyCreateOpts{ - Name: name, - PublicKey: publicKey, - Labels: labels, - } - sshKey, _, err := cli.Client().SSHKey.Create(cli.Context, opts) - if err != nil { - return err - } - fmt.Printf("SSH key %d created\n", sshKey.ID) + fmt.Printf("SSH key %d created\n", sshKey.ID) - return nil + return nil + }, } diff --git a/internal/cmd/sshkey/delete.go b/internal/cmd/sshkey/delete.go index 8ae468f5..aef3c620 100644 --- a/internal/cmd/sshkey/delete.go +++ b/internal/cmd/sshkey/delete.go @@ -11,7 +11,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var deleteCmd = base.DeleteCmd{ +var DeleteCmd = base.DeleteCmd{ ResourceNameSingular: "SSH Key", ShortDescription: "Delete a SSH Key", NameSuggestions: func(c hcapi2.Client) func() []string { return c.SSHKey().Names }, diff --git a/internal/cmd/sshkey/describe.go b/internal/cmd/sshkey/describe.go index 56c99c29..392639d5 100644 --- a/internal/cmd/sshkey/describe.go +++ b/internal/cmd/sshkey/describe.go @@ -14,7 +14,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var describeCmd = base.DescribeCmd{ +var DescribeCmd = base.DescribeCmd{ ResourceNameSingular: "SSH Key", ShortDescription: "Describe a SSH Key", JSONKeyGetByID: "ssh_key", diff --git a/internal/cmd/sshkey/labels.go b/internal/cmd/sshkey/labels.go index c457406e..1980e094 100644 --- a/internal/cmd/sshkey/labels.go +++ b/internal/cmd/sshkey/labels.go @@ -9,7 +9,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var labelCmds = base.LabelCmds{ +var LabelCmds = base.LabelCmds{ ResourceNameSingular: "SSH Key", ShortDescriptionAdd: "Add a label to a SSH Key", ShortDescriptionRemove: "Remove a label from a SSH Key", diff --git a/internal/cmd/sshkey/sshkey.go b/internal/cmd/sshkey/sshkey.go index 10f9719a..3168bda8 100644 --- a/internal/cmd/sshkey/sshkey.go +++ b/internal/cmd/sshkey/sshkey.go @@ -17,12 +17,12 @@ func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command { } cmd.AddCommand( ListCmd.CobraCommand(cli.Context, client, cli), - newCreateCommand(cli), - updateCmd.CobraCommand(cli.Context, client, cli), - deleteCmd.CobraCommand(cli.Context, client, cli, cli), - describeCmd.CobraCommand(cli.Context, client, cli), - labelCmds.AddCobraCommand(cli.Context, client, cli), - labelCmds.RemoveCobraCommand(cli.Context, client, cli), + CreateCmd.CobraCommand(cli.Context, client, cli, cli), + UpdateCmd.CobraCommand(cli.Context, client, cli), + DeleteCmd.CobraCommand(cli.Context, client, cli, cli), + DescribeCmd.CobraCommand(cli.Context, client, cli), + LabelCmds.AddCobraCommand(cli.Context, client, cli), + LabelCmds.RemoveCobraCommand(cli.Context, client, cli), ) return cmd } diff --git a/internal/cmd/sshkey/update.go b/internal/cmd/sshkey/update.go index 1d9c1a5d..635b4c4f 100644 --- a/internal/cmd/sshkey/update.go +++ b/internal/cmd/sshkey/update.go @@ -11,7 +11,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var updateCmd = base.UpdateCmd{ +var UpdateCmd = base.UpdateCmd{ ResourceNameSingular: "SSHKey", ShortDescription: "Update a SSHKey", NameSuggestions: func(c hcapi2.Client) func() []string { return c.SSHKey().Names }, diff --git a/internal/cmd/volume/attach.go b/internal/cmd/volume/attach.go index a7e2da5d..e5b2993d 100644 --- a/internal/cmd/volume/attach.go +++ b/internal/cmd/volume/attach.go @@ -13,7 +13,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var AttachCommand = base.Cmd{ +var AttachCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { cmd := &cobra.Command{ Use: "attach [FLAGS] VOLUME", diff --git a/internal/cmd/volume/create.go b/internal/cmd/volume/create.go index 7e398056..1a5ab167 100644 --- a/internal/cmd/volume/create.go +++ b/internal/cmd/volume/create.go @@ -14,7 +14,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var CreateCommand = base.Cmd{ +var CreateCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { cmd := &cobra.Command{ Use: "create FLAGS", diff --git a/internal/cmd/volume/delete.go b/internal/cmd/volume/delete.go index 80160570..292e5aaa 100644 --- a/internal/cmd/volume/delete.go +++ b/internal/cmd/volume/delete.go @@ -11,7 +11,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var deleteCmd = base.DeleteCmd{ +var DeleteCmd = base.DeleteCmd{ ResourceNameSingular: "Volume", ShortDescription: "Delete a Volume", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Volume().Names }, diff --git a/internal/cmd/volume/describe.go b/internal/cmd/volume/describe.go index 7d5683c4..b089d6b8 100644 --- a/internal/cmd/volume/describe.go +++ b/internal/cmd/volume/describe.go @@ -13,7 +13,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var describeCmd = base.DescribeCmd{ +var DescribeCmd = base.DescribeCmd{ ResourceNameSingular: "volume", ShortDescription: "Describe an Volume", JSONKeyGetByID: "volume", diff --git a/internal/cmd/volume/detach.go b/internal/cmd/volume/detach.go index 256565d1..2b08ec5d 100644 --- a/internal/cmd/volume/detach.go +++ b/internal/cmd/volume/detach.go @@ -12,7 +12,7 @@ import ( "github.com/hetznercloud/cli/internal/state" ) -var DetachCommand = base.Cmd{ +var DetachCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { return &cobra.Command{ Use: "detach [FLAGS] VOLUME", diff --git a/internal/cmd/volume/disable_protection.go b/internal/cmd/volume/disable_protection.go index 81d94fc4..46770bd1 100644 --- a/internal/cmd/volume/disable_protection.go +++ b/internal/cmd/volume/disable_protection.go @@ -12,7 +12,7 @@ import ( "github.com/hetznercloud/cli/internal/state" ) -var DisableProtectionCommand = base.Cmd{ +var DisableProtectionCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { return &cobra.Command{ Use: "disable-protection [FLAGS] VOLUME PROTECTIONLEVEL [PROTECTIONLEVEL...]", diff --git a/internal/cmd/volume/enable_protection.go b/internal/cmd/volume/enable_protection.go index a3c2cffb..9a9281e2 100644 --- a/internal/cmd/volume/enable_protection.go +++ b/internal/cmd/volume/enable_protection.go @@ -57,7 +57,7 @@ func changeProtection(ctx context.Context, client hcapi2.Client, waiter state.Ac return nil } -var EnableProtectionCommand = base.Cmd{ +var EnableProtectionCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { return &cobra.Command{ Use: "enable-protection [FLAGS] VOLUME PROTECTIONLEVEL [PROTECTIONLEVEL...]", diff --git a/internal/cmd/volume/labels.go b/internal/cmd/volume/labels.go index 5e91dd3f..b502f419 100644 --- a/internal/cmd/volume/labels.go +++ b/internal/cmd/volume/labels.go @@ -9,7 +9,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var labelCmds = base.LabelCmds{ +var LabelCmds = base.LabelCmds{ ResourceNameSingular: "Volume", ShortDescriptionAdd: "Add a label to a Volume", ShortDescriptionRemove: "Remove a label from a Volume", diff --git a/internal/cmd/volume/resize.go b/internal/cmd/volume/resize.go index b0e6f0ba..1d319def 100644 --- a/internal/cmd/volume/resize.go +++ b/internal/cmd/volume/resize.go @@ -12,7 +12,7 @@ import ( "github.com/hetznercloud/cli/internal/state" ) -var ResizeCommand = base.Cmd{ +var ResizeCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { cmd := &cobra.Command{ Use: "resize [FLAGS] VOLUME", diff --git a/internal/cmd/volume/update.go b/internal/cmd/volume/update.go index f6d9ed8d..73a76e2e 100644 --- a/internal/cmd/volume/update.go +++ b/internal/cmd/volume/update.go @@ -11,7 +11,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -var updateCmd = base.UpdateCmd{ +var UpdateCmd = base.UpdateCmd{ ResourceNameSingular: "Volume", ShortDescription: "Update a Volume", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Volume().Names }, diff --git a/internal/cmd/volume/volume.go b/internal/cmd/volume/volume.go index 5672d1f9..ea37493c 100644 --- a/internal/cmd/volume/volume.go +++ b/internal/cmd/volume/volume.go @@ -17,17 +17,17 @@ func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command { } cmd.AddCommand( ListCmd.CobraCommand(cli.Context, client, cli), - CreateCommand.CobraCommand(cli.Context, client, cli, cli), - updateCmd.CobraCommand(cli.Context, client, cli), - deleteCmd.CobraCommand(cli.Context, client, cli, cli), - describeCmd.CobraCommand(cli.Context, client, cli), - AttachCommand.CobraCommand(cli.Context, client, cli, cli), - DetachCommand.CobraCommand(cli.Context, client, cli, cli), - ResizeCommand.CobraCommand(cli.Context, client, cli, cli), - EnableProtectionCommand.CobraCommand(cli.Context, client, cli, cli), - DisableProtectionCommand.CobraCommand(cli.Context, client, cli, cli), - labelCmds.AddCobraCommand(cli.Context, client, cli), - labelCmds.RemoveCobraCommand(cli.Context, client, cli), + CreateCmd.CobraCommand(cli.Context, client, cli, cli), + UpdateCmd.CobraCommand(cli.Context, client, cli), + DeleteCmd.CobraCommand(cli.Context, client, cli, cli), + DescribeCmd.CobraCommand(cli.Context, client, cli), + AttachCmd.CobraCommand(cli.Context, client, cli, cli), + DetachCmd.CobraCommand(cli.Context, client, cli, cli), + ResizeCmd.CobraCommand(cli.Context, client, cli, cli), + EnableProtectionCmd.CobraCommand(cli.Context, client, cli, cli), + DisableProtectionCmd.CobraCommand(cli.Context, client, cli, cli), + LabelCmds.AddCobraCommand(cli.Context, client, cli), + LabelCmds.RemoveCobraCommand(cli.Context, client, cli), ) return cmd }