From 9a35b53cd5414defc6029ce09560b8e1e81b4511 Mon Sep 17 00:00:00 2001 From: Michael Frister Date: Fri, 8 Jan 2021 15:57:11 +0100 Subject: [PATCH 1/9] Refactor: Split cli package into cli root, state, and cmds --- cli/root.go | 39 ---- cmd/hcloud/main.go | 14 +- internal/cli/cli.go | 20 ++ internal/cli/root.go | 41 ++++ {cli => internal/cmds}/certificate.go | 9 +- .../cmds}/certificate_add_label.go | 11 +- {cli => internal/cmds}/certificate_create.go | 11 +- {cli => internal/cmds}/certificate_delete.go | 11 +- .../cmds}/certificate_describe.go | 13 +- {cli => internal/cmds}/certificate_list.go | 11 +- .../cmds}/certificate_remove_label.go | 11 +- {cli => internal/cmds}/certificate_update.go | 11 +- {cli => internal/cmds}/completion.go | 5 +- {cli => internal/cmds}/context.go | 9 +- {cli => internal/cmds}/context_active.go | 9 +- {cli => internal/cmds}/context_create.go | 11 +- {cli => internal/cmds}/context_delete.go | 9 +- {cli => internal/cmds}/context_list.go | 9 +- {cli => internal/cmds}/context_use.go | 9 +- {cli => internal/cmds}/datacenter.go | 9 +- {cli => internal/cmds}/datacenter_describe.go | 15 +- {cli => internal/cmds}/datacenter_list.go | 11 +- {cli => internal/cmds}/floatingip.go | 9 +- .../cmds}/floatingip_add_label.go | 11 +- {cli => internal/cmds}/floatingip_assign.go | 11 +- {cli => internal/cmds}/floatingip_create.go | 11 +- {cli => internal/cmds}/floatingip_delete.go | 11 +- {cli => internal/cmds}/floatingip_describe.go | 13 +- .../cmds}/floatingip_disable_protection.go | 11 +- .../cmds}/floatingip_enable_protection.go | 11 +- {cli => internal/cmds}/floatingip_list.go | 13 +- .../cmds}/floatingip_remove_label.go | 11 +- {cli => internal/cmds}/floatingip_set_rdns.go | 11 +- {cli => internal/cmds}/floatingip_unassign.go | 11 +- {cli => internal/cmds}/floatingip_update.go | 11 +- {cli => internal/cmds}/image.go | 9 +- {cli => internal/cmds}/image_add_label.go | 11 +- {cli => internal/cmds}/image_delete.go | 11 +- {cli => internal/cmds}/image_describe.go | 13 +- .../cmds}/image_disable_protection.go | 11 +- .../cmds}/image_enable_protection.go | 11 +- {cli => internal/cmds}/image_list.go | 13 +- {cli => internal/cmds}/image_remove_label.go | 11 +- {cli => internal/cmds}/image_update.go | 11 +- {cli => internal/cmds}/iso.go | 9 +- {cli => internal/cmds}/iso_describe.go | 11 +- {cli => internal/cmds}/iso_list.go | 11 +- {cli => internal/cmds}/load_balancer.go | 9 +- .../cmds}/load_balancer_add_label.go | 11 +- .../cmds}/load_balancer_add_service.go | 11 +- .../cmds}/load_balancer_add_target.go | 11 +- .../cmds}/load_balancer_attach_to_network.go | 11 +- .../cmds}/load_balancer_change_algorithm.go | 11 +- .../cmds}/load_balancer_change_type.go | 11 +- .../cmds}/load_balancer_create.go | 11 +- .../cmds}/load_balancer_delete.go | 11 +- .../cmds}/load_balancer_delete_service.go | 11 +- .../cmds}/load_balancer_describe.go | 15 +- .../load_balancer_detach_from_network.go | 11 +- .../cmds}/load_balancer_disable_protection.go | 11 +- .../load_balancer_disable_public_interface.go | 11 +- .../cmds}/load_balancer_enable_protection.go | 11 +- .../load_balancer_enable_public_interface.go | 11 +- {cli => internal/cmds}/load_balancer_list.go | 16 +- .../cmds}/load_balancer_metrics.go | 14 +- .../cmds}/load_balancer_remove_label.go | 11 +- .../cmds}/load_balancer_remove_target.go | 11 +- {cli => internal/cmds}/load_balancer_type.go | 9 +- .../cmds}/load_balancer_type_describe.go | 13 +- .../cmds}/load_balancer_type_list.go | 11 +- .../cmds}/load_balancer_update.go | 11 +- .../cmds}/load_balancer_update_service.go | 11 +- {cli => internal/cmds}/location.go | 9 +- {cli => internal/cmds}/location_describe.go | 13 +- {cli => internal/cmds}/location_list.go | 11 +- {cli => internal/cmds}/network.go | 9 +- {cli => internal/cmds}/network_add_label.go | 11 +- {cli => internal/cmds}/network_add_route.go | 11 +- {cli => internal/cmds}/network_add_subnet.go | 11 +- .../cmds}/network_change_ip_range.go | 11 +- {cli => internal/cmds}/network_create.go | 11 +- {cli => internal/cmds}/network_delete.go | 11 +- {cli => internal/cmds}/network_describe.go | 13 +- .../cmds}/network_disable_protection.go | 11 +- .../cmds}/network_enable_protection.go | 11 +- {cli => internal/cmds}/network_list.go | 13 +- .../cmds}/network_remove_label.go | 11 +- .../cmds}/network_remove_route.go | 11 +- .../cmds}/network_remove_subnet.go | 11 +- {cli => internal/cmds}/network_update.go | 11 +- {cli => internal/cmds}/output.go | 2 +- {cli => internal/cmds}/output_test.go | 2 +- {cli => internal/cmds}/server.go | 9 +- {cli => internal/cmds}/server_add_label.go | 11 +- {cli => internal/cmds}/server_attach_iso.go | 11 +- .../cmds}/server_attach_to_network.go | 11 +- .../cmds}/server_change_alias_ips.go | 11 +- {cli => internal/cmds}/server_change_type.go | 11 +- {cli => internal/cmds}/server_create.go | 26 +-- {cli => internal/cmds}/server_create_image.go | 11 +- {cli => internal/cmds}/server_delete.go | 11 +- {cli => internal/cmds}/server_describe.go | 19 +- .../cmds}/server_detach_from_network.go | 11 +- {cli => internal/cmds}/server_detach_iso.go | 11 +- .../cmds}/server_disable_backup.go | 11 +- .../cmds}/server_disable_protection.go | 11 +- .../cmds}/server_disable_rescue.go | 11 +- .../cmds}/server_enable_backup.go | 11 +- .../cmds}/server_enable_protection.go | 11 +- .../cmds}/server_enable_rescue.go | 11 +- {cli => internal/cmds}/server_ip.go | 11 +- {cli => internal/cmds}/server_list.go | 13 +- {cli => internal/cmds}/server_metrics.go | 14 +- {cli => internal/cmds}/server_poweroff.go | 11 +- {cli => internal/cmds}/server_poweron.go | 11 +- {cli => internal/cmds}/server_reboot.go | 11 +- {cli => internal/cmds}/server_rebuild.go | 11 +- {cli => internal/cmds}/server_remove_label.go | 11 +- .../cmds}/server_request_console.go | 11 +- {cli => internal/cmds}/server_reset.go | 11 +- .../cmds}/server_reset_password.go | 11 +- {cli => internal/cmds}/server_set_rdns.go | 11 +- {cli => internal/cmds}/server_shutdown.go | 11 +- {cli => internal/cmds}/server_ssh.go | 11 +- {cli => internal/cmds}/server_update.go | 11 +- {cli => internal/cmds}/servertypes.go | 9 +- .../cmds}/servertypes_describe.go | 13 +- {cli => internal/cmds}/servertypes_list.go | 11 +- {cli => internal/cmds}/sshkey.go | 9 +- {cli => internal/cmds}/sshkey_add_label.go | 11 +- {cli => internal/cmds}/sshkey_create.go | 11 +- {cli => internal/cmds}/sshkey_delete.go | 11 +- {cli => internal/cmds}/sshkey_describe.go | 13 +- {cli => internal/cmds}/sshkey_list.go | 11 +- {cli => internal/cmds}/sshkey_remove_label.go | 11 +- {cli => internal/cmds}/sshkey_update.go | 11 +- {cli => internal/cmds}/util.go | 2 +- {cli => internal/cmds}/util_internal_test.go | 2 +- {cli => internal/cmds}/version.go | 9 +- {cli => internal/cmds}/volume.go | 9 +- {cli => internal/cmds}/volume_add_label.go | 11 +- {cli => internal/cmds}/volume_attach.go | 11 +- {cli => internal/cmds}/volume_create.go | 11 +- {cli => internal/cmds}/volume_delete.go | 11 +- {cli => internal/cmds}/volume_describe.go | 13 +- {cli => internal/cmds}/volume_detach.go | 11 +- .../cmds}/volume_disable_protection.go | 11 +- .../cmds}/volume_enable_protection.go | 11 +- {cli => internal/cmds}/volume_list.go | 13 +- {cli => internal/cmds}/volume_remove_label.go | 11 +- {cli => internal/cmds}/volume_resize.go | 11 +- {cli => internal/cmds}/volume_update.go | 11 +- {cli => internal/state}/config.go | 2 +- {cli => internal/state}/config_unix.go | 2 +- {cli => internal/state}/config_windows.go | 2 +- cli/cli.go => internal/state/helpers.go | 176 ++++-------------- internal/state/state.go | 118 ++++++++++++ internal/state/version.go | 4 + 158 files changed, 1124 insertions(+), 912 deletions(-) delete mode 100644 cli/root.go create mode 100644 internal/cli/cli.go create mode 100644 internal/cli/root.go rename {cli => internal/cmds}/certificate.go (76%) rename {cli => internal/cmds}/certificate_add_label.go (84%) rename {cli => internal/cmds}/certificate_create.go (83%) rename {cli => internal/cmds}/certificate_delete.go (71%) rename {cli => internal/cmds}/certificate_describe.go (84%) rename {cli => internal/cmds}/certificate_list.go (90%) rename {cli => internal/cmds}/certificate_remove_label.go (87%) rename {cli => internal/cmds}/certificate_update.go (77%) rename {cli => internal/cmds}/completion.go (95%) rename {cli => internal/cmds}/context.go (72%) rename {cli => internal/cmds}/context_active.go (59%) rename {cli => internal/cmds}/context_create.go (80%) rename {cli => internal/cmds}/context_delete.go (73%) rename {cli => internal/cmds}/context_list.go (84%) rename {cli => internal/cmds}/context_use.go (71%) rename {cli => internal/cmds}/datacenter.go (67%) rename {cli => internal/cmds}/datacenter_describe.go (86%) rename {cli => internal/cmds}/datacenter_list.go (84%) rename {cli => internal/cmds}/floatingip.go (81%) rename {cli => internal/cmds}/floatingip_add_label.go (84%) rename {cli => internal/cmds}/floatingip_assign.go (79%) rename {cli => internal/cmds}/floatingip_create.go (90%) rename {cli => internal/cmds}/floatingip_delete.go (72%) rename {cli => internal/cmds}/floatingip_describe.go (88%) rename {cli => internal/cmds}/floatingip_disable_protection.go (80%) rename {cli => internal/cmds}/floatingip_enable_protection.go (80%) rename {cli => internal/cmds}/floatingip_list.go (91%) rename {cli => internal/cmds}/floatingip_remove_label.go (86%) rename {cli => internal/cmds}/floatingip_set_rdns.go (81%) rename {cli => internal/cmds}/floatingip_unassign.go (74%) rename {cli => internal/cmds}/floatingip_update.go (79%) rename {cli => internal/cmds}/image.go (76%) rename {cli => internal/cmds}/image_add_label.go (81%) rename {cli => internal/cmds}/image_delete.go (72%) rename {cli => internal/cmds}/image_describe.go (87%) rename {cli => internal/cmds}/image_disable_protection.go (80%) rename {cli => internal/cmds}/image_enable_protection.go (80%) rename {cli => internal/cmds}/image_list.go (92%) rename {cli => internal/cmds}/image_remove_label.go (86%) rename {cli => internal/cmds}/image_update.go (80%) rename {cli => internal/cmds}/iso.go (66%) rename {cli => internal/cmds}/iso_describe.go (84%) rename {cli => internal/cmds}/iso_list.go (81%) rename {cli => internal/cmds}/load_balancer.go (87%) rename {cli => internal/cmds}/load_balancer_add_label.go (84%) rename {cli => internal/cmds}/load_balancer_add_service.go (93%) rename {cli => internal/cmds}/load_balancer_add_target.go (89%) rename {cli => internal/cmds}/load_balancer_attach_to_network.go (83%) rename {cli => internal/cmds}/load_balancer_change_algorithm.go (81%) rename {cli => internal/cmds}/load_balancer_change_type.go (81%) rename {cli => internal/cmds}/load_balancer_create.go (89%) rename {cli => internal/cmds}/load_balancer_delete.go (72%) rename {cli => internal/cmds}/load_balancer_delete_service.go (76%) rename {cli => internal/cmds}/load_balancer_describe.go (93%) rename {cli => internal/cmds}/load_balancer_detach_from_network.go (82%) rename {cli => internal/cmds}/load_balancer_disable_protection.go (80%) rename {cli => internal/cmds}/load_balancer_disable_public_interface.go (74%) rename {cli => internal/cmds}/load_balancer_enable_protection.go (80%) rename {cli => internal/cmds}/load_balancer_enable_public_interface.go (74%) rename {cli => internal/cmds}/load_balancer_list.go (94%) rename {cli => internal/cmds}/load_balancer_metrics.go (90%) rename {cli => internal/cmds}/load_balancer_remove_label.go (86%) rename {cli => internal/cmds}/load_balancer_remove_target.go (87%) rename {cli => internal/cmds}/load_balancer_type.go (68%) rename {cli => internal/cmds}/load_balancer_type_describe.go (84%) rename {cli => internal/cmds}/load_balancer_type_list.go (83%) rename {cli => internal/cmds}/load_balancer_update.go (77%) rename {cli => internal/cmds}/load_balancer_update_service.go (95%) rename {cli => internal/cmds}/location.go (66%) rename {cli => internal/cmds}/location_describe.go (82%) rename {cli => internal/cmds}/location_list.go (82%) rename {cli => internal/cmds}/network.go (82%) rename {cli => internal/cmds}/network_add_label.go (84%) rename {cli => internal/cmds}/network_add_route.go (82%) rename {cli => internal/cmds}/network_add_subnet.go (86%) rename {cli => internal/cmds}/network_change_ip_range.go (78%) rename {cli => internal/cmds}/network_create.go (77%) rename {cli => internal/cmds}/network_delete.go (72%) rename {cli => internal/cmds}/network_describe.go (86%) rename {cli => internal/cmds}/network_disable_protection.go (80%) rename {cli => internal/cmds}/network_enable_protection.go (80%) rename {cli => internal/cmds}/network_list.go (90%) rename {cli => internal/cmds}/network_remove_label.go (86%) rename {cli => internal/cmds}/network_remove_route.go (81%) rename {cli => internal/cmds}/network_remove_subnet.go (79%) rename {cli => internal/cmds}/network_update.go (77%) rename {cli => internal/cmds}/output.go (99%) rename {cli => internal/cmds}/output_test.go (99%) rename {cli => internal/cmds}/server.go (89%) rename {cli => internal/cmds}/server_add_label.go (81%) rename {cli => internal/cmds}/server_attach_iso.go (78%) rename {cli => internal/cmds}/server_attach_to_network.go (85%) rename {cli => internal/cmds}/server_change_alias_ips.go (85%) rename {cli => internal/cmds}/server_change_type.go (83%) rename {cli => internal/cmds}/server_create.go (94%) rename {cli => internal/cmds}/server_create_image.go (87%) rename {cli => internal/cmds}/server_delete.go (71%) rename {cli => internal/cmds}/server_describe.go (91%) rename {cli => internal/cmds}/server_detach_from_network.go (81%) rename {cli => internal/cmds}/server_detach_iso.go (74%) rename {cli => internal/cmds}/server_disable_backup.go (73%) rename {cli => internal/cmds}/server_disable_protection.go (81%) rename {cli => internal/cmds}/server_disable_rescue.go (73%) rename {cli => internal/cmds}/server_enable_backup.go (80%) rename {cli => internal/cmds}/server_enable_protection.go (81%) rename {cli => internal/cmds}/server_enable_rescue.go (85%) rename {cli => internal/cmds}/server_ip.go (76%) rename {cli => internal/cmds}/server_list.go (94%) rename {cli => internal/cmds}/server_metrics.go (90%) rename {cli => internal/cmds}/server_poweroff.go (74%) rename {cli => internal/cmds}/server_poweron.go (74%) rename {cli => internal/cmds}/server_reboot.go (74%) rename {cli => internal/cmds}/server_rebuild.go (82%) rename {cli => internal/cmds}/server_remove_label.go (86%) rename {cli => internal/cmds}/server_request_console.go (79%) rename {cli => internal/cmds}/server_reset.go (74%) rename {cli => internal/cmds}/server_reset_password.go (74%) rename {cli => internal/cmds}/server_set_rdns.go (81%) rename {cli => internal/cmds}/server_shutdown.go (74%) rename {cli => internal/cmds}/server_ssh.go (86%) rename {cli => internal/cmds}/server_update.go (77%) rename {cli => internal/cmds}/servertypes.go (67%) rename {cli => internal/cmds}/servertypes_describe.go (84%) rename {cli => internal/cmds}/servertypes_list.go (85%) rename {cli => internal/cmds}/sshkey.go (75%) rename {cli => internal/cmds}/sshkey_add_label.go (82%) rename {cli => internal/cmds}/sshkey_create.go (85%) rename {cli => internal/cmds}/sshkey_delete.go (71%) rename {cli => internal/cmds}/sshkey_describe.go (83%) rename {cli => internal/cmds}/sshkey_list.go (87%) rename {cli => internal/cmds}/sshkey_remove_label.go (86%) rename {cli => internal/cmds}/sshkey_update.go (77%) rename {cli => internal/cmds}/util.go (99%) rename {cli => internal/cmds}/util_internal_test.go (98%) rename {cli => internal/cmds}/version.go (62%) rename {cli => internal/cmds}/volume.go (80%) rename {cli => internal/cmds}/volume_add_label.go (81%) rename {cli => internal/cmds}/volume_attach.go (83%) rename {cli => internal/cmds}/volume_create.go (89%) rename {cli => internal/cmds}/volume_delete.go (71%) rename {cli => internal/cmds}/volume_describe.go (88%) rename {cli => internal/cmds}/volume_detach.go (73%) rename {cli => internal/cmds}/volume_disable_protection.go (80%) rename {cli => internal/cmds}/volume_enable_protection.go (80%) rename {cli => internal/cmds}/volume_list.go (90%) rename {cli => internal/cmds}/volume_remove_label.go (86%) rename {cli => internal/cmds}/volume_resize.go (78%) rename {cli => internal/cmds}/volume_update.go (77%) rename {cli => internal/state}/config.go (99%) rename {cli => internal/state}/config_unix.go (94%) rename {cli => internal/state}/config_windows.go (92%) rename cli/cli.go => internal/state/helpers.go (65%) create mode 100644 internal/state/state.go create mode 100644 internal/state/version.go diff --git a/cli/root.go b/cli/root.go deleted file mode 100644 index 5d453160..00000000 --- a/cli/root.go +++ /dev/null @@ -1,39 +0,0 @@ -package cli - -import ( - "time" - - "github.com/spf13/cobra" -) - -func NewRootCommand(cli *CLI) *cobra.Command { - cmd := &cobra.Command{ - Use: "hcloud", - Short: "Hetzner Cloud CLI", - Long: "A command-line interface for Hetzner Cloud", - TraverseChildren: true, - SilenceUsage: true, - SilenceErrors: true, - DisableFlagsInUseLine: true, - } - cmd.AddCommand( - newFloatingIPCommand(cli), - newImageCommand(cli), - newServerCommand(cli), - newSSHKeyCommand(cli), - newVersionCommand(cli), - newCompletionCommand(cli), - newServerTypeCommand(cli), - newContextCommand(cli), - newDatacenterCommand(cli), - newLocationCommand(cli), - newISOCommand(cli), - newVolumeCommand(cli), - newNetworkCommand(cli), - newLoadBalancerCommand(cli), - newLoadBalancerTypeCommand(cli), - newCertificatesCommand(cli), - ) - cmd.PersistentFlags().Duration("poll-interval", 500*time.Millisecond, "Interval at which to poll information, for example action progress") - return cmd -} diff --git a/cmd/hcloud/main.go b/cmd/hcloud/main.go index a4f7bc82..6461ff97 100644 --- a/cmd/hcloud/main.go +++ b/cmd/hcloud/main.go @@ -4,7 +4,7 @@ import ( "log" "os" - "github.com/hetznercloud/cli/cli" + "github.com/hetznercloud/cli/internal/cli" ) func init() { @@ -16,21 +16,21 @@ func init() { func main() { c := cli.NewCLI() - if c.ConfigPath != "" { - _, err := os.Stat(c.ConfigPath) + if c.State.ConfigPath != "" { + _, err := os.Stat(c.State.ConfigPath) switch { case err == nil: - if err := c.ReadConfig(); err != nil { - log.Fatalf("unable to read config file %q: %s\n", c.ConfigPath, err) + if err := c.State.ReadConfig(); err != nil { + log.Fatalf("unable to read config file %q: %s\n", c.State.ConfigPath, err) } case os.IsNotExist(err): break default: - log.Fatalf("unable to read config file %q: %s\n", c.ConfigPath, err) + log.Fatalf("unable to read config file %q: %s\n", c.State.ConfigPath, err) } } - c.ReadEnv() + c.State.ReadEnv() if err := c.RootCommand.Execute(); err != nil { log.Fatalln(err) diff --git a/internal/cli/cli.go b/internal/cli/cli.go new file mode 100644 index 00000000..2f3af873 --- /dev/null +++ b/internal/cli/cli.go @@ -0,0 +1,20 @@ +package cli + +import ( + "github.com/hetznercloud/cli/internal/state" + "github.com/spf13/cobra" +) + +type CLI struct { + State *state.State + RootCommand *cobra.Command +} + +func NewCLI() *CLI { + cli := &CLI{ + State: state.New(), + } + + cli.RootCommand = NewRootCommand(cli.State) + return cli +} diff --git a/internal/cli/root.go b/internal/cli/root.go new file mode 100644 index 00000000..ba5d7dbc --- /dev/null +++ b/internal/cli/root.go @@ -0,0 +1,41 @@ +package cli + +import ( + "time" + + "github.com/hetznercloud/cli/internal/cmds" + "github.com/hetznercloud/cli/internal/state" + "github.com/spf13/cobra" +) + +func NewRootCommand(state *state.State) *cobra.Command { + cmd := &cobra.Command{ + Use: "hcloud", + Short: "Hetzner Cloud CLI", + Long: "A command-line interface for Hetzner Cloud", + TraverseChildren: true, + SilenceUsage: true, + SilenceErrors: true, + DisableFlagsInUseLine: true, + } + cmd.AddCommand( + cmds.NewFloatingIPCommand(state), + cmds.NewImageCommand(state), + cmds.NewServerCommand(state), + cmds.NewSSHKeyCommand(state), + cmds.NewVersionCommand(state), + cmds.NewCompletionCommand(state), + cmds.NewServerTypeCommand(state), + cmds.NewContextCommand(state), + cmds.NewDatacenterCommand(state), + cmds.NewLocationCommand(state), + cmds.NewISOCommand(state), + cmds.NewVolumeCommand(state), + cmds.NewNetworkCommand(state), + cmds.NewLoadBalancerCommand(state), + cmds.NewLoadBalancerTypeCommand(state), + cmds.NewCertificatesCommand(state), + ) + cmd.PersistentFlags().Duration("poll-interval", 500*time.Millisecond, "Interval at which to poll information, for example action progress") + return cmd +} diff --git a/cli/certificate.go b/internal/cmds/certificate.go similarity index 76% rename from cli/certificate.go rename to internal/cmds/certificate.go index 0f5b377a..d2f7ad0f 100644 --- a/cli/certificate.go +++ b/internal/cmds/certificate.go @@ -1,8 +1,11 @@ -package cli +package cmds -import "github.com/spf13/cobra" +import ( + "github.com/hetznercloud/cli/internal/state" + "github.com/spf13/cobra" +) -func newCertificatesCommand(cli *CLI) *cobra.Command { +func NewCertificatesCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "certificate", Short: "Manage certificates", diff --git a/cli/certificate_add_label.go b/internal/cmds/certificate_add_label.go similarity index 84% rename from cli/certificate_add_label.go rename to internal/cmds/certificate_add_label.go index 8260363c..9ea84972 100644 --- a/cli/certificate_add_label.go +++ b/internal/cmds/certificate_add_label.go @@ -1,14 +1,15 @@ -package cli +package cmds import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newCertificateAddLabelCommand(cli *CLI) *cobra.Command { +func newCertificateAddLabelCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "add-label [FLAGS] CERTIFICATE LABEL", Short: "Add a label to a certificate", @@ -16,8 +17,8 @@ func newCertificateAddLabelCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.CertificateNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: chainRunE(validateCertificateAddLabel, cli.ensureToken), - RunE: cli.wrap(runCertificateAddLabel), + PreRunE: chainRunE(validateCertificateAddLabel, cli.EnsureToken), + RunE: cli.Wrap(runCertificateAddLabel), } cmd.Flags().BoolP("overwrite", "o", false, "Overwrite label if it exists already") @@ -32,7 +33,7 @@ func validateCertificateAddLabel(cmd *cobra.Command, args []string) error { return nil } -func runCertificateAddLabel(cli *CLI, cmd *cobra.Command, args []string) error { +func runCertificateAddLabel(cli *state.State, cmd *cobra.Command, args []string) error { overwrite, err := cmd.Flags().GetBool("overwrite") if err != nil { return err diff --git a/cli/certificate_create.go b/internal/cmds/certificate_create.go similarity index 83% rename from cli/certificate_create.go rename to internal/cmds/certificate_create.go index 9ff88228..a98a4ed6 100644 --- a/cli/certificate_create.go +++ b/internal/cmds/certificate_create.go @@ -1,22 +1,23 @@ -package cli +package cmds import ( "fmt" "io/ioutil" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newCertificateCreateCommand(cli *CLI) *cobra.Command { +func newCertificateCreateCommand(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(runCertificateCreate), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runCertificateCreate), } cmd.Flags().String("name", "", "Certificate name (required)") @@ -31,7 +32,7 @@ func newCertificateCreateCommand(cli *CLI) *cobra.Command { return cmd } -func runCertificateCreate(cli *CLI, cmd *cobra.Command, args []string) error { +func runCertificateCreate(cli *state.State, cmd *cobra.Command, args []string) error { var ( name string diff --git a/cli/certificate_delete.go b/internal/cmds/certificate_delete.go similarity index 71% rename from cli/certificate_delete.go rename to internal/cmds/certificate_delete.go index 5566c791..6f8c8aa0 100644 --- a/cli/certificate_delete.go +++ b/internal/cmds/certificate_delete.go @@ -1,13 +1,14 @@ -package cli +package cmds import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/spf13/cobra" ) -func newCertificateDeleteCommand(cli *CLI) *cobra.Command { +func newCertificateDeleteCommand(cli *state.State) *cobra.Command { return &cobra.Command{ Use: "delete CERTIFICATE", Short: "Delete a certificate", @@ -15,12 +16,12 @@ func newCertificateDeleteCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.CertificateNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runCertificateDelete), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runCertificateDelete), } } -func runCertificateDelete(cli *CLI, cmd *cobra.Command, args []string) error { +func runCertificateDelete(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] cert, _, err := cli.Client().Certificate.Get(cli.Context, idOrName) if err != nil { diff --git a/cli/certificate_describe.go b/internal/cmds/certificate_describe.go similarity index 84% rename from cli/certificate_describe.go rename to internal/cmds/certificate_describe.go index a2283aa0..dab0168b 100644 --- a/cli/certificate_describe.go +++ b/internal/cmds/certificate_describe.go @@ -1,4 +1,4 @@ -package cli +package cmds import ( "encoding/json" @@ -6,11 +6,12 @@ import ( humanize "github.com/dustin/go-humanize" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newCertificateDescribeCommand(cli *CLI) *cobra.Command { +func newCertificateDescribeCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "describe [FLAGS] CERTIFICATE", Short: "Describe a certificate", @@ -18,14 +19,14 @@ func newCertificateDescribeCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.CertificateNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runCertificateDescribe), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runCertificateDescribe), } addOutputFlag(cmd, outputOptionJSON(), outputOptionFormat()) return cmd } -func runCertificateDescribe(cli *CLI, cmd *cobra.Command, args []string) error { +func runCertificateDescribe(cli *state.State, cmd *cobra.Command, args []string) error { outputFlags := outputFlagsForCommand(cmd) idOrName := args[0] @@ -61,7 +62,7 @@ func certificateDescribeJSON(resp *hcloud.Response) error { return describeJSON(data) } -func certificateDescribeText(cli *CLI, cert *hcloud.Certificate) error { +func certificateDescribeText(cli *state.State, cert *hcloud.Certificate) error { fmt.Printf("ID:\t\t\t%d\n", cert.ID) fmt.Printf("Name:\t\t\t%s\n", cert.Name) fmt.Printf("Fingerprint:\t\t%s\n", cert.Fingerprint) diff --git a/cli/certificate_list.go b/internal/cmds/certificate_list.go similarity index 90% rename from cli/certificate_list.go rename to internal/cmds/certificate_list.go index 1230adc1..fad239f5 100644 --- a/cli/certificate_list.go +++ b/internal/cmds/certificate_list.go @@ -1,8 +1,9 @@ -package cli +package cmds import ( "strings" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/hetznercloud/hcloud-go/hcloud/schema" "github.com/spf13/cobra" @@ -14,7 +15,7 @@ func init() { certificateTableOutput = describeCertificatesTableOutput() } -func newCertificatesListCommand(cli *CLI) *cobra.Command { +func newCertificatesListCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "list [FLAGS]", Short: "List Certificates", @@ -24,14 +25,14 @@ func newCertificatesListCommand(cli *CLI) *cobra.Command { ), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runCertificatesList), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runCertificatesList), } addOutputFlag(cmd, outputOptionNoHeader(), outputOptionColumns(serverListTableOutput.Columns()), outputOptionJSON()) return cmd } -func runCertificatesList(cli *CLI, cmd *cobra.Command, args []string) error { +func runCertificatesList(cli *state.State, cmd *cobra.Command, args []string) error { outOpts := outputFlagsForCommand(cmd) labelSelector, _ := cmd.Flags().GetString("selector") diff --git a/cli/certificate_remove_label.go b/internal/cmds/certificate_remove_label.go similarity index 87% rename from cli/certificate_remove_label.go rename to internal/cmds/certificate_remove_label.go index f8f65f29..ac7442b9 100644 --- a/cli/certificate_remove_label.go +++ b/internal/cmds/certificate_remove_label.go @@ -1,15 +1,16 @@ -package cli +package cmds import ( "errors" "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newCertificateRemoveLabelCommand(cli *CLI) *cobra.Command { +func newCertificateRemoveLabelCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "remove-label [FLAGS] CERTIFICATE LABELKEY", Short: "Remove a label from a certificate", @@ -25,8 +26,8 @@ func newCertificateRemoveLabelCommand(cli *CLI) *cobra.Command { ), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: chainRunE(validateCertificateRemoveLabel, cli.ensureToken), - RunE: cli.wrap(runCertificateRemoveLabel), + PreRunE: chainRunE(validateCertificateRemoveLabel, cli.EnsureToken), + RunE: cli.Wrap(runCertificateRemoveLabel), } cmd.Flags().BoolP("all", "a", false, "Remove all labels") return cmd @@ -46,7 +47,7 @@ func validateCertificateRemoveLabel(cmd *cobra.Command, args []string) error { return nil } -func runCertificateRemoveLabel(cli *CLI, cmd *cobra.Command, args []string) error { +func runCertificateRemoveLabel(cli *state.State, cmd *cobra.Command, args []string) error { // We ensured the all flag is a valid boolean in // validateCertificateRemoveLabel. No need to handle the error again here. all, _ := cmd.Flags().GetBool("all") diff --git a/cli/certificate_update.go b/internal/cmds/certificate_update.go similarity index 77% rename from cli/certificate_update.go rename to internal/cmds/certificate_update.go index f8ac43c1..2a44a186 100644 --- a/cli/certificate_update.go +++ b/internal/cmds/certificate_update.go @@ -1,14 +1,15 @@ -package cli +package cmds import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newCertificateUpdateCommand(cli *CLI) *cobra.Command { +func newCertificateUpdateCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "update [FLAGS] CERTIFICATE", Short: "Update an existing Certificate", @@ -16,15 +17,15 @@ func newCertificateUpdateCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.CertificateNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runCertificateUpdate), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runCertificateUpdate), } cmd.Flags().String("name", "", "Certificate name") return cmd } -func runCertificateUpdate(cli *CLI, cmd *cobra.Command, args []string) error { +func runCertificateUpdate(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] cert, _, err := cli.Client().Certificate.Get(cli.Context, idOrName) if err != nil { diff --git a/cli/completion.go b/internal/cmds/completion.go similarity index 95% rename from cli/completion.go rename to internal/cmds/completion.go index 38b4bd43..6a4389da 100644 --- a/cli/completion.go +++ b/internal/cmds/completion.go @@ -1,9 +1,10 @@ -package cli +package cmds import ( "fmt" "os" + "github.com/hetznercloud/cli/internal/state" "github.com/spf13/cobra" ) @@ -29,7 +30,7 @@ import ( // source <(hcloud completion zsh)` // ) -func newCompletionCommand(cli *CLI) *cobra.Command { +func NewCompletionCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "completion [FLAGS] SHELL", Short: "Output shell completion code for the specified shell", diff --git a/cli/context.go b/internal/cmds/context.go similarity index 72% rename from cli/context.go rename to internal/cmds/context.go index c4c9094b..341bf55f 100644 --- a/cli/context.go +++ b/internal/cmds/context.go @@ -1,8 +1,11 @@ -package cli +package cmds -import "github.com/spf13/cobra" +import ( + "github.com/hetznercloud/cli/internal/state" + "github.com/spf13/cobra" +) -func newContextCommand(cli *CLI) *cobra.Command { +func NewContextCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "context [FLAGS]", Short: "Manage contexts", diff --git a/cli/context_active.go b/internal/cmds/context_active.go similarity index 59% rename from cli/context_active.go rename to internal/cmds/context_active.go index 7ee76135..95771c2d 100644 --- a/cli/context_active.go +++ b/internal/cmds/context_active.go @@ -1,24 +1,25 @@ -package cli +package cmds import ( "fmt" + "github.com/hetznercloud/cli/internal/state" "github.com/spf13/cobra" ) -func newContextActiveCommand(cli *CLI) *cobra.Command { +func newContextActiveCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "active [FLAGS]", Short: "Show active context", Args: cobra.NoArgs, TraverseChildren: true, DisableFlagsInUseLine: true, - RunE: cli.wrap(runContextActive), + RunE: cli.Wrap(runContextActive), } return cmd } -func runContextActive(cli *CLI, cmd *cobra.Command, args []string) error { +func runContextActive(cli *state.State, cmd *cobra.Command, args []string) error { if cli.Config.ActiveContext != nil { fmt.Println(cli.Config.ActiveContext.Name) } diff --git a/cli/context_create.go b/internal/cmds/context_create.go similarity index 80% rename from cli/context_create.go rename to internal/cmds/context_create.go index 94a00817..1626b6ea 100644 --- a/cli/context_create.go +++ b/internal/cmds/context_create.go @@ -1,4 +1,4 @@ -package cli +package cmds import ( "bytes" @@ -7,23 +7,24 @@ import ( "strings" "syscall" + "github.com/hetznercloud/cli/internal/state" "github.com/spf13/cobra" "golang.org/x/crypto/ssh/terminal" ) -func newContextCreateCommand(cli *CLI) *cobra.Command { +func newContextCreateCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "create [FLAGS] NAME", Short: "Create a new context", Args: cobra.ExactArgs(1), TraverseChildren: true, DisableFlagsInUseLine: true, - RunE: cli.wrap(runContextCreate), + RunE: cli.Wrap(runContextCreate), } return cmd } -func runContextCreate(cli *CLI, cmd *cobra.Command, args []string) error { +func runContextCreate(cli *state.State, cmd *cobra.Command, args []string) error { if !cli.Terminal() { return errors.New("context create is an interactive command") } @@ -36,7 +37,7 @@ func runContextCreate(cli *CLI, cmd *cobra.Command, args []string) error { return errors.New("name already used") } - context := &ConfigContext{Name: name} + context := &state.ConfigContext{Name: name} for { fmt.Printf("Token: ") diff --git a/cli/context_delete.go b/internal/cmds/context_delete.go similarity index 73% rename from cli/context_delete.go rename to internal/cmds/context_delete.go index 2fcb66d1..72590315 100644 --- a/cli/context_delete.go +++ b/internal/cmds/context_delete.go @@ -1,13 +1,14 @@ -package cli +package cmds import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/spf13/cobra" ) -func newContextDeleteCommand(cli *CLI) *cobra.Command { +func newContextDeleteCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "delete [FLAGS] NAME", Short: "Delete a context", @@ -15,12 +16,12 @@ func newContextDeleteCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.Config.ContextNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - RunE: cli.wrap(runContextDelete), + RunE: cli.Wrap(runContextDelete), } return cmd } -func runContextDelete(cli *CLI, cmd *cobra.Command, args []string) error { +func runContextDelete(cli *state.State, cmd *cobra.Command, args []string) error { name := args[0] context := cli.Config.ContextByName(name) if context == nil { diff --git a/cli/context_list.go b/internal/cmds/context_list.go similarity index 84% rename from cli/context_list.go rename to internal/cmds/context_list.go index 8cec5522..837edd71 100644 --- a/cli/context_list.go +++ b/internal/cmds/context_list.go @@ -1,6 +1,7 @@ -package cli +package cmds import ( + "github.com/hetznercloud/cli/internal/state" "github.com/spf13/cobra" ) @@ -18,7 +19,7 @@ func init() { RemoveAllowedField("token") } -func newContextListCommand(cli *CLI) *cobra.Command { +func newContextListCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "list [FLAGS]", Short: "List contexts", @@ -29,13 +30,13 @@ func newContextListCommand(cli *CLI) *cobra.Command { Args: cobra.NoArgs, TraverseChildren: true, DisableFlagsInUseLine: true, - RunE: cli.wrap(runContextList), + RunE: cli.Wrap(runContextList), } addOutputFlag(cmd, outputOptionNoHeader(), outputOptionColumns(contextListTableOutput.Columns())) return cmd } -func runContextList(cli *CLI, cmd *cobra.Command, args []string) error { +func runContextList(cli *state.State, cmd *cobra.Command, args []string) error { outOpts := outputFlagsForCommand(cmd) cols := []string{"active", "name"} diff --git a/cli/context_use.go b/internal/cmds/context_use.go similarity index 71% rename from cli/context_use.go rename to internal/cmds/context_use.go index 94504466..4763b10b 100644 --- a/cli/context_use.go +++ b/internal/cmds/context_use.go @@ -1,13 +1,14 @@ -package cli +package cmds import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/spf13/cobra" ) -func newContextUseCommand(cli *CLI) *cobra.Command { +func newContextUseCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "use [FLAGS] NAME", Short: "Use a context", @@ -15,12 +16,12 @@ func newContextUseCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.Config.ContextNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - RunE: cli.wrap(runContextUse), + RunE: cli.Wrap(runContextUse), } return cmd } -func runContextUse(cli *CLI, cmd *cobra.Command, args []string) error { +func runContextUse(cli *state.State, cmd *cobra.Command, args []string) error { name := args[0] context := cli.Config.ContextByName(name) if context == nil { diff --git a/cli/datacenter.go b/internal/cmds/datacenter.go similarity index 67% rename from cli/datacenter.go rename to internal/cmds/datacenter.go index 67ed1f6f..60637dbb 100644 --- a/cli/datacenter.go +++ b/internal/cmds/datacenter.go @@ -1,8 +1,11 @@ -package cli +package cmds -import "github.com/spf13/cobra" +import ( + "github.com/hetznercloud/cli/internal/state" + "github.com/spf13/cobra" +) -func newDatacenterCommand(cli *CLI) *cobra.Command { +func NewDatacenterCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "datacenter", Short: "Manage datacenters", diff --git a/cli/datacenter_describe.go b/internal/cmds/datacenter_describe.go similarity index 86% rename from cli/datacenter_describe.go rename to internal/cmds/datacenter_describe.go index cfe91dcb..b0ded4b7 100644 --- a/cli/datacenter_describe.go +++ b/internal/cmds/datacenter_describe.go @@ -1,15 +1,16 @@ -package cli +package cmds import ( "encoding/json" "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newDatacenterDescribeCommand(cli *CLI) *cobra.Command { +func newDatacenterDescribeCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "describe [FLAGS] DATACENTER", Short: "Describe a datacenter", @@ -17,14 +18,14 @@ func newDatacenterDescribeCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.DataCenterNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runDatacenterDescribe), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runDatacenterDescribe), } addOutputFlag(cmd, outputOptionJSON(), outputOptionFormat()) return cmd } -func runDatacenterDescribe(cli *CLI, cmd *cobra.Command, args []string) error { +func runDatacenterDescribe(cli *state.State, cmd *cobra.Command, args []string) error { outputFlags := outputFlagsForCommand(cmd) idOrName := args[0] @@ -46,7 +47,7 @@ func runDatacenterDescribe(cli *CLI, cmd *cobra.Command, args []string) error { } } -func datacenterDescribeText(cli *CLI, datacenter *hcloud.Datacenter) error { +func datacenterDescribeText(cli *state.State, datacenter *hcloud.Datacenter) error { fmt.Printf("ID:\t\t%d\n", datacenter.ID) fmt.Printf("Name:\t\t%s\n", datacenter.Name) fmt.Printf("Description:\t%s\n", datacenter.Description) @@ -68,7 +69,7 @@ func datacenterDescribeText(cli *CLI, datacenter *hcloud.Datacenter) error { } for id := range serverTypesMap { var err error - serverTypesMap[id], _, err = cli.client.ServerType.GetByID(cli.Context, id) + serverTypesMap[id], _, err = cli.Client().ServerType.GetByID(cli.Context, id) if err != nil { return fmt.Errorf("error fetching server type: %v", err) } diff --git a/cli/datacenter_list.go b/internal/cmds/datacenter_list.go similarity index 84% rename from cli/datacenter_list.go rename to internal/cmds/datacenter_list.go index 65809b88..1db98074 100644 --- a/cli/datacenter_list.go +++ b/internal/cmds/datacenter_list.go @@ -1,6 +1,7 @@ -package cli +package cmds import ( + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/hetznercloud/hcloud-go/hcloud/schema" "github.com/spf13/cobra" @@ -17,7 +18,7 @@ func init() { })) } -func newDatacenterListCommand(cli *CLI) *cobra.Command { +func newDatacenterListCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "list [FLAGS]", Short: "List datacenters", @@ -27,14 +28,14 @@ func newDatacenterListCommand(cli *CLI) *cobra.Command { ), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runDatacenterList), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runDatacenterList), } addOutputFlag(cmd, outputOptionNoHeader(), outputOptionColumns(datacenterListTableOutput.Columns()), outputOptionJSON()) return cmd } -func runDatacenterList(cli *CLI, cmd *cobra.Command, args []string) error { +func runDatacenterList(cli *state.State, cmd *cobra.Command, args []string) error { outOpts := outputFlagsForCommand(cmd) datacenters, err := cli.Client().Datacenter.All(cli.Context) diff --git a/cli/floatingip.go b/internal/cmds/floatingip.go similarity index 81% rename from cli/floatingip.go rename to internal/cmds/floatingip.go index 262d6d7c..d0bb69c1 100644 --- a/cli/floatingip.go +++ b/internal/cmds/floatingip.go @@ -1,8 +1,11 @@ -package cli +package cmds -import "github.com/spf13/cobra" +import ( + "github.com/hetznercloud/cli/internal/state" + "github.com/spf13/cobra" +) -func newFloatingIPCommand(cli *CLI) *cobra.Command { +func NewFloatingIPCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "floating-ip", Short: "Manage Floating IPs", diff --git a/cli/floatingip_add_label.go b/internal/cmds/floatingip_add_label.go similarity index 84% rename from cli/floatingip_add_label.go rename to internal/cmds/floatingip_add_label.go index 60681618..ea3d99be 100644 --- a/cli/floatingip_add_label.go +++ b/internal/cmds/floatingip_add_label.go @@ -1,14 +1,15 @@ -package cli +package cmds import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newFloatingIPAddLabelCommand(cli *CLI) *cobra.Command { +func newFloatingIPAddLabelCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "add-label [FLAGS] FLOATINGIP LABEL", Short: "Add a label to a Floating IP", @@ -16,8 +17,8 @@ func newFloatingIPAddLabelCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.FloatingIPNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: chainRunE(validateFloatingIPAddLabel, cli.ensureToken), - RunE: cli.wrap(runFloatingIPAddLabel), + PreRunE: chainRunE(validateFloatingIPAddLabel, cli.EnsureToken), + RunE: cli.Wrap(runFloatingIPAddLabel), } cmd.Flags().BoolP("overwrite", "o", false, "Overwrite label if it exists already") @@ -33,7 +34,7 @@ func validateFloatingIPAddLabel(cmd *cobra.Command, args []string) error { return nil } -func runFloatingIPAddLabel(cli *CLI, cmd *cobra.Command, args []string) error { +func runFloatingIPAddLabel(cli *state.State, cmd *cobra.Command, args []string) error { overwrite, _ := cmd.Flags().GetBool("overwrite") idOrName := args[0] diff --git a/cli/floatingip_assign.go b/internal/cmds/floatingip_assign.go similarity index 79% rename from cli/floatingip_assign.go rename to internal/cmds/floatingip_assign.go index 4b54beda..d23b75cd 100644 --- a/cli/floatingip_assign.go +++ b/internal/cmds/floatingip_assign.go @@ -1,13 +1,14 @@ -package cli +package cmds import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/spf13/cobra" ) -func newFloatingIPAssignCommand(cli *CLI) *cobra.Command { +func newFloatingIPAssignCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "assign [FLAGS] FLOATINGIP SERVER", Short: "Assign a Floating IP to a server", @@ -18,14 +19,14 @@ func newFloatingIPAssignCommand(cli *CLI) *cobra.Command { ), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runFloatingIPAssign), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runFloatingIPAssign), } cmd.MarkFlagRequired("server") return cmd } -func runFloatingIPAssign(cli *CLI, cmd *cobra.Command, args []string) error { +func runFloatingIPAssign(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] floatingIP, _, err := cli.Client().FloatingIP.Get(cli.Context, idOrName) if err != nil { diff --git a/cli/floatingip_create.go b/internal/cmds/floatingip_create.go similarity index 90% rename from cli/floatingip_create.go rename to internal/cmds/floatingip_create.go index 80008c8b..f5c39dd8 100644 --- a/cli/floatingip_create.go +++ b/internal/cmds/floatingip_create.go @@ -1,23 +1,24 @@ -package cli +package cmds import ( "errors" "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newFloatingIPCreateCommand(cli *CLI) *cobra.Command { +func newFloatingIPCreateCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "create FLAGS", Short: "Create a Floating IP", Args: cobra.NoArgs, TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: chainRunE(validateFloatingIPCreate, cli.ensureToken), - RunE: cli.wrap(runFloatingIPCreate), + PreRunE: chainRunE(validateFloatingIPCreate, cli.EnsureToken), + RunE: cli.Wrap(runFloatingIPCreate), } cmd.Flags().String("type", "", "Type (ipv4 or ipv6) (required)") cmd.RegisterFlagCompletionFunc("type", cmpl.SuggestCandidates("ipv4", "ipv6")) @@ -53,7 +54,7 @@ func validateFloatingIPCreate(cmd *cobra.Command, args []string) error { return nil } -func runFloatingIPCreate(cli *CLI, cmd *cobra.Command, args []string) error { +func runFloatingIPCreate(cli *state.State, cmd *cobra.Command, args []string) error { typ, _ := cmd.Flags().GetString("type") name, _ := cmd.Flags().GetString("name") description, _ := cmd.Flags().GetString("description") diff --git a/cli/floatingip_delete.go b/internal/cmds/floatingip_delete.go similarity index 72% rename from cli/floatingip_delete.go rename to internal/cmds/floatingip_delete.go index 5f0a3ea1..4c7e79d5 100644 --- a/cli/floatingip_delete.go +++ b/internal/cmds/floatingip_delete.go @@ -1,13 +1,14 @@ -package cli +package cmds import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/spf13/cobra" ) -func newFloatingIPDeleteCommand(cli *CLI) *cobra.Command { +func newFloatingIPDeleteCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "delete [FLAGS] FLOATINGIP", Short: "Delete a Floating IP", @@ -15,13 +16,13 @@ func newFloatingIPDeleteCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.FloatingIPNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runFloatingIPDelete), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runFloatingIPDelete), } return cmd } -func runFloatingIPDelete(cli *CLI, cmd *cobra.Command, args []string) error { +func runFloatingIPDelete(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] floatingIP, _, err := cli.Client().FloatingIP.Get(cli.Context, idOrName) if err != nil { diff --git a/cli/floatingip_describe.go b/internal/cmds/floatingip_describe.go similarity index 88% rename from cli/floatingip_describe.go rename to internal/cmds/floatingip_describe.go index ce0daa40..22d6d0d9 100644 --- a/cli/floatingip_describe.go +++ b/internal/cmds/floatingip_describe.go @@ -1,4 +1,4 @@ -package cli +package cmds import ( "encoding/json" @@ -6,11 +6,12 @@ import ( "github.com/dustin/go-humanize" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newFloatingIPDescribeCommand(cli *CLI) *cobra.Command { +func newFloatingIPDescribeCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "describe [FLAGS] FLOATINGIP", Short: "Describe a Floating IP", @@ -18,14 +19,14 @@ func newFloatingIPDescribeCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.FloatingIPNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runFloatingIPDescribe), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runFloatingIPDescribe), } addOutputFlag(cmd, outputOptionJSON(), outputOptionFormat()) return cmd } -func runFloatingIPDescribe(cli *CLI, cmd *cobra.Command, args []string) error { +func runFloatingIPDescribe(cli *state.State, cmd *cobra.Command, args []string) error { outputFlags := outputFlagsForCommand(cmd) idOrName := args[0] @@ -47,7 +48,7 @@ func runFloatingIPDescribe(cli *CLI, cmd *cobra.Command, args []string) error { } } -func floatingIPDescribeText(cli *CLI, floatingIP *hcloud.FloatingIP) error { +func floatingIPDescribeText(cli *state.State, floatingIP *hcloud.FloatingIP) error { fmt.Printf("ID:\t\t%d\n", floatingIP.ID) fmt.Printf("Type:\t\t%s\n", floatingIP.Type) fmt.Printf("Name:\t\t%s\n", floatingIP.Name) diff --git a/cli/floatingip_disable_protection.go b/internal/cmds/floatingip_disable_protection.go similarity index 80% rename from cli/floatingip_disable_protection.go rename to internal/cmds/floatingip_disable_protection.go index 8959bee9..1c14c5a1 100644 --- a/cli/floatingip_disable_protection.go +++ b/internal/cmds/floatingip_disable_protection.go @@ -1,15 +1,16 @@ -package cli +package cmds import ( "fmt" "strings" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newFloatingIPDisableProtectionCommand(cli *CLI) *cobra.Command { +func newFloatingIPDisableProtectionCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "disable-protection [FLAGS] FLOATINGIP PROTECTIONLEVEL [PROTECTIONLEVEL...]", Short: "Disable resource protection for a Floating IP", @@ -20,13 +21,13 @@ func newFloatingIPDisableProtectionCommand(cli *CLI) *cobra.Command { ), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runFloatingIPDisableProtection), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runFloatingIPDisableProtection), } return cmd } -func runFloatingIPDisableProtection(cli *CLI, cmd *cobra.Command, args []string) error { +func runFloatingIPDisableProtection(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] floatingIP, _, err := cli.Client().FloatingIP.Get(cli.Context, idOrName) if err != nil { diff --git a/cli/floatingip_enable_protection.go b/internal/cmds/floatingip_enable_protection.go similarity index 80% rename from cli/floatingip_enable_protection.go rename to internal/cmds/floatingip_enable_protection.go index 63625b21..e68e891c 100644 --- a/cli/floatingip_enable_protection.go +++ b/internal/cmds/floatingip_enable_protection.go @@ -1,15 +1,16 @@ -package cli +package cmds import ( "fmt" "strings" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newFloatingIPEnableProtectionCommand(cli *CLI) *cobra.Command { +func newFloatingIPEnableProtectionCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "enable-protection [FLAGS] FLOATINGIP PROTECTIONLEVEL [PROTECTIONLEVEL...]", Short: "Enable resource protection for a Floating IP", @@ -20,13 +21,13 @@ func newFloatingIPEnableProtectionCommand(cli *CLI) *cobra.Command { ), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runFloatingIPEnableProtection), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runFloatingIPEnableProtection), } return cmd } -func runFloatingIPEnableProtection(cli *CLI, cmd *cobra.Command, args []string) error { +func runFloatingIPEnableProtection(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] floatingIP, _, err := cli.Client().FloatingIP.Get(cli.Context, idOrName) if err != nil { diff --git a/cli/floatingip_list.go b/internal/cmds/floatingip_list.go similarity index 91% rename from cli/floatingip_list.go rename to internal/cmds/floatingip_list.go index 6fe3ad06..e9e51d19 100644 --- a/cli/floatingip_list.go +++ b/internal/cmds/floatingip_list.go @@ -1,9 +1,10 @@ -package cli +package cmds import ( "fmt" "strings" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud/schema" "github.com/hetznercloud/hcloud-go/hcloud" @@ -16,7 +17,7 @@ func init() { floatingIPListTableOutput = describeFloatingIPListTableOutput(nil) } -func newFloatingIPListCommand(cli *CLI) *cobra.Command { +func newFloatingIPListCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "list [FLAGS]", Short: "List Floating IPs", @@ -26,15 +27,15 @@ func newFloatingIPListCommand(cli *CLI) *cobra.Command { ), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runFloatingIPList), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runFloatingIPList), } addOutputFlag(cmd, outputOptionNoHeader(), outputOptionColumns(floatingIPListTableOutput.Columns()), outputOptionJSON()) cmd.Flags().StringP("selector", "l", "", "Selector to filter by labels") return cmd } -func runFloatingIPList(cli *CLI, cmd *cobra.Command, args []string) error { +func runFloatingIPList(cli *state.State, cmd *cobra.Command, args []string) error { outOpts := outputFlagsForCommand(cmd) labelSelector, _ := cmd.Flags().GetString("selector") @@ -98,7 +99,7 @@ func runFloatingIPList(cli *CLI, cmd *cobra.Command, args []string) error { return nil } -func describeFloatingIPListTableOutput(cli *CLI) *tableOutput { +func describeFloatingIPListTableOutput(cli *state.State) *tableOutput { return newTableOutput(). AddAllowedFields(hcloud.FloatingIP{}). AddFieldOutputFn("dns", fieldOutputFn(func(obj interface{}) string { diff --git a/cli/floatingip_remove_label.go b/internal/cmds/floatingip_remove_label.go similarity index 86% rename from cli/floatingip_remove_label.go rename to internal/cmds/floatingip_remove_label.go index b714d1c0..82831fe0 100644 --- a/cli/floatingip_remove_label.go +++ b/internal/cmds/floatingip_remove_label.go @@ -1,15 +1,16 @@ -package cli +package cmds import ( "errors" "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newFloatingIPRemoveLabelCommand(cli *CLI) *cobra.Command { +func newFloatingIPRemoveLabelCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "remove-label [FLAGS] FLOATINGIP LABELKEY", Short: "Remove a label from a Floating IP", @@ -24,8 +25,8 @@ func newFloatingIPRemoveLabelCommand(cli *CLI) *cobra.Command { })), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: chainRunE(validateFloatingIPRemoveLabel, cli.ensureToken), - RunE: cli.wrap(runFloatingIPRemoveLabel), + PreRunE: chainRunE(validateFloatingIPRemoveLabel, cli.EnsureToken), + RunE: cli.Wrap(runFloatingIPRemoveLabel), } cmd.Flags().BoolP("all", "a", false, "Remove all labels") @@ -45,7 +46,7 @@ func validateFloatingIPRemoveLabel(cmd *cobra.Command, args []string) error { return nil } -func runFloatingIPRemoveLabel(cli *CLI, cmd *cobra.Command, args []string) error { +func runFloatingIPRemoveLabel(cli *state.State, cmd *cobra.Command, args []string) error { all, _ := cmd.Flags().GetBool("all") idOrName := args[0] diff --git a/cli/floatingip_set_rdns.go b/internal/cmds/floatingip_set_rdns.go similarity index 81% rename from cli/floatingip_set_rdns.go rename to internal/cmds/floatingip_set_rdns.go index 482ddce0..e3689b30 100644 --- a/cli/floatingip_set_rdns.go +++ b/internal/cmds/floatingip_set_rdns.go @@ -1,14 +1,15 @@ -package cli +package cmds import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newFloatingIPSetRDNSCommand(cli *CLI) *cobra.Command { +func newFloatingIPSetRDNSCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "set-rdns [FLAGS] FLOATINGIP", Short: "Change reverse DNS of a Floating IP", @@ -16,8 +17,8 @@ func newFloatingIPSetRDNSCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.FloatingIPNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runFloatingIPSetRDNS), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runFloatingIPSetRDNS), } cmd.Flags().StringP("hostname", "r", "", "Hostname to set as a reverse DNS PTR entry (required)") @@ -27,7 +28,7 @@ func newFloatingIPSetRDNSCommand(cli *CLI) *cobra.Command { return cmd } -func runFloatingIPSetRDNS(cli *CLI, cmd *cobra.Command, args []string) error { +func runFloatingIPSetRDNS(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] floatingIP, _, err := cli.Client().FloatingIP.Get(cli.Context, idOrName) if err != nil { diff --git a/cli/floatingip_unassign.go b/internal/cmds/floatingip_unassign.go similarity index 74% rename from cli/floatingip_unassign.go rename to internal/cmds/floatingip_unassign.go index c40cb444..b583e59b 100644 --- a/cli/floatingip_unassign.go +++ b/internal/cmds/floatingip_unassign.go @@ -1,13 +1,14 @@ -package cli +package cmds import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/spf13/cobra" ) -func newFloatingIPUnassignCommand(cli *CLI) *cobra.Command { +func newFloatingIPUnassignCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "unassign [FLAGS] FLOATINGIP", Short: "Unassign a Floating IP", @@ -15,13 +16,13 @@ func newFloatingIPUnassignCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.FloatingIPNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runFloatingIPUnassign), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runFloatingIPUnassign), } return cmd } -func runFloatingIPUnassign(cli *CLI, cmd *cobra.Command, args []string) error { +func runFloatingIPUnassign(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] floatingIP, _, err := cli.Client().FloatingIP.Get(cli.Context, idOrName) if err != nil { diff --git a/cli/floatingip_update.go b/internal/cmds/floatingip_update.go similarity index 79% rename from cli/floatingip_update.go rename to internal/cmds/floatingip_update.go index 92f513c6..c62d782c 100644 --- a/cli/floatingip_update.go +++ b/internal/cmds/floatingip_update.go @@ -1,14 +1,15 @@ -package cli +package cmds import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newFloatingIPUpdateCommand(cli *CLI) *cobra.Command { +func newFloatingIPUpdateCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "update [FLAGS] FLOATINGIP", Short: "Update a Floating IP", @@ -16,8 +17,8 @@ func newFloatingIPUpdateCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.FloatingIPNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runFloatingIPUpdate), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runFloatingIPUpdate), } cmd.Flags().String("description", "", "Floating IP description") @@ -26,7 +27,7 @@ func newFloatingIPUpdateCommand(cli *CLI) *cobra.Command { return cmd } -func runFloatingIPUpdate(cli *CLI, cmd *cobra.Command, args []string) error { +func runFloatingIPUpdate(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] floatingIP, _, err := cli.Client().FloatingIP.Get(cli.Context, idOrName) if err != nil { diff --git a/cli/image.go b/internal/cmds/image.go similarity index 76% rename from cli/image.go rename to internal/cmds/image.go index 5ba81cea..014f0d11 100644 --- a/cli/image.go +++ b/internal/cmds/image.go @@ -1,8 +1,11 @@ -package cli +package cmds -import "github.com/spf13/cobra" +import ( + "github.com/hetznercloud/cli/internal/state" + "github.com/spf13/cobra" +) -func newImageCommand(cli *CLI) *cobra.Command { +func NewImageCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "image", Short: "Manage images", diff --git a/cli/image_add_label.go b/internal/cmds/image_add_label.go similarity index 81% rename from cli/image_add_label.go rename to internal/cmds/image_add_label.go index 017b54d0..34cbdb71 100644 --- a/cli/image_add_label.go +++ b/internal/cmds/image_add_label.go @@ -1,14 +1,15 @@ -package cli +package cmds import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newImageAddLabelCommand(cli *CLI) *cobra.Command { +func newImageAddLabelCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "add-label [FLAGS] IMAGE LABEL", Short: "Add a label to an image", @@ -16,8 +17,8 @@ func newImageAddLabelCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.ImageNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: chainRunE(validateImageAddLabel, cli.ensureToken), - RunE: cli.wrap(runImageAddLabel), + PreRunE: chainRunE(validateImageAddLabel, cli.EnsureToken), + RunE: cli.Wrap(runImageAddLabel), } cmd.Flags().BoolP("overwrite", "o", false, "Overwrite label if it exists already") @@ -33,7 +34,7 @@ func validateImageAddLabel(cmd *cobra.Command, args []string) error { return nil } -func runImageAddLabel(cli *CLI, cmd *cobra.Command, args []string) error { +func runImageAddLabel(cli *state.State, cmd *cobra.Command, args []string) error { overwrite, _ := cmd.Flags().GetBool("overwrite") idOrName := args[0] image, _, err := cli.Client().Image.Get(cli.Context, idOrName) diff --git a/cli/image_delete.go b/internal/cmds/image_delete.go similarity index 72% rename from cli/image_delete.go rename to internal/cmds/image_delete.go index 25b47101..72a81e81 100644 --- a/cli/image_delete.go +++ b/internal/cmds/image_delete.go @@ -1,13 +1,14 @@ -package cli +package cmds import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/spf13/cobra" ) -func newImageDeleteCommand(cli *CLI) *cobra.Command { +func newImageDeleteCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "delete [FLAGS] IMAGE", Short: "Delete an image", @@ -15,13 +16,13 @@ func newImageDeleteCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.ImageNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runImageDelete), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runImageDelete), } return cmd } -func runImageDelete(cli *CLI, cmd *cobra.Command, args []string) error { +func runImageDelete(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] image, _, err := cli.Client().Image.Get(cli.Context, idOrName) if err != nil { diff --git a/cli/image_describe.go b/internal/cmds/image_describe.go similarity index 87% rename from cli/image_describe.go rename to internal/cmds/image_describe.go index 0c2e2af5..e67f2cb1 100644 --- a/cli/image_describe.go +++ b/internal/cmds/image_describe.go @@ -1,4 +1,4 @@ -package cli +package cmds import ( "encoding/json" @@ -6,11 +6,12 @@ import ( humanize "github.com/dustin/go-humanize" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newImageDescribeCommand(cli *CLI) *cobra.Command { +func newImageDescribeCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "describe [FLAGS] IMAGE", Short: "Describe an image", @@ -18,14 +19,14 @@ func newImageDescribeCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.ImageNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runImageDescribe), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runImageDescribe), } addOutputFlag(cmd, outputOptionJSON(), outputOptionFormat()) return cmd } -func runImageDescribe(cli *CLI, cmd *cobra.Command, args []string) error { +func runImageDescribe(cli *state.State, cmd *cobra.Command, args []string) error { outputFlags := outputFlagsForCommand(cmd) idOrName := args[0] @@ -47,7 +48,7 @@ func runImageDescribe(cli *CLI, cmd *cobra.Command, args []string) error { } } -func imageDescribeText(cli *CLI, image *hcloud.Image) error { +func imageDescribeText(cli *state.State, image *hcloud.Image) error { fmt.Printf("ID:\t\t%d\n", image.ID) fmt.Printf("Type:\t\t%s\n", image.Type) fmt.Printf("Status:\t\t%s\n", image.Status) diff --git a/cli/image_disable_protection.go b/internal/cmds/image_disable_protection.go similarity index 80% rename from cli/image_disable_protection.go rename to internal/cmds/image_disable_protection.go index 229d5abc..cd7750b1 100644 --- a/cli/image_disable_protection.go +++ b/internal/cmds/image_disable_protection.go @@ -1,4 +1,4 @@ -package cli +package cmds import ( "errors" @@ -7,11 +7,12 @@ import ( "strings" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newImageDisableProtectionCommand(cli *CLI) *cobra.Command { +func newImageDisableProtectionCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "disable-protection [FLAGS] IMAGE PROTECTIONLEVEL [PROTECTIONLEVEL...]", Short: "Disable resource protection for an image", @@ -22,13 +23,13 @@ func newImageDisableProtectionCommand(cli *CLI) *cobra.Command { ), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runImageDisableProtection), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runImageDisableProtection), } return cmd } -func runImageDisableProtection(cli *CLI, cmd *cobra.Command, args []string) error { +func runImageDisableProtection(cli *state.State, cmd *cobra.Command, args []string) error { imageID, err := strconv.Atoi(args[0]) if err != nil { return errors.New("invalid image ID") diff --git a/cli/image_enable_protection.go b/internal/cmds/image_enable_protection.go similarity index 80% rename from cli/image_enable_protection.go rename to internal/cmds/image_enable_protection.go index 9f163e44..324b102b 100644 --- a/cli/image_enable_protection.go +++ b/internal/cmds/image_enable_protection.go @@ -1,4 +1,4 @@ -package cli +package cmds import ( "errors" @@ -7,11 +7,12 @@ import ( "strings" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newImageEnableProtectionCommand(cli *CLI) *cobra.Command { +func newImageEnableProtectionCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "enable-protection [FLAGS] IMAGE PROTECTIONLEVEL [PROTECTIONLEVEL...]", Short: "Enable resource protection for an image", @@ -22,13 +23,13 @@ func newImageEnableProtectionCommand(cli *CLI) *cobra.Command { ), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runImageEnableProtection), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runImageEnableProtection), } return cmd } -func runImageEnableProtection(cli *CLI, cmd *cobra.Command, args []string) error { +func runImageEnableProtection(cli *state.State, cmd *cobra.Command, args []string) error { imageID, err := strconv.Atoi(args[0]) if err != nil { return errors.New("invalid image ID") diff --git a/cli/image_list.go b/internal/cmds/image_list.go similarity index 92% rename from cli/image_list.go rename to internal/cmds/image_list.go index 02fdf011..e212c8f5 100644 --- a/cli/image_list.go +++ b/internal/cmds/image_list.go @@ -1,9 +1,10 @@ -package cli +package cmds import ( "fmt" "strings" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud/schema" humanize "github.com/dustin/go-humanize" @@ -18,7 +19,7 @@ func init() { imageListTableOutput = describeImageListTableOutput(nil) } -func newImageListCommand(cli *CLI) *cobra.Command { +func newImageListCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "list [FLAGS]", Short: "List images", @@ -28,8 +29,8 @@ func newImageListCommand(cli *CLI) *cobra.Command { ), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runImageList), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runImageList), } addOutputFlag(cmd, outputOptionNoHeader(), outputOptionColumns(imageListTableOutput.Columns()), outputOptionJSON()) cmd.Flags().StringVarP(&typeFilter, "type", "t", "", "Only show images of given type") @@ -37,7 +38,7 @@ func newImageListCommand(cli *CLI) *cobra.Command { return cmd } -func runImageList(cli *CLI, cmd *cobra.Command, args []string) error { +func runImageList(cli *state.State, cmd *cobra.Command, args []string) error { outOpts := outputFlagsForCommand(cmd) labelSelector, _ := cmd.Flags().GetString("selector") @@ -91,7 +92,7 @@ func runImageList(cli *CLI, cmd *cobra.Command, args []string) error { return nil } -func describeImageListTableOutput(cli *CLI) *tableOutput { +func describeImageListTableOutput(cli *state.State) *tableOutput { return newTableOutput(). AddAllowedFields(hcloud.Image{}). AddFieldAlias("imagesize", "image size"). diff --git a/cli/image_remove_label.go b/internal/cmds/image_remove_label.go similarity index 86% rename from cli/image_remove_label.go rename to internal/cmds/image_remove_label.go index 84c50336..0079b673 100644 --- a/cli/image_remove_label.go +++ b/internal/cmds/image_remove_label.go @@ -1,15 +1,16 @@ -package cli +package cmds import ( "errors" "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newImageRemoveLabelCommand(cli *CLI) *cobra.Command { +func newImageRemoveLabelCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "remove-label [FLAGS] IMAGE LABELKEY", Short: "Remove a label from an image", @@ -25,8 +26,8 @@ func newImageRemoveLabelCommand(cli *CLI) *cobra.Command { ), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: chainRunE(validateImageRemoveLabel, cli.ensureToken), - RunE: cli.wrap(runImageRemoveLabel), + PreRunE: chainRunE(validateImageRemoveLabel, cli.EnsureToken), + RunE: cli.Wrap(runImageRemoveLabel), } cmd.Flags().BoolP("all", "a", false, "Remove all labels") @@ -46,7 +47,7 @@ func validateImageRemoveLabel(cmd *cobra.Command, args []string) error { return nil } -func runImageRemoveLabel(cli *CLI, cmd *cobra.Command, args []string) error { +func runImageRemoveLabel(cli *state.State, cmd *cobra.Command, args []string) error { all, _ := cmd.Flags().GetBool("all") idOrName := args[0] image, _, err := cli.Client().Image.Get(cli.Context, idOrName) diff --git a/cli/image_update.go b/internal/cmds/image_update.go similarity index 80% rename from cli/image_update.go rename to internal/cmds/image_update.go index 1005d5e5..b1df8488 100644 --- a/cli/image_update.go +++ b/internal/cmds/image_update.go @@ -1,14 +1,15 @@ -package cli +package cmds import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newImageUpdateCommand(cli *CLI) *cobra.Command { +func newImageUpdateCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "update [FLAGS] IMAGE", Short: "Update an image", @@ -16,8 +17,8 @@ func newImageUpdateCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.ImageNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runImageUpdate), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runImageUpdate), } cmd.Flags().String("description", "", "Image description") @@ -28,7 +29,7 @@ func newImageUpdateCommand(cli *CLI) *cobra.Command { return cmd } -func runImageUpdate(cli *CLI, cmd *cobra.Command, args []string) error { +func runImageUpdate(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] image, _, err := cli.Client().Image.Get(cli.Context, idOrName) if err != nil { diff --git a/cli/iso.go b/internal/cmds/iso.go similarity index 66% rename from cli/iso.go rename to internal/cmds/iso.go index df26c2c4..3a4d700d 100644 --- a/cli/iso.go +++ b/internal/cmds/iso.go @@ -1,8 +1,11 @@ -package cli +package cmds -import "github.com/spf13/cobra" +import ( + "github.com/hetznercloud/cli/internal/state" + "github.com/spf13/cobra" +) -func newISOCommand(cli *CLI) *cobra.Command { +func NewISOCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "iso", Short: "Manage ISOs", diff --git a/cli/iso_describe.go b/internal/cmds/iso_describe.go similarity index 84% rename from cli/iso_describe.go rename to internal/cmds/iso_describe.go index e9bd2784..8ee48ab0 100644 --- a/cli/iso_describe.go +++ b/internal/cmds/iso_describe.go @@ -1,15 +1,16 @@ -package cli +package cmds import ( "encoding/json" "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newISODescribeCommand(cli *CLI) *cobra.Command { +func newISODescribeCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "describe [FLAGS] ISO", Short: "Describe an ISO", @@ -17,14 +18,14 @@ func newISODescribeCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.ISONames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runISODescribe), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runISODescribe), } addOutputFlag(cmd, outputOptionJSON(), outputOptionFormat()) return cmd } -func runISODescribe(cli *CLI, cmd *cobra.Command, args []string) error { +func runISODescribe(cli *state.State, cmd *cobra.Command, args []string) error { outputFlags := outputFlagsForCommand(cmd) idOrName := args[0] diff --git a/cli/iso_list.go b/internal/cmds/iso_list.go similarity index 81% rename from cli/iso_list.go rename to internal/cmds/iso_list.go index 3697c684..627c736e 100644 --- a/cli/iso_list.go +++ b/internal/cmds/iso_list.go @@ -1,6 +1,7 @@ -package cli +package cmds import ( + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/hetznercloud/hcloud-go/hcloud/schema" "github.com/spf13/cobra" @@ -13,7 +14,7 @@ func init() { AddAllowedFields(hcloud.ISO{}) } -func newISOListCommand(cli *CLI) *cobra.Command { +func newISOListCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "list [FLAGS]", Short: "List ISOs", @@ -23,14 +24,14 @@ func newISOListCommand(cli *CLI) *cobra.Command { ), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runISOList), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runISOList), } addOutputFlag(cmd, outputOptionNoHeader(), outputOptionColumns(isoListTableOutput.Columns()), outputOptionJSON()) return cmd } -func runISOList(cli *CLI, cmd *cobra.Command, args []string) error { +func runISOList(cli *state.State, cmd *cobra.Command, args []string) error { outOpts := outputFlagsForCommand(cmd) isos, err := cli.Client().ISO.All(cli.Context) diff --git a/cli/load_balancer.go b/internal/cmds/load_balancer.go similarity index 87% rename from cli/load_balancer.go rename to internal/cmds/load_balancer.go index 539a17de..0fe04087 100644 --- a/cli/load_balancer.go +++ b/internal/cmds/load_balancer.go @@ -1,8 +1,11 @@ -package cli +package cmds -import "github.com/spf13/cobra" +import ( + "github.com/hetznercloud/cli/internal/state" + "github.com/spf13/cobra" +) -func newLoadBalancerCommand(cli *CLI) *cobra.Command { +func NewLoadBalancerCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "load-balancer", Short: "Manage Load Balancers", diff --git a/cli/load_balancer_add_label.go b/internal/cmds/load_balancer_add_label.go similarity index 84% rename from cli/load_balancer_add_label.go rename to internal/cmds/load_balancer_add_label.go index 209b13c8..db8478b5 100644 --- a/cli/load_balancer_add_label.go +++ b/internal/cmds/load_balancer_add_label.go @@ -1,14 +1,15 @@ -package cli +package cmds import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newLoadBalancerAddLabelCommand(cli *CLI) *cobra.Command { +func newLoadBalancerAddLabelCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "add-label [FLAGS] LOADBALANCER LABEL", Short: "Add a label to a Load Balancer", @@ -16,8 +17,8 @@ func newLoadBalancerAddLabelCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.LoadBalancerNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: chainRunE(validateLoadBalancerAddLabel, cli.ensureToken), - RunE: cli.wrap(runLoadBalancerAddLabel), + PreRunE: chainRunE(validateLoadBalancerAddLabel, cli.EnsureToken), + RunE: cli.Wrap(runLoadBalancerAddLabel), } cmd.Flags().BoolP("overwrite", "o", false, "Overwrite label if it exists already") @@ -33,7 +34,7 @@ func validateLoadBalancerAddLabel(cmd *cobra.Command, args []string) error { return nil } -func runLoadBalancerAddLabel(cli *CLI, cmd *cobra.Command, args []string) error { +func runLoadBalancerAddLabel(cli *state.State, cmd *cobra.Command, args []string) error { overwrite, _ := cmd.Flags().GetBool("overwrite") idOrName := args[0] loadBalancer, _, err := cli.Client().LoadBalancer.Get(cli.Context, idOrName) diff --git a/cli/load_balancer_add_service.go b/internal/cmds/load_balancer_add_service.go similarity index 93% rename from cli/load_balancer_add_service.go rename to internal/cmds/load_balancer_add_service.go index f6ceeed9..d88459ca 100644 --- a/cli/load_balancer_add_service.go +++ b/internal/cmds/load_balancer_add_service.go @@ -1,14 +1,15 @@ -package cli +package cmds import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newLoadBalancerAddServiceCommand(cli *CLI) *cobra.Command { +func newLoadBalancerAddServiceCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "add-service LOADBALANCER FLAGS", Short: "Add a service from a Load Balancer", @@ -16,8 +17,8 @@ func newLoadBalancerAddServiceCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.LoadBalancerNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: chainRunE(validateLoadBalancerAddService, cli.ensureToken), - RunE: cli.wrap(runLoadBalancerAddService), + PreRunE: chainRunE(validateLoadBalancerAddService, cli.EnsureToken), + RunE: cli.Wrap(runLoadBalancerAddService), } cmd.Flags().String("protocol", "", "Protocol of the service (required)") cmd.MarkFlagRequired("protocol") @@ -74,7 +75,7 @@ func validateLoadBalancerAddService(cmd *cobra.Command, args []string) error { return nil } -func runLoadBalancerAddService(cli *CLI, cmd *cobra.Command, args []string) error { +func runLoadBalancerAddService(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] protocol, _ := cmd.Flags().GetString("protocol") listenPort, _ := cmd.Flags().GetInt("listen-port") diff --git a/cli/load_balancer_add_target.go b/internal/cmds/load_balancer_add_target.go similarity index 89% rename from cli/load_balancer_add_target.go rename to internal/cmds/load_balancer_add_target.go index 142d418a..4aed3cf1 100644 --- a/cli/load_balancer_add_target.go +++ b/internal/cmds/load_balancer_add_target.go @@ -1,15 +1,16 @@ -package cli +package cmds import ( "fmt" "net" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newLoadBalancerAddTargetCommand(cli *CLI) *cobra.Command { +func newLoadBalancerAddTargetCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "add-target LOADBALANCER FLAGS", Short: "Add a target to a Load Balancer", @@ -17,8 +18,8 @@ func newLoadBalancerAddTargetCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.LoadBalancerNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runLoadBalancerAddTarget), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runLoadBalancerAddTarget), } cmd.Flags().String("server", "", "Name or ID of the server") @@ -31,7 +32,7 @@ func newLoadBalancerAddTargetCommand(cli *CLI) *cobra.Command { return cmd } -func runLoadBalancerAddTarget(cli *CLI, cmd *cobra.Command, args []string) error { +func runLoadBalancerAddTarget(cli *state.State, cmd *cobra.Command, args []string) error { var ( action *hcloud.Action loadBalancer *hcloud.LoadBalancer diff --git a/cli/load_balancer_attach_to_network.go b/internal/cmds/load_balancer_attach_to_network.go similarity index 83% rename from cli/load_balancer_attach_to_network.go rename to internal/cmds/load_balancer_attach_to_network.go index 36c05d60..47ff2746 100644 --- a/cli/load_balancer_attach_to_network.go +++ b/internal/cmds/load_balancer_attach_to_network.go @@ -1,14 +1,15 @@ -package cli +package cmds import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newLoadBalancerAttachToNetworkCommand(cli *CLI) *cobra.Command { +func newLoadBalancerAttachToNetworkCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "attach-to-network [FLAGS] LOADBALANCER", Short: "Attach a Load Balancer to a Network", @@ -16,8 +17,8 @@ func newLoadBalancerAttachToNetworkCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.LoadBalancerNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runLoadBalancerAttachToNetwork), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runLoadBalancerAttachToNetwork), } cmd.Flags().StringP("network", "n", "", "Network (ID or name) (required)") @@ -29,7 +30,7 @@ func newLoadBalancerAttachToNetworkCommand(cli *CLI) *cobra.Command { return cmd } -func runLoadBalancerAttachToNetwork(cli *CLI, cmd *cobra.Command, args []string) error { +func runLoadBalancerAttachToNetwork(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] loadBalancer, _, err := cli.Client().LoadBalancer.Get(cli.Context, idOrName) if err != nil { diff --git a/cli/load_balancer_change_algorithm.go b/internal/cmds/load_balancer_change_algorithm.go similarity index 81% rename from cli/load_balancer_change_algorithm.go rename to internal/cmds/load_balancer_change_algorithm.go index 187baa6f..b8a7f14c 100644 --- a/cli/load_balancer_change_algorithm.go +++ b/internal/cmds/load_balancer_change_algorithm.go @@ -1,14 +1,15 @@ -package cli +package cmds import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newLoadBalancerChangeAlgorithmCommand(cli *CLI) *cobra.Command { +func newLoadBalancerChangeAlgorithmCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "change-algorithm LOADBALANCER FLAGS", Short: "Changes the algorithm of a Load Balancer", @@ -16,8 +17,8 @@ func newLoadBalancerChangeAlgorithmCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.LoadBalancerNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runLoadBalancerChangeAlgorithm), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runLoadBalancerChangeAlgorithm), } cmd.Flags().String("algorithm-type", "", "The new algorithm of the Load Balancer") @@ -30,7 +31,7 @@ func newLoadBalancerChangeAlgorithmCommand(cli *CLI) *cobra.Command { return cmd } -func runLoadBalancerChangeAlgorithm(cli *CLI, cmd *cobra.Command, args []string) error { +func runLoadBalancerChangeAlgorithm(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] algorithm, _ := cmd.Flags().GetString("algorithm-type") loadBalancer, _, err := cli.Client().LoadBalancer.Get(cli.Context, idOrName) diff --git a/cli/load_balancer_change_type.go b/internal/cmds/load_balancer_change_type.go similarity index 81% rename from cli/load_balancer_change_type.go rename to internal/cmds/load_balancer_change_type.go index b03b7f8b..cd5fffa7 100644 --- a/cli/load_balancer_change_type.go +++ b/internal/cmds/load_balancer_change_type.go @@ -1,14 +1,15 @@ -package cli +package cmds import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newLoadBalancerChangeTypeCommand(cli *CLI) *cobra.Command { +func newLoadBalancerChangeTypeCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "change-type [FLAGS] LOADBALANCER LOADBALANCERTYPE", Short: "Change type of a Load Balancer", @@ -19,14 +20,14 @@ func newLoadBalancerChangeTypeCommand(cli *CLI) *cobra.Command { ), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runLoadBalancerChangeType), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runLoadBalancerChangeType), } return cmd } -func runLoadBalancerChangeType(cli *CLI, cmd *cobra.Command, args []string) error { +func runLoadBalancerChangeType(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] loadBalancer, _, err := cli.Client().LoadBalancer.Get(cli.Context, idOrName) if err != nil { diff --git a/cli/load_balancer_create.go b/internal/cmds/load_balancer_create.go similarity index 89% rename from cli/load_balancer_create.go rename to internal/cmds/load_balancer_create.go index e41bcedb..94231855 100644 --- a/cli/load_balancer_create.go +++ b/internal/cmds/load_balancer_create.go @@ -1,22 +1,23 @@ -package cli +package cmds import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newLoadBalancerCreateCommand(cli *CLI) *cobra.Command { +func newLoadBalancerCreateCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "create [FLAGS]", Short: "Create a Load Balancer", Args: cobra.NoArgs, TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runLoadBalancerCreate), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runLoadBalancerCreate), } cmd.Flags().String("name", "", "Load Balancer name (required)") @@ -42,7 +43,7 @@ func newLoadBalancerCreateCommand(cli *CLI) *cobra.Command { return cmd } -func runLoadBalancerCreate(cli *CLI, cmd *cobra.Command, args []string) error { +func runLoadBalancerCreate(cli *state.State, cmd *cobra.Command, args []string) error { name, _ := cmd.Flags().GetString("name") serverType, _ := cmd.Flags().GetString("type") algorithmType, _ := cmd.Flags().GetString("algorithm-type") diff --git a/cli/load_balancer_delete.go b/internal/cmds/load_balancer_delete.go similarity index 72% rename from cli/load_balancer_delete.go rename to internal/cmds/load_balancer_delete.go index ccba6461..ab8f7a27 100644 --- a/cli/load_balancer_delete.go +++ b/internal/cmds/load_balancer_delete.go @@ -1,13 +1,14 @@ -package cli +package cmds import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/spf13/cobra" ) -func newLoadBalancerDeleteCommand(cli *CLI) *cobra.Command { +func newLoadBalancerDeleteCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "delete [FLAGS] LOADBALANCER", Short: "Delete a Load Balancer", @@ -15,13 +16,13 @@ func newLoadBalancerDeleteCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.LoadBalancerNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runLoadBalancerDelete), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runLoadBalancerDelete), } return cmd } -func runLoadBalancerDelete(cli *CLI, cmd *cobra.Command, args []string) error { +func runLoadBalancerDelete(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] loadBalancer, _, err := cli.Client().LoadBalancer.Get(cli.Context, idOrName) if err != nil { diff --git a/cli/load_balancer_delete_service.go b/internal/cmds/load_balancer_delete_service.go similarity index 76% rename from cli/load_balancer_delete_service.go rename to internal/cmds/load_balancer_delete_service.go index 1a5601a8..10e34d1d 100644 --- a/cli/load_balancer_delete_service.go +++ b/internal/cmds/load_balancer_delete_service.go @@ -1,13 +1,14 @@ -package cli +package cmds import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/spf13/cobra" ) -func newLoadBalancerDeleteServiceCommand(cli *CLI) *cobra.Command { +func newLoadBalancerDeleteServiceCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "delete-service [FLAGS] LOADBALANCER", Short: "Deletes a service from a Load Balancer", @@ -15,8 +16,8 @@ func newLoadBalancerDeleteServiceCommand(cli *CLI) *cobra.Command { Args: cobra.RangeArgs(1, 2), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: chainRunE(cli.ensureToken), - RunE: cli.wrap(runLoadBalancerDeleteService), + PreRunE: chainRunE(cli.EnsureToken), + RunE: cli.Wrap(runLoadBalancerDeleteService), } cmd.Flags().Int("listen-port", 0, "The listen port of the service you want to delete (required)") @@ -24,7 +25,7 @@ func newLoadBalancerDeleteServiceCommand(cli *CLI) *cobra.Command { return cmd } -func runLoadBalancerDeleteService(cli *CLI, cmd *cobra.Command, args []string) error { +func runLoadBalancerDeleteService(cli *state.State, cmd *cobra.Command, args []string) error { listenPort, _ := cmd.Flags().GetInt("listen-port") idOrName := args[0] loadBalancer, _, err := cli.Client().LoadBalancer.Get(cli.Context, idOrName) diff --git a/cli/load_balancer_describe.go b/internal/cmds/load_balancer_describe.go similarity index 93% rename from cli/load_balancer_describe.go rename to internal/cmds/load_balancer_describe.go index 3436591e..134af4f5 100644 --- a/cli/load_balancer_describe.go +++ b/internal/cmds/load_balancer_describe.go @@ -1,4 +1,4 @@ -package cli +package cmds import ( "encoding/json" @@ -6,11 +6,12 @@ import ( humanize "github.com/dustin/go-humanize" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newLoadBalancerDescribeCommand(cli *CLI) *cobra.Command { +func newLoadBalancerDescribeCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "describe [FLAGS] LOADBALANCER", Short: "Describe a Load Balancer", @@ -18,15 +19,15 @@ func newLoadBalancerDescribeCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.LoadBalancerNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runLoadBalancerDescribe), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runLoadBalancerDescribe), } addOutputFlag(cmd, outputOptionJSON(), outputOptionFormat()) cmd.Flags().Bool("expand-targets", false, "Expand all label_selector targets") return cmd } -func runLoadBalancerDescribe(cli *CLI, cmd *cobra.Command, args []string) error { +func runLoadBalancerDescribe(cli *state.State, cmd *cobra.Command, args []string) error { outputFlags := outputFlagsForCommand(cmd) withLabelSelectorTargets, _ := cmd.Flags().GetBool("expand-targets") idOrName := args[0] @@ -48,7 +49,7 @@ func runLoadBalancerDescribe(cli *CLI, cmd *cobra.Command, args []string) error } } -func loadBalancerDescribeText(cli *CLI, loadBalancer *hcloud.LoadBalancer, withLabelSelectorTargets bool) error { +func loadBalancerDescribeText(cli *state.State, loadBalancer *hcloud.LoadBalancer, withLabelSelectorTargets bool) error { fmt.Printf("ID:\t\t\t\t%d\n", loadBalancer.ID) fmt.Printf("Name:\t\t\t\t%s\n", loadBalancer.Name) fmt.Printf("Created:\t\t\t%s (%s)\n", datetime(loadBalancer.Created), humanize.Time(loadBalancer.Created)) @@ -60,7 +61,7 @@ func loadBalancerDescribeText(cli *CLI, loadBalancer *hcloud.LoadBalancer, withL fmt.Printf("Private Net:\n") if len(loadBalancer.PrivateNet) > 0 { for _, n := range loadBalancer.PrivateNet { - network, _, err := cli.client.Network.GetByID(cli.Context, n.Network.ID) + network, _, err := cli.Client().Network.GetByID(cli.Context, n.Network.ID) if err != nil { return fmt.Errorf("error fetching network: %v", err) } diff --git a/cli/load_balancer_detach_from_network.go b/internal/cmds/load_balancer_detach_from_network.go similarity index 82% rename from cli/load_balancer_detach_from_network.go rename to internal/cmds/load_balancer_detach_from_network.go index b1e2534f..5163b534 100644 --- a/cli/load_balancer_detach_from_network.go +++ b/internal/cmds/load_balancer_detach_from_network.go @@ -1,15 +1,16 @@ -package cli +package cmds import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newLoadBalancerDetachFromNetworkCommand(cli *CLI) *cobra.Command { +func newLoadBalancerDetachFromNetworkCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "detach-from-network [FLAGS] LOADBALANCER", Short: "Detach a Load Balancer from a Network", @@ -17,8 +18,8 @@ func newLoadBalancerDetachFromNetworkCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.LoadBalancerNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runLoadBalancerDetachFromNetwork), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runLoadBalancerDetachFromNetwork), } cmd.Flags().StringP("network", "n", "", "Network (ID or name) (required)") cmd.RegisterFlagCompletionFunc("network", cmpl.SuggestCandidatesF(cli.NetworkNames)) @@ -26,7 +27,7 @@ func newLoadBalancerDetachFromNetworkCommand(cli *CLI) *cobra.Command { return cmd } -func runLoadBalancerDetachFromNetwork(cli *CLI, cmd *cobra.Command, args []string) error { +func runLoadBalancerDetachFromNetwork(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] loadBalancer, _, err := cli.Client().LoadBalancer.Get(cli.Context, idOrName) if err != nil { diff --git a/cli/load_balancer_disable_protection.go b/internal/cmds/load_balancer_disable_protection.go similarity index 80% rename from cli/load_balancer_disable_protection.go rename to internal/cmds/load_balancer_disable_protection.go index d811ba7e..b78e0aee 100644 --- a/cli/load_balancer_disable_protection.go +++ b/internal/cmds/load_balancer_disable_protection.go @@ -1,15 +1,16 @@ -package cli +package cmds import ( "fmt" "strings" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newLoadBalancerDisableProtectionCommand(cli *CLI) *cobra.Command { +func newLoadBalancerDisableProtectionCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "disable-protection [FLAGS] LOADBALANCER PROTECTIONLEVEL [PROTECTIONLEVEL...]", Short: "Disable resource protection for a Load Balancer", @@ -20,13 +21,13 @@ func newLoadBalancerDisableProtectionCommand(cli *CLI) *cobra.Command { ), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runLoadBalancerDisableProtection), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runLoadBalancerDisableProtection), } return cmd } -func runLoadBalancerDisableProtection(cli *CLI, cmd *cobra.Command, args []string) error { +func runLoadBalancerDisableProtection(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] loadBalancer, _, err := cli.Client().LoadBalancer.Get(cli.Context, idOrName) if err != nil { diff --git a/cli/load_balancer_disable_public_interface.go b/internal/cmds/load_balancer_disable_public_interface.go similarity index 74% rename from cli/load_balancer_disable_public_interface.go rename to internal/cmds/load_balancer_disable_public_interface.go index fd6a7f1b..15421c87 100644 --- a/cli/load_balancer_disable_public_interface.go +++ b/internal/cmds/load_balancer_disable_public_interface.go @@ -1,13 +1,14 @@ -package cli +package cmds import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/spf13/cobra" ) -func newLoadBalancerDisablePublicInterface(cli *CLI) *cobra.Command { +func newLoadBalancerDisablePublicInterface(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "disable-public-interface [FLAGS] LOADBALANCER", Short: "Disable the public interface of a Load Balancer", @@ -15,14 +16,14 @@ func newLoadBalancerDisablePublicInterface(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.LoadBalancerNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runLoadBalancerDisablePublicInterface), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runLoadBalancerDisablePublicInterface), } return cmd } -func runLoadBalancerDisablePublicInterface(cli *CLI, cmd *cobra.Command, args []string) error { +func runLoadBalancerDisablePublicInterface(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] loadBalancer, _, err := cli.Client().LoadBalancer.Get(cli.Context, idOrName) if err != nil { diff --git a/cli/load_balancer_enable_protection.go b/internal/cmds/load_balancer_enable_protection.go similarity index 80% rename from cli/load_balancer_enable_protection.go rename to internal/cmds/load_balancer_enable_protection.go index 4e763d6b..0afa5752 100644 --- a/cli/load_balancer_enable_protection.go +++ b/internal/cmds/load_balancer_enable_protection.go @@ -1,15 +1,16 @@ -package cli +package cmds import ( "fmt" "strings" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newLoadBalancerEnableProtectionCommand(cli *CLI) *cobra.Command { +func newLoadBalancerEnableProtectionCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "enable-protection [FLAGS] LOADBALANCER PROTECTIONLEVEL [PROTECTIONLEVEL...]", Short: "Enable resource protection for a Load Balancer", @@ -20,13 +21,13 @@ func newLoadBalancerEnableProtectionCommand(cli *CLI) *cobra.Command { ), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runLoadBalancerEnableProtection), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runLoadBalancerEnableProtection), } return cmd } -func runLoadBalancerEnableProtection(cli *CLI, cmd *cobra.Command, args []string) error { +func runLoadBalancerEnableProtection(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] LoadBalancer, _, err := cli.Client().LoadBalancer.Get(cli.Context, idOrName) if err != nil { diff --git a/cli/load_balancer_enable_public_interface.go b/internal/cmds/load_balancer_enable_public_interface.go similarity index 74% rename from cli/load_balancer_enable_public_interface.go rename to internal/cmds/load_balancer_enable_public_interface.go index f8809a3a..77ebf241 100644 --- a/cli/load_balancer_enable_public_interface.go +++ b/internal/cmds/load_balancer_enable_public_interface.go @@ -1,13 +1,14 @@ -package cli +package cmds import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/spf13/cobra" ) -func newLoadBalancerEnablePublicInterface(cli *CLI) *cobra.Command { +func newLoadBalancerEnablePublicInterface(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "enable-public-interface [FLAGS] LOADBALANCER", Short: "Enable the public interface of a Load Balancer", @@ -15,14 +16,14 @@ func newLoadBalancerEnablePublicInterface(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.LoadBalancerNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runLoadBalancerEnablePublicInterface), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runLoadBalancerEnablePublicInterface), } return cmd } -func runLoadBalancerEnablePublicInterface(cli *CLI, cmd *cobra.Command, args []string) error { +func runLoadBalancerEnablePublicInterface(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] loadBalancer, _, err := cli.Client().LoadBalancer.Get(cli.Context, idOrName) if err != nil { diff --git a/cli/load_balancer_list.go b/internal/cmds/load_balancer_list.go similarity index 94% rename from cli/load_balancer_list.go rename to internal/cmds/load_balancer_list.go index f0af2753..9909edb9 100644 --- a/cli/load_balancer_list.go +++ b/internal/cmds/load_balancer_list.go @@ -1,9 +1,11 @@ -package cli +package cmds import ( - "github.com/hetznercloud/hcloud-go/hcloud/schema" "strings" + "github.com/hetznercloud/cli/internal/state" + "github.com/hetznercloud/hcloud-go/hcloud/schema" + "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) @@ -14,7 +16,7 @@ func init() { loadBalancerListTableOutput = describeLoadBalancerListTableOutput(nil) } -func newLoadBalancerListCommand(cli *CLI) *cobra.Command { +func newLoadBalancerListCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "list [FLAGS]", Short: "List Load Balancers", @@ -24,15 +26,15 @@ func newLoadBalancerListCommand(cli *CLI) *cobra.Command { ), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runLoadBalancerList), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runLoadBalancerList), } addOutputFlag(cmd, outputOptionNoHeader(), outputOptionColumns(loadBalancerListTableOutput.Columns()), outputOptionJSON()) cmd.Flags().StringP("selector", "l", "", "Selector to filter by labels") return cmd } -func runLoadBalancerList(cli *CLI, cmd *cobra.Command, args []string) error { +func runLoadBalancerList(cli *state.State, cmd *cobra.Command, args []string) error { outOpts := outputFlagsForCommand(cmd) labelSelector, _ := cmd.Flags().GetString("selector") @@ -153,7 +155,7 @@ func runLoadBalancerList(cli *CLI, cmd *cobra.Command, args []string) error { return nil } -func describeLoadBalancerListTableOutput(cli *CLI) *tableOutput { +func describeLoadBalancerListTableOutput(cli *state.State) *tableOutput { return newTableOutput(). AddAllowedFields(hcloud.LoadBalancer{}). AddFieldOutputFn("ipv4", fieldOutputFn(func(obj interface{}) string { diff --git a/cli/load_balancer_metrics.go b/internal/cmds/load_balancer_metrics.go similarity index 90% rename from cli/load_balancer_metrics.go rename to internal/cmds/load_balancer_metrics.go index acef2e1e..c6dd46ce 100644 --- a/cli/load_balancer_metrics.go +++ b/internal/cmds/load_balancer_metrics.go @@ -1,19 +1,21 @@ -package cli +package cmds import ( "encoding/json" "fmt" - "github.com/guptarohit/asciigraph" "sort" "strconv" "time" + "github.com/guptarohit/asciigraph" + "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newLoadBalancerMetricsCommand(cli *CLI) *cobra.Command { +func newLoadBalancerMetricsCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "metrics [FLAGS] LOADBALANCER", Short: "[ALPHA] Metrics from a Load Balancer", @@ -21,8 +23,8 @@ func newLoadBalancerMetricsCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.LoadBalancerNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runLoadBalancerMetrics), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runLoadBalancerMetrics), } cmd.Flags().String("type", "", "Type of metrics you want to show") @@ -36,7 +38,7 @@ func newLoadBalancerMetricsCommand(cli *CLI) *cobra.Command { return cmd } -func runLoadBalancerMetrics(cli *CLI, cmd *cobra.Command, args []string) error { +func runLoadBalancerMetrics(cli *state.State, cmd *cobra.Command, args []string) error { outputFlags := outputFlagsForCommand(cmd) idOrName := args[0] diff --git a/cli/load_balancer_remove_label.go b/internal/cmds/load_balancer_remove_label.go similarity index 86% rename from cli/load_balancer_remove_label.go rename to internal/cmds/load_balancer_remove_label.go index 70278a09..425422fe 100644 --- a/cli/load_balancer_remove_label.go +++ b/internal/cmds/load_balancer_remove_label.go @@ -1,15 +1,16 @@ -package cli +package cmds import ( "errors" "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newLoadBalancerRemoveLabelCommand(cli *CLI) *cobra.Command { +func newLoadBalancerRemoveLabelCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "remove-label [FLAGS] LOADBALANCER LABELKEY", Short: "Remove a label from a Load Balancer", @@ -25,8 +26,8 @@ func newLoadBalancerRemoveLabelCommand(cli *CLI) *cobra.Command { ), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: chainRunE(validateLoadBalancerRemoveLabel, cli.ensureToken), - RunE: cli.wrap(runLoadBalancerRemoveLabel), + PreRunE: chainRunE(validateLoadBalancerRemoveLabel, cli.EnsureToken), + RunE: cli.Wrap(runLoadBalancerRemoveLabel), } cmd.Flags().BoolP("all", "a", false, "Remove all labels") @@ -46,7 +47,7 @@ func validateLoadBalancerRemoveLabel(cmd *cobra.Command, args []string) error { return nil } -func runLoadBalancerRemoveLabel(cli *CLI, cmd *cobra.Command, args []string) error { +func runLoadBalancerRemoveLabel(cli *state.State, cmd *cobra.Command, args []string) error { all, _ := cmd.Flags().GetBool("all") idOrName := args[0] loadBalancer, _, err := cli.Client().LoadBalancer.Get(cli.Context, idOrName) diff --git a/cli/load_balancer_remove_target.go b/internal/cmds/load_balancer_remove_target.go similarity index 87% rename from cli/load_balancer_remove_target.go rename to internal/cmds/load_balancer_remove_target.go index b02b48f6..5246c6eb 100644 --- a/cli/load_balancer_remove_target.go +++ b/internal/cmds/load_balancer_remove_target.go @@ -1,15 +1,16 @@ -package cli +package cmds import ( "fmt" "net" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newLoadBalancerRemoveTargetCommand(cli *CLI) *cobra.Command { +func newLoadBalancerRemoveTargetCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "remove-target LOADBALANCER FLAGS", Short: "Remove a target to a Load Balancer", @@ -17,8 +18,8 @@ func newLoadBalancerRemoveTargetCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.LoadBalancerNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runLoadBalancerRemoveTarget), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runLoadBalancerRemoveTarget), } cmd.Flags().String("server", "", "Name or ID of the server") @@ -31,7 +32,7 @@ func newLoadBalancerRemoveTargetCommand(cli *CLI) *cobra.Command { return cmd } -func runLoadBalancerRemoveTarget(cli *CLI, cmd *cobra.Command, args []string) error { +func runLoadBalancerRemoveTarget(cli *state.State, cmd *cobra.Command, args []string) error { var ( action *hcloud.Action loadBalancer *hcloud.LoadBalancer diff --git a/cli/load_balancer_type.go b/internal/cmds/load_balancer_type.go similarity index 68% rename from cli/load_balancer_type.go rename to internal/cmds/load_balancer_type.go index 126b773d..2f15ed0f 100644 --- a/cli/load_balancer_type.go +++ b/internal/cmds/load_balancer_type.go @@ -1,8 +1,11 @@ -package cli +package cmds -import "github.com/spf13/cobra" +import ( + "github.com/hetznercloud/cli/internal/state" + "github.com/spf13/cobra" +) -func newLoadBalancerTypeCommand(cli *CLI) *cobra.Command { +func NewLoadBalancerTypeCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "load-balancer-type", Short: "Manage Load Balancer types", diff --git a/cli/load_balancer_type_describe.go b/internal/cmds/load_balancer_type_describe.go similarity index 84% rename from cli/load_balancer_type_describe.go rename to internal/cmds/load_balancer_type_describe.go index 9279e557..21c19697 100644 --- a/cli/load_balancer_type_describe.go +++ b/internal/cmds/load_balancer_type_describe.go @@ -1,15 +1,16 @@ -package cli +package cmds import ( "encoding/json" "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newLoadBalancerTypenDescribeCommand(cli *CLI) *cobra.Command { +func newLoadBalancerTypenDescribeCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "describe [FLAGS] LOADBALANCERTYPE", Short: "Describe a Load Balancer type", @@ -17,14 +18,14 @@ func newLoadBalancerTypenDescribeCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.LoadBalancerTypeNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runLoadBalancerTypeDescribe), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runLoadBalancerTypeDescribe), } addOutputFlag(cmd, outputOptionJSON(), outputOptionFormat()) return cmd } -func runLoadBalancerTypeDescribe(cli *CLI, cmd *cobra.Command, args []string) error { +func runLoadBalancerTypeDescribe(cli *state.State, cmd *cobra.Command, args []string) error { outputFlags := outputFlagsForCommand(cmd) idOrName := args[0] @@ -46,7 +47,7 @@ func runLoadBalancerTypeDescribe(cli *CLI, cmd *cobra.Command, args []string) er } } -func loadBalancerTypeDescribeText(cli *CLI, loadBalancerType *hcloud.LoadBalancerType) error { +func loadBalancerTypeDescribeText(cli *state.State, loadBalancerType *hcloud.LoadBalancerType) error { fmt.Printf("ID:\t\t\t\t%d\n", loadBalancerType.ID) fmt.Printf("Name:\t\t\t\t%s\n", loadBalancerType.Name) fmt.Printf("Description:\t\t\t%s\n", loadBalancerType.Description) diff --git a/cli/load_balancer_type_list.go b/internal/cmds/load_balancer_type_list.go similarity index 83% rename from cli/load_balancer_type_list.go rename to internal/cmds/load_balancer_type_list.go index 2dc89c7e..81601057 100644 --- a/cli/load_balancer_type_list.go +++ b/internal/cmds/load_balancer_type_list.go @@ -1,6 +1,7 @@ -package cli +package cmds import ( + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/hetznercloud/hcloud-go/hcloud/schema" "github.com/spf13/cobra" @@ -13,7 +14,7 @@ func init() { AddAllowedFields(hcloud.LoadBalancerType{}) } -func newLoadBalancerTypeListCommand(cli *CLI) *cobra.Command { +func newLoadBalancerTypeListCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "list [FLAGS]", Short: "List Load Balancer types", @@ -23,14 +24,14 @@ func newLoadBalancerTypeListCommand(cli *CLI) *cobra.Command { ), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runLoadBalancerTypeList), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runLoadBalancerTypeList), } addOutputFlag(cmd, outputOptionNoHeader(), outputOptionColumns(loadBalancerTypeListTableOutput.Columns()), outputOptionJSON()) return cmd } -func runLoadBalancerTypeList(cli *CLI, cmd *cobra.Command, args []string) error { +func runLoadBalancerTypeList(cli *state.State, cmd *cobra.Command, args []string) error { outOpts := outputFlagsForCommand(cmd) loadBalancerTypes, err := cli.Client().LoadBalancerType.All(cli.Context) diff --git a/cli/load_balancer_update.go b/internal/cmds/load_balancer_update.go similarity index 77% rename from cli/load_balancer_update.go rename to internal/cmds/load_balancer_update.go index 58f2b20f..09bdeabc 100644 --- a/cli/load_balancer_update.go +++ b/internal/cmds/load_balancer_update.go @@ -1,15 +1,16 @@ -package cli +package cmds import ( "errors" "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newLoadBalancerUpdateCommand(cli *CLI) *cobra.Command { +func newLoadBalancerUpdateCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "update [FLAGS] LOADBALANCER", Short: "Update a Load Balancer", @@ -17,8 +18,8 @@ func newLoadBalancerUpdateCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.LoadBalancerNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runLoadBalancerUpdate), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runLoadBalancerUpdate), } cmd.Flags().String("name", "", "Load Balancer name") @@ -26,7 +27,7 @@ func newLoadBalancerUpdateCommand(cli *CLI) *cobra.Command { return cmd } -func runLoadBalancerUpdate(cli *CLI, cmd *cobra.Command, args []string) error { +func runLoadBalancerUpdate(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] loadBalancer, _, err := cli.Client().LoadBalancer.Get(cli.Context, idOrName) if err != nil { diff --git a/cli/load_balancer_update_service.go b/internal/cmds/load_balancer_update_service.go similarity index 95% rename from cli/load_balancer_update_service.go rename to internal/cmds/load_balancer_update_service.go index ec6efca2..8ead7be6 100644 --- a/cli/load_balancer_update_service.go +++ b/internal/cmds/load_balancer_update_service.go @@ -1,15 +1,16 @@ -package cli +package cmds import ( "fmt" "time" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newLoadBalancerUpdateServiceCommand(cli *CLI) *cobra.Command { +func newLoadBalancerUpdateServiceCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "update-service LOADBALANCER FLAGS", Short: "Updates a service from a Load Balancer", @@ -17,8 +18,8 @@ func newLoadBalancerUpdateServiceCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.LoadBalancerNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runLoadBalancerUpdateService), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runLoadBalancerUpdateService), } cmd.Flags().Int("listen-port", 0, "The listen port of the service that you want to update (required)") @@ -50,7 +51,7 @@ func newLoadBalancerUpdateServiceCommand(cli *CLI) *cobra.Command { return cmd } -func runLoadBalancerUpdateService(cli *CLI, cmd *cobra.Command, args []string) error { +func runLoadBalancerUpdateService(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] listenPort, _ := cmd.Flags().GetInt("listen-port") diff --git a/cli/location.go b/internal/cmds/location.go similarity index 66% rename from cli/location.go rename to internal/cmds/location.go index a2a6a9d7..b9b1bec1 100644 --- a/cli/location.go +++ b/internal/cmds/location.go @@ -1,8 +1,11 @@ -package cli +package cmds -import "github.com/spf13/cobra" +import ( + "github.com/hetznercloud/cli/internal/state" + "github.com/spf13/cobra" +) -func newLocationCommand(cli *CLI) *cobra.Command { +func NewLocationCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "location", Short: "Manage locations", diff --git a/cli/location_describe.go b/internal/cmds/location_describe.go similarity index 82% rename from cli/location_describe.go rename to internal/cmds/location_describe.go index a354e84b..b51bdb24 100644 --- a/cli/location_describe.go +++ b/internal/cmds/location_describe.go @@ -1,15 +1,16 @@ -package cli +package cmds import ( "encoding/json" "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newLocationDescribeCommand(cli *CLI) *cobra.Command { +func newLocationDescribeCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "describe [FLAGS] LOCATION", Short: "Describe a location", @@ -17,14 +18,14 @@ func newLocationDescribeCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.LocationNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runLocationDescribe), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runLocationDescribe), } addOutputFlag(cmd, outputOptionJSON(), outputOptionFormat()) return cmd } -func runLocationDescribe(cli *CLI, cmd *cobra.Command, args []string) error { +func runLocationDescribe(cli *state.State, cmd *cobra.Command, args []string) error { outputFlags := outputFlagsForCommand(cmd) idOrName := args[0] @@ -46,7 +47,7 @@ func runLocationDescribe(cli *CLI, cmd *cobra.Command, args []string) error { } } -func locationDescribeText(cli *CLI, location *hcloud.Location) error { +func locationDescribeText(cli *state.State, location *hcloud.Location) error { fmt.Printf("ID:\t\t%d\n", location.ID) fmt.Printf("Name:\t\t%s\n", location.Name) fmt.Printf("Description:\t%s\n", location.Description) diff --git a/cli/location_list.go b/internal/cmds/location_list.go similarity index 82% rename from cli/location_list.go rename to internal/cmds/location_list.go index 381e4e2f..e6793554 100644 --- a/cli/location_list.go +++ b/internal/cmds/location_list.go @@ -1,6 +1,7 @@ -package cli +package cmds import ( + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/hetznercloud/hcloud-go/hcloud/schema" "github.com/spf13/cobra" @@ -13,7 +14,7 @@ func init() { AddAllowedFields(hcloud.Location{}) } -func newLocationListCommand(cli *CLI) *cobra.Command { +func newLocationListCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "list [FLAGS]", Short: "List locations", @@ -23,14 +24,14 @@ func newLocationListCommand(cli *CLI) *cobra.Command { ), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runLocationList), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runLocationList), } addOutputFlag(cmd, outputOptionNoHeader(), outputOptionColumns(locationListTableOutput.Columns()), outputOptionJSON()) return cmd } -func runLocationList(cli *CLI, cmd *cobra.Command, args []string) error { +func runLocationList(cli *state.State, cmd *cobra.Command, args []string) error { outOpts := outputFlagsForCommand(cmd) locations, err := cli.Client().Location.All(cli.Context) diff --git a/cli/network.go b/internal/cmds/network.go similarity index 82% rename from cli/network.go rename to internal/cmds/network.go index e6d50c7d..e81e50a1 100644 --- a/cli/network.go +++ b/internal/cmds/network.go @@ -1,8 +1,11 @@ -package cli +package cmds -import "github.com/spf13/cobra" +import ( + "github.com/hetznercloud/cli/internal/state" + "github.com/spf13/cobra" +) -func newNetworkCommand(cli *CLI) *cobra.Command { +func NewNetworkCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "network", Short: "Manage networks", diff --git a/cli/network_add_label.go b/internal/cmds/network_add_label.go similarity index 84% rename from cli/network_add_label.go rename to internal/cmds/network_add_label.go index 9f9ccda3..fb841081 100644 --- a/cli/network_add_label.go +++ b/internal/cmds/network_add_label.go @@ -1,14 +1,15 @@ -package cli +package cmds import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newNetworkAddLabelCommand(cli *CLI) *cobra.Command { +func newNetworkAddLabelCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "add-label [FLAGS] NETWORK LABEL", Short: "Add a label to a network", @@ -16,8 +17,8 @@ func newNetworkAddLabelCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.NetworkNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: chainRunE(validateNetworkAddLabel, cli.ensureToken), - RunE: cli.wrap(runNetworkAddLabel), + PreRunE: chainRunE(validateNetworkAddLabel, cli.EnsureToken), + RunE: cli.Wrap(runNetworkAddLabel), } cmd.Flags().BoolP("overwrite", "o", false, "Overwrite label if it exists already") @@ -33,7 +34,7 @@ func validateNetworkAddLabel(cmd *cobra.Command, args []string) error { return nil } -func runNetworkAddLabel(cli *CLI, cmd *cobra.Command, args []string) error { +func runNetworkAddLabel(cli *state.State, cmd *cobra.Command, args []string) error { overwrite, _ := cmd.Flags().GetBool("overwrite") idOrName := args[0] network, _, err := cli.Client().Network.Get(cli.Context, idOrName) diff --git a/cli/network_add_route.go b/internal/cmds/network_add_route.go similarity index 82% rename from cli/network_add_route.go rename to internal/cmds/network_add_route.go index 4ff8c144..bb20b02e 100644 --- a/cli/network_add_route.go +++ b/internal/cmds/network_add_route.go @@ -1,15 +1,16 @@ -package cli +package cmds import ( "fmt" "net" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newNetworkAddRouteCommand(cli *CLI) *cobra.Command { +func newNetworkAddRouteCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "add-route NETWORK FLAGS", Short: "Add a route to a network", @@ -17,8 +18,8 @@ func newNetworkAddRouteCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.NetworkNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runNetworkAddRoute), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runNetworkAddRoute), } cmd.Flags().IPNet("destination", net.IPNet{}, "Destination network or host (required)") @@ -30,7 +31,7 @@ func newNetworkAddRouteCommand(cli *CLI) *cobra.Command { return cmd } -func runNetworkAddRoute(cli *CLI, cmd *cobra.Command, args []string) error { +func runNetworkAddRoute(cli *state.State, cmd *cobra.Command, args []string) error { gateway, _ := cmd.Flags().GetIP("gateway") destination, _ := cmd.Flags().GetIPNet("destination") idOrName := args[0] diff --git a/cli/network_add_subnet.go b/internal/cmds/network_add_subnet.go similarity index 86% rename from cli/network_add_subnet.go rename to internal/cmds/network_add_subnet.go index 138d576f..ca8e9e8d 100644 --- a/cli/network_add_subnet.go +++ b/internal/cmds/network_add_subnet.go @@ -1,15 +1,16 @@ -package cli +package cmds import ( "fmt" "net" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newNetworkAddSubnetCommand(cli *CLI) *cobra.Command { +func newNetworkAddSubnetCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "add-subnet NETWORK FLAGS", Short: "Add a subnet to a network", @@ -17,8 +18,8 @@ func newNetworkAddSubnetCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.NetworkNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runNetworkAddSubnet), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runNetworkAddSubnet), } cmd.Flags().String("type", "", "Type of subnet (required)") @@ -35,7 +36,7 @@ func newNetworkAddSubnetCommand(cli *CLI) *cobra.Command { return cmd } -func runNetworkAddSubnet(cli *CLI, cmd *cobra.Command, args []string) error { +func runNetworkAddSubnet(cli *state.State, cmd *cobra.Command, args []string) error { subnetType, _ := cmd.Flags().GetString("type") networkZone, _ := cmd.Flags().GetString("network-zone") ipRange, _ := cmd.Flags().GetIPNet("ip-range") diff --git a/cli/network_change_ip_range.go b/internal/cmds/network_change_ip_range.go similarity index 78% rename from cli/network_change_ip_range.go rename to internal/cmds/network_change_ip_range.go index 9200366e..a1d9bc18 100644 --- a/cli/network_change_ip_range.go +++ b/internal/cmds/network_change_ip_range.go @@ -1,15 +1,16 @@ -package cli +package cmds import ( "fmt" "net" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newNetworkChangeIPRangeCommand(cli *CLI) *cobra.Command { +func newNetworkChangeIPRangeCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "change-ip-range [FLAGS] NETWORK", Short: "Change the IP range of a network", @@ -17,8 +18,8 @@ func newNetworkChangeIPRangeCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.NetworkNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runNetworkChangeIPRange), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runNetworkChangeIPRange), } cmd.Flags().IPNet("ip-range", net.IPNet{}, "New IP range (required)") @@ -27,7 +28,7 @@ func newNetworkChangeIPRangeCommand(cli *CLI) *cobra.Command { return cmd } -func runNetworkChangeIPRange(cli *CLI, cmd *cobra.Command, args []string) error { +func runNetworkChangeIPRange(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] network, _, err := cli.Client().Network.Get(cli.Context, idOrName) if err != nil { diff --git a/cli/network_create.go b/internal/cmds/network_create.go similarity index 77% rename from cli/network_create.go rename to internal/cmds/network_create.go index 7f92977f..f73d219c 100644 --- a/cli/network_create.go +++ b/internal/cmds/network_create.go @@ -1,22 +1,23 @@ -package cli +package cmds import ( "fmt" "net" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newNetworkCreateCommand(cli *CLI) *cobra.Command { +func newNetworkCreateCommand(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(runNetworkCreate), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runNetworkCreate), } cmd.Flags().String("name", "", "Network name (required)") @@ -30,7 +31,7 @@ func newNetworkCreateCommand(cli *CLI) *cobra.Command { return cmd } -func runNetworkCreate(cli *CLI, cmd *cobra.Command, args []string) error { +func runNetworkCreate(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") diff --git a/cli/network_delete.go b/internal/cmds/network_delete.go similarity index 72% rename from cli/network_delete.go rename to internal/cmds/network_delete.go index 0bd23452..20b56b7f 100644 --- a/cli/network_delete.go +++ b/internal/cmds/network_delete.go @@ -1,13 +1,14 @@ -package cli +package cmds import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/spf13/cobra" ) -func newNetworkDeleteCommand(cli *CLI) *cobra.Command { +func newNetworkDeleteCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "delete [FLAGS] NETWORK", Short: "Delete a network", @@ -15,13 +16,13 @@ func newNetworkDeleteCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.NetworkNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runNetworkDelete), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runNetworkDelete), } return cmd } -func runNetworkDelete(cli *CLI, cmd *cobra.Command, args []string) error { +func runNetworkDelete(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] network, _, err := cli.Client().Network.Get(cli.Context, idOrName) if err != nil { diff --git a/cli/network_describe.go b/internal/cmds/network_describe.go similarity index 86% rename from cli/network_describe.go rename to internal/cmds/network_describe.go index 8b6b1240..f1b2b341 100644 --- a/cli/network_describe.go +++ b/internal/cmds/network_describe.go @@ -1,15 +1,16 @@ -package cli +package cmds import ( "fmt" humanize "github.com/dustin/go-humanize" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newNetworkDescribeCommand(cli *CLI) *cobra.Command { +func newNetworkDescribeCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "describe [FLAGS] NETWORK", Short: "Describe a network", @@ -17,14 +18,14 @@ func newNetworkDescribeCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.NetworkNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runNetworkDescribe), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runNetworkDescribe), } addOutputFlag(cmd, outputOptionJSON(), outputOptionFormat()) return cmd } -func runNetworkDescribe(cli *CLI, cmd *cobra.Command, args []string) error { +func runNetworkDescribe(cli *state.State, cmd *cobra.Command, args []string) error { outputFlags := outputFlagsForCommand(cmd) idOrName := args[0] @@ -46,7 +47,7 @@ func runNetworkDescribe(cli *CLI, cmd *cobra.Command, args []string) error { } } -func networkDescribeText(cli *CLI, network *hcloud.Network) error { +func networkDescribeText(cli *state.State, network *hcloud.Network) error { fmt.Printf("ID:\t\t%d\n", network.ID) fmt.Printf("Name:\t\t%s\n", network.Name) fmt.Printf("Created:\t%s (%s)\n", datetime(network.Created), humanize.Time(network.Created)) diff --git a/cli/network_disable_protection.go b/internal/cmds/network_disable_protection.go similarity index 80% rename from cli/network_disable_protection.go rename to internal/cmds/network_disable_protection.go index 49cf99b9..fe899ca3 100644 --- a/cli/network_disable_protection.go +++ b/internal/cmds/network_disable_protection.go @@ -1,15 +1,16 @@ -package cli +package cmds import ( "fmt" "strings" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newNetworkDisableProtectionCommand(cli *CLI) *cobra.Command { +func newNetworkDisableProtectionCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "disable-protection [FLAGS] NETWORK PROTECTIONLEVEL [PROTECTIONLEVEL...]", Short: "Disable resource protection for a network", @@ -20,13 +21,13 @@ func newNetworkDisableProtectionCommand(cli *CLI) *cobra.Command { ), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runNetworkDisableProtection), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runNetworkDisableProtection), } return cmd } -func runNetworkDisableProtection(cli *CLI, cmd *cobra.Command, args []string) error { +func runNetworkDisableProtection(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] network, _, err := cli.Client().Network.Get(cli.Context, idOrName) if err != nil { diff --git a/cli/network_enable_protection.go b/internal/cmds/network_enable_protection.go similarity index 80% rename from cli/network_enable_protection.go rename to internal/cmds/network_enable_protection.go index ba5ca818..9b98ab30 100644 --- a/cli/network_enable_protection.go +++ b/internal/cmds/network_enable_protection.go @@ -1,15 +1,16 @@ -package cli +package cmds import ( "fmt" "strings" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newNetworkEnableProtectionCommand(cli *CLI) *cobra.Command { +func newNetworkEnableProtectionCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "enable-protection [FLAGS] NETWORK PROTECTIONLEVEL [PROTECTIONLEVEL...]", Short: "Enable resource protection for a network", @@ -20,13 +21,13 @@ func newNetworkEnableProtectionCommand(cli *CLI) *cobra.Command { ), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runNetworkEnableProtection), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runNetworkEnableProtection), } return cmd } -func runNetworkEnableProtection(cli *CLI, cmd *cobra.Command, args []string) error { +func runNetworkEnableProtection(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] network, _, err := cli.Client().Network.Get(cli.Context, idOrName) if err != nil { diff --git a/cli/network_list.go b/internal/cmds/network_list.go similarity index 90% rename from cli/network_list.go rename to internal/cmds/network_list.go index 6822ad1a..57e9572e 100644 --- a/cli/network_list.go +++ b/internal/cmds/network_list.go @@ -1,9 +1,10 @@ -package cli +package cmds import ( "fmt" "strings" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/hetznercloud/hcloud-go/hcloud/schema" "github.com/spf13/cobra" @@ -15,7 +16,7 @@ func init() { networkListTableOutput = describeNetworkListTableOutput(nil) } -func newNetworkListCommand(cli *CLI) *cobra.Command { +func newNetworkListCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "list [FLAGS]", Short: "List networks", @@ -25,15 +26,15 @@ func newNetworkListCommand(cli *CLI) *cobra.Command { ), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runNetworkList), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runNetworkList), } addOutputFlag(cmd, outputOptionNoHeader(), outputOptionColumns(networkListTableOutput.Columns()), outputOptionJSON()) cmd.Flags().StringP("selector", "l", "", "Selector to filter by labels") return cmd } -func runNetworkList(cli *CLI, cmd *cobra.Command, args []string) error { +func runNetworkList(cli *state.State, cmd *cobra.Command, args []string) error { outOpts := outputFlagsForCommand(cmd) labelSelector, _ := cmd.Flags().GetString("selector") @@ -101,7 +102,7 @@ func runNetworkList(cli *CLI, cmd *cobra.Command, args []string) error { return nil } -func describeNetworkListTableOutput(cli *CLI) *tableOutput { +func describeNetworkListTableOutput(cli *state.State) *tableOutput { return newTableOutput(). AddAllowedFields(hcloud.Network{}). AddFieldOutputFn("servers", fieldOutputFn(func(obj interface{}) string { diff --git a/cli/network_remove_label.go b/internal/cmds/network_remove_label.go similarity index 86% rename from cli/network_remove_label.go rename to internal/cmds/network_remove_label.go index 0deb9750..515a779e 100644 --- a/cli/network_remove_label.go +++ b/internal/cmds/network_remove_label.go @@ -1,15 +1,16 @@ -package cli +package cmds import ( "errors" "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newNetworkRemoveLabelCommand(cli *CLI) *cobra.Command { +func newNetworkRemoveLabelCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "remove-label [FLAGS] NETWORK LABELKEY", Short: "Remove a label from a network", @@ -25,8 +26,8 @@ func newNetworkRemoveLabelCommand(cli *CLI) *cobra.Command { ), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: chainRunE(validateNetworkRemoveLabel, cli.ensureToken), - RunE: cli.wrap(runNetworkRemoveLabel), + PreRunE: chainRunE(validateNetworkRemoveLabel, cli.EnsureToken), + RunE: cli.Wrap(runNetworkRemoveLabel), } cmd.Flags().BoolP("all", "a", false, "Remove all labels") @@ -46,7 +47,7 @@ func validateNetworkRemoveLabel(cmd *cobra.Command, args []string) error { return nil } -func runNetworkRemoveLabel(cli *CLI, cmd *cobra.Command, args []string) error { +func runNetworkRemoveLabel(cli *state.State, cmd *cobra.Command, args []string) error { all, _ := cmd.Flags().GetBool("all") idOrName := args[0] network, _, err := cli.Client().Network.Get(cli.Context, idOrName) diff --git a/cli/network_remove_route.go b/internal/cmds/network_remove_route.go similarity index 81% rename from cli/network_remove_route.go rename to internal/cmds/network_remove_route.go index bee52877..9afa8bb8 100644 --- a/cli/network_remove_route.go +++ b/internal/cmds/network_remove_route.go @@ -1,15 +1,16 @@ -package cli +package cmds import ( "fmt" "net" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newNetworkRemoveRouteCommand(cli *CLI) *cobra.Command { +func newNetworkRemoveRouteCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "remove-route NETWORK FLAGS", Short: "Remove a route from a network", @@ -17,8 +18,8 @@ func newNetworkRemoveRouteCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.NetworkNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runNetworkRemoveRoute), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runNetworkRemoveRoute), } cmd.Flags().IPNet("destination", net.IPNet{}, "Destination network or host (required)") @@ -30,7 +31,7 @@ func newNetworkRemoveRouteCommand(cli *CLI) *cobra.Command { return cmd } -func runNetworkRemoveRoute(cli *CLI, cmd *cobra.Command, args []string) error { +func runNetworkRemoveRoute(cli *state.State, cmd *cobra.Command, args []string) error { gateway, _ := cmd.Flags().GetIP("gateway") destination, _ := cmd.Flags().GetIPNet("destination") idOrName := args[0] diff --git a/cli/network_remove_subnet.go b/internal/cmds/network_remove_subnet.go similarity index 79% rename from cli/network_remove_subnet.go rename to internal/cmds/network_remove_subnet.go index c9806297..9050308e 100644 --- a/cli/network_remove_subnet.go +++ b/internal/cmds/network_remove_subnet.go @@ -1,15 +1,16 @@ -package cli +package cmds import ( "fmt" "net" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newNetworkRemoveSubnetCommand(cli *CLI) *cobra.Command { +func newNetworkRemoveSubnetCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "remove-subnet NETWORK FLAGS", Short: "Remove a subnet from a network", @@ -17,15 +18,15 @@ func newNetworkRemoveSubnetCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.NetworkNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runNetworkRemoveSubnet), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runNetworkRemoveSubnet), } cmd.Flags().IPNet("ip-range", net.IPNet{}, "Subnet IP range (required)") cmd.MarkFlagRequired("ip-range") return cmd } -func runNetworkRemoveSubnet(cli *CLI, cmd *cobra.Command, args []string) error { +func runNetworkRemoveSubnet(cli *state.State, cmd *cobra.Command, args []string) error { ipRange, _ := cmd.Flags().GetIPNet("ip-range") idOrName := args[0] diff --git a/cli/network_update.go b/internal/cmds/network_update.go similarity index 77% rename from cli/network_update.go rename to internal/cmds/network_update.go index 16a68fca..294bf7c2 100644 --- a/cli/network_update.go +++ b/internal/cmds/network_update.go @@ -1,15 +1,16 @@ -package cli +package cmds import ( "errors" "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newNetworkUpdateCommand(cli *CLI) *cobra.Command { +func newNetworkUpdateCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "update [FLAGS] NETWORK", Short: "Update a network", @@ -17,8 +18,8 @@ func newNetworkUpdateCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.NetworkNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runNetworkUpdate), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runNetworkUpdate), } cmd.Flags().String("name", "", "Network name") @@ -26,7 +27,7 @@ func newNetworkUpdateCommand(cli *CLI) *cobra.Command { return cmd } -func runNetworkUpdate(cli *CLI, cmd *cobra.Command, args []string) error { +func runNetworkUpdate(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] network, _, err := cli.Client().Network.Get(cli.Context, idOrName) if err != nil { diff --git a/cli/output.go b/internal/cmds/output.go similarity index 99% rename from cli/output.go rename to internal/cmds/output.go index 895faba3..f09817f9 100644 --- a/cli/output.go +++ b/internal/cmds/output.go @@ -1,4 +1,4 @@ -package cli +package cmds import ( "fmt" diff --git a/cli/output_test.go b/internal/cmds/output_test.go similarity index 99% rename from cli/output_test.go rename to internal/cmds/output_test.go index 3a6c04ee..3c188e56 100644 --- a/cli/output_test.go +++ b/internal/cmds/output_test.go @@ -1,4 +1,4 @@ -package cli +package cmds import ( "bytes" diff --git a/cli/server.go b/internal/cmds/server.go similarity index 89% rename from cli/server.go rename to internal/cmds/server.go index f4261294..c53add95 100644 --- a/cli/server.go +++ b/internal/cmds/server.go @@ -1,8 +1,11 @@ -package cli +package cmds -import "github.com/spf13/cobra" +import ( + "github.com/hetznercloud/cli/internal/state" + "github.com/spf13/cobra" +) -func newServerCommand(cli *CLI) *cobra.Command { +func NewServerCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "server", Short: "Manage servers", diff --git a/cli/server_add_label.go b/internal/cmds/server_add_label.go similarity index 81% rename from cli/server_add_label.go rename to internal/cmds/server_add_label.go index fe118c18..bdfcf26f 100644 --- a/cli/server_add_label.go +++ b/internal/cmds/server_add_label.go @@ -1,14 +1,15 @@ -package cli +package cmds import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newServerAddLabelCommand(cli *CLI) *cobra.Command { +func newServerAddLabelCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "add-label [FLAGS] SERVER LABEL", Short: "Add a label to a server", @@ -16,8 +17,8 @@ func newServerAddLabelCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.ServerNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: chainRunE(validateServerAddLabel, cli.ensureToken), - RunE: cli.wrap(runServerAddLabel), + PreRunE: chainRunE(validateServerAddLabel, cli.EnsureToken), + RunE: cli.Wrap(runServerAddLabel), } cmd.Flags().BoolP("overwrite", "o", false, "Overwrite label if it exists already") @@ -33,7 +34,7 @@ func validateServerAddLabel(cmd *cobra.Command, args []string) error { return nil } -func runServerAddLabel(cli *CLI, cmd *cobra.Command, args []string) error { +func runServerAddLabel(cli *state.State, cmd *cobra.Command, args []string) error { overwrite, _ := cmd.Flags().GetBool("overwrite") idOrName := args[0] server, _, err := cli.Client().Server.Get(cli.Context, idOrName) diff --git a/cli/server_attach_iso.go b/internal/cmds/server_attach_iso.go similarity index 78% rename from cli/server_attach_iso.go rename to internal/cmds/server_attach_iso.go index d723e0cb..cd79d40e 100644 --- a/cli/server_attach_iso.go +++ b/internal/cmds/server_attach_iso.go @@ -1,13 +1,14 @@ -package cli +package cmds import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/spf13/cobra" ) -func newServerAttachISOCommand(cli *CLI) *cobra.Command { +func newServerAttachISOCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "attach-iso [FLAGS] SERVER ISO", Short: "Attach an ISO to a server", @@ -18,14 +19,14 @@ func newServerAttachISOCommand(cli *CLI) *cobra.Command { cmpl.SuggestCandidatesF(cli.ISONames), ), DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runServerAttachISO), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runServerAttachISO), } return cmd } -func runServerAttachISO(cli *CLI, cmd *cobra.Command, args []string) error { +func runServerAttachISO(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] server, _, err := cli.Client().Server.Get(cli.Context, idOrName) if err != nil { diff --git a/cli/server_attach_to_network.go b/internal/cmds/server_attach_to_network.go similarity index 85% rename from cli/server_attach_to_network.go rename to internal/cmds/server_attach_to_network.go index 813d39a6..c845425e 100644 --- a/cli/server_attach_to_network.go +++ b/internal/cmds/server_attach_to_network.go @@ -1,15 +1,16 @@ -package cli +package cmds import ( "fmt" "net" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newServerAttachToNetworkCommand(cli *CLI) *cobra.Command { +func newServerAttachToNetworkCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "attach-to-network [FLAGS] SERVER", Short: "Attach a server to a network", @@ -17,8 +18,8 @@ func newServerAttachToNetworkCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.ServerNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runServerAttachToNetwork), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runServerAttachToNetwork), } cmd.Flags().StringP("network", "n", "", "Network (ID or name) (required)") @@ -31,7 +32,7 @@ func newServerAttachToNetworkCommand(cli *CLI) *cobra.Command { return cmd } -func runServerAttachToNetwork(cli *CLI, cmd *cobra.Command, args []string) error { +func runServerAttachToNetwork(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] server, _, err := cli.Client().Server.Get(cli.Context, idOrName) if err != nil { diff --git a/cli/server_change_alias_ips.go b/internal/cmds/server_change_alias_ips.go similarity index 85% rename from cli/server_change_alias_ips.go rename to internal/cmds/server_change_alias_ips.go index 3d40d5f8..99b2dc27 100644 --- a/cli/server_change_alias_ips.go +++ b/internal/cmds/server_change_alias_ips.go @@ -1,15 +1,16 @@ -package cli +package cmds import ( "fmt" "net" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newServerChangeAliasIPsCommand(cli *CLI) *cobra.Command { +func newServerChangeAliasIPsCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "change-alias-ips [FLAGS] SERVER", Short: "Change a server's alias IPs in a network", @@ -17,8 +18,8 @@ func newServerChangeAliasIPsCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.ServerNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runServerChangeAliasIPsk), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runServerChangeAliasIPsk), } cmd.Flags().StringP("network", "n", "", "Network (ID or name) (required)") @@ -31,7 +32,7 @@ func newServerChangeAliasIPsCommand(cli *CLI) *cobra.Command { return cmd } -func runServerChangeAliasIPsk(cli *CLI, cmd *cobra.Command, args []string) error { +func runServerChangeAliasIPsk(cli *state.State, cmd *cobra.Command, args []string) error { clear, _ := cmd.Flags().GetBool("clear") idOrName := args[0] server, _, err := cli.Client().Server.Get(cli.Context, idOrName) diff --git a/cli/server_change_type.go b/internal/cmds/server_change_type.go similarity index 83% rename from cli/server_change_type.go rename to internal/cmds/server_change_type.go index 3e9b84d3..707c68cc 100644 --- a/cli/server_change_type.go +++ b/internal/cmds/server_change_type.go @@ -1,14 +1,15 @@ -package cli +package cmds import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newServerChangeTypeCommand(cli *CLI) *cobra.Command { +func newServerChangeTypeCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "change-type [FLAGS] SERVER SERVERTYPE", Short: "Change type of a server", @@ -19,15 +20,15 @@ func newServerChangeTypeCommand(cli *CLI) *cobra.Command { ), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runServerChangeType), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runServerChangeType), } cmd.Flags().Bool("keep-disk", false, "Keep disk size of current server type. This enables downgrading the server.") return cmd } -func runServerChangeType(cli *CLI, cmd *cobra.Command, args []string) error { +func runServerChangeType(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] server, _, err := cli.Client().Server.Get(cli.Context, idOrName) if err != nil { diff --git a/cli/server_create.go b/internal/cmds/server_create.go similarity index 94% rename from cli/server_create.go rename to internal/cmds/server_create.go index b5fee845..819e279d 100644 --- a/cli/server_create.go +++ b/internal/cmds/server_create.go @@ -1,30 +1,32 @@ -package cli +package cmds import ( "bytes" "encoding/base64" "fmt" - "github.com/hetznercloud/cli/internal/cmd/cmpl" - "github.com/hetznercloud/hcloud-go/hcloud" - "github.com/spf13/cobra" - "github.com/spf13/pflag" - "golang.org/x/crypto/ssh" "io/ioutil" "mime/multipart" "net/textproto" "os" "strings" + + "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" + "github.com/hetznercloud/hcloud-go/hcloud" + "github.com/spf13/cobra" + "github.com/spf13/pflag" + "golang.org/x/crypto/ssh" ) -func newServerCreateCommand(cli *CLI) *cobra.Command { +func newServerCreateCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "create FLAGS", Short: "Create a server", Args: cobra.NoArgs, TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runServerCreate), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runServerCreate), } cmd.Flags().String("name", "", "Server name (required)") cmd.MarkFlagRequired("name") @@ -63,7 +65,7 @@ func newServerCreateCommand(cli *CLI) *cobra.Command { return cmd } -func runServerCreate(cli *CLI, cmd *cobra.Command, args []string) error { +func runServerCreate(cli *state.State, cmd *cobra.Command, args []string) error { opts, err := optsFromFlags(cli, cmd.Flags()) if err != nil { return err @@ -160,7 +162,7 @@ func buildUserData(files []string) (string, error) { return buf.String(), nil } -func optsFromFlags(cli *CLI, flags *pflag.FlagSet) (opts hcloud.ServerCreateOpts, err error) { +func optsFromFlags(cli *state.State, flags *pflag.FlagSet) (opts hcloud.ServerCreateOpts, err error) { name, _ := flags.GetString("name") serverType, _ := flags.GetString("type") imageIDorName, _ := flags.GetString("image") @@ -284,7 +286,7 @@ func optsFromFlags(cli *CLI, flags *pflag.FlagSet) (opts hcloud.ServerCreateOpts return } -func getSSHKeyForFingerprint(cli *CLI, file string) (sshKey *hcloud.SSHKey, err error) { +func getSSHKeyForFingerprint(cli *state.State, file string) (sshKey *hcloud.SSHKey, err error) { var ( fileContent []byte publicKey ssh.PublicKey diff --git a/cli/server_create_image.go b/internal/cmds/server_create_image.go similarity index 87% rename from cli/server_create_image.go rename to internal/cmds/server_create_image.go index 69ba9884..18bd6764 100644 --- a/cli/server_create_image.go +++ b/internal/cmds/server_create_image.go @@ -1,22 +1,23 @@ -package cli +package cmds import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newServerCreateImageCommand(cli *CLI) *cobra.Command { +func newServerCreateImageCommand(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: chainRunE(validateServerCreateImage, cli.ensureToken), - RunE: cli.wrap(runServerCreateImage), + PreRunE: chainRunE(validateServerCreateImage, cli.EnsureToken), + RunE: cli.Wrap(runServerCreateImage), } cmd.Flags().String("type", "", "Image type (required)") cmd.RegisterFlagCompletionFunc("type", cmpl.SuggestCandidates("backup", "snapshot")) @@ -41,7 +42,7 @@ func validateServerCreateImage(cmd *cobra.Command, args []string) error { return nil } -func runServerCreateImage(cli *CLI, cmd *cobra.Command, args []string) error { +func runServerCreateImage(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] server, _, err := cli.Client().Server.Get(cli.Context, idOrName) if err != nil { diff --git a/cli/server_delete.go b/internal/cmds/server_delete.go similarity index 71% rename from cli/server_delete.go rename to internal/cmds/server_delete.go index 7100aeed..46c65fc4 100644 --- a/cli/server_delete.go +++ b/internal/cmds/server_delete.go @@ -1,13 +1,14 @@ -package cli +package cmds import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/spf13/cobra" ) -func newServerDeleteCommand(cli *CLI) *cobra.Command { +func newServerDeleteCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "delete SERVER", Short: "Delete a server", @@ -15,13 +16,13 @@ func newServerDeleteCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.ServerNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runServerDelete), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runServerDelete), } return cmd } -func runServerDelete(cli *CLI, cmd *cobra.Command, args []string) error { +func runServerDelete(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] server, _, err := cli.Client().Server.Get(cli.Context, idOrName) if err != nil { diff --git a/cli/server_describe.go b/internal/cmds/server_describe.go similarity index 91% rename from cli/server_describe.go rename to internal/cmds/server_describe.go index 92a087cd..559b459b 100644 --- a/cli/server_describe.go +++ b/internal/cmds/server_describe.go @@ -1,4 +1,4 @@ -package cli +package cmds import ( "encoding/json" @@ -6,11 +6,12 @@ import ( humanize "github.com/dustin/go-humanize" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newServerDescribeCommand(cli *CLI) *cobra.Command { +func newServerDescribeCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "describe [FLAGS] SERVER", Short: "Describe a server", @@ -18,14 +19,14 @@ func newServerDescribeCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.ServerNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runServerDescribe), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runServerDescribe), } addOutputFlag(cmd, outputOptionJSON(), outputOptionFormat()) return cmd } -func runServerDescribe(cli *CLI, cmd *cobra.Command, args []string) error { +func runServerDescribe(cli *state.State, cmd *cobra.Command, args []string) error { outputFlags := outputFlagsForCommand(cmd) idOrName := args[0] @@ -47,7 +48,7 @@ func runServerDescribe(cli *CLI, cmd *cobra.Command, args []string) error { } } -func serverDescribeText(cli *CLI, server *hcloud.Server) error { +func serverDescribeText(cli *state.State, server *hcloud.Server) error { fmt.Printf("ID:\t\t%d\n", server.ID) fmt.Printf("Name:\t\t%s\n", server.Name) fmt.Printf("Status:\t\t%s\n", server.Status) @@ -74,7 +75,7 @@ func serverDescribeText(cli *CLI, server *hcloud.Server) error { fmt.Printf(" Floating IPs:\n") if len(server.PublicNet.FloatingIPs) > 0 { for _, f := range server.PublicNet.FloatingIPs { - floatingIP, _, err := cli.client.FloatingIP.GetByID(cli.Context, f.ID) + floatingIP, _, err := cli.Client().FloatingIP.GetByID(cli.Context, f.ID) if err != nil { return fmt.Errorf("error fetching Floating IP: %v", err) } @@ -89,7 +90,7 @@ func serverDescribeText(cli *CLI, server *hcloud.Server) error { fmt.Printf("Private Net:\n") if len(server.PrivateNet) > 0 { for _, n := range server.PrivateNet { - network, _, err := cli.client.Network.GetByID(cli.Context, n.Network.ID) + network, _, err := cli.Client().Network.GetByID(cli.Context, n.Network.ID) if err != nil { return fmt.Errorf("error fetching network: %v", err) } @@ -113,7 +114,7 @@ func serverDescribeText(cli *CLI, server *hcloud.Server) error { fmt.Printf("Volumes:\n") if len(server.Volumes) > 0 { for _, v := range server.Volumes { - volume, _, err := cli.client.Volume.GetByID(cli.Context, v.ID) + volume, _, err := cli.Client().Volume.GetByID(cli.Context, v.ID) if err != nil { return fmt.Errorf("error fetching Volume: %v", err) } diff --git a/cli/server_detach_from_network.go b/internal/cmds/server_detach_from_network.go similarity index 81% rename from cli/server_detach_from_network.go rename to internal/cmds/server_detach_from_network.go index c8b415f8..fca60763 100644 --- a/cli/server_detach_from_network.go +++ b/internal/cmds/server_detach_from_network.go @@ -1,15 +1,16 @@ -package cli +package cmds import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newServerDetachFromNetworkCommand(cli *CLI) *cobra.Command { +func newServerDetachFromNetworkCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "detach-from-network [FLAGS] SERVER", Short: "Detach a server from a network", @@ -17,8 +18,8 @@ func newServerDetachFromNetworkCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.ServerNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runServerDetachFromNetwork), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runServerDetachFromNetwork), } cmd.Flags().StringP("network", "n", "", "Network (ID or name) (required)") cmd.RegisterFlagCompletionFunc("network", cmpl.SuggestCandidatesF(cli.NetworkNames)) @@ -27,7 +28,7 @@ func newServerDetachFromNetworkCommand(cli *CLI) *cobra.Command { return cmd } -func runServerDetachFromNetwork(cli *CLI, cmd *cobra.Command, args []string) error { +func runServerDetachFromNetwork(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] server, _, err := cli.Client().Server.Get(cli.Context, idOrName) if err != nil { diff --git a/cli/server_detach_iso.go b/internal/cmds/server_detach_iso.go similarity index 74% rename from cli/server_detach_iso.go rename to internal/cmds/server_detach_iso.go index d3333cef..928910af 100644 --- a/cli/server_detach_iso.go +++ b/internal/cmds/server_detach_iso.go @@ -1,13 +1,14 @@ -package cli +package cmds import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/spf13/cobra" ) -func newServerDetachISOCommand(cli *CLI) *cobra.Command { +func newServerDetachISOCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "detach-iso [FLAGS] SERVER", Short: "Detach an ISO from a server", @@ -15,14 +16,14 @@ func newServerDetachISOCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.ServerNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runServerDetachISO), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runServerDetachISO), } return cmd } -func runServerDetachISO(cli *CLI, cmd *cobra.Command, args []string) error { +func runServerDetachISO(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] server, _, err := cli.Client().Server.Get(cli.Context, idOrName) if err != nil { diff --git a/cli/server_disable_backup.go b/internal/cmds/server_disable_backup.go similarity index 73% rename from cli/server_disable_backup.go rename to internal/cmds/server_disable_backup.go index 86b5e9a5..df2ecaf7 100644 --- a/cli/server_disable_backup.go +++ b/internal/cmds/server_disable_backup.go @@ -1,13 +1,14 @@ -package cli +package cmds import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/spf13/cobra" ) -func newServerDisableBackupCommand(cli *CLI) *cobra.Command { +func newServerDisableBackupCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "disable-backup [FLAGS] SERVER", Short: "Disable backup for a server", @@ -15,13 +16,13 @@ func newServerDisableBackupCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.ServerNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runServerDisableBackup), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runServerDisableBackup), } return cmd } -func runServerDisableBackup(cli *CLI, cmd *cobra.Command, args []string) error { +func runServerDisableBackup(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] server, _, err := cli.Client().Server.Get(cli.Context, idOrName) if err != nil { diff --git a/cli/server_disable_protection.go b/internal/cmds/server_disable_protection.go similarity index 81% rename from cli/server_disable_protection.go rename to internal/cmds/server_disable_protection.go index 1cfa2a73..ff33c23c 100644 --- a/cli/server_disable_protection.go +++ b/internal/cmds/server_disable_protection.go @@ -1,15 +1,16 @@ -package cli +package cmds import ( "fmt" "strings" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newServerDisableProtectionCommand(cli *CLI) *cobra.Command { +func newServerDisableProtectionCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "disable-protection [FLAGS] SERVER PROTECTIONLEVEL [PROTECTIONLEVEL...]", Short: "Disable resource protection for a server", @@ -20,13 +21,13 @@ func newServerDisableProtectionCommand(cli *CLI) *cobra.Command { ), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runServerDisableProtection), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runServerDisableProtection), } return cmd } -func runServerDisableProtection(cli *CLI, cmd *cobra.Command, args []string) error { +func runServerDisableProtection(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] server, _, err := cli.Client().Server.Get(cli.Context, idOrName) if err != nil { diff --git a/cli/server_disable_rescue.go b/internal/cmds/server_disable_rescue.go similarity index 73% rename from cli/server_disable_rescue.go rename to internal/cmds/server_disable_rescue.go index b8a72342..220efac9 100644 --- a/cli/server_disable_rescue.go +++ b/internal/cmds/server_disable_rescue.go @@ -1,13 +1,14 @@ -package cli +package cmds import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/spf13/cobra" ) -func newServerDisableRescueCommand(cli *CLI) *cobra.Command { +func newServerDisableRescueCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "disable-rescue [FLAGS] SERVER", Short: "Disable rescue for a server", @@ -15,13 +16,13 @@ func newServerDisableRescueCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.ServerNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runServerDisableRescue), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runServerDisableRescue), } return cmd } -func runServerDisableRescue(cli *CLI, cmd *cobra.Command, args []string) error { +func runServerDisableRescue(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] server, _, err := cli.Client().Server.Get(cli.Context, idOrName) if err != nil { diff --git a/cli/server_enable_backup.go b/internal/cmds/server_enable_backup.go similarity index 80% rename from cli/server_enable_backup.go rename to internal/cmds/server_enable_backup.go index 6222c894..d1e00287 100644 --- a/cli/server_enable_backup.go +++ b/internal/cmds/server_enable_backup.go @@ -1,13 +1,14 @@ -package cli +package cmds import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/spf13/cobra" ) -func newServerEnableBackupCommand(cli *CLI) *cobra.Command { +func newServerEnableBackupCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "enable-backup [FLAGS] SERVER", Short: "Enable backup for a server", @@ -15,8 +16,8 @@ func newServerEnableBackupCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.ServerNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runServerEnableBackup), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runServerEnableBackup), } cmd.Flags().String( "window", "", @@ -24,7 +25,7 @@ func newServerEnableBackupCommand(cli *CLI) *cobra.Command { return cmd } -func runServerEnableBackup(cli *CLI, cmd *cobra.Command, args []string) error { +func runServerEnableBackup(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] server, _, err := cli.Client().Server.Get(cli.Context, idOrName) if err != nil { diff --git a/cli/server_enable_protection.go b/internal/cmds/server_enable_protection.go similarity index 81% rename from cli/server_enable_protection.go rename to internal/cmds/server_enable_protection.go index 13f293a3..197920b8 100644 --- a/cli/server_enable_protection.go +++ b/internal/cmds/server_enable_protection.go @@ -1,15 +1,16 @@ -package cli +package cmds import ( "fmt" "strings" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newServerEnableProtectionCommand(cli *CLI) *cobra.Command { +func newServerEnableProtectionCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "enable-protection [FLAGS] SERVER PROTECTIONLEVEL [PROTECTIONLEVEL...]", Short: "Enable resource protection for a server", @@ -20,13 +21,13 @@ func newServerEnableProtectionCommand(cli *CLI) *cobra.Command { ), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runServerEnableProtection), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runServerEnableProtection), } return cmd } -func runServerEnableProtection(cli *CLI, cmd *cobra.Command, args []string) error { +func runServerEnableProtection(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] server, _, err := cli.Client().Server.Get(cli.Context, idOrName) if err != nil { diff --git a/cli/server_enable_rescue.go b/internal/cmds/server_enable_rescue.go similarity index 85% rename from cli/server_enable_rescue.go rename to internal/cmds/server_enable_rescue.go index 068da97e..a77e3da4 100644 --- a/cli/server_enable_rescue.go +++ b/internal/cmds/server_enable_rescue.go @@ -1,14 +1,15 @@ -package cli +package cmds import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newServerEnableRescueCommand(cli *CLI) *cobra.Command { +func newServerEnableRescueCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "enable-rescue [FLAGS] SERVER", Short: "Enable rescue for a server", @@ -16,8 +17,8 @@ func newServerEnableRescueCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.ServerNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runServerEnableRescue), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runServerEnableRescue), } cmd.Flags().String("type", "linux64", "Rescue type") cmd.RegisterFlagCompletionFunc("type", cmpl.SuggestCandidates("linux64", "linux32", "freebsd64")) @@ -27,7 +28,7 @@ func newServerEnableRescueCommand(cli *CLI) *cobra.Command { return cmd } -func runServerEnableRescue(cli *CLI, cmd *cobra.Command, args []string) error { +func runServerEnableRescue(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] server, _, err := cli.Client().Server.Get(cli.Context, idOrName) if err != nil { diff --git a/cli/server_ip.go b/internal/cmds/server_ip.go similarity index 76% rename from cli/server_ip.go rename to internal/cmds/server_ip.go index 2b375111..4f59d512 100644 --- a/cli/server_ip.go +++ b/internal/cmds/server_ip.go @@ -1,13 +1,14 @@ -package cli +package cmds import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/spf13/cobra" ) -func newServerIPCommand(cli *CLI) *cobra.Command { +func newServerIPCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "ip SERVER FLAGS", Short: "Print a server's IP address", @@ -15,14 +16,14 @@ func newServerIPCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.ServerNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runServerIP), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runServerIP), } cmd.Flags().BoolP("ipv6", "6", false, "Print the first address of the IPv6 public server network") return cmd } -func runServerIP(cli *CLI, cmd *cobra.Command, args []string) error { +func runServerIP(cli *state.State, cmd *cobra.Command, args []string) error { ipv6, err := cmd.Flags().GetBool("ipv6") idOrName := args[0] server, _, err := cli.Client().Server.Get(cli.Context, idOrName) diff --git a/cli/server_list.go b/internal/cmds/server_list.go similarity index 94% rename from cli/server_list.go rename to internal/cmds/server_list.go index a276fde6..5e3ec1bc 100644 --- a/cli/server_list.go +++ b/internal/cmds/server_list.go @@ -1,9 +1,10 @@ -package cli +package cmds import ( "strconv" "strings" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud/schema" "github.com/hetznercloud/hcloud-go/hcloud" @@ -16,7 +17,7 @@ func init() { serverListTableOutput = describeServerListTableOutput(nil) } -func newServerListCommand(cli *CLI) *cobra.Command { +func newServerListCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "list [FLAGS]", Short: "List servers", @@ -26,15 +27,15 @@ func newServerListCommand(cli *CLI) *cobra.Command { ), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runServerList), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runServerList), } addOutputFlag(cmd, outputOptionNoHeader(), outputOptionColumns(serverListTableOutput.Columns()), outputOptionJSON()) cmd.Flags().StringP("selector", "l", "", "Selector to filter by labels") return cmd } -func runServerList(cli *CLI, cmd *cobra.Command, args []string) error { +func runServerList(cli *state.State, cmd *cobra.Command, args []string) error { outOpts := outputFlagsForCommand(cmd) labelSelector, _ := cmd.Flags().GetString("selector") @@ -138,7 +139,7 @@ func runServerList(cli *CLI, cmd *cobra.Command, args []string) error { return nil } -func describeServerListTableOutput(cli *CLI) *tableOutput { +func describeServerListTableOutput(cli *state.State) *tableOutput { return newTableOutput(). AddAllowedFields(hcloud.Server{}). AddFieldOutputFn("ipv4", fieldOutputFn(func(obj interface{}) string { diff --git a/cli/server_metrics.go b/internal/cmds/server_metrics.go similarity index 90% rename from cli/server_metrics.go rename to internal/cmds/server_metrics.go index d6063d0b..68873880 100644 --- a/cli/server_metrics.go +++ b/internal/cmds/server_metrics.go @@ -1,19 +1,21 @@ -package cli +package cmds import ( "encoding/json" "fmt" - "github.com/guptarohit/asciigraph" "sort" "strconv" "time" + "github.com/guptarohit/asciigraph" + "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newServerMetricsCommand(cli *CLI) *cobra.Command { +func newServerMetricsCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "metrics [FLAGS] SERVER", Short: "[ALPHA] Metrics from a Server", @@ -21,8 +23,8 @@ func newServerMetricsCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.ServerNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runServerMetrics), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runServerMetrics), } cmd.Flags().String("type", "", "Type of metrics you want to show") @@ -36,7 +38,7 @@ func newServerMetricsCommand(cli *CLI) *cobra.Command { return cmd } -func runServerMetrics(cli *CLI, cmd *cobra.Command, args []string) error { +func runServerMetrics(cli *state.State, cmd *cobra.Command, args []string) error { outputFlags := outputFlagsForCommand(cmd) idOrName := args[0] diff --git a/cli/server_poweroff.go b/internal/cmds/server_poweroff.go similarity index 74% rename from cli/server_poweroff.go rename to internal/cmds/server_poweroff.go index 162f28b8..c9ff4369 100644 --- a/cli/server_poweroff.go +++ b/internal/cmds/server_poweroff.go @@ -1,13 +1,14 @@ -package cli +package cmds import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/spf13/cobra" ) -func newServerPoweroffCommand(cli *CLI) *cobra.Command { +func newServerPoweroffCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "poweroff [FLAGS] SERVER", Short: "Poweroff a server", @@ -15,13 +16,13 @@ func newServerPoweroffCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.ServerNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runServerPoweroff), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runServerPoweroff), } return cmd } -func runServerPoweroff(cli *CLI, cmd *cobra.Command, args []string) error { +func runServerPoweroff(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] server, _, err := cli.Client().Server.Get(cli.Context, idOrName) if err != nil { diff --git a/cli/server_poweron.go b/internal/cmds/server_poweron.go similarity index 74% rename from cli/server_poweron.go rename to internal/cmds/server_poweron.go index 85e9df8f..60ffc4b4 100644 --- a/cli/server_poweron.go +++ b/internal/cmds/server_poweron.go @@ -1,13 +1,14 @@ -package cli +package cmds import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/spf13/cobra" ) -func newServerPoweronCommand(cli *CLI) *cobra.Command { +func newServerPoweronCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "poweron [FLAGS] SERVER", Short: "Poweron a server", @@ -15,13 +16,13 @@ func newServerPoweronCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.ServerNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runServerPoweron), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runServerPoweron), } return cmd } -func runServerPoweron(cli *CLI, cmd *cobra.Command, args []string) error { +func runServerPoweron(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] server, _, err := cli.Client().Server.Get(cli.Context, idOrName) if err != nil { diff --git a/cli/server_reboot.go b/internal/cmds/server_reboot.go similarity index 74% rename from cli/server_reboot.go rename to internal/cmds/server_reboot.go index 56a4a46f..42d35029 100644 --- a/cli/server_reboot.go +++ b/internal/cmds/server_reboot.go @@ -1,13 +1,14 @@ -package cli +package cmds import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/spf13/cobra" ) -func newServerRebootCommand(cli *CLI) *cobra.Command { +func newServerRebootCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "reboot [FLAGS] SERVER", Short: "Reboot a server", @@ -15,13 +16,13 @@ func newServerRebootCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.ServerNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runServerReboot), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runServerReboot), } return cmd } -func runServerReboot(cli *CLI, cmd *cobra.Command, args []string) error { +func runServerReboot(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] server, _, err := cli.Client().Server.Get(cli.Context, idOrName) if err != nil { diff --git a/cli/server_rebuild.go b/internal/cmds/server_rebuild.go similarity index 82% rename from cli/server_rebuild.go rename to internal/cmds/server_rebuild.go index d331d3d1..207fbe48 100644 --- a/cli/server_rebuild.go +++ b/internal/cmds/server_rebuild.go @@ -1,14 +1,15 @@ -package cli +package cmds import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newServerRebuildCommand(cli *CLI) *cobra.Command { +func newServerRebuildCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "rebuild [FLAGS] SERVER", Short: "Rebuild a server", @@ -16,8 +17,8 @@ func newServerRebuildCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.ServerNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runServerRebuild), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runServerRebuild), } cmd.Flags().String("image", "", "ID or name of image to rebuild from (required)") @@ -27,7 +28,7 @@ func newServerRebuildCommand(cli *CLI) *cobra.Command { return cmd } -func runServerRebuild(cli *CLI, cmd *cobra.Command, args []string) error { +func runServerRebuild(cli *state.State, cmd *cobra.Command, args []string) error { serverIDOrName := args[0] server, _, err := cli.Client().Server.Get(cli.Context, serverIDOrName) if err != nil { diff --git a/cli/server_remove_label.go b/internal/cmds/server_remove_label.go similarity index 86% rename from cli/server_remove_label.go rename to internal/cmds/server_remove_label.go index 31fe200b..efca8ce2 100644 --- a/cli/server_remove_label.go +++ b/internal/cmds/server_remove_label.go @@ -1,15 +1,16 @@ -package cli +package cmds import ( "errors" "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newServerRemoveLabelCommand(cli *CLI) *cobra.Command { +func newServerRemoveLabelCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "remove-label [FLAGS] SERVER LABELKEY", Short: "Remove a label from a server", @@ -25,8 +26,8 @@ func newServerRemoveLabelCommand(cli *CLI) *cobra.Command { ), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: chainRunE(validateServerRemoveLabel, cli.ensureToken), - RunE: cli.wrap(runServerRemoveLabel), + PreRunE: chainRunE(validateServerRemoveLabel, cli.EnsureToken), + RunE: cli.Wrap(runServerRemoveLabel), } cmd.Flags().BoolP("all", "a", false, "Remove all labels") @@ -46,7 +47,7 @@ func validateServerRemoveLabel(cmd *cobra.Command, args []string) error { return nil } -func runServerRemoveLabel(cli *CLI, cmd *cobra.Command, args []string) error { +func runServerRemoveLabel(cli *state.State, cmd *cobra.Command, args []string) error { all, _ := cmd.Flags().GetBool("all") idOrName := args[0] server, _, err := cli.Client().Server.Get(cli.Context, idOrName) diff --git a/cli/server_request_console.go b/internal/cmds/server_request_console.go similarity index 79% rename from cli/server_request_console.go rename to internal/cmds/server_request_console.go index f3e153a7..17112ad4 100644 --- a/cli/server_request_console.go +++ b/internal/cmds/server_request_console.go @@ -1,13 +1,14 @@ -package cli +package cmds import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/spf13/cobra" ) -func newServerRequestConsoleCommand(cli *CLI) *cobra.Command { +func newServerRequestConsoleCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "request-console [FLAGS] SERVER", Short: "Request a WebSocket VNC console for a server", @@ -15,14 +16,14 @@ func newServerRequestConsoleCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.ServerNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runServerRequestConsole), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runServerRequestConsole), } addOutputFlag(cmd, outputOptionJSON()) return cmd } -func runServerRequestConsole(cli *CLI, cmd *cobra.Command, args []string) error { +func runServerRequestConsole(cli *state.State, cmd *cobra.Command, args []string) error { outOpts := outputFlagsForCommand(cmd) idOrName := args[0] server, _, err := cli.Client().Server.Get(cli.Context, idOrName) diff --git a/cli/server_reset.go b/internal/cmds/server_reset.go similarity index 74% rename from cli/server_reset.go rename to internal/cmds/server_reset.go index 0b1e0bc2..8fd45d1b 100644 --- a/cli/server_reset.go +++ b/internal/cmds/server_reset.go @@ -1,13 +1,14 @@ -package cli +package cmds import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/spf13/cobra" ) -func newServerResetCommand(cli *CLI) *cobra.Command { +func newServerResetCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "reset [FLAGS] SERVER", Short: "Reset a server", @@ -15,13 +16,13 @@ func newServerResetCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.ServerNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runServerReset), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runServerReset), } return cmd } -func runServerReset(cli *CLI, cmd *cobra.Command, args []string) error { +func runServerReset(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] server, _, err := cli.Client().Server.Get(cli.Context, idOrName) if err != nil { diff --git a/cli/server_reset_password.go b/internal/cmds/server_reset_password.go similarity index 74% rename from cli/server_reset_password.go rename to internal/cmds/server_reset_password.go index 238a094b..afbb2424 100644 --- a/cli/server_reset_password.go +++ b/internal/cmds/server_reset_password.go @@ -1,13 +1,14 @@ -package cli +package cmds import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/spf13/cobra" ) -func newServerResetPasswordCommand(cli *CLI) *cobra.Command { +func newServerResetPasswordCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "reset-password [FLAGS] SERVER", Short: "Reset the root password of a server", @@ -15,13 +16,13 @@ func newServerResetPasswordCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.ServerNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runServerResetPassword), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runServerResetPassword), } return cmd } -func runServerResetPassword(cli *CLI, cmd *cobra.Command, args []string) error { +func runServerResetPassword(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] server, _, err := cli.Client().Server.Get(cli.Context, idOrName) if err != nil { diff --git a/cli/server_set_rdns.go b/internal/cmds/server_set_rdns.go similarity index 81% rename from cli/server_set_rdns.go rename to internal/cmds/server_set_rdns.go index 220501ba..b42c8123 100644 --- a/cli/server_set_rdns.go +++ b/internal/cmds/server_set_rdns.go @@ -1,14 +1,15 @@ -package cli +package cmds import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newServerSetRDNSCommand(cli *CLI) *cobra.Command { +func newServerSetRDNSCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "set-rdns [FLAGS] SERVER", Short: "Change reverse DNS of a server", @@ -16,8 +17,8 @@ func newServerSetRDNSCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.ServerNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runServerSetRDNS), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runServerSetRDNS), } cmd.Flags().StringP("hostname", "r", "", "Hostname to set as a reverse DNS PTR entry (required)") @@ -28,7 +29,7 @@ func newServerSetRDNSCommand(cli *CLI) *cobra.Command { return cmd } -func runServerSetRDNS(cli *CLI, cmd *cobra.Command, args []string) error { +func runServerSetRDNS(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] server, _, err := cli.Client().Server.Get(cli.Context, idOrName) if err != nil { diff --git a/cli/server_shutdown.go b/internal/cmds/server_shutdown.go similarity index 74% rename from cli/server_shutdown.go rename to internal/cmds/server_shutdown.go index bf4c03b4..c27081c2 100644 --- a/cli/server_shutdown.go +++ b/internal/cmds/server_shutdown.go @@ -1,13 +1,14 @@ -package cli +package cmds import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/spf13/cobra" ) -func newServerShutdownCommand(cli *CLI) *cobra.Command { +func newServerShutdownCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "shutdown [FLAGS] SERVER", Short: "Shutdown a server", @@ -15,13 +16,13 @@ func newServerShutdownCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.ServerNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runServerShutdown), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runServerShutdown), } return cmd } -func runServerShutdown(cli *CLI, cmd *cobra.Command, args []string) error { +func runServerShutdown(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] server, _, err := cli.Client().Server.Get(cli.Context, idOrName) if err != nil { diff --git a/cli/server_ssh.go b/internal/cmds/server_ssh.go similarity index 86% rename from cli/server_ssh.go rename to internal/cmds/server_ssh.go index 07ef62e6..7f6975cb 100644 --- a/cli/server_ssh.go +++ b/internal/cmds/server_ssh.go @@ -1,4 +1,4 @@ -package cli +package cmds import ( "fmt" @@ -8,10 +8,11 @@ import ( "syscall" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/spf13/cobra" ) -func newServerSSHCommand(cli *CLI) *cobra.Command { +func newServerSSHCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "ssh [FLAGS] SERVER [COMMAND...]", Short: "Spawn an SSH connection for the server", @@ -19,8 +20,8 @@ func newServerSSHCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.ServerNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runServerSSH), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runServerSSH), } cmd.Flags().SetInterspersed(false) // To make "hcloud server ssh uname -a" execute "uname -a" cmd.Flags().Bool("ipv6", false, "Establish SSH connection to IPv6 address") @@ -29,7 +30,7 @@ func newServerSSHCommand(cli *CLI) *cobra.Command { return cmd } -func runServerSSH(cli *CLI, cmd *cobra.Command, args []string) error { +func runServerSSH(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] server, _, err := cli.Client().Server.Get(cli.Context, idOrName) if err != nil { diff --git a/cli/server_update.go b/internal/cmds/server_update.go similarity index 77% rename from cli/server_update.go rename to internal/cmds/server_update.go index 5ff3eadf..86742758 100644 --- a/cli/server_update.go +++ b/internal/cmds/server_update.go @@ -1,15 +1,16 @@ -package cli +package cmds import ( "errors" "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newServerUpdateCommand(cli *CLI) *cobra.Command { +func newServerUpdateCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "update [FLAGS] SERVER", Short: "Update a server", @@ -17,8 +18,8 @@ func newServerUpdateCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.ServerNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runServerUpdate), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runServerUpdate), } cmd.Flags().String("name", "", "Server name") @@ -26,7 +27,7 @@ func newServerUpdateCommand(cli *CLI) *cobra.Command { return cmd } -func runServerUpdate(cli *CLI, cmd *cobra.Command, args []string) error { +func runServerUpdate(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] server, _, err := cli.Client().Server.Get(cli.Context, idOrName) if err != nil { diff --git a/cli/servertypes.go b/internal/cmds/servertypes.go similarity index 67% rename from cli/servertypes.go rename to internal/cmds/servertypes.go index f8396b5a..b7901125 100644 --- a/cli/servertypes.go +++ b/internal/cmds/servertypes.go @@ -1,8 +1,11 @@ -package cli +package cmds -import "github.com/spf13/cobra" +import ( + "github.com/hetznercloud/cli/internal/state" + "github.com/spf13/cobra" +) -func newServerTypeCommand(cli *CLI) *cobra.Command { +func NewServerTypeCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "server-type", Short: "Manage server types", diff --git a/cli/servertypes_describe.go b/internal/cmds/servertypes_describe.go similarity index 84% rename from cli/servertypes_describe.go rename to internal/cmds/servertypes_describe.go index 7d494a20..ec71fc79 100644 --- a/cli/servertypes_describe.go +++ b/internal/cmds/servertypes_describe.go @@ -1,15 +1,16 @@ -package cli +package cmds import ( "encoding/json" "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newServerTypeDescribeCommand(cli *CLI) *cobra.Command { +func newServerTypeDescribeCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "describe [FLAGS] SERVERTYPE", Short: "Describe a server type", @@ -17,14 +18,14 @@ func newServerTypeDescribeCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.ServerTypeNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runServerTypeDescribe), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runServerTypeDescribe), } addOutputFlag(cmd, outputOptionJSON(), outputOptionFormat()) return cmd } -func runServerTypeDescribe(cli *CLI, cmd *cobra.Command, args []string) error { +func runServerTypeDescribe(cli *state.State, cmd *cobra.Command, args []string) error { outputFlags := outputFlagsForCommand(cmd) idOrName := args[0] @@ -46,7 +47,7 @@ func runServerTypeDescribe(cli *CLI, cmd *cobra.Command, args []string) error { } } -func serverTypeDescribeText(cli *CLI, serverType *hcloud.ServerType) error { +func serverTypeDescribeText(cli *state.State, serverType *hcloud.ServerType) error { fmt.Printf("ID:\t\t%d\n", serverType.ID) fmt.Printf("Name:\t\t%s\n", serverType.Name) fmt.Printf("Description:\t%s\n", serverType.Description) diff --git a/cli/servertypes_list.go b/internal/cmds/servertypes_list.go similarity index 85% rename from cli/servertypes_list.go rename to internal/cmds/servertypes_list.go index 2fa53d62..bff15491 100644 --- a/cli/servertypes_list.go +++ b/internal/cmds/servertypes_list.go @@ -1,8 +1,9 @@ -package cli +package cmds import ( "fmt" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud/schema" "github.com/hetznercloud/hcloud-go/hcloud" @@ -25,7 +26,7 @@ func init() { })) } -func newServerTypeListCommand(cli *CLI) *cobra.Command { +func newServerTypeListCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "list [FLAGS]", Short: "List server types", @@ -35,14 +36,14 @@ func newServerTypeListCommand(cli *CLI) *cobra.Command { ), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runServerTypeList), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runServerTypeList), } addOutputFlag(cmd, outputOptionNoHeader(), outputOptionColumns(serverTypeListTableOutput.Columns()), outputOptionJSON()) return cmd } -func runServerTypeList(cli *CLI, cmd *cobra.Command, args []string) error { +func runServerTypeList(cli *state.State, cmd *cobra.Command, args []string) error { outOpts := outputFlagsForCommand(cmd) serverTypes, err := cli.Client().ServerType.All(cli.Context) diff --git a/cli/sshkey.go b/internal/cmds/sshkey.go similarity index 75% rename from cli/sshkey.go rename to internal/cmds/sshkey.go index 93e6a496..f447cc72 100644 --- a/cli/sshkey.go +++ b/internal/cmds/sshkey.go @@ -1,8 +1,11 @@ -package cli +package cmds -import "github.com/spf13/cobra" +import ( + "github.com/hetznercloud/cli/internal/state" + "github.com/spf13/cobra" +) -func newSSHKeyCommand(cli *CLI) *cobra.Command { +func NewSSHKeyCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "ssh-key", Short: "Manage SSH keys", diff --git a/cli/sshkey_add_label.go b/internal/cmds/sshkey_add_label.go similarity index 82% rename from cli/sshkey_add_label.go rename to internal/cmds/sshkey_add_label.go index 279c007c..8606f9ed 100644 --- a/cli/sshkey_add_label.go +++ b/internal/cmds/sshkey_add_label.go @@ -1,14 +1,15 @@ -package cli +package cmds import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newSSHKeyAddLabelCommand(cli *CLI) *cobra.Command { +func newSSHKeyAddLabelCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "add-label [FLAGS] SSHKEY LABEL", Short: "Add a label to a SSH key", @@ -16,8 +17,8 @@ func newSSHKeyAddLabelCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.SSHKeyNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: chainRunE(validateSSHKeyAddLabel, cli.ensureToken), - RunE: cli.wrap(runSSHKeyAddLabel), + PreRunE: chainRunE(validateSSHKeyAddLabel, cli.EnsureToken), + RunE: cli.Wrap(runSSHKeyAddLabel), } cmd.Flags().BoolP("overwrite", "o", false, "Overwrite label if it exists already") @@ -33,7 +34,7 @@ func validateSSHKeyAddLabel(cmd *cobra.Command, args []string) error { return nil } -func runSSHKeyAddLabel(cli *CLI, cmd *cobra.Command, args []string) error { +func runSSHKeyAddLabel(cli *state.State, cmd *cobra.Command, args []string) error { overwrite, _ := cmd.Flags().GetBool("overwrite") idOrName := args[0] sshKey, _, err := cli.Client().SSHKey.Get(cli.Context, idOrName) diff --git a/cli/sshkey_create.go b/internal/cmds/sshkey_create.go similarity index 85% rename from cli/sshkey_create.go rename to internal/cmds/sshkey_create.go index b4311f55..8d19b796 100644 --- a/cli/sshkey_create.go +++ b/internal/cmds/sshkey_create.go @@ -1,4 +1,4 @@ -package cli +package cmds import ( "errors" @@ -6,19 +6,20 @@ import ( "io/ioutil" "os" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newSSHKeyCreateCommand(cli *CLI) *cobra.Command { +func newSSHKeyCreateCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "create FLAGS", Short: "Create a SSH key", Args: cobra.NoArgs, TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: chainRunE(validateSSHKeyCreate, cli.ensureToken), - RunE: cli.wrap(runSSHKeyCreate), + PreRunE: chainRunE(validateSSHKeyCreate, cli.EnsureToken), + RunE: cli.Wrap(runSSHKeyCreate), } cmd.Flags().String("name", "", "Key name (required)") cmd.Flags().String("public-key", "", "Public key") @@ -41,7 +42,7 @@ func validateSSHKeyCreate(cmd *cobra.Command, args []string) error { return nil } -func runSSHKeyCreate(cli *CLI, cmd *cobra.Command, args []string) error { +func runSSHKeyCreate(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") diff --git a/cli/sshkey_delete.go b/internal/cmds/sshkey_delete.go similarity index 71% rename from cli/sshkey_delete.go rename to internal/cmds/sshkey_delete.go index 3dfac6d7..51328f71 100644 --- a/cli/sshkey_delete.go +++ b/internal/cmds/sshkey_delete.go @@ -1,13 +1,14 @@ -package cli +package cmds import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/spf13/cobra" ) -func newSSHKeyDeleteCommand(cli *CLI) *cobra.Command { +func newSSHKeyDeleteCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "delete [FLAGS] SSHKEY", Short: "Delete a SSH key", @@ -15,13 +16,13 @@ func newSSHKeyDeleteCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.SSHKeyNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runSSHKeyDelete), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runSSHKeyDelete), } return cmd } -func runSSHKeyDelete(cli *CLI, cmd *cobra.Command, args []string) error { +func runSSHKeyDelete(cli *state.State, cmd *cobra.Command, args []string) error { sshKey, _, err := cli.Client().SSHKey.Get(cli.Context, args[0]) if err != nil { return err diff --git a/cli/sshkey_describe.go b/internal/cmds/sshkey_describe.go similarity index 83% rename from cli/sshkey_describe.go rename to internal/cmds/sshkey_describe.go index d592e7a6..9922b833 100644 --- a/cli/sshkey_describe.go +++ b/internal/cmds/sshkey_describe.go @@ -1,4 +1,4 @@ -package cli +package cmds import ( "encoding/json" @@ -8,11 +8,12 @@ import ( "github.com/dustin/go-humanize" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newSSHKeyDescribeCommand(cli *CLI) *cobra.Command { +func newSSHKeyDescribeCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "describe [FLAGS] SSHKEY", Short: "Describe a SSH key", @@ -20,14 +21,14 @@ func newSSHKeyDescribeCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.SSHKeyNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runSSHKeyDescribe), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runSSHKeyDescribe), } addOutputFlag(cmd, outputOptionJSON(), outputOptionFormat()) return cmd } -func runSSHKeyDescribe(cli *CLI, cmd *cobra.Command, args []string) error { +func runSSHKeyDescribe(cli *state.State, cmd *cobra.Command, args []string) error { outputFlags := outputFlagsForCommand(cmd) sshKey, resp, err := cli.Client().SSHKey.Get(cli.Context, args[0]) @@ -48,7 +49,7 @@ func runSSHKeyDescribe(cli *CLI, cmd *cobra.Command, args []string) error { } } -func sshKeyDescribeText(cli *CLI, sshKey *hcloud.SSHKey) error { +func sshKeyDescribeText(cli *state.State, sshKey *hcloud.SSHKey) error { fmt.Printf("ID:\t\t%d\n", sshKey.ID) fmt.Printf("Name:\t\t%s\n", sshKey.Name) fmt.Printf("Created:\t%s (%s)\n", datetime(sshKey.Created), humanize.Time(sshKey.Created)) diff --git a/cli/sshkey_list.go b/internal/cmds/sshkey_list.go similarity index 87% rename from cli/sshkey_list.go rename to internal/cmds/sshkey_list.go index 00c882f3..15e99465 100644 --- a/cli/sshkey_list.go +++ b/internal/cmds/sshkey_list.go @@ -1,6 +1,7 @@ -package cli +package cmds import ( + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/hetznercloud/hcloud-go/hcloud/schema" "github.com/spf13/cobra" @@ -21,7 +22,7 @@ func init() { })) } -func newSSHKeyListCommand(cli *CLI) *cobra.Command { +func newSSHKeyListCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "list [FLAGS]", Short: "List SSH keys", @@ -31,15 +32,15 @@ func newSSHKeyListCommand(cli *CLI) *cobra.Command { ), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runSSHKeyList), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runSSHKeyList), } addOutputFlag(cmd, outputOptionNoHeader(), outputOptionColumns(sshKeyListTableOutput.Columns()), outputOptionJSON()) cmd.Flags().StringP("selector", "l", "", "Selector to filter by labels") return cmd } -func runSSHKeyList(cli *CLI, cmd *cobra.Command, args []string) error { +func runSSHKeyList(cli *state.State, cmd *cobra.Command, args []string) error { outOpts := outputFlagsForCommand(cmd) labelSelector, _ := cmd.Flags().GetString("selector") diff --git a/cli/sshkey_remove_label.go b/internal/cmds/sshkey_remove_label.go similarity index 86% rename from cli/sshkey_remove_label.go rename to internal/cmds/sshkey_remove_label.go index 737fe352..118b94a7 100644 --- a/cli/sshkey_remove_label.go +++ b/internal/cmds/sshkey_remove_label.go @@ -1,15 +1,16 @@ -package cli +package cmds import ( "errors" "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newSSHKeyRemoveLabelCommand(cli *CLI) *cobra.Command { +func newSSHKeyRemoveLabelCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "remove-label [FLAGS] SSHKEY LABELKEY", Short: "Remove a label from a SSH key", @@ -25,8 +26,8 @@ func newSSHKeyRemoveLabelCommand(cli *CLI) *cobra.Command { ), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: chainRunE(validateSSHKeyRemoveLabel, cli.ensureToken), - RunE: cli.wrap(runSSHKeyRemoveLabel), + PreRunE: chainRunE(validateSSHKeyRemoveLabel, cli.EnsureToken), + RunE: cli.Wrap(runSSHKeyRemoveLabel), } cmd.Flags().BoolP("all", "a", false, "Remove all labels") @@ -46,7 +47,7 @@ func validateSSHKeyRemoveLabel(cmd *cobra.Command, args []string) error { return nil } -func runSSHKeyRemoveLabel(cli *CLI, cmd *cobra.Command, args []string) error { +func runSSHKeyRemoveLabel(cli *state.State, cmd *cobra.Command, args []string) error { all, _ := cmd.Flags().GetBool("all") idOrName := args[0] sshKey, _, err := cli.Client().SSHKey.Get(cli.Context, idOrName) diff --git a/cli/sshkey_update.go b/internal/cmds/sshkey_update.go similarity index 77% rename from cli/sshkey_update.go rename to internal/cmds/sshkey_update.go index dba781d6..fc0bc997 100644 --- a/cli/sshkey_update.go +++ b/internal/cmds/sshkey_update.go @@ -1,15 +1,16 @@ -package cli +package cmds import ( "errors" "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newSSHKeyUpdateCommand(cli *CLI) *cobra.Command { +func newSSHKeyUpdateCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "update [FLAGS] SSHKEY", Short: "Update a SSH key", @@ -17,8 +18,8 @@ func newSSHKeyUpdateCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.SSHKeyNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runSSHKeyUpdate), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runSSHKeyUpdate), } cmd.Flags().String("name", "", "SSH key name") @@ -26,7 +27,7 @@ func newSSHKeyUpdateCommand(cli *CLI) *cobra.Command { return cmd } -func runSSHKeyUpdate(cli *CLI, cmd *cobra.Command, args []string) error { +func runSSHKeyUpdate(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] sshKey, _, err := cli.Client().SSHKey.Get(cli.Context, idOrName) if err != nil { diff --git a/cli/util.go b/internal/cmds/util.go similarity index 99% rename from cli/util.go rename to internal/cmds/util.go index 0787e10e..bebfad87 100644 --- a/cli/util.go +++ b/internal/cmds/util.go @@ -1,4 +1,4 @@ -package cli +package cmds import ( "encoding/json" diff --git a/cli/util_internal_test.go b/internal/cmds/util_internal_test.go similarity index 98% rename from cli/util_internal_test.go rename to internal/cmds/util_internal_test.go index 0e80524e..e165b0ba 100644 --- a/cli/util_internal_test.go +++ b/internal/cmds/util_internal_test.go @@ -1,4 +1,4 @@ -package cli +package cmds import "testing" diff --git a/cli/version.go b/internal/cmds/version.go similarity index 62% rename from cli/version.go rename to internal/cmds/version.go index 239e03a9..ca5e61ca 100644 --- a/cli/version.go +++ b/internal/cmds/version.go @@ -1,26 +1,27 @@ -package cli +package cmds import ( "fmt" + "github.com/hetznercloud/cli/internal/state" "github.com/spf13/cobra" ) // Version is set via compiler flags (see script/build.bash) var Version = "was not built properly" -func newVersionCommand(cli *CLI) *cobra.Command { +func NewVersionCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "version", Short: "Print version information", Args: cobra.NoArgs, DisableFlagsInUseLine: true, - RunE: cli.wrap(runVersion), + RunE: cli.Wrap(runVersion), } return cmd } -func runVersion(cli *CLI, cmd *cobra.Command, args []string) error { +func runVersion(cli *state.State, cmd *cobra.Command, args []string) error { fmt.Printf("hcloud %s\n", Version) return nil } diff --git a/cli/volume.go b/internal/cmds/volume.go similarity index 80% rename from cli/volume.go rename to internal/cmds/volume.go index 0da21744..16e05752 100644 --- a/cli/volume.go +++ b/internal/cmds/volume.go @@ -1,8 +1,11 @@ -package cli +package cmds -import "github.com/spf13/cobra" +import ( + "github.com/hetznercloud/cli/internal/state" + "github.com/spf13/cobra" +) -func newVolumeCommand(cli *CLI) *cobra.Command { +func NewVolumeCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "volume", Short: "Manage Volumes", diff --git a/cli/volume_add_label.go b/internal/cmds/volume_add_label.go similarity index 81% rename from cli/volume_add_label.go rename to internal/cmds/volume_add_label.go index 6566811e..1026ca3b 100644 --- a/cli/volume_add_label.go +++ b/internal/cmds/volume_add_label.go @@ -1,14 +1,15 @@ -package cli +package cmds import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newVolumeAddLabelCommand(cli *CLI) *cobra.Command { +func newVolumeAddLabelCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "add-label [FLAGS] VOLUME LABEL", Short: "Add a label to a volume", @@ -16,8 +17,8 @@ func newVolumeAddLabelCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.VolumeNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: chainRunE(validateVolumeAddLabel, cli.ensureToken), - RunE: cli.wrap(runVolumeAddLabel), + PreRunE: chainRunE(validateVolumeAddLabel, cli.EnsureToken), + RunE: cli.Wrap(runVolumeAddLabel), } cmd.Flags().BoolP("overwrite", "o", false, "Overwrite label if it exists already") @@ -33,7 +34,7 @@ func validateVolumeAddLabel(cmd *cobra.Command, args []string) error { return nil } -func runVolumeAddLabel(cli *CLI, cmd *cobra.Command, args []string) error { +func runVolumeAddLabel(cli *state.State, cmd *cobra.Command, args []string) error { overwrite, _ := cmd.Flags().GetBool("overwrite") volume, _, err := cli.Client().Volume.Get(cli.Context, args[0]) if err != nil { diff --git a/cli/volume_attach.go b/internal/cmds/volume_attach.go similarity index 83% rename from cli/volume_attach.go rename to internal/cmds/volume_attach.go index ce4d73f3..5a9f6aae 100644 --- a/cli/volume_attach.go +++ b/internal/cmds/volume_attach.go @@ -1,14 +1,15 @@ -package cli +package cmds import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newVolumeAttachCommand(cli *CLI) *cobra.Command { +func newVolumeAttachCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "attach [FLAGS] VOLUME", Short: "Attach a volume to a server", @@ -16,8 +17,8 @@ func newVolumeAttachCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.VolumeNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runVolumeAttach), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runVolumeAttach), } cmd.Flags().String("server", "", "Server (ID or name) (required)") cmd.RegisterFlagCompletionFunc("server", cmpl.SuggestCandidatesF(cli.ServerNames)) @@ -27,7 +28,7 @@ func newVolumeAttachCommand(cli *CLI) *cobra.Command { return cmd } -func runVolumeAttach(cli *CLI, cmd *cobra.Command, args []string) error { +func runVolumeAttach(cli *state.State, cmd *cobra.Command, args []string) error { volume, _, err := cli.Client().Volume.Get(cli.Context, args[0]) if err != nil { return err diff --git a/cli/volume_create.go b/internal/cmds/volume_create.go similarity index 89% rename from cli/volume_create.go rename to internal/cmds/volume_create.go index e0189822..1f4cd362 100644 --- a/cli/volume_create.go +++ b/internal/cmds/volume_create.go @@ -1,23 +1,24 @@ -package cli +package cmds import ( "fmt" "strconv" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newVolumeCreateCommand(cli *CLI) *cobra.Command { +func newVolumeCreateCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "create FLAGS", Short: "Create a volume", Args: cobra.NoArgs, TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runVolumeCreate), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runVolumeCreate), } cmd.Flags().String("name", "", "Volume name (required)") cmd.MarkFlagRequired("name") @@ -39,7 +40,7 @@ func newVolumeCreateCommand(cli *CLI) *cobra.Command { return cmd } -func runVolumeCreate(cli *CLI, cmd *cobra.Command, args []string) error { +func runVolumeCreate(cli *state.State, cmd *cobra.Command, args []string) error { name, _ := cmd.Flags().GetString("name") serverIDOrName, _ := cmd.Flags().GetString("server") size, _ := cmd.Flags().GetInt("size") diff --git a/cli/volume_delete.go b/internal/cmds/volume_delete.go similarity index 71% rename from cli/volume_delete.go rename to internal/cmds/volume_delete.go index ebe6bd93..58cdab17 100644 --- a/cli/volume_delete.go +++ b/internal/cmds/volume_delete.go @@ -1,13 +1,14 @@ -package cli +package cmds import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/spf13/cobra" ) -func newVolumeDeleteCommand(cli *CLI) *cobra.Command { +func newVolumeDeleteCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "delete [FLAGS] VOLUME", Short: "Delete a volume", @@ -15,13 +16,13 @@ func newVolumeDeleteCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.VolumeNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runVolumeDelete), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runVolumeDelete), } return cmd } -func runVolumeDelete(cli *CLI, cmd *cobra.Command, args []string) error { +func runVolumeDelete(cli *state.State, cmd *cobra.Command, args []string) error { volume, _, err := cli.Client().Volume.Get(cli.Context, args[0]) if err != nil { return err diff --git a/cli/volume_describe.go b/internal/cmds/volume_describe.go similarity index 88% rename from cli/volume_describe.go rename to internal/cmds/volume_describe.go index 0990dbb5..34cd9052 100644 --- a/cli/volume_describe.go +++ b/internal/cmds/volume_describe.go @@ -1,4 +1,4 @@ -package cli +package cmds import ( "encoding/json" @@ -6,11 +6,12 @@ import ( humanize "github.com/dustin/go-humanize" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newVolumeDescribeCommand(cli *CLI) *cobra.Command { +func newVolumeDescribeCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "describe [FLAGS] VOLUME", Short: "Describe a volume", @@ -18,14 +19,14 @@ func newVolumeDescribeCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.VolumeNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runVolumeDescribe), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runVolumeDescribe), } addOutputFlag(cmd, outputOptionJSON(), outputOptionFormat()) return cmd } -func runVolumeDescribe(cli *CLI, cmd *cobra.Command, args []string) error { +func runVolumeDescribe(cli *state.State, cmd *cobra.Command, args []string) error { outputFlags := outputFlagsForCommand(cmd) volume, resp, err := cli.Client().Volume.Get(cli.Context, args[0]) @@ -46,7 +47,7 @@ func runVolumeDescribe(cli *CLI, cmd *cobra.Command, args []string) error { } } -func volumeDescribeText(cli *CLI, volume *hcloud.Volume) error { +func volumeDescribeText(cli *state.State, volume *hcloud.Volume) error { fmt.Printf("ID:\t\t%d\n", volume.ID) fmt.Printf("Name:\t\t%s\n", volume.Name) fmt.Printf("Created:\t%s (%s)\n", datetime(volume.Created), humanize.Time(volume.Created)) diff --git a/cli/volume_detach.go b/internal/cmds/volume_detach.go similarity index 73% rename from cli/volume_detach.go rename to internal/cmds/volume_detach.go index abd2d5c0..87a712fa 100644 --- a/cli/volume_detach.go +++ b/internal/cmds/volume_detach.go @@ -1,13 +1,14 @@ -package cli +package cmds import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/spf13/cobra" ) -func newVolumeDetachCommand(cli *CLI) *cobra.Command { +func newVolumeDetachCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "detach [FLAGS] VOLUME", Short: "Detach a volume", @@ -15,13 +16,13 @@ func newVolumeDetachCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.VolumeNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runVolumeDetach), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runVolumeDetach), } return cmd } -func runVolumeDetach(cli *CLI, cmd *cobra.Command, args []string) error { +func runVolumeDetach(cli *state.State, cmd *cobra.Command, args []string) error { volume, _, err := cli.Client().Volume.Get(cli.Context, args[0]) if err != nil { return err diff --git a/cli/volume_disable_protection.go b/internal/cmds/volume_disable_protection.go similarity index 80% rename from cli/volume_disable_protection.go rename to internal/cmds/volume_disable_protection.go index e07e4ebc..cc42185c 100644 --- a/cli/volume_disable_protection.go +++ b/internal/cmds/volume_disable_protection.go @@ -1,15 +1,16 @@ -package cli +package cmds import ( "fmt" "strings" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newVolumeDisableProtectionCommand(cli *CLI) *cobra.Command { +func newVolumeDisableProtectionCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "disable-protection [FLAGS] VOLUME PROTECTIONLEVEL [PROTECTIONLEVEL...]", Short: "Disable resource protection for a volume", @@ -20,13 +21,13 @@ func newVolumeDisableProtectionCommand(cli *CLI) *cobra.Command { ), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runVolumeDisableProtection), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runVolumeDisableProtection), } return cmd } -func runVolumeDisableProtection(cli *CLI, cmd *cobra.Command, args []string) error { +func runVolumeDisableProtection(cli *state.State, cmd *cobra.Command, args []string) error { volume, _, err := cli.Client().Volume.Get(cli.Context, args[0]) if err != nil { return err diff --git a/cli/volume_enable_protection.go b/internal/cmds/volume_enable_protection.go similarity index 80% rename from cli/volume_enable_protection.go rename to internal/cmds/volume_enable_protection.go index 12f1cfc9..40532a56 100644 --- a/cli/volume_enable_protection.go +++ b/internal/cmds/volume_enable_protection.go @@ -1,15 +1,16 @@ -package cli +package cmds import ( "fmt" "strings" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newVolumeEnableProtectionCommand(cli *CLI) *cobra.Command { +func newVolumeEnableProtectionCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "enable-protection [FLAGS] VOLUME PROTECTIONLEVEL [PROTECTIONLEVEL...]", Short: "Enable resource protection for a volume", @@ -20,13 +21,13 @@ func newVolumeEnableProtectionCommand(cli *CLI) *cobra.Command { ), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runVolumeEnableProtection), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runVolumeEnableProtection), } return cmd } -func runVolumeEnableProtection(cli *CLI, cmd *cobra.Command, args []string) error { +func runVolumeEnableProtection(cli *state.State, cmd *cobra.Command, args []string) error { volume, _, err := cli.Client().Volume.Get(cli.Context, args[0]) if err != nil { return err diff --git a/cli/volume_list.go b/internal/cmds/volume_list.go similarity index 90% rename from cli/volume_list.go rename to internal/cmds/volume_list.go index 93b3e21a..8418a466 100644 --- a/cli/volume_list.go +++ b/internal/cmds/volume_list.go @@ -1,8 +1,9 @@ -package cli +package cmds import ( "strings" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud/schema" humanize "github.com/dustin/go-humanize" @@ -16,7 +17,7 @@ func init() { volumeListTableOutput = describeVolumeListTableOutput(nil) } -func newVolumeListCommand(cli *CLI) *cobra.Command { +func newVolumeListCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "list [FLAGS]", Short: "List volumes", @@ -26,15 +27,15 @@ func newVolumeListCommand(cli *CLI) *cobra.Command { ), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runVolumeList), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runVolumeList), } addOutputFlag(cmd, outputOptionNoHeader(), outputOptionColumns(volumeListTableOutput.Columns()), outputOptionJSON()) cmd.Flags().StringP("selector", "l", "", "Selector to filter by labels") return cmd } -func runVolumeList(cli *CLI, cmd *cobra.Command, args []string) error { +func runVolumeList(cli *state.State, cmd *cobra.Command, args []string) error { outOpts := outputFlagsForCommand(cmd) labelSelector, _ := cmd.Flags().GetString("selector") @@ -90,7 +91,7 @@ func runVolumeList(cli *CLI, cmd *cobra.Command, args []string) error { return nil } -func describeVolumeListTableOutput(cli *CLI) *tableOutput { +func describeVolumeListTableOutput(cli *state.State) *tableOutput { return newTableOutput(). AddAllowedFields(hcloud.Volume{}). AddFieldOutputFn("server", fieldOutputFn(func(obj interface{}) string { diff --git a/cli/volume_remove_label.go b/internal/cmds/volume_remove_label.go similarity index 86% rename from cli/volume_remove_label.go rename to internal/cmds/volume_remove_label.go index 0c857a05..9d351595 100644 --- a/cli/volume_remove_label.go +++ b/internal/cmds/volume_remove_label.go @@ -1,15 +1,16 @@ -package cli +package cmds import ( "errors" "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newVolumeRemoveLabelCommand(cli *CLI) *cobra.Command { +func newVolumeRemoveLabelCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "remove-label [FLAGS] VOLUME LABELKEY", Short: "Remove a label from a volume", @@ -25,8 +26,8 @@ func newVolumeRemoveLabelCommand(cli *CLI) *cobra.Command { ), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: chainRunE(validateVolumeRemoveLabel, cli.ensureToken), - RunE: cli.wrap(runVolumeRemoveLabel), + PreRunE: chainRunE(validateVolumeRemoveLabel, cli.EnsureToken), + RunE: cli.Wrap(runVolumeRemoveLabel), } cmd.Flags().BoolP("all", "a", false, "Remove all labels") @@ -46,7 +47,7 @@ func validateVolumeRemoveLabel(cmd *cobra.Command, args []string) error { return nil } -func runVolumeRemoveLabel(cli *CLI, cmd *cobra.Command, args []string) error { +func runVolumeRemoveLabel(cli *state.State, cmd *cobra.Command, args []string) error { all, _ := cmd.Flags().GetBool("all") volume, _, err := cli.Client().Volume.Get(cli.Context, args[0]) if err != nil { diff --git a/cli/volume_resize.go b/internal/cmds/volume_resize.go similarity index 78% rename from cli/volume_resize.go rename to internal/cmds/volume_resize.go index 53d8d370..bf0254d8 100644 --- a/cli/volume_resize.go +++ b/internal/cmds/volume_resize.go @@ -1,13 +1,14 @@ -package cli +package cmds import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/spf13/cobra" ) -func newVolumeResizeCommand(cli *CLI) *cobra.Command { +func newVolumeResizeCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "resize [FLAGS] VOLUME", Short: "Resize a volume", @@ -15,15 +16,15 @@ func newVolumeResizeCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.VolumeNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runVolumeResize), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runVolumeResize), } cmd.Flags().Int("size", 0, "New size (GB) of the volume (required)") cmd.MarkFlagRequired("size") return cmd } -func runVolumeResize(cli *CLI, cmd *cobra.Command, args []string) error { +func runVolumeResize(cli *state.State, cmd *cobra.Command, args []string) error { volume, _, err := cli.Client().Volume.Get(cli.Context, args[0]) if err != nil { return err diff --git a/cli/volume_update.go b/internal/cmds/volume_update.go similarity index 77% rename from cli/volume_update.go rename to internal/cmds/volume_update.go index b787fb80..b7c68ada 100644 --- a/cli/volume_update.go +++ b/internal/cmds/volume_update.go @@ -1,15 +1,16 @@ -package cli +package cmds import ( "errors" "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" ) -func newVolumeUpdateCommand(cli *CLI) *cobra.Command { +func newVolumeUpdateCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "update [FLAGS] VOLUME", Short: "Update a volume", @@ -17,8 +18,8 @@ func newVolumeUpdateCommand(cli *CLI) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.VolumeNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureToken, - RunE: cli.wrap(runVolumeUpdate), + PreRunE: cli.EnsureToken, + RunE: cli.Wrap(runVolumeUpdate), } cmd.Flags().String("name", "", "Volume name") @@ -26,7 +27,7 @@ func newVolumeUpdateCommand(cli *CLI) *cobra.Command { return cmd } -func runVolumeUpdate(cli *CLI, cmd *cobra.Command, args []string) error { +func runVolumeUpdate(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] volume, _, err := cli.Client().Volume.Get(cli.Context, idOrName) if err != nil { diff --git a/cli/config.go b/internal/state/config.go similarity index 99% rename from cli/config.go rename to internal/state/config.go index a87edca1..db982e65 100644 --- a/cli/config.go +++ b/internal/state/config.go @@ -1,4 +1,4 @@ -package cli +package state import ( "fmt" diff --git a/cli/config_unix.go b/internal/state/config_unix.go similarity index 94% rename from cli/config_unix.go rename to internal/state/config_unix.go index 2cd6969b..7c0d1bcb 100644 --- a/cli/config_unix.go +++ b/internal/state/config_unix.go @@ -1,6 +1,6 @@ // +build !windows -package cli +package state import ( "os/user" diff --git a/cli/config_windows.go b/internal/state/config_windows.go similarity index 92% rename from cli/config_windows.go rename to internal/state/config_windows.go index 7e405abb..b5a1ffe2 100644 --- a/cli/config_windows.go +++ b/internal/state/config_windows.go @@ -1,6 +1,6 @@ // +build windows -package cli +package state import ( "os" diff --git a/cli/cli.go b/internal/state/helpers.go similarity index 65% rename from cli/cli.go rename to internal/state/helpers.go index 0a6c7d79..e0bced42 100644 --- a/cli/cli.go +++ b/internal/state/helpers.go @@ -1,13 +1,11 @@ -package cli +package state import ( "context" "errors" "fmt" - "io/ioutil" - "log" "os" - "path/filepath" + "time" "github.com/cheggaaa/pb/v3" "github.com/hetznercloud/cli/internal/hcapi" @@ -16,126 +14,18 @@ import ( "golang.org/x/crypto/ssh/terminal" ) -var ErrConfigPathUnknown = errors.New("config file path unknown") - const ( progressCircleTpl = `{{ cycle . " . " " . " " ." " . " }}` progressBarTpl = `{{ etime . }} {{ bar . "" "=" }} {{ percent . }}` ) -type CLI struct { - Token string - Endpoint string - Context context.Context - Config *Config - ConfigPath string - Debug bool - DebugFilePath string - - RootCommand *cobra.Command - - client *hcloud.Client - isoClient *hcapi.ISOClient - imageClient *hcapi.ImageClient - locationClient *hcapi.LocationClient - dataCenterClient *hcapi.DataCenterClient - sshKeyClient *hcapi.SSHKeyClient - volumeClient *hcapi.VolumeClient - certificateClient *hcapi.CertificateClient - floatingIPClient *hcapi.FloatingIPClient - networkClient *hcapi.NetworkClient - loadBalancerClient *hcapi.LoadBalancerClient - serverClient *hcapi.ServerClient -} - -func NewCLI() *CLI { - cli := &CLI{ - Context: context.Background(), - Config: &Config{}, - ConfigPath: DefaultConfigPath, - } - if s := os.Getenv("HCLOUD_CONFIG"); s != "" { - cli.ConfigPath = s - } - cli.RootCommand = NewRootCommand(cli) - return cli -} - -func (c *CLI) ReadEnv() { - if s := os.Getenv("HCLOUD_TOKEN"); s != "" { - c.Token = s - } - if s := os.Getenv("HCLOUD_ENDPOINT"); s != "" { - c.Endpoint = s - } - if s := os.Getenv("HCLOUD_DEBUG"); s != "" { - c.Debug = true - } - if s := os.Getenv("HCLOUD_DEBUG_FILE"); s != "" { - c.DebugFilePath = s - } - if s := os.Getenv("HCLOUD_CONTEXT"); s != "" && c.Config != nil { - if cfgCtx := c.Config.ContextByName(s); cfgCtx != nil { - c.Config.ActiveContext = cfgCtx - c.Token = cfgCtx.Token - } else { - log.Printf("warning: context %q specified in HCLOUD_CONTEXT does not exist\n", s) - } - } -} - -func (c *CLI) ReadConfig() error { - if c.ConfigPath == "" { - return ErrConfigPathUnknown - } - - data, err := ioutil.ReadFile(c.ConfigPath) - if err != nil { - return err - } - - if err = UnmarshalConfig(c.Config, data); err != nil { - return err - } - - if c.Config.ActiveContext != nil { - c.Token = c.Config.ActiveContext.Token - } - if c.Config.Endpoint != "" { - c.Endpoint = c.Config.Endpoint - } - - return nil -} - -func (c *CLI) WriteConfig() error { - if c.ConfigPath == "" { - return ErrConfigPathUnknown - } - if c.Config == nil { - return nil - } - - data, err := MarshalConfig(c.Config) - if err != nil { - return err - } - if err := os.MkdirAll(filepath.Dir(c.ConfigPath), 0777); err != nil { - return err - } - if err := ioutil.WriteFile(c.ConfigPath, data, 0600); err != nil { - return err - } - return nil -} - -func (c *CLI) wrap(f func(*CLI, *cobra.Command, []string) error) func(*cobra.Command, []string) error { +func (c *State) Wrap(f func(*State, *cobra.Command, []string) error) func(*cobra.Command, []string) error { return func(cmd *cobra.Command, args []string) error { return f(c, cmd, args) } } -func (c *CLI) Client() *hcloud.Client { +func (c *State) Client() *hcloud.Client { if c.client == nil { opts := []hcloud.ClientOption{ hcloud.WithToken(c.Token), @@ -152,7 +42,9 @@ func (c *CLI) Client() *hcloud.Client { opts = append(opts, hcloud.WithDebugWriter(writer)) } } - pollInterval, _ := c.RootCommand.PersistentFlags().GetDuration("poll-interval") + // TODO Somehow pass here + // pollInterval, _ := c.RootCommand.PersistentFlags().GetDuration("poll-interval") + pollInterval := 500 * time.Millisecond if pollInterval > 0 { opts = append(opts, hcloud.WithPollInterval(pollInterval)) } @@ -161,7 +53,7 @@ func (c *CLI) Client() *hcloud.Client { return c.client } -func (c *CLI) CertificateNames() []string { +func (c *State) CertificateNames() []string { if c.certificateClient == nil { client := c.Client() c.certificateClient = &hcapi.CertificateClient{CertificateClient: &client.Certificate} @@ -169,7 +61,7 @@ func (c *CLI) CertificateNames() []string { return c.certificateClient.CertificateNames() } -func (c *CLI) CertificateLabelKeys(idOrName string) []string { +func (c *State) CertificateLabelKeys(idOrName string) []string { if c.certificateClient == nil { client := c.Client() c.certificateClient = &hcapi.CertificateClient{CertificateClient: &client.Certificate} @@ -177,7 +69,7 @@ func (c *CLI) CertificateLabelKeys(idOrName string) []string { return c.certificateClient.CertificateLabelKeys(idOrName) } -func (c *CLI) FloatingIPNames() []string { +func (c *State) FloatingIPNames() []string { if c.floatingIPClient == nil { client := c.Client() c.floatingIPClient = &hcapi.FloatingIPClient{FloatingIPClient: &client.FloatingIP} @@ -185,7 +77,7 @@ func (c *CLI) FloatingIPNames() []string { return c.floatingIPClient.FloatingIPNames() } -func (c *CLI) FloatingIPLabelKeys(idOrName string) []string { +func (c *State) FloatingIPLabelKeys(idOrName string) []string { if c.floatingIPClient == nil { client := c.Client() c.floatingIPClient = &hcapi.FloatingIPClient{FloatingIPClient: &client.FloatingIP} @@ -193,7 +85,7 @@ func (c *CLI) FloatingIPLabelKeys(idOrName string) []string { return c.floatingIPClient.FloatingIPLabelKeys(idOrName) } -func (c *CLI) ISONames() []string { +func (c *State) ISONames() []string { if c.isoClient == nil { client := c.Client() c.isoClient = &hcapi.ISOClient{ISOClient: &client.ISO} @@ -201,7 +93,7 @@ func (c *CLI) ISONames() []string { return c.isoClient.ISONames() } -func (c *CLI) ImageNames() []string { +func (c *State) ImageNames() []string { if c.imageClient == nil { client := c.Client() c.imageClient = &hcapi.ImageClient{ImageClient: &client.Image} @@ -209,7 +101,7 @@ func (c *CLI) ImageNames() []string { return c.imageClient.ImageNames() } -func (c *CLI) ImageLabelKeys(idOrName string) []string { +func (c *State) ImageLabelKeys(idOrName string) []string { if c.imageClient == nil { client := c.Client() c.imageClient = &hcapi.ImageClient{ImageClient: &client.Image} @@ -217,7 +109,7 @@ func (c *CLI) ImageLabelKeys(idOrName string) []string { return c.imageClient.ImageLabelKeys(idOrName) } -func (c *CLI) LocationNames() []string { +func (c *State) LocationNames() []string { if c.locationClient == nil { client := c.Client() c.locationClient = &hcapi.LocationClient{LocationClient: &client.Location} @@ -225,7 +117,7 @@ func (c *CLI) LocationNames() []string { return c.locationClient.LocationNames() } -func (c *CLI) DataCenterNames() []string { +func (c *State) DataCenterNames() []string { if c.dataCenterClient == nil { client := c.Client() c.dataCenterClient = &hcapi.DataCenterClient{DatacenterClient: &client.Datacenter} @@ -233,7 +125,7 @@ func (c *CLI) DataCenterNames() []string { return c.dataCenterClient.DataCenterNames() } -func (c *CLI) SSHKeyNames() []string { +func (c *State) SSHKeyNames() []string { if c.sshKeyClient == nil { client := c.Client() c.sshKeyClient = &hcapi.SSHKeyClient{SSHKeyClient: &client.SSHKey} @@ -241,7 +133,7 @@ func (c *CLI) SSHKeyNames() []string { return c.sshKeyClient.SSHKeyNames() } -func (c *CLI) SSHKeyLabelKeys(idOrName string) []string { +func (c *State) SSHKeyLabelKeys(idOrName string) []string { if c.sshKeyClient == nil { client := c.Client() c.sshKeyClient = &hcapi.SSHKeyClient{SSHKeyClient: &client.SSHKey} @@ -249,7 +141,7 @@ func (c *CLI) SSHKeyLabelKeys(idOrName string) []string { return c.sshKeyClient.SSHKeyLabelKeys(idOrName) } -func (c *CLI) VolumeNames() []string { +func (c *State) VolumeNames() []string { if c.volumeClient == nil { client := c.Client() c.volumeClient = &hcapi.VolumeClient{VolumeClient: &client.Volume} @@ -257,7 +149,7 @@ func (c *CLI) VolumeNames() []string { return c.volumeClient.VolumeNames() } -func (c *CLI) VolumeLabelKeys(idOrName string) []string { +func (c *State) VolumeLabelKeys(idOrName string) []string { if c.volumeClient == nil { client := c.Client() c.volumeClient = &hcapi.VolumeClient{VolumeClient: &client.Volume} @@ -266,11 +158,11 @@ func (c *CLI) VolumeLabelKeys(idOrName string) []string { } // Terminal returns whether the CLI is run in a terminal. -func (c *CLI) Terminal() bool { +func (c *State) Terminal() bool { return terminal.IsTerminal(int(os.Stdout.Fd())) } -func (c *CLI) ActionProgress(ctx context.Context, action *hcloud.Action) error { +func (c *State) ActionProgress(ctx context.Context, action *hcloud.Action) error { progressCh, errCh := c.Client().Action.WatchProgress(ctx, action) if c.Terminal() { @@ -296,14 +188,14 @@ func (c *CLI) ActionProgress(ctx context.Context, action *hcloud.Action) error { } } -func (c *CLI) ensureToken(cmd *cobra.Command, args []string) error { +func (c *State) EnsureToken(cmd *cobra.Command, args []string) error { if c.Token == "" { return errors.New("no active context or token (see `hcloud context --help`)") } return nil } -func (c *CLI) WaitForActions(ctx context.Context, actions []*hcloud.Action) error { +func (c *State) WaitForActions(ctx context.Context, actions []*hcloud.Action) error { const ( done = "done" failed = "failed" @@ -354,7 +246,7 @@ func (c *CLI) WaitForActions(ctx context.Context, actions []*hcloud.Action) erro return nil } -func (c *CLI) ServerTypeNames() []string { +func (c *State) ServerTypeNames() []string { if c.serverClient == nil { client := c.Client() c.serverClient = &hcapi.ServerClient{ @@ -365,7 +257,7 @@ func (c *CLI) ServerTypeNames() []string { return c.serverClient.ServerTypeNames() } -func (c *CLI) ServerNames() []string { +func (c *State) ServerNames() []string { if c.serverClient == nil { client := c.Client() c.serverClient = &hcapi.ServerClient{ @@ -376,7 +268,7 @@ func (c *CLI) ServerNames() []string { return c.serverClient.ServerNames() } -func (c *CLI) ServerLabelKeys(idOrName string) []string { +func (c *State) ServerLabelKeys(idOrName string) []string { if c.serverClient == nil { client := c.Client() c.serverClient = &hcapi.ServerClient{ @@ -387,7 +279,7 @@ func (c *CLI) ServerLabelKeys(idOrName string) []string { return c.serverClient.ServerLabelKeys(idOrName) } -func (c *CLI) ServerName(id int) string { +func (c *State) ServerName(id int) string { if c.serverClient == nil { client := c.Client() c.serverClient = &hcapi.ServerClient{ @@ -398,7 +290,7 @@ func (c *CLI) ServerName(id int) string { return c.serverClient.ServerName(id) } -func (c *CLI) NetworkNames() []string { +func (c *State) NetworkNames() []string { if c.networkClient == nil { client := c.Client() c.networkClient = &hcapi.NetworkClient{NetworkClient: &client.Network} @@ -406,7 +298,7 @@ func (c *CLI) NetworkNames() []string { return c.networkClient.NetworkNames() } -func (c *CLI) NetworkName(id int) string { +func (c *State) NetworkName(id int) string { if c.networkClient == nil { client := c.Client() c.networkClient = &hcapi.NetworkClient{NetworkClient: &client.Network} @@ -414,7 +306,7 @@ func (c *CLI) NetworkName(id int) string { return c.networkClient.NetworkName(id) } -func (c *CLI) NetworkLabelKeys(idOrName string) []string { +func (c *State) NetworkLabelKeys(idOrName string) []string { if c.networkClient == nil { client := c.Client() c.networkClient = &hcapi.NetworkClient{NetworkClient: &client.Network} @@ -422,7 +314,7 @@ func (c *CLI) NetworkLabelKeys(idOrName string) []string { return c.networkClient.NetworkLabelKeys(idOrName) } -func (c *CLI) LoadBalancerNames() []string { +func (c *State) LoadBalancerNames() []string { if c.loadBalancerClient == nil { client := c.Client() c.loadBalancerClient = &hcapi.LoadBalancerClient{ @@ -433,7 +325,7 @@ func (c *CLI) LoadBalancerNames() []string { return c.loadBalancerClient.LoadBalancerNames() } -func (c *CLI) LoadBalancerLabelKeys(idOrName string) []string { +func (c *State) LoadBalancerLabelKeys(idOrName string) []string { if c.loadBalancerClient == nil { client := c.Client() c.loadBalancerClient = &hcapi.LoadBalancerClient{ @@ -444,7 +336,7 @@ func (c *CLI) LoadBalancerLabelKeys(idOrName string) []string { return c.loadBalancerClient.LoadBalancerLabelKeys(idOrName) } -func (c *CLI) LoadBalancerTypeNames() []string { +func (c *State) LoadBalancerTypeNames() []string { if c.loadBalancerClient == nil { client := c.Client() c.loadBalancerClient = &hcapi.LoadBalancerClient{ diff --git a/internal/state/state.go b/internal/state/state.go new file mode 100644 index 00000000..6db5d27d --- /dev/null +++ b/internal/state/state.go @@ -0,0 +1,118 @@ +package state + +import ( + "context" + "errors" + "io/ioutil" + "log" + "os" + "path/filepath" + + "github.com/hetznercloud/cli/internal/hcapi" + "github.com/hetznercloud/hcloud-go/hcloud" +) + +type State struct { + Token string + Endpoint string + Context context.Context + Config *Config + ConfigPath string + Debug bool + DebugFilePath string + + client *hcloud.Client + isoClient *hcapi.ISOClient + imageClient *hcapi.ImageClient + locationClient *hcapi.LocationClient + dataCenterClient *hcapi.DataCenterClient + sshKeyClient *hcapi.SSHKeyClient + volumeClient *hcapi.VolumeClient + certificateClient *hcapi.CertificateClient + floatingIPClient *hcapi.FloatingIPClient + networkClient *hcapi.NetworkClient + loadBalancerClient *hcapi.LoadBalancerClient + serverClient *hcapi.ServerClient +} + +func New() *State { + s := &State{ + Context: context.Background(), + Config: &Config{}, + ConfigPath: DefaultConfigPath, + } + if p := os.Getenv("HCLOUD_CONFIG"); p != "" { + s.ConfigPath = p + } + return s +} + +var ErrConfigPathUnknown = errors.New("config file path unknown") + +func (c *State) ReadEnv() { + if s := os.Getenv("HCLOUD_TOKEN"); s != "" { + c.Token = s + } + if s := os.Getenv("HCLOUD_ENDPOINT"); s != "" { + c.Endpoint = s + } + if s := os.Getenv("HCLOUD_DEBUG"); s != "" { + c.Debug = true + } + if s := os.Getenv("HCLOUD_DEBUG_FILE"); s != "" { + c.DebugFilePath = s + } + if s := os.Getenv("HCLOUD_CONTEXT"); s != "" && c.Config != nil { + if cfgCtx := c.Config.ContextByName(s); cfgCtx != nil { + c.Config.ActiveContext = cfgCtx + c.Token = cfgCtx.Token + } else { + log.Printf("warning: context %q specified in HCLOUD_CONTEXT does not exist\n", s) + } + } +} + +func (c *State) ReadConfig() error { + if c.ConfigPath == "" { + return ErrConfigPathUnknown + } + + data, err := ioutil.ReadFile(c.ConfigPath) + if err != nil { + return err + } + + if err = UnmarshalConfig(c.Config, data); err != nil { + return err + } + + if c.Config.ActiveContext != nil { + c.Token = c.Config.ActiveContext.Token + } + if c.Config.Endpoint != "" { + c.Endpoint = c.Config.Endpoint + } + + return nil +} + +func (c *State) WriteConfig() error { + if c.ConfigPath == "" { + return ErrConfigPathUnknown + } + if c.Config == nil { + return nil + } + + data, err := MarshalConfig(c.Config) + if err != nil { + return err + } + if err := os.MkdirAll(filepath.Dir(c.ConfigPath), 0777); err != nil { + return err + } + if err := ioutil.WriteFile(c.ConfigPath, data, 0600); err != nil { + return err + } + return nil +} diff --git a/internal/state/version.go b/internal/state/version.go new file mode 100644 index 00000000..2c96a9cb --- /dev/null +++ b/internal/state/version.go @@ -0,0 +1,4 @@ +package state + +// Version is set via compiler flags (see script/build.bash) +var Version = "TODO DUPLICATE, see cli package" From efaced5ef9573dbac91a95f0cd4f48c8642e2082 Mon Sep 17 00:00:00 2001 From: Michael Frister Date: Mon, 11 Jan 2021 08:49:57 +0100 Subject: [PATCH 2/9] Refactor: Move cmds utils to separate package Preparation for splitting up cmds into multiple packages. --- internal/{cmds => cmd/util}/util.go | 36 +++++++++---------- .../{cmds => cmd/util}/util_internal_test.go | 4 +-- internal/cmds/certificate_add_label.go | 7 ++-- internal/cmds/certificate_describe.go | 15 ++++---- internal/cmds/certificate_list.go | 13 +++---- internal/cmds/certificate_remove_label.go | 3 +- internal/cmds/context_list.go | 3 +- internal/cmds/datacenter_describe.go | 9 ++--- internal/cmds/datacenter_list.go | 7 ++-- internal/cmds/floatingip_add_label.go | 7 ++-- internal/cmds/floatingip_create.go | 3 +- internal/cmds/floatingip_describe.go | 17 ++++----- internal/cmds/floatingip_list.go | 15 ++++---- internal/cmds/floatingip_remove_label.go | 3 +- internal/cmds/image_add_label.go | 7 ++-- internal/cmds/image_describe.go | 23 ++++++------ internal/cmds/image_list.go | 21 +++++------ internal/cmds/image_remove_label.go | 3 +- internal/cmds/iso_describe.go | 9 ++--- internal/cmds/iso_list.go | 7 ++-- internal/cmds/load_balancer_add_label.go | 7 ++-- internal/cmds/load_balancer_add_service.go | 3 +- internal/cmds/load_balancer_add_target.go | 3 +- internal/cmds/load_balancer_delete_service.go | 3 +- internal/cmds/load_balancer_describe.go | 23 ++++++------ internal/cmds/load_balancer_list.go | 13 +++---- internal/cmds/load_balancer_metrics.go | 3 +- internal/cmds/load_balancer_remove_label.go | 3 +- internal/cmds/load_balancer_remove_target.go | 3 +- internal/cmds/load_balancer_type_describe.go | 9 ++--- internal/cmds/load_balancer_type_list.go | 7 ++-- internal/cmds/location_describe.go | 9 ++--- internal/cmds/location_list.go | 7 ++-- internal/cmds/network_add_label.go | 7 ++-- internal/cmds/network_describe.go | 7 ++-- internal/cmds/network_list.go | 9 ++--- internal/cmds/network_remove_label.go | 3 +- internal/cmds/output.go | 9 ++--- internal/cmds/server_add_label.go | 7 ++-- internal/cmds/server_create_image.go | 3 +- internal/cmds/server_describe.go | 33 ++++++++--------- internal/cmds/server_list.go | 19 +++++----- internal/cmds/server_metrics.go | 3 +- internal/cmds/server_remove_label.go | 3 +- internal/cmds/server_request_console.go | 3 +- internal/cmds/servertypes_describe.go | 9 ++--- internal/cmds/servertypes_list.go | 7 ++-- internal/cmds/sshkey_add_label.go | 7 ++-- internal/cmds/sshkey_create.go | 3 +- internal/cmds/sshkey_describe.go | 11 +++--- internal/cmds/sshkey_list.go | 9 ++--- internal/cmds/sshkey_remove_label.go | 3 +- internal/cmds/volume_add_label.go | 7 ++-- internal/cmds/volume_describe.go | 13 +++---- internal/cmds/volume_list.go | 13 +++---- internal/cmds/volume_remove_label.go | 3 +- 56 files changed, 275 insertions(+), 221 deletions(-) rename internal/{cmds => cmd/util}/util.go (85%) rename internal/{cmds => cmd/util}/util_internal_test.go (93%) diff --git a/internal/cmds/util.go b/internal/cmd/util/util.go similarity index 85% rename from internal/cmds/util.go rename to internal/cmd/util/util.go index bebfad87..363beaba 100644 --- a/internal/cmds/util.go +++ b/internal/cmd/util/util.go @@ -1,4 +1,4 @@ -package cmds +package util import ( "encoding/json" @@ -15,25 +15,25 @@ import ( "github.com/spf13/cobra" ) -func yesno(b bool) string { +func YesNo(b bool) string { if b { return "yes" } return "no" } -func na(s string) string { +func NA(s string) string { if s == "" { return "-" } return s } -func datetime(t time.Time) string { +func Datetime(t time.Time) string { return t.Local().Format(time.UnixDate) } -func chainRunE(fns ...func(cmd *cobra.Command, args []string) error) func(cmd *cobra.Command, args []string) error { +func ChainRunE(fns ...func(cmd *cobra.Command, args []string) error) func(cmd *cobra.Command, args []string) error { return func(cmd *cobra.Command, args []string) error { for _, fn := range fns { if fn == nil { @@ -47,7 +47,7 @@ func chainRunE(fns ...func(cmd *cobra.Command, args []string) error) func(cmd *c } } -func exactlyOneSet(s string, ss ...string) bool { +func ExactlyOneSet(s string, ss ...string) bool { set := s != "" for _, s := range ss { if set && s != "" { @@ -62,7 +62,7 @@ var outputDescription = `Output can be controlled with the -o flag. Use -o nohea table header. Displayed columns and their order can be set with -o columns=%s (see available columns below).` -func listLongDescription(intro string, columns []string) string { +func ListLongDescription(intro string, columns []string) string { var colExample []string if len(columns) > 2 { colExample = columns[0:2] @@ -77,11 +77,11 @@ func listLongDescription(intro string, columns []string) string { ) } -func splitLabel(label string) []string { +func SplitLabel(label string) []string { return strings.SplitN(label, "=", 2) } -func labelsToString(labels map[string]string) string { +func LabelsToString(labels map[string]string) string { var labelsString []string keys := make([]string, 0, len(labels)) for key := range labels { @@ -99,7 +99,7 @@ func labelsToString(labels map[string]string) string { return strings.Join(labelsString, ", ") } -func describeFormat(object interface{}, format string) error { +func DescribeFormat(object interface{}, format string) error { if !strings.HasSuffix(format, "\n") { format = format + "\n" } @@ -110,12 +110,12 @@ func describeFormat(object interface{}, format string) error { return t.Execute(os.Stdout, object) } -func describeJSON(object interface{}) error { +func DescribeJSON(object interface{}) error { enc := json.NewEncoder(os.Stdout) return enc.Encode(object) } -func locationToSchema(location hcloud.Location) schema.Location { +func LocationToSchema(location hcloud.Location) schema.Location { return schema.Location{ ID: location.ID, Name: location.Name, @@ -128,12 +128,12 @@ func locationToSchema(location hcloud.Location) schema.Location { } } -func datacenterToSchema(datacenter hcloud.Datacenter) schema.Datacenter { +func DatacenterToSchema(datacenter hcloud.Datacenter) schema.Datacenter { datacenterSchema := schema.Datacenter{ ID: datacenter.ID, Name: datacenter.Name, Description: datacenter.Description, - Location: locationToSchema(*datacenter.Location), + Location: LocationToSchema(*datacenter.Location), } for _, st := range datacenter.ServerTypes.Supported { datacenterSchema.ServerTypes.Supported = append(datacenterSchema.ServerTypes.Supported, st.ID) @@ -144,7 +144,7 @@ func datacenterToSchema(datacenter hcloud.Datacenter) schema.Datacenter { return datacenterSchema } -func serverTypeToSchema(serverType hcloud.ServerType) schema.ServerType { +func ServerTypeToSchema(serverType hcloud.ServerType) schema.ServerType { serverTypeSchema := schema.ServerType{ ID: serverType.ID, Name: serverType.Name, @@ -171,7 +171,7 @@ func serverTypeToSchema(serverType hcloud.ServerType) schema.ServerType { return serverTypeSchema } -func imageToSchema(image hcloud.Image) schema.Image { +func ImageToSchema(image hcloud.Image) schema.Image { imageSchema := schema.Image{ ID: image.ID, Name: hcloud.String(image.Name), @@ -202,7 +202,7 @@ func imageToSchema(image hcloud.Image) schema.Image { return imageSchema } -func isoToSchema(iso hcloud.ISO) schema.ISO { +func ISOToSchema(iso hcloud.ISO) schema.ISO { return schema.ISO{ ID: iso.ID, Name: iso.Name, @@ -211,7 +211,7 @@ func isoToSchema(iso hcloud.ISO) schema.ISO { } } -func loadBalancerTypeToSchema(loadBalancerType hcloud.LoadBalancerType) schema.LoadBalancerType { +func LoadBalancerTypeToSchema(loadBalancerType hcloud.LoadBalancerType) schema.LoadBalancerType { loadBalancerTypeSchema := schema.LoadBalancerType{ ID: loadBalancerType.ID, Name: loadBalancerType.Name, diff --git a/internal/cmds/util_internal_test.go b/internal/cmd/util/util_internal_test.go similarity index 93% rename from internal/cmds/util_internal_test.go rename to internal/cmd/util/util_internal_test.go index e165b0ba..3f5aa38c 100644 --- a/internal/cmds/util_internal_test.go +++ b/internal/cmd/util/util_internal_test.go @@ -1,4 +1,4 @@ -package cmds +package util import "testing" @@ -39,7 +39,7 @@ func TestOnlyOneSet(t *testing.T) { for _, tt := range tests { tt := tt t.Run(tt.name, func(t *testing.T) { - actual := exactlyOneSet(tt.s, tt.ss...) + actual := ExactlyOneSet(tt.s, tt.ss...) if tt.expected != actual { t.Errorf("expected %t; got %t", tt.expected, actual) } diff --git a/internal/cmds/certificate_add_label.go b/internal/cmds/certificate_add_label.go index 9ea84972..2fea9b21 100644 --- a/internal/cmds/certificate_add_label.go +++ b/internal/cmds/certificate_add_label.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" @@ -17,7 +18,7 @@ func newCertificateAddLabelCommand(cli *state.State) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.CertificateNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: chainRunE(validateCertificateAddLabel, cli.EnsureToken), + PreRunE: util.ChainRunE(validateCertificateAddLabel, cli.EnsureToken), RunE: cli.Wrap(runCertificateAddLabel), } @@ -26,7 +27,7 @@ func newCertificateAddLabelCommand(cli *state.State) *cobra.Command { } func validateCertificateAddLabel(cmd *cobra.Command, args []string) error { - label := splitLabel(args[1]) + label := util.SplitLabel(args[1]) if len(label) != 2 { return fmt.Errorf("invalid label: %s", args[1]) } @@ -46,7 +47,7 @@ func runCertificateAddLabel(cli *state.State, cmd *cobra.Command, args []string) if cert == nil { return fmt.Errorf("Certificate not found: %s", idOrName) } - label := splitLabel(args[1]) + label := util.SplitLabel(args[1]) if _, ok := cert.Labels[label[0]]; ok && !overwrite { return fmt.Errorf("Label %s on certificate %d already exists", label[0], cert.ID) } diff --git a/internal/cmds/certificate_describe.go b/internal/cmds/certificate_describe.go index dab0168b..01567b41 100644 --- a/internal/cmds/certificate_describe.go +++ b/internal/cmds/certificate_describe.go @@ -6,6 +6,7 @@ import ( humanize "github.com/dustin/go-humanize" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" @@ -42,7 +43,7 @@ func runCertificateDescribe(cli *state.State, cmd *cobra.Command, args []string) case outputFlags.IsSet("json"): return certificateDescribeJSON(resp) case outputFlags.IsSet("format"): - return describeFormat(cert, outputFlags["format"][0]) + return util.DescribeFormat(cert, outputFlags["format"][0]) default: return certificateDescribeText(cli, cert) } @@ -54,21 +55,21 @@ func certificateDescribeJSON(resp *hcloud.Response) error { return err } if server, ok := data["certificate"]; ok { - return describeJSON(server) + return util.DescribeJSON(server) } if servers, ok := data["certificates"].([]interface{}); ok { - return describeJSON(servers[0]) + return util.DescribeJSON(servers[0]) } - return describeJSON(data) + return util.DescribeJSON(data) } func certificateDescribeText(cli *state.State, cert *hcloud.Certificate) error { fmt.Printf("ID:\t\t\t%d\n", cert.ID) fmt.Printf("Name:\t\t\t%s\n", cert.Name) fmt.Printf("Fingerprint:\t\t%s\n", cert.Fingerprint) - fmt.Printf("Created:\t\t%s (%s)\n", datetime(cert.Created), humanize.Time(cert.Created)) - fmt.Printf("Not valid before:\t%s (%s)\n", datetime(cert.NotValidBefore), humanize.Time(cert.NotValidBefore)) - fmt.Printf("Not valid after:\t%s (%s)\n", datetime(cert.NotValidAfter), humanize.Time(cert.NotValidAfter)) + fmt.Printf("Created:\t\t%s (%s)\n", util.Datetime(cert.Created), humanize.Time(cert.Created)) + fmt.Printf("Not valid before:\t%s (%s)\n", util.Datetime(cert.NotValidBefore), humanize.Time(cert.NotValidBefore)) + fmt.Printf("Not valid after:\t%s (%s)\n", util.Datetime(cert.NotValidAfter), humanize.Time(cert.NotValidAfter)) fmt.Printf("Domain names:\n") for _, domainName := range cert.DomainNames { fmt.Printf(" - %s\n", domainName) diff --git a/internal/cmds/certificate_list.go b/internal/cmds/certificate_list.go index fad239f5..a48b89a3 100644 --- a/internal/cmds/certificate_list.go +++ b/internal/cmds/certificate_list.go @@ -3,6 +3,7 @@ package cmds import ( "strings" + "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/hetznercloud/hcloud-go/hcloud/schema" @@ -19,7 +20,7 @@ func newCertificatesListCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "list [FLAGS]", Short: "List Certificates", - Long: listLongDescription( + Long: util.ListLongDescription( "Displays a list of certificates", certificateTableOutput.Columns(), ), @@ -67,7 +68,7 @@ func runCertificatesList(cli *state.State, cmd *cobra.Command, args []string) er certSchemas = append(certSchemas, certSchema) } - return describeJSON(certSchemas) + return util.DescribeJSON(certSchemas) } cols := []string{"id", "name", "domain_names", "not_valid_after"} @@ -93,15 +94,15 @@ func describeCertificatesTableOutput() *tableOutput { RemoveAllowedField("certificate", "chain"). AddFieldOutputFn("labels", fieldOutputFn(func(obj interface{}) string { cert := obj.(*hcloud.Certificate) - return labelsToString(cert.Labels) + return util.LabelsToString(cert.Labels) })). AddFieldOutputFn("not_valid_before", func(obj interface{}) string { cert := obj.(*hcloud.Certificate) - return datetime(cert.NotValidBefore) + return util.Datetime(cert.NotValidBefore) }). AddFieldOutputFn("not_valid_after", func(obj interface{}) string { cert := obj.(*hcloud.Certificate) - return datetime(cert.NotValidAfter) + return util.Datetime(cert.NotValidAfter) }). AddFieldOutputFn("domain_names", func(obj interface{}) string { cert := obj.(*hcloud.Certificate) @@ -109,6 +110,6 @@ func describeCertificatesTableOutput() *tableOutput { }). AddFieldOutputFn("created", fieldOutputFn(func(obj interface{}) string { cert := obj.(*hcloud.Certificate) - return datetime(cert.Created) + return util.Datetime(cert.Created) })) } diff --git a/internal/cmds/certificate_remove_label.go b/internal/cmds/certificate_remove_label.go index ac7442b9..61c1bfca 100644 --- a/internal/cmds/certificate_remove_label.go +++ b/internal/cmds/certificate_remove_label.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" @@ -26,7 +27,7 @@ func newCertificateRemoveLabelCommand(cli *state.State) *cobra.Command { ), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: chainRunE(validateCertificateRemoveLabel, cli.EnsureToken), + PreRunE: util.ChainRunE(validateCertificateRemoveLabel, cli.EnsureToken), RunE: cli.Wrap(runCertificateRemoveLabel), } cmd.Flags().BoolP("all", "a", false, "Remove all labels") diff --git a/internal/cmds/context_list.go b/internal/cmds/context_list.go index 837edd71..36538391 100644 --- a/internal/cmds/context_list.go +++ b/internal/cmds/context_list.go @@ -1,6 +1,7 @@ package cmds import ( + "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/spf13/cobra" ) @@ -23,7 +24,7 @@ func newContextListCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "list [FLAGS]", Short: "List contexts", - Long: listLongDescription( + Long: util.ListLongDescription( "Displays a list of contexts.", contextListTableOutput.Columns(), ), diff --git a/internal/cmds/datacenter_describe.go b/internal/cmds/datacenter_describe.go index b0ded4b7..ca10c9ca 100644 --- a/internal/cmds/datacenter_describe.go +++ b/internal/cmds/datacenter_describe.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" @@ -41,7 +42,7 @@ func runDatacenterDescribe(cli *state.State, cmd *cobra.Command, args []string) case outputFlags.IsSet("json"): return datacenterDescribeJSON(resp) case outputFlags.IsSet("format"): - return describeFormat(datacenter, outputFlags["format"][0]) + return util.DescribeFormat(datacenter, outputFlags["format"][0]) default: return datacenterDescribeText(cli, datacenter) } @@ -106,10 +107,10 @@ func datacenterDescribeJSON(resp *hcloud.Response) error { return err } if datacenter, ok := data["datacenter"]; ok { - return describeJSON(datacenter) + return util.DescribeJSON(datacenter) } if datacenters, ok := data["datacenters"].([]interface{}); ok { - return describeJSON(datacenters[0]) + return util.DescribeJSON(datacenters[0]) } - return describeJSON(data) + return util.DescribeJSON(data) } diff --git a/internal/cmds/datacenter_list.go b/internal/cmds/datacenter_list.go index 1db98074..ac176c40 100644 --- a/internal/cmds/datacenter_list.go +++ b/internal/cmds/datacenter_list.go @@ -1,6 +1,7 @@ package cmds import ( + "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/hetznercloud/hcloud-go/hcloud/schema" @@ -22,7 +23,7 @@ func newDatacenterListCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "list [FLAGS]", Short: "List datacenters", - Long: listLongDescription( + Long: util.ListLongDescription( "Displays a list of datacenters.", datacenterListTableOutput.Columns(), ), @@ -43,9 +44,9 @@ func runDatacenterList(cli *state.State, cmd *cobra.Command, args []string) erro if outOpts.IsSet("json") { var datacenterSchemas []schema.Datacenter for _, datacenter := range datacenters { - datacenterSchemas = append(datacenterSchemas, datacenterToSchema(*datacenter)) + datacenterSchemas = append(datacenterSchemas, util.DatacenterToSchema(*datacenter)) } - return describeJSON(datacenterSchemas) + return util.DescribeJSON(datacenterSchemas) } if err != nil { diff --git a/internal/cmds/floatingip_add_label.go b/internal/cmds/floatingip_add_label.go index ea3d99be..8aae78a3 100644 --- a/internal/cmds/floatingip_add_label.go +++ b/internal/cmds/floatingip_add_label.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" @@ -17,7 +18,7 @@ func newFloatingIPAddLabelCommand(cli *state.State) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.FloatingIPNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: chainRunE(validateFloatingIPAddLabel, cli.EnsureToken), + PreRunE: util.ChainRunE(validateFloatingIPAddLabel, cli.EnsureToken), RunE: cli.Wrap(runFloatingIPAddLabel), } @@ -26,7 +27,7 @@ func newFloatingIPAddLabelCommand(cli *state.State) *cobra.Command { } func validateFloatingIPAddLabel(cmd *cobra.Command, args []string) error { - label := splitLabel(args[1]) + label := util.SplitLabel(args[1]) if len(label) != 2 { return fmt.Errorf("invalid label: %s", args[1]) } @@ -46,7 +47,7 @@ func runFloatingIPAddLabel(cli *state.State, cmd *cobra.Command, args []string) return fmt.Errorf("Floating IP not found: %v", idOrName) } - label := splitLabel(args[1]) + label := util.SplitLabel(args[1]) if _, ok := floatingIP.Labels[label[0]]; ok && !overwrite { return fmt.Errorf("label %s on Floating IP %d already exists", label[0], floatingIP.ID) diff --git a/internal/cmds/floatingip_create.go b/internal/cmds/floatingip_create.go index f5c39dd8..8b80c7c9 100644 --- a/internal/cmds/floatingip_create.go +++ b/internal/cmds/floatingip_create.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" @@ -17,7 +18,7 @@ func newFloatingIPCreateCommand(cli *state.State) *cobra.Command { Args: cobra.NoArgs, TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: chainRunE(validateFloatingIPCreate, cli.EnsureToken), + PreRunE: util.ChainRunE(validateFloatingIPCreate, cli.EnsureToken), RunE: cli.Wrap(runFloatingIPCreate), } cmd.Flags().String("type", "", "Type (ipv4 or ipv6) (required)") diff --git a/internal/cmds/floatingip_describe.go b/internal/cmds/floatingip_describe.go index 22d6d0d9..2ad19325 100644 --- a/internal/cmds/floatingip_describe.go +++ b/internal/cmds/floatingip_describe.go @@ -6,6 +6,7 @@ import ( "github.com/dustin/go-humanize" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" @@ -42,7 +43,7 @@ func runFloatingIPDescribe(cli *state.State, cmd *cobra.Command, args []string) case outputFlags.IsSet("json"): return floatingIPDescribeJSON(resp) case outputFlags.IsSet("format"): - return describeFormat(floatingIP, outputFlags["format"][0]) + return util.DescribeFormat(floatingIP, outputFlags["format"][0]) default: return floatingIPDescribeText(cli, floatingIP) } @@ -52,14 +53,14 @@ func floatingIPDescribeText(cli *state.State, floatingIP *hcloud.FloatingIP) err fmt.Printf("ID:\t\t%d\n", floatingIP.ID) fmt.Printf("Type:\t\t%s\n", floatingIP.Type) fmt.Printf("Name:\t\t%s\n", floatingIP.Name) - fmt.Printf("Description:\t%s\n", na(floatingIP.Description)) - fmt.Printf("Created:\t%s (%s)\n", datetime(floatingIP.Created), humanize.Time(floatingIP.Created)) + fmt.Printf("Description:\t%s\n", util.NA(floatingIP.Description)) + fmt.Printf("Created:\t%s (%s)\n", util.Datetime(floatingIP.Created), humanize.Time(floatingIP.Created)) if floatingIP.Network != nil { fmt.Printf("IP:\t\t%s\n", floatingIP.Network.String()) } else { fmt.Printf("IP:\t\t%s\n", floatingIP.IP.String()) } - fmt.Printf("Blocked:\t%s\n", yesno(floatingIP.Blocked)) + fmt.Printf("Blocked:\t%s\n", util.YesNo(floatingIP.Blocked)) fmt.Printf("Home Location:\t%s\n", floatingIP.HomeLocation.Name) if floatingIP.Server != nil { server, _, err := cli.Client().Server.GetByID(cli.Context, floatingIP.Server.ID) @@ -85,7 +86,7 @@ func floatingIPDescribeText(cli *state.State, floatingIP *hcloud.FloatingIP) err } fmt.Printf("Protection:\n") - fmt.Printf(" Delete:\t%s\n", yesno(floatingIP.Protection.Delete)) + fmt.Printf(" Delete:\t%s\n", util.YesNo(floatingIP.Protection.Delete)) fmt.Print("Labels:\n") if len(floatingIP.Labels) == 0 { @@ -104,10 +105,10 @@ func floatingIPDescribeJSON(resp *hcloud.Response) error { return err } if floatingIP, ok := data["floating_ip"]; ok { - return describeJSON(floatingIP) + return util.DescribeJSON(floatingIP) } if floatingIPs, ok := data["floating_ips"].([]interface{}); ok { - return describeJSON(floatingIPs[0]) + return util.DescribeJSON(floatingIPs[0]) } - return describeJSON(data) + return util.DescribeJSON(data) } diff --git a/internal/cmds/floatingip_list.go b/internal/cmds/floatingip_list.go index e9e51d19..e3bce5ff 100644 --- a/internal/cmds/floatingip_list.go +++ b/internal/cmds/floatingip_list.go @@ -4,6 +4,7 @@ import ( "fmt" "strings" + "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud/schema" @@ -21,7 +22,7 @@ func newFloatingIPListCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "list [FLAGS]", Short: "List Floating IPs", - Long: listLongDescription( + Long: util.ListLongDescription( "Displays a list of Floating IPs.", floatingIPListTableOutput.Columns(), ), @@ -60,7 +61,7 @@ func runFloatingIPList(cli *state.State, cmd *cobra.Command, args []string) erro IP: floatingIP.IP.String(), Created: floatingIP.Created, Type: string(floatingIP.Type), - HomeLocation: locationToSchema(*floatingIP.HomeLocation), + HomeLocation: util.LocationToSchema(*floatingIP.HomeLocation), Blocked: floatingIP.Blocked, Protection: schema.FloatingIPProtection{Delete: floatingIP.Protection.Delete}, Labels: floatingIP.Labels, @@ -76,7 +77,7 @@ func runFloatingIPList(cli *state.State, cmd *cobra.Command, args []string) erro } floatingIPSchemas = append(floatingIPSchemas, floatingIPSchema) } - return describeJSON(floatingIPSchemas) + return util.DescribeJSON(floatingIPSchemas) } cols := []string{"id", "type", "name", "description", "ip", "home", "server", "dns"} @@ -113,7 +114,7 @@ func describeFloatingIPListTableOutput(cli *state.State) *tableOutput { if len(floatingIP.DNSPtr) > 1 { dns = fmt.Sprintf("%d entries", len(floatingIP.DNSPtr)) } - return na(dns) + return util.NA(dns) })). AddFieldOutputFn("server", fieldOutputFn(func(obj interface{}) string { floatingIP := obj.(*hcloud.FloatingIP) @@ -121,7 +122,7 @@ func describeFloatingIPListTableOutput(cli *state.State) *tableOutput { if floatingIP.Server != nil && cli != nil { return cli.ServerName(floatingIP.Server.ID) } - return na(server) + return util.NA(server) })). AddFieldOutputFn("home", fieldOutputFn(func(obj interface{}) string { floatingIP := obj.(*hcloud.FloatingIP) @@ -144,10 +145,10 @@ func describeFloatingIPListTableOutput(cli *state.State) *tableOutput { })). AddFieldOutputFn("labels", fieldOutputFn(func(obj interface{}) string { floatingIP := obj.(*hcloud.FloatingIP) - return labelsToString(floatingIP.Labels) + return util.LabelsToString(floatingIP.Labels) })). AddFieldOutputFn("created", fieldOutputFn(func(obj interface{}) string { floatingIP := obj.(*hcloud.FloatingIP) - return datetime(floatingIP.Created) + return util.Datetime(floatingIP.Created) })) } diff --git a/internal/cmds/floatingip_remove_label.go b/internal/cmds/floatingip_remove_label.go index 82831fe0..db841518 100644 --- a/internal/cmds/floatingip_remove_label.go +++ b/internal/cmds/floatingip_remove_label.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" @@ -25,7 +26,7 @@ func newFloatingIPRemoveLabelCommand(cli *state.State) *cobra.Command { })), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: chainRunE(validateFloatingIPRemoveLabel, cli.EnsureToken), + PreRunE: util.ChainRunE(validateFloatingIPRemoveLabel, cli.EnsureToken), RunE: cli.Wrap(runFloatingIPRemoveLabel), } diff --git a/internal/cmds/image_add_label.go b/internal/cmds/image_add_label.go index 34cbdb71..11c18048 100644 --- a/internal/cmds/image_add_label.go +++ b/internal/cmds/image_add_label.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" @@ -17,7 +18,7 @@ func newImageAddLabelCommand(cli *state.State) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.ImageNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: chainRunE(validateImageAddLabel, cli.EnsureToken), + PreRunE: util.ChainRunE(validateImageAddLabel, cli.EnsureToken), RunE: cli.Wrap(runImageAddLabel), } @@ -26,7 +27,7 @@ func newImageAddLabelCommand(cli *state.State) *cobra.Command { } func validateImageAddLabel(cmd *cobra.Command, args []string) error { - label := splitLabel(args[1]) + label := util.SplitLabel(args[1]) if len(label) != 2 { return fmt.Errorf("invalid label: %s", args[1]) } @@ -44,7 +45,7 @@ func runImageAddLabel(cli *state.State, cmd *cobra.Command, args []string) error if image == nil { return fmt.Errorf("image not found: %s", idOrName) } - label := splitLabel(args[1]) + label := util.SplitLabel(args[1]) if _, ok := image.Labels[label[0]]; ok && !overwrite { return fmt.Errorf("label %s on image %d already exists", label[0], image.ID) diff --git a/internal/cmds/image_describe.go b/internal/cmds/image_describe.go index e67f2cb1..48e89e1c 100644 --- a/internal/cmds/image_describe.go +++ b/internal/cmds/image_describe.go @@ -6,6 +6,7 @@ import ( humanize "github.com/dustin/go-humanize" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" @@ -42,7 +43,7 @@ func runImageDescribe(cli *state.State, cmd *cobra.Command, args []string) error case outputFlags.IsSet("json"): return imageDescribeJSON(resp) case outputFlags.IsSet("format"): - return describeFormat(image, outputFlags["format"][0]) + return util.DescribeFormat(image, outputFlags["format"][0]) default: return imageDescribeText(cli, image) } @@ -52,23 +53,23 @@ func imageDescribeText(cli *state.State, image *hcloud.Image) error { fmt.Printf("ID:\t\t%d\n", image.ID) fmt.Printf("Type:\t\t%s\n", image.Type) fmt.Printf("Status:\t\t%s\n", image.Status) - fmt.Printf("Name:\t\t%s\n", na(image.Name)) - fmt.Printf("Created:\t%s (%s)\n", datetime(image.Created), humanize.Time(image.Created)) + fmt.Printf("Name:\t\t%s\n", util.NA(image.Name)) + fmt.Printf("Created:\t%s (%s)\n", util.Datetime(image.Created), humanize.Time(image.Created)) if !image.Deprecated.IsZero() { - fmt.Printf("Deprecated:\t%s (%s)\n", datetime(image.Deprecated), humanize.Time(image.Deprecated)) + fmt.Printf("Deprecated:\t%s (%s)\n", util.Datetime(image.Deprecated), humanize.Time(image.Deprecated)) } fmt.Printf("Description:\t%s\n", image.Description) if image.ImageSize != 0 { fmt.Printf("Image size:\t%.2f GB\n", image.ImageSize) } else { - fmt.Printf("Image size:\t%s\n", na("")) + fmt.Printf("Image size:\t%s\n", util.NA("")) } fmt.Printf("Disk size:\t%.0f GB\n", image.DiskSize) fmt.Printf("OS flavor:\t%s\n", image.OSFlavor) - fmt.Printf("OS version:\t%s\n", na(image.OSVersion)) - fmt.Printf("Rapid deploy:\t%s\n", yesno(image.RapidDeploy)) + fmt.Printf("OS version:\t%s\n", util.NA(image.OSVersion)) + fmt.Printf("Rapid deploy:\t%s\n", util.YesNo(image.RapidDeploy)) fmt.Printf("Protection:\n") - fmt.Printf(" Delete:\t%s\n", yesno(image.Protection.Delete)) + fmt.Printf(" Delete:\t%s\n", util.YesNo(image.Protection.Delete)) fmt.Print("Labels:\n") if len(image.Labels) == 0 { @@ -91,10 +92,10 @@ func imageDescribeJSON(resp *hcloud.Response) error { return err } if image, ok := data["image"]; ok { - return describeJSON(image) + return util.DescribeJSON(image) } if images, ok := data["images"].([]interface{}); ok { - return describeJSON(images[0]) + return util.DescribeJSON(images[0]) } - return describeJSON(data) + return util.DescribeJSON(data) } diff --git a/internal/cmds/image_list.go b/internal/cmds/image_list.go index e212c8f5..a1c8b5ad 100644 --- a/internal/cmds/image_list.go +++ b/internal/cmds/image_list.go @@ -4,6 +4,7 @@ import ( "fmt" "strings" + "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud/schema" @@ -23,7 +24,7 @@ func newImageListCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "list [FLAGS]", Short: "List images", - Long: listLongDescription( + Long: util.ListLongDescription( "Displays a list of images.", imageListTableOutput.Columns(), ), @@ -66,9 +67,9 @@ func runImageList(cli *state.State, cmd *cobra.Command, args []string) error { if outOpts.IsSet("json") { var imageSchemas []schema.Image for _, image := range images { - imageSchemas = append(imageSchemas, imageToSchema(*image)) + imageSchemas = append(imageSchemas, util.ImageToSchema(*image)) } - return describeJSON(imageSchemas) + return util.DescribeJSON(imageSchemas) } cols := []string{"id", "type", "name", "description", "image_size", "disk_size", "created", "deprecated"} @@ -104,12 +105,12 @@ func describeImageListTableOutput(cli *state.State) *tableOutput { AddFieldAlias("boundto", "bound to"). AddFieldOutputFn("name", fieldOutputFn(func(obj interface{}) string { image := obj.(*hcloud.Image) - return na(image.Name) + return util.NA(image.Name) })). AddFieldOutputFn("image_size", fieldOutputFn(func(obj interface{}) string { image := obj.(*hcloud.Image) if image.ImageSize == 0 { - return na("") + return util.NA("") } return fmt.Sprintf("%.2f GB", image.ImageSize) })). @@ -126,14 +127,14 @@ func describeImageListTableOutput(cli *state.State) *tableOutput { if image.BoundTo != nil && cli != nil { return cli.ServerName(image.BoundTo.ID) } - return na("") + return util.NA("") })). AddFieldOutputFn("created_from", fieldOutputFn(func(obj interface{}) string { image := obj.(*hcloud.Image) if image.CreatedFrom != nil && cli != nil { return cli.ServerName(image.CreatedFrom.ID) } - return na("") + return util.NA("") })). AddFieldOutputFn("protection", fieldOutputFn(func(obj interface{}) string { image := obj.(*hcloud.Image) @@ -145,17 +146,17 @@ func describeImageListTableOutput(cli *state.State) *tableOutput { })). AddFieldOutputFn("labels", fieldOutputFn(func(obj interface{}) string { image := obj.(*hcloud.Image) - return labelsToString(image.Labels) + return util.LabelsToString(image.Labels) })). AddFieldOutputFn("created", fieldOutputFn(func(obj interface{}) string { image := obj.(*hcloud.Image) - return datetime(image.Created) + return util.Datetime(image.Created) })). AddFieldOutputFn("deprecated", fieldOutputFn(func(obj interface{}) string { image := obj.(*hcloud.Image) if image.Deprecated.IsZero() { return "-" } - return datetime(image.Deprecated) + return util.Datetime(image.Deprecated) })) } diff --git a/internal/cmds/image_remove_label.go b/internal/cmds/image_remove_label.go index 0079b673..f647c515 100644 --- a/internal/cmds/image_remove_label.go +++ b/internal/cmds/image_remove_label.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" @@ -26,7 +27,7 @@ func newImageRemoveLabelCommand(cli *state.State) *cobra.Command { ), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: chainRunE(validateImageRemoveLabel, cli.EnsureToken), + PreRunE: util.ChainRunE(validateImageRemoveLabel, cli.EnsureToken), RunE: cli.Wrap(runImageRemoveLabel), } diff --git a/internal/cmds/iso_describe.go b/internal/cmds/iso_describe.go index 8ee48ab0..bc1865bd 100644 --- a/internal/cmds/iso_describe.go +++ b/internal/cmds/iso_describe.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" @@ -41,7 +42,7 @@ func runISODescribe(cli *state.State, cmd *cobra.Command, args []string) error { case outputFlags.IsSet("json"): return isoDescribeJSON(resp) case outputFlags.IsSet("format"): - return describeFormat(iso, outputFlags["format"][0]) + return util.DescribeFormat(iso, outputFlags["format"][0]) default: return isoDescribeText(iso) } @@ -61,10 +62,10 @@ func isoDescribeJSON(resp *hcloud.Response) error { return err } if iso, ok := data["iso"]; ok { - return describeJSON(iso) + return util.DescribeJSON(iso) } if isos, ok := data["isos"].([]interface{}); ok { - return describeJSON(isos[0]) + return util.DescribeJSON(isos[0]) } - return describeJSON(data) + return util.DescribeJSON(data) } diff --git a/internal/cmds/iso_list.go b/internal/cmds/iso_list.go index 627c736e..948e58f0 100644 --- a/internal/cmds/iso_list.go +++ b/internal/cmds/iso_list.go @@ -1,6 +1,7 @@ package cmds import ( + "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/hetznercloud/hcloud-go/hcloud/schema" @@ -18,7 +19,7 @@ func newISOListCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "list [FLAGS]", Short: "List ISOs", - Long: listLongDescription( + Long: util.ListLongDescription( "Displays a list of ISOs.", isoListTableOutput.Columns(), ), @@ -42,9 +43,9 @@ func runISOList(cli *state.State, cmd *cobra.Command, args []string) error { if outOpts.IsSet("json") { var isoSchemas []schema.ISO for _, iso := range isos { - isoSchemas = append(isoSchemas, isoToSchema(*iso)) + isoSchemas = append(isoSchemas, util.ISOToSchema(*iso)) } - return describeJSON(isoSchemas) + return util.DescribeJSON(isoSchemas) } cols := []string{"id", "name", "description", "type"} diff --git a/internal/cmds/load_balancer_add_label.go b/internal/cmds/load_balancer_add_label.go index db8478b5..528c402a 100644 --- a/internal/cmds/load_balancer_add_label.go +++ b/internal/cmds/load_balancer_add_label.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" @@ -17,7 +18,7 @@ func newLoadBalancerAddLabelCommand(cli *state.State) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.LoadBalancerNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: chainRunE(validateLoadBalancerAddLabel, cli.EnsureToken), + PreRunE: util.ChainRunE(validateLoadBalancerAddLabel, cli.EnsureToken), RunE: cli.Wrap(runLoadBalancerAddLabel), } @@ -26,7 +27,7 @@ func newLoadBalancerAddLabelCommand(cli *state.State) *cobra.Command { } func validateLoadBalancerAddLabel(cmd *cobra.Command, args []string) error { - label := splitLabel(args[1]) + label := util.SplitLabel(args[1]) if len(label) != 2 { return fmt.Errorf("invalid label: %s", args[1]) } @@ -44,7 +45,7 @@ func runLoadBalancerAddLabel(cli *state.State, cmd *cobra.Command, args []string if loadBalancer == nil { return fmt.Errorf("Load Balancer not found: %s", idOrName) } - label := splitLabel(args[1]) + label := util.SplitLabel(args[1]) if _, ok := loadBalancer.Labels[label[0]]; ok && !overwrite { return fmt.Errorf("label %s on Load Balancer %d already exists", label[0], loadBalancer.ID) diff --git a/internal/cmds/load_balancer_add_service.go b/internal/cmds/load_balancer_add_service.go index d88459ca..f8d11b98 100644 --- a/internal/cmds/load_balancer_add_service.go +++ b/internal/cmds/load_balancer_add_service.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" @@ -17,7 +18,7 @@ func newLoadBalancerAddServiceCommand(cli *state.State) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.LoadBalancerNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: chainRunE(validateLoadBalancerAddService, cli.EnsureToken), + PreRunE: util.ChainRunE(validateLoadBalancerAddService, cli.EnsureToken), RunE: cli.Wrap(runLoadBalancerAddService), } cmd.Flags().String("protocol", "", "Protocol of the service (required)") diff --git a/internal/cmds/load_balancer_add_target.go b/internal/cmds/load_balancer_add_target.go index 4aed3cf1..209d36c2 100644 --- a/internal/cmds/load_balancer_add_target.go +++ b/internal/cmds/load_balancer_add_target.go @@ -5,6 +5,7 @@ import ( "net" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" @@ -45,7 +46,7 @@ func runLoadBalancerAddTarget(cli *state.State, cmd *cobra.Command, args []strin labelSelector, _ := cmd.Flags().GetString("label-selector") ipAddr, _ := cmd.Flags().GetString("ip") - if !exactlyOneSet(serverIDOrName, labelSelector, ipAddr) { + if !util.ExactlyOneSet(serverIDOrName, labelSelector, ipAddr) { return fmt.Errorf("--server, --label-selector, and --ip are mutually exclusive") } if loadBalancer, _, err = cli.Client().LoadBalancer.Get(cli.Context, idOrName); err != nil { diff --git a/internal/cmds/load_balancer_delete_service.go b/internal/cmds/load_balancer_delete_service.go index 10e34d1d..1ce2adac 100644 --- a/internal/cmds/load_balancer_delete_service.go +++ b/internal/cmds/load_balancer_delete_service.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/spf13/cobra" ) @@ -16,7 +17,7 @@ func newLoadBalancerDeleteServiceCommand(cli *state.State) *cobra.Command { Args: cobra.RangeArgs(1, 2), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: chainRunE(cli.EnsureToken), + PreRunE: util.ChainRunE(cli.EnsureToken), RunE: cli.Wrap(runLoadBalancerDeleteService), } diff --git a/internal/cmds/load_balancer_describe.go b/internal/cmds/load_balancer_describe.go index 134af4f5..db469343 100644 --- a/internal/cmds/load_balancer_describe.go +++ b/internal/cmds/load_balancer_describe.go @@ -6,6 +6,7 @@ import ( humanize "github.com/dustin/go-humanize" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" @@ -43,7 +44,7 @@ func runLoadBalancerDescribe(cli *state.State, cmd *cobra.Command, args []string case outputFlags.IsSet("json"): return loadBalancerDescribeJSON(resp) case outputFlags.IsSet("format"): - return describeFormat(loadBalancer, outputFlags["format"][0]) + return util.DescribeFormat(loadBalancer, outputFlags["format"][0]) default: return loadBalancerDescribeText(cli, loadBalancer, withLabelSelectorTargets) } @@ -52,9 +53,9 @@ func runLoadBalancerDescribe(cli *state.State, cmd *cobra.Command, args []string func loadBalancerDescribeText(cli *state.State, loadBalancer *hcloud.LoadBalancer, withLabelSelectorTargets bool) error { fmt.Printf("ID:\t\t\t\t%d\n", loadBalancer.ID) fmt.Printf("Name:\t\t\t\t%s\n", loadBalancer.Name) - fmt.Printf("Created:\t\t\t%s (%s)\n", datetime(loadBalancer.Created), humanize.Time(loadBalancer.Created)) + fmt.Printf("Created:\t\t\t%s (%s)\n", util.Datetime(loadBalancer.Created), humanize.Time(loadBalancer.Created)) fmt.Printf("Public Net:\n") - fmt.Printf(" Enabled:\t\t\t%s\n", yesno(loadBalancer.PublicNet.Enabled)) + fmt.Printf(" Enabled:\t\t\t%s\n", util.YesNo(loadBalancer.PublicNet.Enabled)) fmt.Printf(" IPv4:\t\t\t\t%s\n", loadBalancer.PublicNet.IPv4.IP.String()) fmt.Printf(" IPv6:\t\t\t\t%s\n", loadBalancer.PublicNet.IPv6.IP.String()) @@ -91,9 +92,9 @@ func loadBalancerDescribeText(cli *state.State, loadBalancer *hcloud.LoadBalance fmt.Printf(" - Protocol:\t\t\t%s\n", service.Protocol) fmt.Printf(" Listen Port:\t\t%d\n", service.ListenPort) fmt.Printf(" Destination Port:\t\t%d\n", service.DestinationPort) - fmt.Printf(" Proxy Protocol:\t\t%s\n", yesno(service.Proxyprotocol)) + fmt.Printf(" Proxy Protocol:\t\t%s\n", util.YesNo(service.Proxyprotocol)) if service.Protocol != hcloud.LoadBalancerServiceProtocolTCP { - fmt.Printf(" Sticky Sessions:\t\t%s\n", yesno(service.HTTP.StickySessions)) + fmt.Printf(" Sticky Sessions:\t\t%s\n", util.YesNo(service.HTTP.StickySessions)) if service.HTTP.StickySessions { fmt.Printf(" Sticky Cookie Name:\t\t%s\n", service.HTTP.CookieName) fmt.Printf(" Sticky Cookie Lifetime:\t%vs\n", service.HTTP.CookieLifetime.Seconds()) @@ -115,7 +116,7 @@ func loadBalancerDescribeText(cli *state.State, loadBalancer *hcloud.LoadBalance fmt.Printf(" HTTP Domain:\t\t%s\n", service.HealthCheck.HTTP.Domain) fmt.Printf(" HTTP Path:\t\t%s\n", service.HealthCheck.HTTP.Path) fmt.Printf(" Response:\t\t%s\n", service.HealthCheck.HTTP.Response) - fmt.Printf(" TLS:\t\t\t%s\n", yesno(service.HealthCheck.HTTP.TLS)) + fmt.Printf(" TLS:\t\t\t%s\n", util.YesNo(service.HealthCheck.HTTP.TLS)) fmt.Printf(" Status Codes:\t\t%v\n", service.HealthCheck.HTTP.StatusCodes) } } @@ -132,7 +133,7 @@ func loadBalancerDescribeText(cli *state.State, loadBalancer *hcloud.LoadBalance fmt.Printf(" Server:\n") fmt.Printf(" ID:\t\t\t%d\n", target.Server.Server.ID) fmt.Printf(" Name:\t\t\t%s\n", cli.ServerName(target.Server.Server.ID)) - fmt.Printf(" Use Private IP:\t\t%s\n", yesno(target.UsePrivateIP)) + fmt.Printf(" Use Private IP:\t\t%s\n", util.YesNo(target.UsePrivateIP)) fmt.Printf(" Status:\n") for _, healthStatus := range target.HealthStatus { fmt.Printf(" - Service:\t\t\t%d\n", healthStatus.ListenPort) @@ -172,7 +173,7 @@ func loadBalancerDescribeText(cli *state.State, loadBalancer *hcloud.LoadBalance fmt.Printf(" Included:\t%v\n", humanize.IBytes(loadBalancer.IncludedTraffic)) fmt.Printf("Protection:\n") - fmt.Printf(" Delete:\t%s\n", yesno(loadBalancer.Protection.Delete)) + fmt.Printf(" Delete:\t%s\n", util.YesNo(loadBalancer.Protection.Delete)) fmt.Print("Labels:\n") if len(loadBalancer.Labels) == 0 { @@ -192,10 +193,10 @@ func loadBalancerDescribeJSON(resp *hcloud.Response) error { return err } if loadBalancer, ok := data["load_balancer"]; ok { - return describeJSON(loadBalancer) + return util.DescribeJSON(loadBalancer) } if loadBalancers, ok := data["load_balancers"].([]interface{}); ok { - return describeJSON(loadBalancers[0]) + return util.DescribeJSON(loadBalancers[0]) } - return describeJSON(data) + return util.DescribeJSON(data) } diff --git a/internal/cmds/load_balancer_list.go b/internal/cmds/load_balancer_list.go index 9909edb9..b9ee79b1 100644 --- a/internal/cmds/load_balancer_list.go +++ b/internal/cmds/load_balancer_list.go @@ -3,6 +3,7 @@ package cmds import ( "strings" + "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud/schema" @@ -20,7 +21,7 @@ func newLoadBalancerListCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "list [FLAGS]", Short: "List Load Balancers", - Long: listLongDescription( + Long: util.ListLongDescription( "Displays a list of Load Balancers.", loadBalancerListTableOutput.Columns(), ), @@ -65,8 +66,8 @@ func runLoadBalancerList(cli *state.State, cmd *cobra.Command, args []string) er }, Created: loadBalancer.Created, Labels: loadBalancer.Labels, - LoadBalancerType: loadBalancerTypeToSchema(*loadBalancer.LoadBalancerType), - Location: locationToSchema(*loadBalancer.Location), + LoadBalancerType: util.LoadBalancerTypeToSchema(*loadBalancer.LoadBalancerType), + Location: util.LocationToSchema(*loadBalancer.Location), IncludedTraffic: loadBalancer.IncludedTraffic, OutgoingTraffic: &loadBalancer.OutgoingTraffic, IngoingTraffic: &loadBalancer.IngoingTraffic, @@ -133,7 +134,7 @@ func runLoadBalancerList(cli *state.State, cmd *cobra.Command, args []string) er loadBalancerSchemas = append(loadBalancerSchemas, loadBalancerSchema) } - return describeJSON(loadBalancerSchemas) + return util.DescribeJSON(loadBalancerSchemas) } cols := []string{"id", "name", "ipv4", "ipv6", "type", "location", "network_zone"} if outOpts.IsSet("columns") { @@ -180,7 +181,7 @@ func describeLoadBalancerListTableOutput(cli *state.State) *tableOutput { })). AddFieldOutputFn("labels", fieldOutputFn(func(obj interface{}) string { loadBalancer := obj.(*hcloud.LoadBalancer) - return labelsToString(loadBalancer.Labels) + return util.LabelsToString(loadBalancer.Labels) })). AddFieldOutputFn("protection", fieldOutputFn(func(obj interface{}) string { loadBalancer := obj.(*hcloud.LoadBalancer) @@ -192,6 +193,6 @@ func describeLoadBalancerListTableOutput(cli *state.State) *tableOutput { })). AddFieldOutputFn("created", fieldOutputFn(func(obj interface{}) string { loadBalancer := obj.(*hcloud.LoadBalancer) - return datetime(loadBalancer.Created) + return util.Datetime(loadBalancer.Created) })) } diff --git a/internal/cmds/load_balancer_metrics.go b/internal/cmds/load_balancer_metrics.go index c6dd46ce..95f36a08 100644 --- a/internal/cmds/load_balancer_metrics.go +++ b/internal/cmds/load_balancer_metrics.go @@ -10,6 +10,7 @@ import ( "github.com/guptarohit/asciigraph" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" @@ -112,5 +113,5 @@ func loadBalancerMetricsJSON(resp *hcloud.Response) error { if err := json.NewDecoder(resp.Body).Decode(&data); err != nil { return err } - return describeJSON(data) + return util.DescribeJSON(data) } diff --git a/internal/cmds/load_balancer_remove_label.go b/internal/cmds/load_balancer_remove_label.go index 425422fe..e1e30dd5 100644 --- a/internal/cmds/load_balancer_remove_label.go +++ b/internal/cmds/load_balancer_remove_label.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" @@ -26,7 +27,7 @@ func newLoadBalancerRemoveLabelCommand(cli *state.State) *cobra.Command { ), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: chainRunE(validateLoadBalancerRemoveLabel, cli.EnsureToken), + PreRunE: util.ChainRunE(validateLoadBalancerRemoveLabel, cli.EnsureToken), RunE: cli.Wrap(runLoadBalancerRemoveLabel), } diff --git a/internal/cmds/load_balancer_remove_target.go b/internal/cmds/load_balancer_remove_target.go index 5246c6eb..301672c3 100644 --- a/internal/cmds/load_balancer_remove_target.go +++ b/internal/cmds/load_balancer_remove_target.go @@ -5,6 +5,7 @@ import ( "net" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" @@ -53,7 +54,7 @@ func runLoadBalancerRemoveTarget(cli *state.State, cmd *cobra.Command, args []st return fmt.Errorf("Load Balancer not found: %s", idOrName) } - if !exactlyOneSet(serverIDOrName, labelSelector, ipAddr) { + if !util.ExactlyOneSet(serverIDOrName, labelSelector, ipAddr) { return fmt.Errorf("--server, --label-selector, and --ip are mutually exclusive") } switch { diff --git a/internal/cmds/load_balancer_type_describe.go b/internal/cmds/load_balancer_type_describe.go index 21c19697..b3e1bdc0 100644 --- a/internal/cmds/load_balancer_type_describe.go +++ b/internal/cmds/load_balancer_type_describe.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" @@ -41,7 +42,7 @@ func runLoadBalancerTypeDescribe(cli *state.State, cmd *cobra.Command, args []st case outputFlags.IsSet("json"): return loadBalancerTypeDescribeJSON(resp) case outputFlags.IsSet("format"): - return describeFormat(loadBalancerType, outputFlags["format"][0]) + return util.DescribeFormat(loadBalancerType, outputFlags["format"][0]) default: return loadBalancerTypeDescribeText(cli, loadBalancerType) } @@ -71,10 +72,10 @@ func loadBalancerTypeDescribeJSON(resp *hcloud.Response) error { return err } if loadBalancerType, ok := data["loadBalancerType"]; ok { - return describeJSON(loadBalancerType) + return util.DescribeJSON(loadBalancerType) } if loadBalancerTypes, ok := data["loadBalancerTypes"].([]interface{}); ok { - return describeJSON(loadBalancerTypes[0]) + return util.DescribeJSON(loadBalancerTypes[0]) } - return describeJSON(data) + return util.DescribeJSON(data) } diff --git a/internal/cmds/load_balancer_type_list.go b/internal/cmds/load_balancer_type_list.go index 81601057..2b502e55 100644 --- a/internal/cmds/load_balancer_type_list.go +++ b/internal/cmds/load_balancer_type_list.go @@ -1,6 +1,7 @@ package cmds import ( + "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/hetznercloud/hcloud-go/hcloud/schema" @@ -18,7 +19,7 @@ func newLoadBalancerTypeListCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "list [FLAGS]", Short: "List Load Balancer types", - Long: listLongDescription( + Long: util.ListLongDescription( "Displays a list of Load Balancer types.", loadBalancerTypeListTableOutput.Columns(), ), @@ -42,9 +43,9 @@ func runLoadBalancerTypeList(cli *state.State, cmd *cobra.Command, args []string if outOpts.IsSet("json") { var loadBalancerTypeSchemas []schema.LoadBalancerType for _, loadBalancerType := range loadBalancerTypes { - loadBalancerTypeSchemas = append(loadBalancerTypeSchemas, loadBalancerTypeToSchema(*loadBalancerType)) + loadBalancerTypeSchemas = append(loadBalancerTypeSchemas, util.LoadBalancerTypeToSchema(*loadBalancerType)) } - return describeJSON(loadBalancerTypeSchemas) + return util.DescribeJSON(loadBalancerTypeSchemas) } cols := []string{"id", "name", "description", "max_services", "max_connections", "max_targets"} diff --git a/internal/cmds/location_describe.go b/internal/cmds/location_describe.go index b51bdb24..b28172bf 100644 --- a/internal/cmds/location_describe.go +++ b/internal/cmds/location_describe.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" @@ -41,7 +42,7 @@ func runLocationDescribe(cli *state.State, cmd *cobra.Command, args []string) er case outputFlags.IsSet("json"): return locationDescribeJSON(resp) case outputFlags.IsSet("format"): - return describeFormat(location, outputFlags["format"][0]) + return util.DescribeFormat(location, outputFlags["format"][0]) default: return locationDescribeText(cli, location) } @@ -65,10 +66,10 @@ func locationDescribeJSON(resp *hcloud.Response) error { return err } if location, ok := data["location"]; ok { - return describeJSON(location) + return util.DescribeJSON(location) } if locations, ok := data["locations"].([]interface{}); ok { - return describeJSON(locations[0]) + return util.DescribeJSON(locations[0]) } - return describeJSON(data) + return util.DescribeJSON(data) } diff --git a/internal/cmds/location_list.go b/internal/cmds/location_list.go index e6793554..b429cfb2 100644 --- a/internal/cmds/location_list.go +++ b/internal/cmds/location_list.go @@ -1,6 +1,7 @@ package cmds import ( + "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/hetznercloud/hcloud-go/hcloud/schema" @@ -18,7 +19,7 @@ func newLocationListCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "list [FLAGS]", Short: "List locations", - Long: listLongDescription( + Long: util.ListLongDescription( "Displays a list of locations.", locationListTableOutput.Columns(), ), @@ -42,9 +43,9 @@ func runLocationList(cli *state.State, cmd *cobra.Command, args []string) error if outOpts.IsSet("json") { var locationSchemas []schema.Location for _, location := range locations { - locationSchemas = append(locationSchemas, locationToSchema(*location)) + locationSchemas = append(locationSchemas, util.LocationToSchema(*location)) } - return describeJSON(locationSchemas) + return util.DescribeJSON(locationSchemas) } cols := []string{"id", "name", "description", "network_zone", "country", "city"} diff --git a/internal/cmds/network_add_label.go b/internal/cmds/network_add_label.go index fb841081..5158b90b 100644 --- a/internal/cmds/network_add_label.go +++ b/internal/cmds/network_add_label.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" @@ -17,7 +18,7 @@ func newNetworkAddLabelCommand(cli *state.State) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.NetworkNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: chainRunE(validateNetworkAddLabel, cli.EnsureToken), + PreRunE: util.ChainRunE(validateNetworkAddLabel, cli.EnsureToken), RunE: cli.Wrap(runNetworkAddLabel), } @@ -26,7 +27,7 @@ func newNetworkAddLabelCommand(cli *state.State) *cobra.Command { } func validateNetworkAddLabel(cmd *cobra.Command, args []string) error { - label := splitLabel(args[1]) + label := util.SplitLabel(args[1]) if len(label) != 2 { return fmt.Errorf("invalid label: %s", args[1]) } @@ -44,7 +45,7 @@ func runNetworkAddLabel(cli *state.State, cmd *cobra.Command, args []string) err if network == nil { return fmt.Errorf("network not found: %s", idOrName) } - label := splitLabel(args[1]) + label := util.SplitLabel(args[1]) if _, ok := network.Labels[label[0]]; ok && !overwrite { return fmt.Errorf("label %s on network %d already exists", label[0], network.ID) diff --git a/internal/cmds/network_describe.go b/internal/cmds/network_describe.go index f1b2b341..2f4d4d1d 100644 --- a/internal/cmds/network_describe.go +++ b/internal/cmds/network_describe.go @@ -5,6 +5,7 @@ import ( humanize "github.com/dustin/go-humanize" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" @@ -41,7 +42,7 @@ func runNetworkDescribe(cli *state.State, cmd *cobra.Command, args []string) err case outputFlags.IsSet("json"): return serverDescribeJSON(resp) case outputFlags.IsSet("format"): - return describeFormat(network, outputFlags["format"][0]) + return util.DescribeFormat(network, outputFlags["format"][0]) default: return networkDescribeText(cli, network) } @@ -50,7 +51,7 @@ func runNetworkDescribe(cli *state.State, cmd *cobra.Command, args []string) err func networkDescribeText(cli *state.State, network *hcloud.Network) error { fmt.Printf("ID:\t\t%d\n", network.ID) fmt.Printf("Name:\t\t%s\n", network.Name) - fmt.Printf("Created:\t%s (%s)\n", datetime(network.Created), humanize.Time(network.Created)) + fmt.Printf("Created:\t%s (%s)\n", util.Datetime(network.Created), humanize.Time(network.Created)) fmt.Printf("IP Range:\t%s\n", network.IPRange.String()) fmt.Printf("Subnets:\n") @@ -79,7 +80,7 @@ func networkDescribeText(cli *state.State, network *hcloud.Network) error { } fmt.Printf("Protection:\n") - fmt.Printf(" Delete:\t%s\n", yesno(network.Protection.Delete)) + fmt.Printf(" Delete:\t%s\n", util.YesNo(network.Protection.Delete)) fmt.Print("Labels:\n") if len(network.Labels) == 0 { diff --git a/internal/cmds/network_list.go b/internal/cmds/network_list.go index 57e9572e..9a06043b 100644 --- a/internal/cmds/network_list.go +++ b/internal/cmds/network_list.go @@ -4,6 +4,7 @@ import ( "fmt" "strings" + "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/hetznercloud/hcloud-go/hcloud/schema" @@ -20,7 +21,7 @@ func newNetworkListCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "list [FLAGS]", Short: "List networks", - Long: listLongDescription( + Long: util.ListLongDescription( "Displays a list of networks.", networkListTableOutput.Columns(), ), @@ -79,7 +80,7 @@ func runNetworkList(cli *state.State, cmd *cobra.Command, args []string) error { } networkSchemas = append(networkSchemas, networkSchema) } - return describeJSON(networkSchemas) + return util.DescribeJSON(networkSchemas) } cols := []string{"id", "name", "ip_range", "servers"} @@ -119,7 +120,7 @@ func describeNetworkListTableOutput(cli *state.State) *tableOutput { })). AddFieldOutputFn("labels", fieldOutputFn(func(obj interface{}) string { network := obj.(*hcloud.Network) - return labelsToString(network.Labels) + return util.LabelsToString(network.Labels) })). AddFieldOutputFn("protection", fieldOutputFn(func(obj interface{}) string { network := obj.(*hcloud.Network) @@ -131,6 +132,6 @@ func describeNetworkListTableOutput(cli *state.State) *tableOutput { })). AddFieldOutputFn("created", fieldOutputFn(func(obj interface{}) string { network := obj.(*hcloud.Network) - return datetime(network.Created) + return util.Datetime(network.Created) })) } diff --git a/internal/cmds/network_remove_label.go b/internal/cmds/network_remove_label.go index 515a779e..8471eb48 100644 --- a/internal/cmds/network_remove_label.go +++ b/internal/cmds/network_remove_label.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" @@ -26,7 +27,7 @@ func newNetworkRemoveLabelCommand(cli *state.State) *cobra.Command { ), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: chainRunE(validateNetworkRemoveLabel, cli.EnsureToken), + PreRunE: util.ChainRunE(validateNetworkRemoveLabel, cli.EnsureToken), RunE: cli.Wrap(runNetworkRemoveLabel), } diff --git a/internal/cmds/output.go b/internal/cmds/output.go index f09817f9..7be1b22d 100644 --- a/internal/cmds/output.go +++ b/internal/cmds/output.go @@ -12,6 +12,7 @@ import ( "github.com/fatih/structs" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/cmd/util" "github.com/spf13/cobra" ) @@ -58,7 +59,7 @@ func addOutputFlag(cmd *cobra.Command, options ...outputOption) { fmt.Sprintf("output options: %s", strings.Join(names, "|")), ) cmd.RegisterFlagCompletionFunc(flagName, cmpl.SuggestCandidates(values...)) - cmd.PreRunE = chainRunE(cmd.PreRunE, validateOutputFlag(options)) + cmd.PreRunE = util.ChainRunE(cmd.PreRunE, validateOutputFlag(options)) } func validateOutputFlag(options []outputOption) func(cmd *cobra.Command, args []string) error { @@ -275,15 +276,15 @@ func (o *tableOutput) Write(columns []string, obj interface{}) { } if value, ok := dataL[strings.Replace(colName, "_", "", -1)]; ok { if value == nil { - out = append(out, na("")) + out = append(out, util.NA("")) continue } if b, ok := value.(bool); ok { - out = append(out, yesno(b)) + out = append(out, util.YesNo(b)) continue } if s, ok := value.(string); ok { - out = append(out, na(s)) + out = append(out, util.NA(s)) continue } out = append(out, fmt.Sprintf("%v", value)) diff --git a/internal/cmds/server_add_label.go b/internal/cmds/server_add_label.go index bdfcf26f..4a89376c 100644 --- a/internal/cmds/server_add_label.go +++ b/internal/cmds/server_add_label.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" @@ -17,7 +18,7 @@ func newServerAddLabelCommand(cli *state.State) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.ServerNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: chainRunE(validateServerAddLabel, cli.EnsureToken), + PreRunE: util.ChainRunE(validateServerAddLabel, cli.EnsureToken), RunE: cli.Wrap(runServerAddLabel), } @@ -26,7 +27,7 @@ func newServerAddLabelCommand(cli *state.State) *cobra.Command { } func validateServerAddLabel(cmd *cobra.Command, args []string) error { - label := splitLabel(args[1]) + label := util.SplitLabel(args[1]) if len(label) != 2 { return fmt.Errorf("invalid label: %s", args[1]) } @@ -44,7 +45,7 @@ func runServerAddLabel(cli *state.State, cmd *cobra.Command, args []string) erro if server == nil { return fmt.Errorf("server not found: %s", idOrName) } - label := splitLabel(args[1]) + label := util.SplitLabel(args[1]) if _, ok := server.Labels[label[0]]; ok && !overwrite { return fmt.Errorf("label %s on server %d already exists", label[0], server.ID) diff --git a/internal/cmds/server_create_image.go b/internal/cmds/server_create_image.go index 18bd6764..e5dd71f7 100644 --- a/internal/cmds/server_create_image.go +++ b/internal/cmds/server_create_image.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" @@ -16,7 +17,7 @@ func newServerCreateImageCommand(cli *state.State) *cobra.Command { Args: cobra.ExactArgs(1), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: chainRunE(validateServerCreateImage, cli.EnsureToken), + PreRunE: util.ChainRunE(validateServerCreateImage, cli.EnsureToken), RunE: cli.Wrap(runServerCreateImage), } cmd.Flags().String("type", "", "Image type (required)") diff --git a/internal/cmds/server_describe.go b/internal/cmds/server_describe.go index 559b459b..c5c8bfad 100644 --- a/internal/cmds/server_describe.go +++ b/internal/cmds/server_describe.go @@ -6,6 +6,7 @@ import ( humanize "github.com/dustin/go-humanize" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" @@ -42,7 +43,7 @@ func runServerDescribe(cli *state.State, cmd *cobra.Command, args []string) erro case outputFlags.IsSet("json"): return serverDescribeJSON(resp) case outputFlags.IsSet("format"): - return describeFormat(server, outputFlags["format"][0]) + return util.DescribeFormat(server, outputFlags["format"][0]) default: return serverDescribeText(cli, server) } @@ -52,7 +53,7 @@ func serverDescribeText(cli *state.State, server *hcloud.Server) error { fmt.Printf("ID:\t\t%d\n", server.ID) fmt.Printf("Name:\t\t%s\n", server.Name) fmt.Printf("Status:\t\t%s\n", server.Status) - fmt.Printf("Created:\t%s (%s)\n", datetime(server.Created), humanize.Time(server.Created)) + fmt.Printf("Created:\t%s (%s)\n", util.Datetime(server.Created), humanize.Time(server.Created)) fmt.Printf("Server Type:\t%s (ID: %d)\n", server.ServerType.Name, server.ServerType.ID) fmt.Printf(" ID:\t\t%d\n", server.ServerType.ID) @@ -67,11 +68,11 @@ func serverDescribeText(cli *state.State, server *hcloud.Server) error { fmt.Printf("Public Net:\n") fmt.Printf(" IPv4:\n") fmt.Printf(" IP:\t\t%s\n", server.PublicNet.IPv4.IP) - fmt.Printf(" Blocked:\t%s\n", yesno(server.PublicNet.IPv4.Blocked)) + fmt.Printf(" Blocked:\t%s\n", util.YesNo(server.PublicNet.IPv4.Blocked)) fmt.Printf(" DNS:\t%s\n", server.PublicNet.IPv4.DNSPtr) fmt.Printf(" IPv6:\n") fmt.Printf(" IP:\t\t%s\n", server.PublicNet.IPv6.Network.String()) - fmt.Printf(" Blocked:\t%s\n", yesno(server.PublicNet.IPv6.Blocked)) + fmt.Printf(" Blocked:\t%s\n", util.YesNo(server.PublicNet.IPv6.Blocked)) fmt.Printf(" Floating IPs:\n") if len(server.PublicNet.FloatingIPs) > 0 { for _, f := range server.PublicNet.FloatingIPs { @@ -80,7 +81,7 @@ func serverDescribeText(cli *state.State, server *hcloud.Server) error { return fmt.Errorf("error fetching Floating IP: %v", err) } fmt.Printf(" - ID:\t\t\t%d\n", floatingIP.ID) - fmt.Printf(" Description:\t%s\n", na(floatingIP.Description)) + fmt.Printf(" Description:\t%s\n", util.NA(floatingIP.Description)) fmt.Printf(" IP:\t\t\t%s\n", floatingIP.IP) } } else { @@ -104,7 +105,7 @@ func serverDescribeText(cli *state.State, server *hcloud.Server) error { fmt.Printf(" -\t\t\t%s\n", a) } } else { - fmt.Printf(" Alias IPs:\t\t%s\n", na("")) + fmt.Printf(" Alias IPs:\t\t%s\n", util.NA("")) } } } else { @@ -131,18 +132,18 @@ func serverDescribeText(cli *state.State, server *hcloud.Server) error { fmt.Printf(" ID:\t\t%d\n", image.ID) fmt.Printf(" Type:\t\t%s\n", image.Type) fmt.Printf(" Status:\t%s\n", image.Status) - fmt.Printf(" Name:\t\t%s\n", na(image.Name)) + fmt.Printf(" Name:\t\t%s\n", util.NA(image.Name)) fmt.Printf(" Description:\t%s\n", image.Description) if image.ImageSize != 0 { fmt.Printf(" Image size:\t%.2f GB\n", image.ImageSize) } else { - fmt.Printf(" Image size:\t%s\n", na("")) + fmt.Printf(" Image size:\t%s\n", util.NA("")) } fmt.Printf(" Disk size:\t%.0f GB\n", image.DiskSize) - fmt.Printf(" Created:\t%s (%s)\n", datetime(image.Created), humanize.Time(image.Created)) + fmt.Printf(" Created:\t%s (%s)\n", util.Datetime(image.Created), humanize.Time(image.Created)) fmt.Printf(" OS flavor:\t%s\n", image.OSFlavor) - fmt.Printf(" OS version:\t%s\n", na(image.OSVersion)) - fmt.Printf(" Rapid deploy:\t%s\n", yesno(image.RapidDeploy)) + fmt.Printf(" OS version:\t%s\n", util.NA(image.OSVersion)) + fmt.Printf(" Rapid deploy:\t%s\n", util.YesNo(image.RapidDeploy)) } else { fmt.Printf(" No Image\n") } @@ -187,8 +188,8 @@ func serverDescribeText(cli *state.State, server *hcloud.Server) error { } fmt.Printf("Protection:\n") - fmt.Printf(" Delete:\t%s\n", yesno(server.Protection.Delete)) - fmt.Printf(" Rebuild:\t%s\n", yesno(server.Protection.Rebuild)) + fmt.Printf(" Delete:\t%s\n", util.YesNo(server.Protection.Delete)) + fmt.Printf(" Rebuild:\t%s\n", util.YesNo(server.Protection.Rebuild)) fmt.Print("Labels:\n") if len(server.Labels) == 0 { @@ -208,10 +209,10 @@ func serverDescribeJSON(resp *hcloud.Response) error { return err } if server, ok := data["server"]; ok { - return describeJSON(server) + return util.DescribeJSON(server) } if servers, ok := data["servers"].([]interface{}); ok { - return describeJSON(servers[0]) + return util.DescribeJSON(servers[0]) } - return describeJSON(data) + return util.DescribeJSON(data) } diff --git a/internal/cmds/server_list.go b/internal/cmds/server_list.go index 5e3ec1bc..1916d579 100644 --- a/internal/cmds/server_list.go +++ b/internal/cmds/server_list.go @@ -4,6 +4,7 @@ import ( "strconv" "strings" + "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud/schema" @@ -21,7 +22,7 @@ func newServerListCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "list [FLAGS]", Short: "List servers", - Long: listLongDescription( + Long: util.ListLongDescription( "Displays a list of servers.", serverListTableOutput.Columns(), ), @@ -58,8 +59,8 @@ func runServerList(cli *state.State, cmd *cobra.Command, args []string) error { Name: server.Name, Status: string(server.Status), Created: server.Created, - Datacenter: datacenterToSchema(*server.Datacenter), - ServerType: serverTypeToSchema(*server.ServerType), + Datacenter: util.DatacenterToSchema(*server.Datacenter), + ServerType: util.ServerTypeToSchema(*server.ServerType), PublicNet: schema.ServerPublicNet{ IPv4: schema.ServerPublicNetIPv4{ IP: server.PublicNet.IPv4.IP.String(), @@ -84,11 +85,11 @@ func runServerList(cli *state.State, cmd *cobra.Command, args []string) error { PrimaryDiskSize: server.PrimaryDiskSize, } if server.Image != nil { - serverImage := imageToSchema(*server.Image) + serverImage := util.ImageToSchema(*server.Image) serverSchema.Image = &serverImage } if server.ISO != nil { - serverISO := isoToSchema(*server.ISO) + serverISO := util.ISOToSchema(*server.ISO) serverSchema.ISO = &serverISO } for ip, dnsPTR := range server.PublicNet.IPv6.DNSPtr { @@ -116,7 +117,7 @@ func runServerList(cli *state.State, cmd *cobra.Command, args []string) error { } serversSchema = append(serversSchema, serverSchema) } - return describeJSON(serversSchema) + return util.DescribeJSON(serversSchema) } cols := []string{"id", "name", "status", "ipv4", "ipv6", "datacenter"} @@ -160,7 +161,7 @@ func describeServerListTableOutput(cli *state.State) *tableOutput { })). AddFieldOutputFn("labels", fieldOutputFn(func(obj interface{}) string { server := obj.(*hcloud.Server) - return labelsToString(server.Labels) + return util.LabelsToString(server.Labels) })). AddFieldOutputFn("type", fieldOutputFn(func(obj interface{}) string { server := obj.(*hcloud.Server) @@ -183,7 +184,7 @@ func describeServerListTableOutput(cli *state.State) *tableOutput { networks = append(networks, cli.NetworkName(network.Network.ID)) } } - return na(strings.Join(networks, ", ")) + return util.NA(strings.Join(networks, ", ")) })). AddFieldOutputFn("protection", fieldOutputFn(func(obj interface{}) string { server := obj.(*hcloud.Server) @@ -198,6 +199,6 @@ func describeServerListTableOutput(cli *state.State) *tableOutput { })). AddFieldOutputFn("created", fieldOutputFn(func(obj interface{}) string { server := obj.(*hcloud.Server) - return datetime(server.Created) + return util.Datetime(server.Created) })) } diff --git a/internal/cmds/server_metrics.go b/internal/cmds/server_metrics.go index 68873880..ed9c27e8 100644 --- a/internal/cmds/server_metrics.go +++ b/internal/cmds/server_metrics.go @@ -10,6 +10,7 @@ import ( "github.com/guptarohit/asciigraph" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" @@ -111,5 +112,5 @@ func serverMetricsJSON(resp *hcloud.Response) error { if err := json.NewDecoder(resp.Body).Decode(&data); err != nil { return err } - return describeJSON(data) + return util.DescribeJSON(data) } diff --git a/internal/cmds/server_remove_label.go b/internal/cmds/server_remove_label.go index efca8ce2..505a66a2 100644 --- a/internal/cmds/server_remove_label.go +++ b/internal/cmds/server_remove_label.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" @@ -26,7 +27,7 @@ func newServerRemoveLabelCommand(cli *state.State) *cobra.Command { ), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: chainRunE(validateServerRemoveLabel, cli.EnsureToken), + PreRunE: util.ChainRunE(validateServerRemoveLabel, cli.EnsureToken), RunE: cli.Wrap(runServerRemoveLabel), } diff --git a/internal/cmds/server_request_console.go b/internal/cmds/server_request_console.go index 17112ad4..7d754270 100644 --- a/internal/cmds/server_request_console.go +++ b/internal/cmds/server_request_console.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/spf13/cobra" ) @@ -44,7 +45,7 @@ func runServerRequestConsole(cli *state.State, cmd *cobra.Command, args []string } if outOpts.IsSet("json") { - return describeJSON(struct { + return util.DescribeJSON(struct { WSSURL string Password string }{ diff --git a/internal/cmds/servertypes_describe.go b/internal/cmds/servertypes_describe.go index ec71fc79..812ea4b4 100644 --- a/internal/cmds/servertypes_describe.go +++ b/internal/cmds/servertypes_describe.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" @@ -41,7 +42,7 @@ func runServerTypeDescribe(cli *state.State, cmd *cobra.Command, args []string) case outputFlags.IsSet("json"): return serverTypeDescribeJSON(resp) case outputFlags.IsSet("format"): - return describeFormat(serverType, outputFlags["format"][0]) + return util.DescribeFormat(serverType, outputFlags["format"][0]) default: return serverTypeDescribeText(cli, serverType) } @@ -72,10 +73,10 @@ func serverTypeDescribeJSON(resp *hcloud.Response) error { return err } if serverType, ok := data["server_type"]; ok { - return describeJSON(serverType) + return util.DescribeJSON(serverType) } if serverTypes, ok := data["server_types"].([]interface{}); ok { - return describeJSON(serverTypes[0]) + return util.DescribeJSON(serverTypes[0]) } - return describeJSON(data) + return util.DescribeJSON(data) } diff --git a/internal/cmds/servertypes_list.go b/internal/cmds/servertypes_list.go index bff15491..b8704ed1 100644 --- a/internal/cmds/servertypes_list.go +++ b/internal/cmds/servertypes_list.go @@ -3,6 +3,7 @@ package cmds import ( "fmt" + "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud/schema" @@ -30,7 +31,7 @@ func newServerTypeListCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "list [FLAGS]", Short: "List server types", - Long: listLongDescription( + Long: util.ListLongDescription( "Displays a list of server types.", serverTypeListTableOutput.Columns(), ), @@ -54,9 +55,9 @@ func runServerTypeList(cli *state.State, cmd *cobra.Command, args []string) erro if outOpts.IsSet("json") { var serverTypeSchemas []schema.ServerType for _, serverType := range serverTypes { - serverTypeSchemas = append(serverTypeSchemas, serverTypeToSchema(*serverType)) + serverTypeSchemas = append(serverTypeSchemas, util.ServerTypeToSchema(*serverType)) } - return describeJSON(serverTypeSchemas) + return util.DescribeJSON(serverTypeSchemas) } cols := []string{"id", "name", "cores", "cpu_type", "memory", "disk", "storage_type"} diff --git a/internal/cmds/sshkey_add_label.go b/internal/cmds/sshkey_add_label.go index 8606f9ed..c9d6843c 100644 --- a/internal/cmds/sshkey_add_label.go +++ b/internal/cmds/sshkey_add_label.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" @@ -17,7 +18,7 @@ func newSSHKeyAddLabelCommand(cli *state.State) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.SSHKeyNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: chainRunE(validateSSHKeyAddLabel, cli.EnsureToken), + PreRunE: util.ChainRunE(validateSSHKeyAddLabel, cli.EnsureToken), RunE: cli.Wrap(runSSHKeyAddLabel), } @@ -26,7 +27,7 @@ func newSSHKeyAddLabelCommand(cli *state.State) *cobra.Command { } func validateSSHKeyAddLabel(cmd *cobra.Command, args []string) error { - label := splitLabel(args[1]) + label := util.SplitLabel(args[1]) if len(label) != 2 { return fmt.Errorf("invalid label: %s", args[1]) } @@ -44,7 +45,7 @@ func runSSHKeyAddLabel(cli *state.State, cmd *cobra.Command, args []string) erro if sshKey == nil { return fmt.Errorf("SSH key not found: %s", idOrName) } - label := splitLabel(args[1]) + label := util.SplitLabel(args[1]) if _, ok := sshKey.Labels[label[0]]; ok && !overwrite { return fmt.Errorf("label %s on SSH key %d already exists", label[0], sshKey.ID) diff --git a/internal/cmds/sshkey_create.go b/internal/cmds/sshkey_create.go index 8d19b796..b1cb334e 100644 --- a/internal/cmds/sshkey_create.go +++ b/internal/cmds/sshkey_create.go @@ -6,6 +6,7 @@ import ( "io/ioutil" "os" + "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" @@ -18,7 +19,7 @@ func newSSHKeyCreateCommand(cli *state.State) *cobra.Command { Args: cobra.NoArgs, TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: chainRunE(validateSSHKeyCreate, cli.EnsureToken), + PreRunE: util.ChainRunE(validateSSHKeyCreate, cli.EnsureToken), RunE: cli.Wrap(runSSHKeyCreate), } cmd.Flags().String("name", "", "Key name (required)") diff --git a/internal/cmds/sshkey_describe.go b/internal/cmds/sshkey_describe.go index 9922b833..364a34a8 100644 --- a/internal/cmds/sshkey_describe.go +++ b/internal/cmds/sshkey_describe.go @@ -8,6 +8,7 @@ import ( "github.com/dustin/go-humanize" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" @@ -43,7 +44,7 @@ func runSSHKeyDescribe(cli *state.State, cmd *cobra.Command, args []string) erro case outputFlags.IsSet("json"): return sshKeyDescribeJSON(resp) case outputFlags.IsSet("format"): - return describeFormat(sshKey, outputFlags["format"][0]) + return util.DescribeFormat(sshKey, outputFlags["format"][0]) default: return sshKeyDescribeText(cli, sshKey) } @@ -52,7 +53,7 @@ func runSSHKeyDescribe(cli *state.State, cmd *cobra.Command, args []string) erro func sshKeyDescribeText(cli *state.State, sshKey *hcloud.SSHKey) error { fmt.Printf("ID:\t\t%d\n", sshKey.ID) fmt.Printf("Name:\t\t%s\n", sshKey.Name) - fmt.Printf("Created:\t%s (%s)\n", datetime(sshKey.Created), humanize.Time(sshKey.Created)) + fmt.Printf("Created:\t%s (%s)\n", util.Datetime(sshKey.Created), humanize.Time(sshKey.Created)) fmt.Printf("Fingerprint:\t%s\n", sshKey.Fingerprint) fmt.Printf("Public Key:\n%s\n", strings.TrimSpace(sshKey.PublicKey)) fmt.Print("Labels:\n") @@ -73,10 +74,10 @@ func sshKeyDescribeJSON(resp *hcloud.Response) error { return err } if sshKey, ok := data["ssh_key"]; ok { - return describeJSON(sshKey) + return util.DescribeJSON(sshKey) } if sshKeys, ok := data["ssh_keys"].([]interface{}); ok { - return describeJSON(sshKeys[0]) + return util.DescribeJSON(sshKeys[0]) } - return describeJSON(data) + return util.DescribeJSON(data) } diff --git a/internal/cmds/sshkey_list.go b/internal/cmds/sshkey_list.go index 15e99465..dd20f956 100644 --- a/internal/cmds/sshkey_list.go +++ b/internal/cmds/sshkey_list.go @@ -1,6 +1,7 @@ package cmds import ( + "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/hetznercloud/hcloud-go/hcloud/schema" @@ -14,11 +15,11 @@ func init() { AddAllowedFields(hcloud.SSHKey{}). AddFieldOutputFn("labels", fieldOutputFn(func(obj interface{}) string { sshKey := obj.(*hcloud.SSHKey) - return labelsToString(sshKey.Labels) + return util.LabelsToString(sshKey.Labels) })). AddFieldOutputFn("created", fieldOutputFn(func(obj interface{}) string { sshKey := obj.(*hcloud.SSHKey) - return datetime(sshKey.Created) + return util.Datetime(sshKey.Created) })) } @@ -26,7 +27,7 @@ func newSSHKeyListCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "list [FLAGS]", Short: "List SSH keys", - Long: listLongDescription( + Long: util.ListLongDescription( "Displays a list of SSH keys.", sshKeyListTableOutput.Columns(), ), @@ -68,7 +69,7 @@ func runSSHKeyList(cli *state.State, cmd *cobra.Command, args []string) error { } sshKeySchemas = append(sshKeySchemas, sshKeySchema) } - return describeJSON(sshKeySchemas) + return util.DescribeJSON(sshKeySchemas) } cols := []string{"id", "name", "fingerprint"} diff --git a/internal/cmds/sshkey_remove_label.go b/internal/cmds/sshkey_remove_label.go index 118b94a7..3915cb5b 100644 --- a/internal/cmds/sshkey_remove_label.go +++ b/internal/cmds/sshkey_remove_label.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" @@ -26,7 +27,7 @@ func newSSHKeyRemoveLabelCommand(cli *state.State) *cobra.Command { ), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: chainRunE(validateSSHKeyRemoveLabel, cli.EnsureToken), + PreRunE: util.ChainRunE(validateSSHKeyRemoveLabel, cli.EnsureToken), RunE: cli.Wrap(runSSHKeyRemoveLabel), } diff --git a/internal/cmds/volume_add_label.go b/internal/cmds/volume_add_label.go index 1026ca3b..94bc4334 100644 --- a/internal/cmds/volume_add_label.go +++ b/internal/cmds/volume_add_label.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" @@ -17,7 +18,7 @@ func newVolumeAddLabelCommand(cli *state.State) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.VolumeNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: chainRunE(validateVolumeAddLabel, cli.EnsureToken), + PreRunE: util.ChainRunE(validateVolumeAddLabel, cli.EnsureToken), RunE: cli.Wrap(runVolumeAddLabel), } @@ -26,7 +27,7 @@ func newVolumeAddLabelCommand(cli *state.State) *cobra.Command { } func validateVolumeAddLabel(cmd *cobra.Command, args []string) error { - label := splitLabel(args[1]) + label := util.SplitLabel(args[1]) if len(label) != 2 { return fmt.Errorf("invalid label: %s", args[1]) } @@ -43,7 +44,7 @@ func runVolumeAddLabel(cli *state.State, cmd *cobra.Command, args []string) erro if volume == nil { return fmt.Errorf("volume not found: %s", args[0]) } - label := splitLabel(args[1]) + label := util.SplitLabel(args[1]) if _, ok := volume.Labels[label[0]]; ok && !overwrite { return fmt.Errorf("label %s on volume %d already exists", label[0], volume.ID) diff --git a/internal/cmds/volume_describe.go b/internal/cmds/volume_describe.go index 34cd9052..47b727f6 100644 --- a/internal/cmds/volume_describe.go +++ b/internal/cmds/volume_describe.go @@ -6,6 +6,7 @@ import ( humanize "github.com/dustin/go-humanize" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" @@ -41,7 +42,7 @@ func runVolumeDescribe(cli *state.State, cmd *cobra.Command, args []string) erro case outputFlags.IsSet("json"): return volumeDescribeJSON(resp) case outputFlags.IsSet("format"): - return describeFormat(volume, outputFlags["format"][0]) + return util.DescribeFormat(volume, outputFlags["format"][0]) default: return volumeDescribeText(cli, volume) } @@ -50,7 +51,7 @@ func runVolumeDescribe(cli *state.State, cmd *cobra.Command, args []string) erro func volumeDescribeText(cli *state.State, volume *hcloud.Volume) error { fmt.Printf("ID:\t\t%d\n", volume.ID) fmt.Printf("Name:\t\t%s\n", volume.Name) - fmt.Printf("Created:\t%s (%s)\n", datetime(volume.Created), humanize.Time(volume.Created)) + fmt.Printf("Created:\t%s (%s)\n", util.Datetime(volume.Created), humanize.Time(volume.Created)) fmt.Printf("Size:\t\t%s\n", humanize.Bytes(uint64(volume.Size*humanize.GByte))) fmt.Printf("Linux Device:\t%s\n", volume.LinuxDevice) fmt.Printf("Location:\n") @@ -75,7 +76,7 @@ func volumeDescribeText(cli *state.State, volume *hcloud.Volume) error { fmt.Print("Server:\n Not attached\n") } fmt.Printf("Protection:\n") - fmt.Printf(" Delete:\t%s\n", yesno(volume.Protection.Delete)) + fmt.Printf(" Delete:\t%s\n", util.YesNo(volume.Protection.Delete)) fmt.Print("Labels:\n") if len(volume.Labels) == 0 { @@ -95,10 +96,10 @@ func volumeDescribeJSON(resp *hcloud.Response) error { return err } if volume, ok := data["volume"]; ok { - return describeJSON(volume) + return util.DescribeJSON(volume) } if volumes, ok := data["volumes"].([]interface{}); ok { - return describeJSON(volumes[0]) + return util.DescribeJSON(volumes[0]) } - return describeJSON(data) + return util.DescribeJSON(data) } diff --git a/internal/cmds/volume_list.go b/internal/cmds/volume_list.go index 8418a466..55bcf5fe 100644 --- a/internal/cmds/volume_list.go +++ b/internal/cmds/volume_list.go @@ -3,6 +3,7 @@ package cmds import ( "strings" + "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud/schema" @@ -21,7 +22,7 @@ func newVolumeListCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "list [FLAGS]", Short: "List volumes", - Long: listLongDescription( + Long: util.ListLongDescription( "Displays a list of volumes.", volumeListTableOutput.Columns(), ), @@ -56,7 +57,7 @@ func runVolumeList(cli *state.State, cmd *cobra.Command, args []string) error { volumeSchema := schema.Volume{ ID: volume.ID, Name: volume.Name, - Location: locationToSchema(*volume.Location), + Location: util.LocationToSchema(*volume.Location), Size: volume.Size, LinuxDevice: volume.LinuxDevice, Labels: volume.Labels, @@ -68,7 +69,7 @@ func runVolumeList(cli *state.State, cmd *cobra.Command, args []string) error { } volumesSchema = append(volumesSchema, volumeSchema) } - return describeJSON(volumesSchema) + return util.DescribeJSON(volumesSchema) } cols := []string{"id", "name", "size", "server", "location"} @@ -100,7 +101,7 @@ func describeVolumeListTableOutput(cli *state.State) *tableOutput { if volume.Server != nil && cli != nil { return cli.ServerName(volume.Server.ID) } - return na(server) + return util.NA(server) })). AddFieldOutputFn("size", fieldOutputFn(func(obj interface{}) string { volume := obj.(*hcloud.Volume) @@ -120,10 +121,10 @@ func describeVolumeListTableOutput(cli *state.State) *tableOutput { })). AddFieldOutputFn("labels", fieldOutputFn(func(obj interface{}) string { volume := obj.(*hcloud.Volume) - return labelsToString(volume.Labels) + return util.LabelsToString(volume.Labels) })). AddFieldOutputFn("created", fieldOutputFn(func(obj interface{}) string { volume := obj.(*hcloud.Volume) - return datetime(volume.Created) + return util.Datetime(volume.Created) })) } diff --git a/internal/cmds/volume_remove_label.go b/internal/cmds/volume_remove_label.go index 9d351595..2f144176 100644 --- a/internal/cmds/volume_remove_label.go +++ b/internal/cmds/volume_remove_label.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" @@ -26,7 +27,7 @@ func newVolumeRemoveLabelCommand(cli *state.State) *cobra.Command { ), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: chainRunE(validateVolumeRemoveLabel, cli.EnsureToken), + PreRunE: util.ChainRunE(validateVolumeRemoveLabel, cli.EnsureToken), RunE: cli.Wrap(runVolumeRemoveLabel), } From 5e2f07530f75952a0452e0595464661666364611 Mon Sep 17 00:00:00 2001 From: Michael Frister Date: Mon, 11 Jan 2021 09:12:12 +0100 Subject: [PATCH 3/9] Refactor: cmds: Move output utils to separate package Preparation for splitting up cmds into multiple packages. --- internal/{cmds => cmd/output}/output.go | 52 ++++++++++---------- internal/{cmds => cmd/output}/output_test.go | 6 +-- internal/cmds/certificate_describe.go | 5 +- internal/cmds/certificate_list.go | 21 ++++---- internal/cmds/context_list.go | 9 ++-- internal/cmds/datacenter_describe.go | 5 +- internal/cmds/datacenter_list.go | 11 +++-- internal/cmds/floatingip_describe.go | 5 +- internal/cmds/floatingip_list.go | 25 +++++----- internal/cmds/image_describe.go | 5 +- internal/cmds/image_list.go | 31 ++++++------ internal/cmds/iso_describe.go | 5 +- internal/cmds/iso_list.go | 9 ++-- internal/cmds/load_balancer_describe.go | 5 +- internal/cmds/load_balancer_list.go | 27 +++++----- internal/cmds/load_balancer_metrics.go | 5 +- internal/cmds/load_balancer_type_describe.go | 5 +- internal/cmds/load_balancer_type_list.go | 9 ++-- internal/cmds/location_describe.go | 5 +- internal/cmds/location_list.go | 9 ++-- internal/cmds/network_describe.go | 5 +- internal/cmds/network_list.go | 21 ++++---- internal/cmds/server_describe.go | 5 +- internal/cmds/server_list.go | 31 ++++++------ internal/cmds/server_metrics.go | 5 +- internal/cmds/server_request_console.go | 5 +- internal/cmds/servertypes_describe.go | 5 +- internal/cmds/servertypes_list.go | 13 ++--- internal/cmds/sshkey_describe.go | 5 +- internal/cmds/sshkey_list.go | 13 ++--- internal/cmds/volume_describe.go | 5 +- internal/cmds/volume_list.go | 23 ++++----- 32 files changed, 210 insertions(+), 180 deletions(-) rename internal/{cmds => cmd/output}/output.go (82%) rename internal/{cmds => cmd/output}/output_test.go (95%) diff --git a/internal/cmds/output.go b/internal/cmd/output/output.go similarity index 82% rename from internal/cmds/output.go rename to internal/cmd/output/output.go index 7be1b22d..f96e774a 100644 --- a/internal/cmds/output.go +++ b/internal/cmd/output/output.go @@ -1,4 +1,4 @@ -package cmds +package output import ( "fmt" @@ -23,23 +23,23 @@ type outputOption struct { Values []string } -func outputOptionNoHeader() outputOption { +func OptionNoHeader() outputOption { return outputOption{Name: "noheader"} } -func outputOptionJSON() outputOption { +func OptionJSON() outputOption { return outputOption{Name: "json"} } -func outputOptionFormat() outputOption { +func OptionFormat() outputOption { return outputOption{Name: "format"} } -func outputOptionColumns(columns []string) outputOption { +func OptionColumns(columns []string) outputOption { return outputOption{Name: "columns", Values: columns} } -func addOutputFlag(cmd *cobra.Command, options ...outputOption) { +func AddFlag(cmd *cobra.Command, options ...outputOption) { var ( names []string values []string @@ -100,7 +100,7 @@ func validateOutputFlag(options []outputOption) func(cmd *cobra.Command, args [] } } -func outputFlagsForCommand(cmd *cobra.Command) outputOpts { +func FlagsForCommand(cmd *cobra.Command) outputOpts { opts, _ := cmd.Flags().GetStringArray(flagName) return parseOutputFlags(opts) } @@ -139,35 +139,35 @@ func parseOutputFlags(in []string) outputOpts { return o } -// newTableOutput creates a new tableOutput. -func newTableOutput() *tableOutput { - return &tableOutput{ +// NewTable creates a new Table. +func NewTable() *Table { + return &Table{ w: tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0), columns: map[string]bool{}, - fieldMapping: map[string]fieldOutputFn{}, + fieldMapping: map[string]FieldFn{}, fieldAlias: map[string]string{}, allowedFields: map[string]bool{}, } } -type fieldOutputFn func(obj interface{}) string +type FieldFn func(obj interface{}) string type writerFlusher interface { io.Writer Flush() error } -// tableOutput is a generic way to format object as a table. -type tableOutput struct { +// Table is a generic way to format object as a table. +type Table struct { w writerFlusher columns map[string]bool - fieldMapping map[string]fieldOutputFn + fieldMapping map[string]FieldFn fieldAlias map[string]string allowedFields map[string]bool } // Columns returns a list of known output columns. -func (o *tableOutput) Columns() (cols []string) { +func (o *Table) Columns() (cols []string) { for c := range o.columns { cols = append(cols, c) } @@ -176,13 +176,13 @@ func (o *tableOutput) Columns() (cols []string) { } // AddFieldAlias overrides the field name to allow custom column headers. -func (o *tableOutput) AddFieldAlias(field, alias string) *tableOutput { +func (o *Table) AddFieldAlias(field, alias string) *Table { o.fieldAlias[field] = alias return o } -// AddFieldOutputFn adds a function which handles the output of the specified field. -func (o *tableOutput) AddFieldOutputFn(field string, fn fieldOutputFn) *tableOutput { +// AddFieldFn adds a function which handles the output of the specified field. +func (o *Table) AddFieldFn(field string, fn FieldFn) *Table { o.fieldMapping[field] = fn o.allowedFields[field] = true o.columns[field] = true @@ -190,7 +190,7 @@ func (o *tableOutput) AddFieldOutputFn(field string, fn fieldOutputFn) *tableOut } // AddAllowedFields reads all first level fieldnames of the struct and allows them to be used. -func (o *tableOutput) AddAllowedFields(obj interface{}) *tableOutput { +func (o *Table) AddAllowedFields(obj interface{}) *Table { v := reflect.ValueOf(obj) if v.Kind() != reflect.Struct { panic("AddAllowedFields input must be a struct") @@ -204,7 +204,7 @@ func (o *tableOutput) AddAllowedFields(obj interface{}) *tableOutput { k != reflect.String && k != reflect.Int { // only allow simple values - // complex values need to be mapped via a fieldOutputFn + // complex values need to be mapped via a FieldFn continue } o.allowedFields[strings.ToLower(t.Field(i).Name)] = true @@ -215,7 +215,7 @@ func (o *tableOutput) AddAllowedFields(obj interface{}) *tableOutput { } // RemoveAllowedField removes fields from the allowed list. -func (o *tableOutput) RemoveAllowedField(fields ...string) *tableOutput { +func (o *Table) RemoveAllowedField(fields ...string) *Table { for _, field := range fields { delete(o.allowedFields, field) delete(o.columns, field) @@ -224,7 +224,7 @@ func (o *tableOutput) RemoveAllowedField(fields ...string) *tableOutput { } // ValidateColumns returns an error if invalid columns are specified. -func (o *tableOutput) ValidateColumns(cols []string) error { +func (o *Table) ValidateColumns(cols []string) error { var invalidCols []string for _, col := range cols { if _, ok := o.allowedFields[strings.ToLower(col)]; !ok { @@ -238,7 +238,7 @@ func (o *tableOutput) ValidateColumns(cols []string) error { } // WriteHeader writes the table header. -func (o *tableOutput) WriteHeader(columns []string) { +func (o *Table) WriteHeader(columns []string) { var header []string for _, col := range columns { if alias, ok := o.fieldAlias[col]; ok { @@ -249,12 +249,12 @@ func (o *tableOutput) WriteHeader(columns []string) { fmt.Fprintln(o.w, strings.Join(header, "\t")) } -func (o *tableOutput) Flush() error { +func (o *Table) Flush() error { return o.w.Flush() } // Write writes a table line. -func (o *tableOutput) Write(columns []string, obj interface{}) { +func (o *Table) Write(columns []string, obj interface{}) { data := structs.Map(obj) dataL := map[string]interface{}{} for key, value := range data { diff --git a/internal/cmds/output_test.go b/internal/cmd/output/output_test.go similarity index 95% rename from internal/cmds/output_test.go rename to internal/cmd/output/output_test.go index 3c188e56..ef23dbbd 100644 --- a/internal/cmds/output_test.go +++ b/internal/cmd/output/output_test.go @@ -1,4 +1,4 @@ -package cmds +package output import ( "bytes" @@ -21,7 +21,7 @@ type testFieldsStruct struct { func TestTableOutput(t *testing.T) { var wfs writerFlusherStub - to := newTableOutput() + to := NewTable() to.w = &wfs t.Run("AddAllowedFields", func(t *testing.T) { @@ -37,7 +37,7 @@ func TestTableOutput(t *testing.T) { } }) t.Run("AddFieldOutputFn", func(t *testing.T) { - to.AddFieldOutputFn("leeroy jenkins", fieldOutputFn(func(obj interface{}) string { + to.AddFieldFn("leeroy jenkins", FieldFn(func(obj interface{}) string { return "LEEROY JENKINS!!!" })) if _, ok := to.fieldMapping["leeroy jenkins"]; !ok { diff --git a/internal/cmds/certificate_describe.go b/internal/cmds/certificate_describe.go index 01567b41..ecd6c9c3 100644 --- a/internal/cmds/certificate_describe.go +++ b/internal/cmds/certificate_describe.go @@ -6,6 +6,7 @@ import ( humanize "github.com/dustin/go-humanize" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/cmd/output" "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" @@ -23,12 +24,12 @@ func newCertificateDescribeCommand(cli *state.State) *cobra.Command { PreRunE: cli.EnsureToken, RunE: cli.Wrap(runCertificateDescribe), } - addOutputFlag(cmd, outputOptionJSON(), outputOptionFormat()) + output.AddFlag(cmd, output.OptionJSON(), output.OptionFormat()) return cmd } func runCertificateDescribe(cli *state.State, cmd *cobra.Command, args []string) error { - outputFlags := outputFlagsForCommand(cmd) + outputFlags := output.FlagsForCommand(cmd) idOrName := args[0] cert, resp, err := cli.Client().Certificate.Get(cli.Context, idOrName) diff --git a/internal/cmds/certificate_list.go b/internal/cmds/certificate_list.go index a48b89a3..1e121bdd 100644 --- a/internal/cmds/certificate_list.go +++ b/internal/cmds/certificate_list.go @@ -3,6 +3,7 @@ package cmds import ( "strings" + "github.com/hetznercloud/cli/internal/cmd/output" "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" @@ -10,7 +11,7 @@ import ( "github.com/spf13/cobra" ) -var certificateTableOutput *tableOutput +var certificateTableOutput *output.Table func init() { certificateTableOutput = describeCertificatesTableOutput() @@ -29,12 +30,12 @@ func newCertificatesListCommand(cli *state.State) *cobra.Command { PreRunE: cli.EnsureToken, RunE: cli.Wrap(runCertificatesList), } - addOutputFlag(cmd, outputOptionNoHeader(), outputOptionColumns(serverListTableOutput.Columns()), outputOptionJSON()) + output.AddFlag(cmd, output.OptionNoHeader(), output.OptionColumns(serverListTableOutput.Columns()), output.OptionJSON()) return cmd } func runCertificatesList(cli *state.State, cmd *cobra.Command, args []string) error { - outOpts := outputFlagsForCommand(cmd) + outOpts := output.FlagsForCommand(cmd) labelSelector, _ := cmd.Flags().GetString("selector") @@ -88,27 +89,27 @@ func runCertificatesList(cli *state.State, cmd *cobra.Command, args []string) er return tw.Flush() } -func describeCertificatesTableOutput() *tableOutput { - return newTableOutput(). +func describeCertificatesTableOutput() *output.Table { + return output.NewTable(). AddAllowedFields(hcloud.Certificate{}). RemoveAllowedField("certificate", "chain"). - AddFieldOutputFn("labels", fieldOutputFn(func(obj interface{}) string { + AddFieldFn("labels", output.FieldFn(func(obj interface{}) string { cert := obj.(*hcloud.Certificate) return util.LabelsToString(cert.Labels) })). - AddFieldOutputFn("not_valid_before", func(obj interface{}) string { + AddFieldFn("not_valid_before", func(obj interface{}) string { cert := obj.(*hcloud.Certificate) return util.Datetime(cert.NotValidBefore) }). - AddFieldOutputFn("not_valid_after", func(obj interface{}) string { + AddFieldFn("not_valid_after", func(obj interface{}) string { cert := obj.(*hcloud.Certificate) return util.Datetime(cert.NotValidAfter) }). - AddFieldOutputFn("domain_names", func(obj interface{}) string { + AddFieldFn("domain_names", func(obj interface{}) string { cert := obj.(*hcloud.Certificate) return strings.Join(cert.DomainNames, ", ") }). - AddFieldOutputFn("created", fieldOutputFn(func(obj interface{}) string { + AddFieldFn("created", output.FieldFn(func(obj interface{}) string { cert := obj.(*hcloud.Certificate) return util.Datetime(cert.Created) })) diff --git a/internal/cmds/context_list.go b/internal/cmds/context_list.go index 36538391..6ad8dd0d 100644 --- a/internal/cmds/context_list.go +++ b/internal/cmds/context_list.go @@ -1,12 +1,13 @@ package cmds import ( + "github.com/hetznercloud/cli/internal/cmd/output" "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/spf13/cobra" ) -var contextListTableOutput *tableOutput +var contextListTableOutput *output.Table type ContextPresentation struct { Name string @@ -15,7 +16,7 @@ type ContextPresentation struct { } func init() { - contextListTableOutput = newTableOutput(). + contextListTableOutput = output.NewTable(). AddAllowedFields(ContextPresentation{}). RemoveAllowedField("token") } @@ -33,12 +34,12 @@ func newContextListCommand(cli *state.State) *cobra.Command { DisableFlagsInUseLine: true, RunE: cli.Wrap(runContextList), } - addOutputFlag(cmd, outputOptionNoHeader(), outputOptionColumns(contextListTableOutput.Columns())) + output.AddFlag(cmd, output.OptionNoHeader(), output.OptionColumns(contextListTableOutput.Columns())) return cmd } func runContextList(cli *state.State, cmd *cobra.Command, args []string) error { - outOpts := outputFlagsForCommand(cmd) + outOpts := output.FlagsForCommand(cmd) cols := []string{"active", "name"} if outOpts.IsSet("columns") { diff --git a/internal/cmds/datacenter_describe.go b/internal/cmds/datacenter_describe.go index ca10c9ca..dd7088ae 100644 --- a/internal/cmds/datacenter_describe.go +++ b/internal/cmds/datacenter_describe.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/cmd/output" "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" @@ -22,12 +23,12 @@ func newDatacenterDescribeCommand(cli *state.State) *cobra.Command { PreRunE: cli.EnsureToken, RunE: cli.Wrap(runDatacenterDescribe), } - addOutputFlag(cmd, outputOptionJSON(), outputOptionFormat()) + output.AddFlag(cmd, output.OptionJSON(), output.OptionFormat()) return cmd } func runDatacenterDescribe(cli *state.State, cmd *cobra.Command, args []string) error { - outputFlags := outputFlagsForCommand(cmd) + outputFlags := output.FlagsForCommand(cmd) idOrName := args[0] datacenter, resp, err := cli.Client().Datacenter.Get(cli.Context, idOrName) diff --git a/internal/cmds/datacenter_list.go b/internal/cmds/datacenter_list.go index ac176c40..1c9429e0 100644 --- a/internal/cmds/datacenter_list.go +++ b/internal/cmds/datacenter_list.go @@ -1,6 +1,7 @@ package cmds import ( + "github.com/hetznercloud/cli/internal/cmd/output" "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" @@ -8,12 +9,12 @@ import ( "github.com/spf13/cobra" ) -var datacenterListTableOutput *tableOutput +var datacenterListTableOutput *output.Table func init() { - datacenterListTableOutput = newTableOutput(). + datacenterListTableOutput = output.NewTable(). AddAllowedFields(hcloud.Datacenter{}). - AddFieldOutputFn("location", fieldOutputFn(func(obj interface{}) string { + AddFieldFn("location", output.FieldFn(func(obj interface{}) string { datacenter := obj.(*hcloud.Datacenter) return datacenter.Location.Name })) @@ -32,12 +33,12 @@ func newDatacenterListCommand(cli *state.State) *cobra.Command { PreRunE: cli.EnsureToken, RunE: cli.Wrap(runDatacenterList), } - addOutputFlag(cmd, outputOptionNoHeader(), outputOptionColumns(datacenterListTableOutput.Columns()), outputOptionJSON()) + output.AddFlag(cmd, output.OptionNoHeader(), output.OptionColumns(datacenterListTableOutput.Columns()), output.OptionJSON()) return cmd } func runDatacenterList(cli *state.State, cmd *cobra.Command, args []string) error { - outOpts := outputFlagsForCommand(cmd) + outOpts := output.FlagsForCommand(cmd) datacenters, err := cli.Client().Datacenter.All(cli.Context) diff --git a/internal/cmds/floatingip_describe.go b/internal/cmds/floatingip_describe.go index 2ad19325..586c690f 100644 --- a/internal/cmds/floatingip_describe.go +++ b/internal/cmds/floatingip_describe.go @@ -6,6 +6,7 @@ import ( "github.com/dustin/go-humanize" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/cmd/output" "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" @@ -23,12 +24,12 @@ func newFloatingIPDescribeCommand(cli *state.State) *cobra.Command { PreRunE: cli.EnsureToken, RunE: cli.Wrap(runFloatingIPDescribe), } - addOutputFlag(cmd, outputOptionJSON(), outputOptionFormat()) + output.AddFlag(cmd, output.OptionJSON(), output.OptionFormat()) return cmd } func runFloatingIPDescribe(cli *state.State, cmd *cobra.Command, args []string) error { - outputFlags := outputFlagsForCommand(cmd) + outputFlags := output.FlagsForCommand(cmd) idOrName := args[0] floatingIP, resp, err := cli.Client().FloatingIP.Get(cli.Context, idOrName) diff --git a/internal/cmds/floatingip_list.go b/internal/cmds/floatingip_list.go index e3bce5ff..9794ab09 100644 --- a/internal/cmds/floatingip_list.go +++ b/internal/cmds/floatingip_list.go @@ -4,6 +4,7 @@ import ( "fmt" "strings" + "github.com/hetznercloud/cli/internal/cmd/output" "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud/schema" @@ -12,7 +13,7 @@ import ( "github.com/spf13/cobra" ) -var floatingIPListTableOutput *tableOutput +var floatingIPListTableOutput *output.Table func init() { floatingIPListTableOutput = describeFloatingIPListTableOutput(nil) @@ -31,13 +32,13 @@ func newFloatingIPListCommand(cli *state.State) *cobra.Command { PreRunE: cli.EnsureToken, RunE: cli.Wrap(runFloatingIPList), } - addOutputFlag(cmd, outputOptionNoHeader(), outputOptionColumns(floatingIPListTableOutput.Columns()), outputOptionJSON()) + output.AddFlag(cmd, output.OptionNoHeader(), output.OptionColumns(floatingIPListTableOutput.Columns()), output.OptionJSON()) cmd.Flags().StringP("selector", "l", "", "Selector to filter by labels") return cmd } func runFloatingIPList(cli *state.State, cmd *cobra.Command, args []string) error { - outOpts := outputFlagsForCommand(cmd) + outOpts := output.FlagsForCommand(cmd) labelSelector, _ := cmd.Flags().GetString("selector") opts := hcloud.FloatingIPListOpts{ @@ -100,10 +101,10 @@ func runFloatingIPList(cli *state.State, cmd *cobra.Command, args []string) erro return nil } -func describeFloatingIPListTableOutput(cli *state.State) *tableOutput { - return newTableOutput(). +func describeFloatingIPListTableOutput(cli *state.State) *output.Table { + return output.NewTable(). AddAllowedFields(hcloud.FloatingIP{}). - AddFieldOutputFn("dns", fieldOutputFn(func(obj interface{}) string { + AddFieldFn("dns", output.FieldFn(func(obj interface{}) string { floatingIP := obj.(*hcloud.FloatingIP) var dns string if len(floatingIP.DNSPtr) == 1 { @@ -116,7 +117,7 @@ func describeFloatingIPListTableOutput(cli *state.State) *tableOutput { } return util.NA(dns) })). - AddFieldOutputFn("server", fieldOutputFn(func(obj interface{}) string { + AddFieldFn("server", output.FieldFn(func(obj interface{}) string { floatingIP := obj.(*hcloud.FloatingIP) var server string if floatingIP.Server != nil && cli != nil { @@ -124,18 +125,18 @@ func describeFloatingIPListTableOutput(cli *state.State) *tableOutput { } return util.NA(server) })). - AddFieldOutputFn("home", fieldOutputFn(func(obj interface{}) string { + AddFieldFn("home", output.FieldFn(func(obj interface{}) string { floatingIP := obj.(*hcloud.FloatingIP) return floatingIP.HomeLocation.Name })). - AddFieldOutputFn("ip", fieldOutputFn(func(obj interface{}) string { + AddFieldFn("ip", output.FieldFn(func(obj interface{}) string { floatingIP := obj.(*hcloud.FloatingIP) if floatingIP.Network != nil { return floatingIP.Network.String() } return floatingIP.IP.String() })). - AddFieldOutputFn("protection", fieldOutputFn(func(obj interface{}) string { + AddFieldFn("protection", output.FieldFn(func(obj interface{}) string { floatingIP := obj.(*hcloud.FloatingIP) var protection []string if floatingIP.Protection.Delete { @@ -143,11 +144,11 @@ func describeFloatingIPListTableOutput(cli *state.State) *tableOutput { } return strings.Join(protection, ", ") })). - AddFieldOutputFn("labels", fieldOutputFn(func(obj interface{}) string { + AddFieldFn("labels", output.FieldFn(func(obj interface{}) string { floatingIP := obj.(*hcloud.FloatingIP) return util.LabelsToString(floatingIP.Labels) })). - AddFieldOutputFn("created", fieldOutputFn(func(obj interface{}) string { + AddFieldFn("created", output.FieldFn(func(obj interface{}) string { floatingIP := obj.(*hcloud.FloatingIP) return util.Datetime(floatingIP.Created) })) diff --git a/internal/cmds/image_describe.go b/internal/cmds/image_describe.go index 48e89e1c..649c32ac 100644 --- a/internal/cmds/image_describe.go +++ b/internal/cmds/image_describe.go @@ -6,6 +6,7 @@ import ( humanize "github.com/dustin/go-humanize" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/cmd/output" "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" @@ -23,12 +24,12 @@ func newImageDescribeCommand(cli *state.State) *cobra.Command { PreRunE: cli.EnsureToken, RunE: cli.Wrap(runImageDescribe), } - addOutputFlag(cmd, outputOptionJSON(), outputOptionFormat()) + output.AddFlag(cmd, output.OptionJSON(), output.OptionFormat()) return cmd } func runImageDescribe(cli *state.State, cmd *cobra.Command, args []string) error { - outputFlags := outputFlagsForCommand(cmd) + outputFlags := output.FlagsForCommand(cmd) idOrName := args[0] image, resp, err := cli.Client().Image.Get(cli.Context, idOrName) diff --git a/internal/cmds/image_list.go b/internal/cmds/image_list.go index a1c8b5ad..2303c826 100644 --- a/internal/cmds/image_list.go +++ b/internal/cmds/image_list.go @@ -4,6 +4,7 @@ import ( "fmt" "strings" + "github.com/hetznercloud/cli/internal/cmd/output" "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud/schema" @@ -13,7 +14,7 @@ import ( "github.com/spf13/cobra" ) -var imageListTableOutput *tableOutput +var imageListTableOutput *output.Table var typeFilter string func init() { @@ -33,14 +34,14 @@ func newImageListCommand(cli *state.State) *cobra.Command { PreRunE: cli.EnsureToken, RunE: cli.Wrap(runImageList), } - addOutputFlag(cmd, outputOptionNoHeader(), outputOptionColumns(imageListTableOutput.Columns()), outputOptionJSON()) + output.AddFlag(cmd, output.OptionNoHeader(), output.OptionColumns(imageListTableOutput.Columns()), output.OptionJSON()) cmd.Flags().StringVarP(&typeFilter, "type", "t", "", "Only show images of given type") cmd.Flags().StringP("selector", "l", "", "Selector to filter by labels") return cmd } func runImageList(cli *state.State, cmd *cobra.Command, args []string) error { - outOpts := outputFlagsForCommand(cmd) + outOpts := output.FlagsForCommand(cmd) labelSelector, _ := cmd.Flags().GetString("selector") opts := hcloud.ImageListOpts{ @@ -93,8 +94,8 @@ func runImageList(cli *state.State, cmd *cobra.Command, args []string) error { return nil } -func describeImageListTableOutput(cli *state.State) *tableOutput { - return newTableOutput(). +func describeImageListTableOutput(cli *state.State) *output.Table { + return output.NewTable(). AddAllowedFields(hcloud.Image{}). AddFieldAlias("imagesize", "image size"). AddFieldAlias("disksize", "disk size"). @@ -103,40 +104,40 @@ func describeImageListTableOutput(cli *state.State) *tableOutput { AddFieldAlias("rapiddeploy", "rapid deploy"). AddFieldAlias("createdfrom", "created from"). AddFieldAlias("boundto", "bound to"). - AddFieldOutputFn("name", fieldOutputFn(func(obj interface{}) string { + AddFieldFn("name", output.FieldFn(func(obj interface{}) string { image := obj.(*hcloud.Image) return util.NA(image.Name) })). - AddFieldOutputFn("image_size", fieldOutputFn(func(obj interface{}) string { + AddFieldFn("image_size", output.FieldFn(func(obj interface{}) string { image := obj.(*hcloud.Image) if image.ImageSize == 0 { return util.NA("") } return fmt.Sprintf("%.2f GB", image.ImageSize) })). - AddFieldOutputFn("disk_size", fieldOutputFn(func(obj interface{}) string { + AddFieldFn("disk_size", output.FieldFn(func(obj interface{}) string { image := obj.(*hcloud.Image) return fmt.Sprintf("%.0f GB", image.DiskSize) })). - AddFieldOutputFn("created", fieldOutputFn(func(obj interface{}) string { + AddFieldFn("created", output.FieldFn(func(obj interface{}) string { image := obj.(*hcloud.Image) return humanize.Time(image.Created) })). - AddFieldOutputFn("bound_to", fieldOutputFn(func(obj interface{}) string { + AddFieldFn("bound_to", output.FieldFn(func(obj interface{}) string { image := obj.(*hcloud.Image) if image.BoundTo != nil && cli != nil { return cli.ServerName(image.BoundTo.ID) } return util.NA("") })). - AddFieldOutputFn("created_from", fieldOutputFn(func(obj interface{}) string { + AddFieldFn("created_from", output.FieldFn(func(obj interface{}) string { image := obj.(*hcloud.Image) if image.CreatedFrom != nil && cli != nil { return cli.ServerName(image.CreatedFrom.ID) } return util.NA("") })). - AddFieldOutputFn("protection", fieldOutputFn(func(obj interface{}) string { + AddFieldFn("protection", output.FieldFn(func(obj interface{}) string { image := obj.(*hcloud.Image) var protection []string if image.Protection.Delete { @@ -144,15 +145,15 @@ func describeImageListTableOutput(cli *state.State) *tableOutput { } return strings.Join(protection, ", ") })). - AddFieldOutputFn("labels", fieldOutputFn(func(obj interface{}) string { + AddFieldFn("labels", output.FieldFn(func(obj interface{}) string { image := obj.(*hcloud.Image) return util.LabelsToString(image.Labels) })). - AddFieldOutputFn("created", fieldOutputFn(func(obj interface{}) string { + AddFieldFn("created", output.FieldFn(func(obj interface{}) string { image := obj.(*hcloud.Image) return util.Datetime(image.Created) })). - AddFieldOutputFn("deprecated", fieldOutputFn(func(obj interface{}) string { + AddFieldFn("deprecated", output.FieldFn(func(obj interface{}) string { image := obj.(*hcloud.Image) if image.Deprecated.IsZero() { return "-" diff --git a/internal/cmds/iso_describe.go b/internal/cmds/iso_describe.go index bc1865bd..7ee766d9 100644 --- a/internal/cmds/iso_describe.go +++ b/internal/cmds/iso_describe.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/cmd/output" "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" @@ -22,12 +23,12 @@ func newISODescribeCommand(cli *state.State) *cobra.Command { PreRunE: cli.EnsureToken, RunE: cli.Wrap(runISODescribe), } - addOutputFlag(cmd, outputOptionJSON(), outputOptionFormat()) + output.AddFlag(cmd, output.OptionJSON(), output.OptionFormat()) return cmd } func runISODescribe(cli *state.State, cmd *cobra.Command, args []string) error { - outputFlags := outputFlagsForCommand(cmd) + outputFlags := output.FlagsForCommand(cmd) idOrName := args[0] iso, resp, err := cli.Client().ISO.Get(cli.Context, idOrName) diff --git a/internal/cmds/iso_list.go b/internal/cmds/iso_list.go index 948e58f0..3cc3db84 100644 --- a/internal/cmds/iso_list.go +++ b/internal/cmds/iso_list.go @@ -1,6 +1,7 @@ package cmds import ( + "github.com/hetznercloud/cli/internal/cmd/output" "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" @@ -8,10 +9,10 @@ import ( "github.com/spf13/cobra" ) -var isoListTableOutput *tableOutput +var isoListTableOutput *output.Table func init() { - isoListTableOutput = newTableOutput(). + isoListTableOutput = output.NewTable(). AddAllowedFields(hcloud.ISO{}) } @@ -28,12 +29,12 @@ func newISOListCommand(cli *state.State) *cobra.Command { PreRunE: cli.EnsureToken, RunE: cli.Wrap(runISOList), } - addOutputFlag(cmd, outputOptionNoHeader(), outputOptionColumns(isoListTableOutput.Columns()), outputOptionJSON()) + output.AddFlag(cmd, output.OptionNoHeader(), output.OptionColumns(isoListTableOutput.Columns()), output.OptionJSON()) return cmd } func runISOList(cli *state.State, cmd *cobra.Command, args []string) error { - outOpts := outputFlagsForCommand(cmd) + outOpts := output.FlagsForCommand(cmd) isos, err := cli.Client().ISO.All(cli.Context) if err != nil { diff --git a/internal/cmds/load_balancer_describe.go b/internal/cmds/load_balancer_describe.go index db469343..016dc3c9 100644 --- a/internal/cmds/load_balancer_describe.go +++ b/internal/cmds/load_balancer_describe.go @@ -6,6 +6,7 @@ import ( humanize "github.com/dustin/go-humanize" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/cmd/output" "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" @@ -23,13 +24,13 @@ func newLoadBalancerDescribeCommand(cli *state.State) *cobra.Command { PreRunE: cli.EnsureToken, RunE: cli.Wrap(runLoadBalancerDescribe), } - addOutputFlag(cmd, outputOptionJSON(), outputOptionFormat()) + output.AddFlag(cmd, output.OptionJSON(), output.OptionFormat()) cmd.Flags().Bool("expand-targets", false, "Expand all label_selector targets") return cmd } func runLoadBalancerDescribe(cli *state.State, cmd *cobra.Command, args []string) error { - outputFlags := outputFlagsForCommand(cmd) + outputFlags := output.FlagsForCommand(cmd) withLabelSelectorTargets, _ := cmd.Flags().GetBool("expand-targets") idOrName := args[0] loadBalancer, resp, err := cli.Client().LoadBalancer.Get(cli.Context, idOrName) diff --git a/internal/cmds/load_balancer_list.go b/internal/cmds/load_balancer_list.go index b9ee79b1..70d375a1 100644 --- a/internal/cmds/load_balancer_list.go +++ b/internal/cmds/load_balancer_list.go @@ -3,6 +3,7 @@ package cmds import ( "strings" + "github.com/hetznercloud/cli/internal/cmd/output" "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud/schema" @@ -11,7 +12,7 @@ import ( "github.com/spf13/cobra" ) -var loadBalancerListTableOutput *tableOutput +var loadBalancerListTableOutput *output.Table func init() { loadBalancerListTableOutput = describeLoadBalancerListTableOutput(nil) @@ -30,13 +31,13 @@ func newLoadBalancerListCommand(cli *state.State) *cobra.Command { PreRunE: cli.EnsureToken, RunE: cli.Wrap(runLoadBalancerList), } - addOutputFlag(cmd, outputOptionNoHeader(), outputOptionColumns(loadBalancerListTableOutput.Columns()), outputOptionJSON()) + output.AddFlag(cmd, output.OptionNoHeader(), output.OptionColumns(loadBalancerListTableOutput.Columns()), output.OptionJSON()) cmd.Flags().StringP("selector", "l", "", "Selector to filter by labels") return cmd } func runLoadBalancerList(cli *state.State, cmd *cobra.Command, args []string) error { - outOpts := outputFlagsForCommand(cmd) + outOpts := output.FlagsForCommand(cmd) labelSelector, _ := cmd.Flags().GetString("selector") opts := hcloud.LoadBalancerListOpts{ @@ -156,34 +157,34 @@ func runLoadBalancerList(cli *state.State, cmd *cobra.Command, args []string) er return nil } -func describeLoadBalancerListTableOutput(cli *state.State) *tableOutput { - return newTableOutput(). +func describeLoadBalancerListTableOutput(cli *state.State) *output.Table { + return output.NewTable(). AddAllowedFields(hcloud.LoadBalancer{}). - AddFieldOutputFn("ipv4", fieldOutputFn(func(obj interface{}) string { + AddFieldFn("ipv4", output.FieldFn(func(obj interface{}) string { loadbalancer := obj.(*hcloud.LoadBalancer) return loadbalancer.PublicNet.IPv4.IP.String() })). - AddFieldOutputFn("ipv6", fieldOutputFn(func(obj interface{}) string { + AddFieldFn("ipv6", output.FieldFn(func(obj interface{}) string { loadbalancer := obj.(*hcloud.LoadBalancer) return loadbalancer.PublicNet.IPv6.IP.String() })). - AddFieldOutputFn("type", fieldOutputFn(func(obj interface{}) string { + AddFieldFn("type", output.FieldFn(func(obj interface{}) string { loadbalancer := obj.(*hcloud.LoadBalancer) return loadbalancer.LoadBalancerType.Name })). - AddFieldOutputFn("location", fieldOutputFn(func(obj interface{}) string { + AddFieldFn("location", output.FieldFn(func(obj interface{}) string { loadbalancer := obj.(*hcloud.LoadBalancer) return loadbalancer.Location.Name })). - AddFieldOutputFn("network_zone", fieldOutputFn(func(obj interface{}) string { + AddFieldFn("network_zone", output.FieldFn(func(obj interface{}) string { loadbalancer := obj.(*hcloud.LoadBalancer) return string(loadbalancer.Location.NetworkZone) })). - AddFieldOutputFn("labels", fieldOutputFn(func(obj interface{}) string { + AddFieldFn("labels", output.FieldFn(func(obj interface{}) string { loadBalancer := obj.(*hcloud.LoadBalancer) return util.LabelsToString(loadBalancer.Labels) })). - AddFieldOutputFn("protection", fieldOutputFn(func(obj interface{}) string { + AddFieldFn("protection", output.FieldFn(func(obj interface{}) string { loadBalancer := obj.(*hcloud.LoadBalancer) var protection []string if loadBalancer.Protection.Delete { @@ -191,7 +192,7 @@ func describeLoadBalancerListTableOutput(cli *state.State) *tableOutput { } return strings.Join(protection, ", ") })). - AddFieldOutputFn("created", fieldOutputFn(func(obj interface{}) string { + AddFieldFn("created", output.FieldFn(func(obj interface{}) string { loadBalancer := obj.(*hcloud.LoadBalancer) return util.Datetime(loadBalancer.Created) })) diff --git a/internal/cmds/load_balancer_metrics.go b/internal/cmds/load_balancer_metrics.go index 95f36a08..f23d046b 100644 --- a/internal/cmds/load_balancer_metrics.go +++ b/internal/cmds/load_balancer_metrics.go @@ -10,6 +10,7 @@ import ( "github.com/guptarohit/asciigraph" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/cmd/output" "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" @@ -35,12 +36,12 @@ func newLoadBalancerMetricsCommand(cli *state.State) *cobra.Command { cmd.Flags().String("start", "", "ISO 8601 timestamp") cmd.Flags().String("end", "", "ISO 8601 timestamp") - addOutputFlag(cmd, outputOptionJSON()) + output.AddFlag(cmd, output.OptionJSON()) return cmd } func runLoadBalancerMetrics(cli *state.State, cmd *cobra.Command, args []string) error { - outputFlags := outputFlagsForCommand(cmd) + outputFlags := output.FlagsForCommand(cmd) idOrName := args[0] LoadBalancer, _, err := cli.Client().LoadBalancer.Get(cli.Context, idOrName) diff --git a/internal/cmds/load_balancer_type_describe.go b/internal/cmds/load_balancer_type_describe.go index b3e1bdc0..727d73e3 100644 --- a/internal/cmds/load_balancer_type_describe.go +++ b/internal/cmds/load_balancer_type_describe.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/cmd/output" "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" @@ -22,12 +23,12 @@ func newLoadBalancerTypenDescribeCommand(cli *state.State) *cobra.Command { PreRunE: cli.EnsureToken, RunE: cli.Wrap(runLoadBalancerTypeDescribe), } - addOutputFlag(cmd, outputOptionJSON(), outputOptionFormat()) + output.AddFlag(cmd, output.OptionJSON(), output.OptionFormat()) return cmd } func runLoadBalancerTypeDescribe(cli *state.State, cmd *cobra.Command, args []string) error { - outputFlags := outputFlagsForCommand(cmd) + outputFlags := output.FlagsForCommand(cmd) idOrName := args[0] loadBalancerType, resp, err := cli.Client().LoadBalancerType.Get(cli.Context, idOrName) diff --git a/internal/cmds/load_balancer_type_list.go b/internal/cmds/load_balancer_type_list.go index 2b502e55..e0cc3cba 100644 --- a/internal/cmds/load_balancer_type_list.go +++ b/internal/cmds/load_balancer_type_list.go @@ -1,6 +1,7 @@ package cmds import ( + "github.com/hetznercloud/cli/internal/cmd/output" "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" @@ -8,10 +9,10 @@ import ( "github.com/spf13/cobra" ) -var loadBalancerTypeListTableOutput *tableOutput +var loadBalancerTypeListTableOutput *output.Table func init() { - loadBalancerTypeListTableOutput = newTableOutput(). + loadBalancerTypeListTableOutput = output.NewTable(). AddAllowedFields(hcloud.LoadBalancerType{}) } @@ -28,12 +29,12 @@ func newLoadBalancerTypeListCommand(cli *state.State) *cobra.Command { PreRunE: cli.EnsureToken, RunE: cli.Wrap(runLoadBalancerTypeList), } - addOutputFlag(cmd, outputOptionNoHeader(), outputOptionColumns(loadBalancerTypeListTableOutput.Columns()), outputOptionJSON()) + output.AddFlag(cmd, output.OptionNoHeader(), output.OptionColumns(loadBalancerTypeListTableOutput.Columns()), output.OptionJSON()) return cmd } func runLoadBalancerTypeList(cli *state.State, cmd *cobra.Command, args []string) error { - outOpts := outputFlagsForCommand(cmd) + outOpts := output.FlagsForCommand(cmd) loadBalancerTypes, err := cli.Client().LoadBalancerType.All(cli.Context) if err != nil { diff --git a/internal/cmds/location_describe.go b/internal/cmds/location_describe.go index b28172bf..e0ee53a4 100644 --- a/internal/cmds/location_describe.go +++ b/internal/cmds/location_describe.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/cmd/output" "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" @@ -22,12 +23,12 @@ func newLocationDescribeCommand(cli *state.State) *cobra.Command { PreRunE: cli.EnsureToken, RunE: cli.Wrap(runLocationDescribe), } - addOutputFlag(cmd, outputOptionJSON(), outputOptionFormat()) + output.AddFlag(cmd, output.OptionJSON(), output.OptionFormat()) return cmd } func runLocationDescribe(cli *state.State, cmd *cobra.Command, args []string) error { - outputFlags := outputFlagsForCommand(cmd) + outputFlags := output.FlagsForCommand(cmd) idOrName := args[0] location, resp, err := cli.Client().Location.Get(cli.Context, idOrName) diff --git a/internal/cmds/location_list.go b/internal/cmds/location_list.go index b429cfb2..b86c447b 100644 --- a/internal/cmds/location_list.go +++ b/internal/cmds/location_list.go @@ -1,6 +1,7 @@ package cmds import ( + "github.com/hetznercloud/cli/internal/cmd/output" "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" @@ -8,10 +9,10 @@ import ( "github.com/spf13/cobra" ) -var locationListTableOutput *tableOutput +var locationListTableOutput *output.Table func init() { - locationListTableOutput = newTableOutput(). + locationListTableOutput = output.NewTable(). AddAllowedFields(hcloud.Location{}) } @@ -28,12 +29,12 @@ func newLocationListCommand(cli *state.State) *cobra.Command { PreRunE: cli.EnsureToken, RunE: cli.Wrap(runLocationList), } - addOutputFlag(cmd, outputOptionNoHeader(), outputOptionColumns(locationListTableOutput.Columns()), outputOptionJSON()) + output.AddFlag(cmd, output.OptionNoHeader(), output.OptionColumns(locationListTableOutput.Columns()), output.OptionJSON()) return cmd } func runLocationList(cli *state.State, cmd *cobra.Command, args []string) error { - outOpts := outputFlagsForCommand(cmd) + outOpts := output.FlagsForCommand(cmd) locations, err := cli.Client().Location.All(cli.Context) if err != nil { diff --git a/internal/cmds/network_describe.go b/internal/cmds/network_describe.go index 2f4d4d1d..9a0a1eae 100644 --- a/internal/cmds/network_describe.go +++ b/internal/cmds/network_describe.go @@ -5,6 +5,7 @@ import ( humanize "github.com/dustin/go-humanize" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/cmd/output" "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" @@ -22,12 +23,12 @@ func newNetworkDescribeCommand(cli *state.State) *cobra.Command { PreRunE: cli.EnsureToken, RunE: cli.Wrap(runNetworkDescribe), } - addOutputFlag(cmd, outputOptionJSON(), outputOptionFormat()) + output.AddFlag(cmd, output.OptionJSON(), output.OptionFormat()) return cmd } func runNetworkDescribe(cli *state.State, cmd *cobra.Command, args []string) error { - outputFlags := outputFlagsForCommand(cmd) + outputFlags := output.FlagsForCommand(cmd) idOrName := args[0] network, resp, err := cli.Client().Network.Get(cli.Context, idOrName) diff --git a/internal/cmds/network_list.go b/internal/cmds/network_list.go index 9a06043b..e48b3e4c 100644 --- a/internal/cmds/network_list.go +++ b/internal/cmds/network_list.go @@ -4,6 +4,7 @@ import ( "fmt" "strings" + "github.com/hetznercloud/cli/internal/cmd/output" "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" @@ -11,7 +12,7 @@ import ( "github.com/spf13/cobra" ) -var networkListTableOutput *tableOutput +var networkListTableOutput *output.Table func init() { networkListTableOutput = describeNetworkListTableOutput(nil) @@ -30,13 +31,13 @@ func newNetworkListCommand(cli *state.State) *cobra.Command { PreRunE: cli.EnsureToken, RunE: cli.Wrap(runNetworkList), } - addOutputFlag(cmd, outputOptionNoHeader(), outputOptionColumns(networkListTableOutput.Columns()), outputOptionJSON()) + output.AddFlag(cmd, output.OptionNoHeader(), output.OptionColumns(networkListTableOutput.Columns()), output.OptionJSON()) cmd.Flags().StringP("selector", "l", "", "Selector to filter by labels") return cmd } func runNetworkList(cli *state.State, cmd *cobra.Command, args []string) error { - outOpts := outputFlagsForCommand(cmd) + outOpts := output.FlagsForCommand(cmd) labelSelector, _ := cmd.Flags().GetString("selector") opts := hcloud.NetworkListOpts{ @@ -103,10 +104,10 @@ func runNetworkList(cli *state.State, cmd *cobra.Command, args []string) error { return nil } -func describeNetworkListTableOutput(cli *state.State) *tableOutput { - return newTableOutput(). +func describeNetworkListTableOutput(cli *state.State) *output.Table { + return output.NewTable(). AddAllowedFields(hcloud.Network{}). - AddFieldOutputFn("servers", fieldOutputFn(func(obj interface{}) string { + AddFieldFn("servers", output.FieldFn(func(obj interface{}) string { network := obj.(*hcloud.Network) serverCount := len(network.Servers) if serverCount <= 1 { @@ -114,15 +115,15 @@ func describeNetworkListTableOutput(cli *state.State) *tableOutput { } return fmt.Sprintf("%v servers", serverCount) })). - AddFieldOutputFn("ip_range", fieldOutputFn(func(obj interface{}) string { + AddFieldFn("ip_range", output.FieldFn(func(obj interface{}) string { network := obj.(*hcloud.Network) return network.IPRange.String() })). - AddFieldOutputFn("labels", fieldOutputFn(func(obj interface{}) string { + AddFieldFn("labels", output.FieldFn(func(obj interface{}) string { network := obj.(*hcloud.Network) return util.LabelsToString(network.Labels) })). - AddFieldOutputFn("protection", fieldOutputFn(func(obj interface{}) string { + AddFieldFn("protection", output.FieldFn(func(obj interface{}) string { network := obj.(*hcloud.Network) var protection []string if network.Protection.Delete { @@ -130,7 +131,7 @@ func describeNetworkListTableOutput(cli *state.State) *tableOutput { } return strings.Join(protection, ", ") })). - AddFieldOutputFn("created", fieldOutputFn(func(obj interface{}) string { + AddFieldFn("created", output.FieldFn(func(obj interface{}) string { network := obj.(*hcloud.Network) return util.Datetime(network.Created) })) diff --git a/internal/cmds/server_describe.go b/internal/cmds/server_describe.go index c5c8bfad..aacb889b 100644 --- a/internal/cmds/server_describe.go +++ b/internal/cmds/server_describe.go @@ -6,6 +6,7 @@ import ( humanize "github.com/dustin/go-humanize" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/cmd/output" "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" @@ -23,12 +24,12 @@ func newServerDescribeCommand(cli *state.State) *cobra.Command { PreRunE: cli.EnsureToken, RunE: cli.Wrap(runServerDescribe), } - addOutputFlag(cmd, outputOptionJSON(), outputOptionFormat()) + output.AddFlag(cmd, output.OptionJSON(), output.OptionFormat()) return cmd } func runServerDescribe(cli *state.State, cmd *cobra.Command, args []string) error { - outputFlags := outputFlagsForCommand(cmd) + outputFlags := output.FlagsForCommand(cmd) idOrName := args[0] server, resp, err := cli.Client().Server.Get(cli.Context, idOrName) diff --git a/internal/cmds/server_list.go b/internal/cmds/server_list.go index 1916d579..43546c06 100644 --- a/internal/cmds/server_list.go +++ b/internal/cmds/server_list.go @@ -4,6 +4,7 @@ import ( "strconv" "strings" + "github.com/hetznercloud/cli/internal/cmd/output" "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud/schema" @@ -12,7 +13,7 @@ import ( "github.com/spf13/cobra" ) -var serverListTableOutput *tableOutput +var serverListTableOutput *output.Table func init() { serverListTableOutput = describeServerListTableOutput(nil) @@ -31,13 +32,13 @@ func newServerListCommand(cli *state.State) *cobra.Command { PreRunE: cli.EnsureToken, RunE: cli.Wrap(runServerList), } - addOutputFlag(cmd, outputOptionNoHeader(), outputOptionColumns(serverListTableOutput.Columns()), outputOptionJSON()) + output.AddFlag(cmd, output.OptionNoHeader(), output.OptionColumns(serverListTableOutput.Columns()), output.OptionJSON()) cmd.Flags().StringP("selector", "l", "", "Selector to filter by labels") return cmd } func runServerList(cli *state.State, cmd *cobra.Command, args []string) error { - outOpts := outputFlagsForCommand(cmd) + outOpts := output.FlagsForCommand(cmd) labelSelector, _ := cmd.Flags().GetString("selector") opts := hcloud.ServerListOpts{ @@ -140,34 +141,34 @@ func runServerList(cli *state.State, cmd *cobra.Command, args []string) error { return nil } -func describeServerListTableOutput(cli *state.State) *tableOutput { - return newTableOutput(). +func describeServerListTableOutput(cli *state.State) *output.Table { + return output.NewTable(). AddAllowedFields(hcloud.Server{}). - AddFieldOutputFn("ipv4", fieldOutputFn(func(obj interface{}) string { + AddFieldFn("ipv4", output.FieldFn(func(obj interface{}) string { server := obj.(*hcloud.Server) return server.PublicNet.IPv4.IP.String() })). - AddFieldOutputFn("ipv6", fieldOutputFn(func(obj interface{}) string { + AddFieldFn("ipv6", output.FieldFn(func(obj interface{}) string { server := obj.(*hcloud.Server) return server.PublicNet.IPv6.Network.String() })). - AddFieldOutputFn("datacenter", fieldOutputFn(func(obj interface{}) string { + AddFieldFn("datacenter", output.FieldFn(func(obj interface{}) string { server := obj.(*hcloud.Server) return server.Datacenter.Name })). - AddFieldOutputFn("location", fieldOutputFn(func(obj interface{}) string { + AddFieldFn("location", output.FieldFn(func(obj interface{}) string { server := obj.(*hcloud.Server) return server.Datacenter.Location.Name })). - AddFieldOutputFn("labels", fieldOutputFn(func(obj interface{}) string { + AddFieldFn("labels", output.FieldFn(func(obj interface{}) string { server := obj.(*hcloud.Server) return util.LabelsToString(server.Labels) })). - AddFieldOutputFn("type", fieldOutputFn(func(obj interface{}) string { + AddFieldFn("type", output.FieldFn(func(obj interface{}) string { server := obj.(*hcloud.Server) return server.ServerType.Name })). - AddFieldOutputFn("volumes", fieldOutputFn(func(obj interface{}) string { + AddFieldFn("volumes", output.FieldFn(func(obj interface{}) string { server := obj.(*hcloud.Server) var volumes []string for _, volume := range server.Volumes { @@ -176,7 +177,7 @@ func describeServerListTableOutput(cli *state.State) *tableOutput { } return strings.Join(volumes, ", ") })). - AddFieldOutputFn("private_net", fieldOutputFn(func(obj interface{}) string { + AddFieldFn("private_net", output.FieldFn(func(obj interface{}) string { server := obj.(*hcloud.Server) var networks []string if cli != nil { @@ -186,7 +187,7 @@ func describeServerListTableOutput(cli *state.State) *tableOutput { } return util.NA(strings.Join(networks, ", ")) })). - AddFieldOutputFn("protection", fieldOutputFn(func(obj interface{}) string { + AddFieldFn("protection", output.FieldFn(func(obj interface{}) string { server := obj.(*hcloud.Server) var protection []string if server.Protection.Delete { @@ -197,7 +198,7 @@ func describeServerListTableOutput(cli *state.State) *tableOutput { } return strings.Join(protection, ", ") })). - AddFieldOutputFn("created", fieldOutputFn(func(obj interface{}) string { + AddFieldFn("created", output.FieldFn(func(obj interface{}) string { server := obj.(*hcloud.Server) return util.Datetime(server.Created) })) diff --git a/internal/cmds/server_metrics.go b/internal/cmds/server_metrics.go index ed9c27e8..dc45f065 100644 --- a/internal/cmds/server_metrics.go +++ b/internal/cmds/server_metrics.go @@ -10,6 +10,7 @@ import ( "github.com/guptarohit/asciigraph" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/cmd/output" "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" @@ -35,12 +36,12 @@ func newServerMetricsCommand(cli *state.State) *cobra.Command { cmd.Flags().String("start", "", "ISO 8601 timestamp") cmd.Flags().String("end", "", "ISO 8601 timestamp") - addOutputFlag(cmd, outputOptionJSON()) + output.AddFlag(cmd, output.OptionJSON()) return cmd } func runServerMetrics(cli *state.State, cmd *cobra.Command, args []string) error { - outputFlags := outputFlagsForCommand(cmd) + outputFlags := output.FlagsForCommand(cmd) idOrName := args[0] server, _, err := cli.Client().Server.Get(cli.Context, idOrName) diff --git a/internal/cmds/server_request_console.go b/internal/cmds/server_request_console.go index 7d754270..3078ef98 100644 --- a/internal/cmds/server_request_console.go +++ b/internal/cmds/server_request_console.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/cmd/output" "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/spf13/cobra" @@ -20,12 +21,12 @@ func newServerRequestConsoleCommand(cli *state.State) *cobra.Command { PreRunE: cli.EnsureToken, RunE: cli.Wrap(runServerRequestConsole), } - addOutputFlag(cmd, outputOptionJSON()) + output.AddFlag(cmd, output.OptionJSON()) return cmd } func runServerRequestConsole(cli *state.State, cmd *cobra.Command, args []string) error { - outOpts := outputFlagsForCommand(cmd) + outOpts := output.FlagsForCommand(cmd) idOrName := args[0] server, _, err := cli.Client().Server.Get(cli.Context, idOrName) if err != nil { diff --git a/internal/cmds/servertypes_describe.go b/internal/cmds/servertypes_describe.go index 812ea4b4..2f6e579d 100644 --- a/internal/cmds/servertypes_describe.go +++ b/internal/cmds/servertypes_describe.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/cmd/output" "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" @@ -22,12 +23,12 @@ func newServerTypeDescribeCommand(cli *state.State) *cobra.Command { PreRunE: cli.EnsureToken, RunE: cli.Wrap(runServerTypeDescribe), } - addOutputFlag(cmd, outputOptionJSON(), outputOptionFormat()) + output.AddFlag(cmd, output.OptionJSON(), output.OptionFormat()) return cmd } func runServerTypeDescribe(cli *state.State, cmd *cobra.Command, args []string) error { - outputFlags := outputFlagsForCommand(cmd) + outputFlags := output.FlagsForCommand(cmd) idOrName := args[0] serverType, resp, err := cli.Client().ServerType.Get(cli.Context, idOrName) diff --git a/internal/cmds/servertypes_list.go b/internal/cmds/servertypes_list.go index b8704ed1..91d7e12b 100644 --- a/internal/cmds/servertypes_list.go +++ b/internal/cmds/servertypes_list.go @@ -3,6 +3,7 @@ package cmds import ( "fmt" + "github.com/hetznercloud/cli/internal/cmd/output" "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud/schema" @@ -11,17 +12,17 @@ import ( "github.com/spf13/cobra" ) -var serverTypeListTableOutput *tableOutput +var serverTypeListTableOutput *output.Table func init() { - serverTypeListTableOutput = newTableOutput(). + serverTypeListTableOutput = output.NewTable(). AddAllowedFields(hcloud.ServerType{}). AddFieldAlias("storagetype", "storage type"). - AddFieldOutputFn("memory", fieldOutputFn(func(obj interface{}) string { + AddFieldFn("memory", output.FieldFn(func(obj interface{}) string { serverType := obj.(*hcloud.ServerType) return fmt.Sprintf("%.1f GB", serverType.Memory) })). - AddFieldOutputFn("disk", fieldOutputFn(func(obj interface{}) string { + AddFieldFn("disk", output.FieldFn(func(obj interface{}) string { serverType := obj.(*hcloud.ServerType) return fmt.Sprintf("%d GB", serverType.Disk) })) @@ -40,12 +41,12 @@ func newServerTypeListCommand(cli *state.State) *cobra.Command { PreRunE: cli.EnsureToken, RunE: cli.Wrap(runServerTypeList), } - addOutputFlag(cmd, outputOptionNoHeader(), outputOptionColumns(serverTypeListTableOutput.Columns()), outputOptionJSON()) + output.AddFlag(cmd, output.OptionNoHeader(), output.OptionColumns(serverTypeListTableOutput.Columns()), output.OptionJSON()) return cmd } func runServerTypeList(cli *state.State, cmd *cobra.Command, args []string) error { - outOpts := outputFlagsForCommand(cmd) + outOpts := output.FlagsForCommand(cmd) serverTypes, err := cli.Client().ServerType.All(cli.Context) if err != nil { diff --git a/internal/cmds/sshkey_describe.go b/internal/cmds/sshkey_describe.go index 364a34a8..0cba6278 100644 --- a/internal/cmds/sshkey_describe.go +++ b/internal/cmds/sshkey_describe.go @@ -8,6 +8,7 @@ import ( "github.com/dustin/go-humanize" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/cmd/output" "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" @@ -25,12 +26,12 @@ func newSSHKeyDescribeCommand(cli *state.State) *cobra.Command { PreRunE: cli.EnsureToken, RunE: cli.Wrap(runSSHKeyDescribe), } - addOutputFlag(cmd, outputOptionJSON(), outputOptionFormat()) + output.AddFlag(cmd, output.OptionJSON(), output.OptionFormat()) return cmd } func runSSHKeyDescribe(cli *state.State, cmd *cobra.Command, args []string) error { - outputFlags := outputFlagsForCommand(cmd) + outputFlags := output.FlagsForCommand(cmd) sshKey, resp, err := cli.Client().SSHKey.Get(cli.Context, args[0]) if err != nil { diff --git a/internal/cmds/sshkey_list.go b/internal/cmds/sshkey_list.go index dd20f956..f8c44566 100644 --- a/internal/cmds/sshkey_list.go +++ b/internal/cmds/sshkey_list.go @@ -1,6 +1,7 @@ package cmds import ( + "github.com/hetznercloud/cli/internal/cmd/output" "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" @@ -8,16 +9,16 @@ import ( "github.com/spf13/cobra" ) -var sshKeyListTableOutput *tableOutput +var sshKeyListTableOutput *output.Table func init() { - sshKeyListTableOutput = newTableOutput(). + sshKeyListTableOutput = output.NewTable(). AddAllowedFields(hcloud.SSHKey{}). - AddFieldOutputFn("labels", fieldOutputFn(func(obj interface{}) string { + AddFieldFn("labels", output.FieldFn(func(obj interface{}) string { sshKey := obj.(*hcloud.SSHKey) return util.LabelsToString(sshKey.Labels) })). - AddFieldOutputFn("created", fieldOutputFn(func(obj interface{}) string { + AddFieldFn("created", output.FieldFn(func(obj interface{}) string { sshKey := obj.(*hcloud.SSHKey) return util.Datetime(sshKey.Created) })) @@ -36,13 +37,13 @@ func newSSHKeyListCommand(cli *state.State) *cobra.Command { PreRunE: cli.EnsureToken, RunE: cli.Wrap(runSSHKeyList), } - addOutputFlag(cmd, outputOptionNoHeader(), outputOptionColumns(sshKeyListTableOutput.Columns()), outputOptionJSON()) + output.AddFlag(cmd, output.OptionNoHeader(), output.OptionColumns(sshKeyListTableOutput.Columns()), output.OptionJSON()) cmd.Flags().StringP("selector", "l", "", "Selector to filter by labels") return cmd } func runSSHKeyList(cli *state.State, cmd *cobra.Command, args []string) error { - outOpts := outputFlagsForCommand(cmd) + outOpts := output.FlagsForCommand(cmd) labelSelector, _ := cmd.Flags().GetString("selector") opts := hcloud.SSHKeyListOpts{ diff --git a/internal/cmds/volume_describe.go b/internal/cmds/volume_describe.go index 47b727f6..ed8773e6 100644 --- a/internal/cmds/volume_describe.go +++ b/internal/cmds/volume_describe.go @@ -6,6 +6,7 @@ import ( humanize "github.com/dustin/go-humanize" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/cmd/output" "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud" @@ -23,12 +24,12 @@ func newVolumeDescribeCommand(cli *state.State) *cobra.Command { PreRunE: cli.EnsureToken, RunE: cli.Wrap(runVolumeDescribe), } - addOutputFlag(cmd, outputOptionJSON(), outputOptionFormat()) + output.AddFlag(cmd, output.OptionJSON(), output.OptionFormat()) return cmd } func runVolumeDescribe(cli *state.State, cmd *cobra.Command, args []string) error { - outputFlags := outputFlagsForCommand(cmd) + outputFlags := output.FlagsForCommand(cmd) volume, resp, err := cli.Client().Volume.Get(cli.Context, args[0]) if err != nil { diff --git a/internal/cmds/volume_list.go b/internal/cmds/volume_list.go index 55bcf5fe..a1b2d2ba 100644 --- a/internal/cmds/volume_list.go +++ b/internal/cmds/volume_list.go @@ -3,6 +3,7 @@ package cmds import ( "strings" + "github.com/hetznercloud/cli/internal/cmd/output" "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/hcloud/schema" @@ -12,7 +13,7 @@ import ( "github.com/spf13/cobra" ) -var volumeListTableOutput *tableOutput +var volumeListTableOutput *output.Table func init() { volumeListTableOutput = describeVolumeListTableOutput(nil) @@ -31,13 +32,13 @@ func newVolumeListCommand(cli *state.State) *cobra.Command { PreRunE: cli.EnsureToken, RunE: cli.Wrap(runVolumeList), } - addOutputFlag(cmd, outputOptionNoHeader(), outputOptionColumns(volumeListTableOutput.Columns()), outputOptionJSON()) + output.AddFlag(cmd, output.OptionNoHeader(), output.OptionColumns(volumeListTableOutput.Columns()), output.OptionJSON()) cmd.Flags().StringP("selector", "l", "", "Selector to filter by labels") return cmd } func runVolumeList(cli *state.State, cmd *cobra.Command, args []string) error { - outOpts := outputFlagsForCommand(cmd) + outOpts := output.FlagsForCommand(cmd) labelSelector, _ := cmd.Flags().GetString("selector") opts := hcloud.VolumeListOpts{ @@ -92,10 +93,10 @@ func runVolumeList(cli *state.State, cmd *cobra.Command, args []string) error { return nil } -func describeVolumeListTableOutput(cli *state.State) *tableOutput { - return newTableOutput(). +func describeVolumeListTableOutput(cli *state.State) *output.Table { + return output.NewTable(). AddAllowedFields(hcloud.Volume{}). - AddFieldOutputFn("server", fieldOutputFn(func(obj interface{}) string { + AddFieldFn("server", output.FieldFn(func(obj interface{}) string { volume := obj.(*hcloud.Volume) var server string if volume.Server != nil && cli != nil { @@ -103,15 +104,15 @@ func describeVolumeListTableOutput(cli *state.State) *tableOutput { } return util.NA(server) })). - AddFieldOutputFn("size", fieldOutputFn(func(obj interface{}) string { + AddFieldFn("size", output.FieldFn(func(obj interface{}) string { volume := obj.(*hcloud.Volume) return humanize.Bytes(uint64(volume.Size * humanize.GByte)) })). - AddFieldOutputFn("location", fieldOutputFn(func(obj interface{}) string { + AddFieldFn("location", output.FieldFn(func(obj interface{}) string { volume := obj.(*hcloud.Volume) return volume.Location.Name })). - AddFieldOutputFn("protection", fieldOutputFn(func(obj interface{}) string { + AddFieldFn("protection", output.FieldFn(func(obj interface{}) string { volume := obj.(*hcloud.Volume) var protection []string if volume.Protection.Delete { @@ -119,11 +120,11 @@ func describeVolumeListTableOutput(cli *state.State) *tableOutput { } return strings.Join(protection, ", ") })). - AddFieldOutputFn("labels", fieldOutputFn(func(obj interface{}) string { + AddFieldFn("labels", output.FieldFn(func(obj interface{}) string { volume := obj.(*hcloud.Volume) return util.LabelsToString(volume.Labels) })). - AddFieldOutputFn("created", fieldOutputFn(func(obj interface{}) string { + AddFieldFn("created", output.FieldFn(func(obj interface{}) string { volume := obj.(*hcloud.Volume) return util.Datetime(volume.Created) })) From 970a33c7e051ea22b767dbd62787c6848cb3a102 Mon Sep 17 00:00:00 2001 From: Michael Frister Date: Mon, 11 Jan 2021 09:36:39 +0100 Subject: [PATCH 4/9] certificate list: Fix column selection flag --- internal/cmds/certificate_list.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/cmds/certificate_list.go b/internal/cmds/certificate_list.go index 1e121bdd..6fc0e042 100644 --- a/internal/cmds/certificate_list.go +++ b/internal/cmds/certificate_list.go @@ -30,7 +30,7 @@ func newCertificatesListCommand(cli *state.State) *cobra.Command { PreRunE: cli.EnsureToken, RunE: cli.Wrap(runCertificatesList), } - output.AddFlag(cmd, output.OptionNoHeader(), output.OptionColumns(serverListTableOutput.Columns()), output.OptionJSON()) + output.AddFlag(cmd, output.OptionNoHeader(), output.OptionColumns(certificateTableOutput.Columns()), output.OptionJSON()) return cmd } From 63167842aa9b7378223a45cc8840fb2b3fca8399 Mon Sep 17 00:00:00 2001 From: Michael Frister Date: Mon, 11 Jan 2021 09:46:01 +0100 Subject: [PATCH 5/9] network describe: Fix JSON output --- internal/cmds/network_describe.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/internal/cmds/network_describe.go b/internal/cmds/network_describe.go index 9a0a1eae..98d07286 100644 --- a/internal/cmds/network_describe.go +++ b/internal/cmds/network_describe.go @@ -1,6 +1,7 @@ package cmds import ( + "encoding/json" "fmt" humanize "github.com/dustin/go-humanize" @@ -41,7 +42,7 @@ func runNetworkDescribe(cli *state.State, cmd *cobra.Command, args []string) err switch { case outputFlags.IsSet("json"): - return serverDescribeJSON(resp) + return networkDescribeJSON(resp) case outputFlags.IsSet("format"): return util.DescribeFormat(network, outputFlags["format"][0]) default: @@ -94,3 +95,17 @@ func networkDescribeText(cli *state.State, network *hcloud.Network) error { return nil } + +func networkDescribeJSON(resp *hcloud.Response) error { + var data map[string]interface{} + if err := json.NewDecoder(resp.Body).Decode(&data); err != nil { + return err + } + if network, ok := data["network"]; ok { + return util.DescribeJSON(network) + } + if networks, ok := data["networks"].([]interface{}); ok { + return util.DescribeJSON(networks[0]) + } + return util.DescribeJSON(data) +} From 1e7f826822aee71a07777054bc33116c647970c9 Mon Sep 17 00:00:00 2001 From: Michael Frister Date: Mon, 11 Jan 2021 09:49:33 +0100 Subject: [PATCH 6/9] Refactor: cmds: Split up into one package per root command --- internal/cli/root.go | 49 +++++++++++------- .../certificate/add_label.go} | 4 +- .../{cmds => cmd/certificate}/certificate.go | 18 +++---- .../certificate/create.go} | 4 +- .../certificate/delete.go} | 4 +- .../certificate/describe.go} | 4 +- .../certificate/list.go} | 4 +- .../certificate/remove_label.go} | 4 +- .../certificate/update.go} | 4 +- .../{cmds => cmd/completion}/completion.go | 4 +- .../context/active.go} | 4 +- internal/{cmds => cmd/context}/context.go | 14 ++--- .../context/create.go} | 4 +- .../context/delete.go} | 4 +- .../context_list.go => cmd/context/list.go} | 4 +- .../context_use.go => cmd/context/use.go} | 4 +- .../{cmds => cmd/datacenter}/datacenter.go | 8 +-- .../datacenter/describe.go} | 4 +- .../datacenter/list.go} | 4 +- .../floatingip/add_label.go} | 4 +- .../floatingip/assign.go} | 4 +- .../floatingip/create.go} | 4 +- .../floatingip/delete.go} | 4 +- .../floatingip/describe.go} | 4 +- .../floatingip/disable_protection.go} | 4 +- .../floatingip/enable_protection.go} | 4 +- internal/cmd/floatingip/floatingip.go | 31 +++++++++++ .../floatingip/list.go} | 4 +- .../floatingip/remove_label.go} | 4 +- .../floatingip/set_rdns.go} | 4 +- .../floatingip/unassign.go} | 4 +- .../floatingip/update.go} | 4 +- .../image/add_label.go} | 4 +- .../image_delete.go => cmd/image/delete.go} | 4 +- .../image/describe.go} | 4 +- .../image/disable_protection.go} | 4 +- .../image/enable_protection.go} | 4 +- internal/cmd/image/image.go | 27 ++++++++++ .../{cmds/image_list.go => cmd/image/list.go} | 4 +- .../image/remove_label.go} | 4 +- .../image_update.go => cmd/image/update.go} | 4 +- .../iso_describe.go => cmd/iso/describe.go} | 4 +- internal/{cmds => cmd/iso}/iso.go | 8 +-- .../{cmds/iso_list.go => cmd/iso/list.go} | 4 +- .../loadbalancer/add_label.go} | 4 +- .../loadbalancer/add_service.go} | 4 +- .../loadbalancer/add_target.go} | 4 +- .../loadbalancer/attach_to_network.go} | 4 +- .../loadbalancer/change_algorithm.go} | 4 +- .../loadbalancer/change_type.go} | 4 +- .../loadbalancer/create.go} | 4 +- .../loadbalancer/delete.go} | 4 +- .../loadbalancer/delete_service.go} | 4 +- .../loadbalancer/describe.go} | 4 +- .../loadbalancer/detach_from_network.go} | 4 +- .../loadbalancer/disable_protection.go} | 4 +- .../loadbalancer/disable_public_interface.go} | 4 +- .../loadbalancer/enable_protection.go} | 4 +- .../loadbalancer/enable_public_interface.go} | 4 +- .../loadbalancer/list.go} | 4 +- internal/cmd/loadbalancer/load_balancer.go | 40 +++++++++++++++ .../loadbalancer/metrics.go} | 4 +- .../loadbalancer/remove_label.go} | 4 +- .../loadbalancer/remove_target.go} | 4 +- .../loadbalancer/update.go} | 4 +- .../loadbalancer/update_service.go} | 4 +- .../loadbalancertype/describe.go} | 4 +- .../loadbalancertype/list.go} | 4 +- .../loadbalancertype}/load_balancer_type.go | 8 +-- .../location/describe.go} | 4 +- .../location_list.go => cmd/location/list.go} | 4 +- internal/{cmds => cmd/location}/location.go | 8 +-- .../network/add_label.go} | 4 +- .../network/add_route.go} | 4 +- .../network/add_subnet.go} | 4 +- .../network/change_ip_range.go} | 4 +- .../network/create.go} | 4 +- .../network/delete.go} | 4 +- .../network/describe.go} | 4 +- .../network/disable_protection.go} | 4 +- .../network/enable_protection.go} | 4 +- .../network_list.go => cmd/network/list.go} | 4 +- internal/cmd/network/network.go | 33 ++++++++++++ .../network/remove_label.go} | 4 +- .../network/remove_route.go} | 4 +- .../network/remove_subnet.go} | 4 +- .../network/update.go} | 4 +- .../server/add_label.go} | 4 +- .../server/attach_iso.go} | 4 +- .../server/attach_to_network.go} | 4 +- .../server/change_alias_ips.go} | 4 +- .../server/change_type.go} | 4 +- .../server_create.go => cmd/server/create.go} | 4 +- .../server/create_image.go} | 4 +- .../server_delete.go => cmd/server/delete.go} | 4 +- .../server/describe.go} | 4 +- .../server/detach_from_network.go} | 4 +- .../server/detach_iso.go} | 4 +- .../server/disable_backup.go} | 4 +- .../server/disable_protection.go} | 4 +- .../server/disable_rescue.go} | 4 +- .../server/enable_backup.go} | 4 +- .../server/enable_protection.go} | 4 +- .../server/enable_rescue.go} | 4 +- .../{cmds/server_ip.go => cmd/server/ip.go} | 4 +- .../server_list.go => cmd/server/list.go} | 4 +- .../server/metrics.go} | 4 +- .../server/poweroff.go} | 4 +- .../server/poweron.go} | 4 +- .../server_reboot.go => cmd/server/reboot.go} | 4 +- .../server/rebuild.go} | 4 +- .../server/remove_label.go} | 4 +- .../server/request_console.go} | 4 +- .../server_reset.go => cmd/server/reset.go} | 4 +- .../server/reset_password.go} | 4 +- internal/cmd/server/server.go | 51 +++++++++++++++++++ .../server/set_rdns.go} | 4 +- .../server/shutdown.go} | 4 +- .../{cmds/server_ssh.go => cmd/server/ssh.go} | 4 +- .../server_update.go => cmd/server/update.go} | 4 +- .../servertype/describe.go} | 4 +- .../servertype/list.go} | 4 +- .../servertype/server_type.go} | 8 +-- .../sshkey/add_label.go} | 4 +- .../sshkey_create.go => cmd/sshkey/create.go} | 4 +- .../sshkey_delete.go => cmd/sshkey/delete.go} | 4 +- .../sshkey/describe.go} | 4 +- .../sshkey_list.go => cmd/sshkey/list.go} | 4 +- .../sshkey/remove_label.go} | 4 +- internal/{cmds => cmd/sshkey}/sshkey.go | 18 +++---- .../sshkey_update.go => cmd/sshkey/update.go} | 4 +- internal/{cmds => cmd/version}/version.go | 4 +- .../volume/add_label.go} | 4 +- .../volume_attach.go => cmd/volume/attach.go} | 4 +- .../volume_create.go => cmd/volume/create.go} | 4 +- .../volume_delete.go => cmd/volume/delete.go} | 4 +- .../volume/describe.go} | 4 +- .../volume_detach.go => cmd/volume/detach.go} | 4 +- .../volume/disable_protection.go} | 4 +- .../volume/enable_protection.go} | 4 +- .../volume_list.go => cmd/volume/list.go} | 4 +- .../volume/remove_label.go} | 4 +- .../volume_resize.go => cmd/volume/resize.go} | 4 +- .../volume_update.go => cmd/volume/update.go} | 4 +- internal/cmd/volume/volume.go | 31 +++++++++++ internal/cmds/floatingip.go | 31 ----------- internal/cmds/image.go | 27 ---------- internal/cmds/load_balancer.go | 40 --------------- internal/cmds/network.go | 33 ------------ internal/cmds/server.go | 51 ------------------- internal/cmds/volume.go | 31 ----------- 151 files changed, 550 insertions(+), 535 deletions(-) rename internal/{cmds/certificate_add_label.go => cmd/certificate/add_label.go} (95%) rename internal/{cmds => cmd/certificate}/certificate.go (50%) rename internal/{cmds/certificate_create.go => cmd/certificate/create.go} (95%) rename internal/{cmds/certificate_delete.go => cmd/certificate/delete.go} (92%) rename internal/{cmds/certificate_describe.go => cmd/certificate/describe.go} (96%) rename internal/{cmds/certificate_list.go => cmd/certificate/list.go} (97%) rename internal/{cmds/certificate_remove_label.go => cmd/certificate/remove_label.go} (96%) rename internal/{cmds/certificate_update.go => cmd/certificate/update.go} (94%) rename internal/{cmds => cmd/completion}/completion.go (97%) rename internal/{cmds/context_active.go => cmd/context/active.go} (87%) rename internal/{cmds => cmd/context}/context.go (59%) rename internal/{cmds/context_create.go => cmd/context/create.go} (95%) rename internal/{cmds/context_delete.go => cmd/context/delete.go} (92%) rename internal/{cmds/context_list.go => cmd/context/list.go} (95%) rename internal/{cmds/context_use.go => cmd/context/use.go} (91%) rename internal/{cmds => cmd/datacenter}/datacenter.go (69%) rename internal/{cmds/datacenter_describe.go => cmd/datacenter/describe.go} (97%) rename internal/{cmds/datacenter_list.go => cmd/datacenter/list.go} (96%) rename internal/{cmds/floatingip_add_label.go => cmd/floatingip/add_label.go} (95%) rename internal/{cmds/floatingip_assign.go => cmd/floatingip/assign.go} (94%) rename internal/{cmds/floatingip_create.go => cmd/floatingip/create.go} (97%) rename internal/{cmds/floatingip_delete.go => cmd/floatingip/delete.go} (92%) rename internal/{cmds/floatingip_describe.go => cmd/floatingip/describe.go} (97%) rename internal/{cmds/floatingip_disable_protection.go => cmd/floatingip/disable_protection.go} (94%) rename internal/{cmds/floatingip_enable_protection.go => cmd/floatingip/enable_protection.go} (94%) create mode 100644 internal/cmd/floatingip/floatingip.go rename internal/{cmds/floatingip_list.go => cmd/floatingip/list.go} (98%) rename internal/{cmds/floatingip_remove_label.go => cmd/floatingip/remove_label.go} (96%) rename internal/{cmds/floatingip_set_rdns.go => cmd/floatingip/set_rdns.go} (95%) rename internal/{cmds/floatingip_unassign.go => cmd/floatingip/unassign.go} (93%) rename internal/{cmds/floatingip_update.go => cmd/floatingip/update.go} (94%) rename internal/{cmds/image_add_label.go => cmd/image/add_label.go} (95%) rename internal/{cmds/image_delete.go => cmd/image/delete.go} (92%) rename internal/{cmds/image_describe.go => cmd/image/describe.go} (97%) rename internal/{cmds/image_disable_protection.go => cmd/image/disable_protection.go} (94%) rename internal/{cmds/image_enable_protection.go => cmd/image/enable_protection.go} (94%) create mode 100644 internal/cmd/image/image.go rename internal/{cmds/image_list.go => cmd/image/list.go} (98%) rename internal/{cmds/image_remove_label.go => cmd/image/remove_label.go} (96%) rename internal/{cmds/image_update.go => cmd/image/update.go} (95%) rename internal/{cmds/iso_describe.go => cmd/iso/describe.go} (96%) rename internal/{cmds => cmd/iso}/iso.go (72%) rename internal/{cmds/iso_list.go => cmd/iso/list.go} (95%) rename internal/{cmds/load_balancer_add_label.go => cmd/loadbalancer/add_label.go} (95%) rename internal/{cmds/load_balancer_add_service.go => cmd/loadbalancer/add_service.go} (98%) rename internal/{cmds/load_balancer_add_target.go => cmd/loadbalancer/add_target.go} (97%) rename internal/{cmds/load_balancer_attach_to_network.go => cmd/loadbalancer/attach_to_network.go} (95%) rename internal/{cmds/load_balancer_change_algorithm.go => cmd/loadbalancer/change_algorithm.go} (95%) rename internal/{cmds/load_balancer_change_type.go => cmd/loadbalancer/change_type.go} (95%) rename internal/{cmds/load_balancer_create.go => cmd/loadbalancer/create.go} (97%) rename internal/{cmds/load_balancer_delete.go => cmd/loadbalancer/delete.go} (92%) rename internal/{cmds/load_balancer_delete_service.go => cmd/loadbalancer/delete_service.go} (94%) rename internal/{cmds/load_balancer_describe.go => cmd/loadbalancer/describe.go} (98%) rename internal/{cmds/load_balancer_detach_from_network.go => cmd/loadbalancer/detach_from_network.go} (95%) rename internal/{cmds/load_balancer_disable_protection.go => cmd/loadbalancer/disable_protection.go} (94%) rename internal/{cmds/load_balancer_disable_public_interface.go => cmd/loadbalancer/disable_public_interface.go} (93%) rename internal/{cmds/load_balancer_enable_protection.go => cmd/loadbalancer/enable_protection.go} (94%) rename internal/{cmds/load_balancer_enable_public_interface.go => cmd/loadbalancer/enable_public_interface.go} (93%) rename internal/{cmds/load_balancer_list.go => cmd/loadbalancer/list.go} (98%) create mode 100644 internal/cmd/loadbalancer/load_balancer.go rename internal/{cmds/load_balancer_metrics.go => cmd/loadbalancer/metrics.go} (97%) rename internal/{cmds/load_balancer_remove_label.go => cmd/loadbalancer/remove_label.go} (96%) rename internal/{cmds/load_balancer_remove_target.go => cmd/loadbalancer/remove_target.go} (96%) rename internal/{cmds/load_balancer_update.go => cmd/loadbalancer/update.go} (94%) rename internal/{cmds/load_balancer_update_service.go => cmd/loadbalancer/update_service.go} (98%) rename internal/{cmds/load_balancer_type_describe.go => cmd/loadbalancertype/describe.go} (96%) rename internal/{cmds/load_balancer_type_list.go => cmd/loadbalancertype/list.go} (95%) rename internal/{cmds => cmd/loadbalancertype}/load_balancer_type.go (68%) rename internal/{cmds/location_describe.go => cmd/location/describe.go} (96%) rename internal/{cmds/location_list.go => cmd/location/list.go} (95%) rename internal/{cmds => cmd/location}/location.go (70%) rename internal/{cmds/network_add_label.go => cmd/network/add_label.go} (95%) rename internal/{cmds/network_add_route.go => cmd/network/add_route.go} (95%) rename internal/{cmds/network_add_subnet.go => cmd/network/add_subnet.go} (96%) rename internal/{cmds/network_change_ip_range.go => cmd/network/change_ip_range.go} (94%) rename internal/{cmds/network_create.go => cmd/network/create.go} (94%) rename internal/{cmds/network_delete.go => cmd/network/delete.go} (92%) rename internal/{cmds/network_describe.go => cmd/network/describe.go} (97%) rename internal/{cmds/network_disable_protection.go => cmd/network/disable_protection.go} (94%) rename internal/{cmds/network_enable_protection.go => cmd/network/enable_protection.go} (94%) rename internal/{cmds/network_list.go => cmd/network/list.go} (98%) create mode 100644 internal/cmd/network/network.go rename internal/{cmds/network_remove_label.go => cmd/network/remove_label.go} (96%) rename internal/{cmds/network_remove_route.go => cmd/network/remove_route.go} (95%) rename internal/{cmds/network_remove_subnet.go => cmd/network/remove_subnet.go} (94%) rename internal/{cmds/network_update.go => cmd/network/update.go} (94%) rename internal/{cmds/server_add_label.go => cmd/server/add_label.go} (95%) rename internal/{cmds/server_attach_iso.go => cmd/server/attach_iso.go} (94%) rename internal/{cmds/server_attach_to_network.go => cmd/server/attach_to_network.go} (96%) rename internal/{cmds/server_change_alias_ips.go => cmd/server/change_alias_ips.go} (96%) rename internal/{cmds/server_change_type.go => cmd/server/change_type.go} (95%) rename internal/{cmds/server_create.go => cmd/server/create.go} (99%) rename internal/{cmds/server_create_image.go => cmd/server/create_image.go} (96%) rename internal/{cmds/server_delete.go => cmd/server/delete.go} (92%) rename internal/{cmds/server_describe.go => cmd/server/describe.go} (98%) rename internal/{cmds/server_detach_from_network.go => cmd/server/detach_from_network.go} (95%) rename internal/{cmds/server_detach_iso.go => cmd/server/detach_iso.go} (93%) rename internal/{cmds/server_disable_backup.go => cmd/server/disable_backup.go} (93%) rename internal/{cmds/server_disable_protection.go => cmd/server/disable_protection.go} (95%) rename internal/{cmds/server_disable_rescue.go => cmd/server/disable_rescue.go} (93%) rename internal/{cmds/server_enable_backup.go => cmd/server/enable_backup.go} (94%) rename internal/{cmds/server_enable_protection.go => cmd/server/enable_protection.go} (95%) rename internal/{cmds/server_enable_rescue.go => cmd/server/enable_rescue.go} (96%) rename internal/{cmds/server_ip.go => cmd/server/ip.go} (93%) rename internal/{cmds/server_list.go => cmd/server/list.go} (98%) rename internal/{cmds/server_metrics.go => cmd/server/metrics.go} (97%) rename internal/{cmds/server_poweroff.go => cmd/server/poweroff.go} (93%) rename internal/{cmds/server_poweron.go => cmd/server/poweron.go} (93%) rename internal/{cmds/server_reboot.go => cmd/server/reboot.go} (93%) rename internal/{cmds/server_rebuild.go => cmd/server/rebuild.go} (95%) rename internal/{cmds/server_remove_label.go => cmd/server/remove_label.go} (96%) rename internal/{cmds/server_request_console.go => cmd/server/request_console.go} (95%) rename internal/{cmds/server_reset.go => cmd/server/reset.go} (93%) rename internal/{cmds/server_reset_password.go => cmd/server/reset_password.go} (93%) create mode 100644 internal/cmd/server/server.go rename internal/{cmds/server_set_rdns.go => cmd/server/set_rdns.go} (95%) rename internal/{cmds/server_shutdown.go => cmd/server/shutdown.go} (93%) rename internal/{cmds/server_ssh.go => cmd/server/ssh.go} (96%) rename internal/{cmds/server_update.go => cmd/server/update.go} (94%) rename internal/{cmds/servertypes_describe.go => cmd/servertype/describe.go} (96%) rename internal/{cmds/servertypes_list.go => cmd/servertype/list.go} (96%) rename internal/{cmds/servertypes.go => cmd/servertype/server_type.go} (69%) rename internal/{cmds/sshkey_add_label.go => cmd/sshkey/add_label.go} (95%) rename internal/{cmds/sshkey_create.go => cmd/sshkey/create.go} (96%) rename internal/{cmds/sshkey_delete.go => cmd/sshkey/delete.go} (92%) rename internal/{cmds/sshkey_describe.go => cmd/sshkey/describe.go} (96%) rename internal/{cmds/sshkey_list.go => cmd/sshkey/list.go} (97%) rename internal/{cmds/sshkey_remove_label.go => cmd/sshkey/remove_label.go} (96%) rename internal/{cmds => cmd/sshkey}/sshkey.go (52%) rename internal/{cmds/sshkey_update.go => cmd/sshkey/update.go} (94%) rename internal/{cmds => cmd/version}/version.go (88%) rename internal/{cmds/volume_add_label.go => cmd/volume/add_label.go} (95%) rename internal/{cmds/volume_attach.go => cmd/volume/attach.go} (95%) rename internal/{cmds/volume_create.go => cmd/volume/create.go} (97%) rename internal/{cmds/volume_delete.go => cmd/volume/delete.go} (92%) rename internal/{cmds/volume_describe.go => cmd/volume/describe.go} (97%) rename internal/{cmds/volume_detach.go => cmd/volume/detach.go} (93%) rename internal/{cmds/volume_disable_protection.go => cmd/volume/disable_protection.go} (94%) rename internal/{cmds/volume_enable_protection.go => cmd/volume/enable_protection.go} (94%) rename internal/{cmds/volume_list.go => cmd/volume/list.go} (98%) rename internal/{cmds/volume_remove_label.go => cmd/volume/remove_label.go} (96%) rename internal/{cmds/volume_resize.go => cmd/volume/resize.go} (94%) rename internal/{cmds/volume_update.go => cmd/volume/update.go} (94%) create mode 100644 internal/cmd/volume/volume.go delete mode 100644 internal/cmds/floatingip.go delete mode 100644 internal/cmds/image.go delete mode 100644 internal/cmds/load_balancer.go delete mode 100644 internal/cmds/network.go delete mode 100644 internal/cmds/server.go delete mode 100644 internal/cmds/volume.go diff --git a/internal/cli/root.go b/internal/cli/root.go index ba5d7dbc..48843a0d 100644 --- a/internal/cli/root.go +++ b/internal/cli/root.go @@ -3,7 +3,22 @@ package cli import ( "time" - "github.com/hetznercloud/cli/internal/cmds" + "github.com/hetznercloud/cli/internal/cmd/certificate" + "github.com/hetznercloud/cli/internal/cmd/completion" + "github.com/hetznercloud/cli/internal/cmd/context" + "github.com/hetznercloud/cli/internal/cmd/datacenter" + "github.com/hetznercloud/cli/internal/cmd/floatingip" + "github.com/hetznercloud/cli/internal/cmd/image" + "github.com/hetznercloud/cli/internal/cmd/iso" + "github.com/hetznercloud/cli/internal/cmd/loadbalancer" + "github.com/hetznercloud/cli/internal/cmd/loadbalancertype" + "github.com/hetznercloud/cli/internal/cmd/location" + "github.com/hetznercloud/cli/internal/cmd/network" + "github.com/hetznercloud/cli/internal/cmd/server" + "github.com/hetznercloud/cli/internal/cmd/servertype" + "github.com/hetznercloud/cli/internal/cmd/sshkey" + "github.com/hetznercloud/cli/internal/cmd/version" + "github.com/hetznercloud/cli/internal/cmd/volume" "github.com/hetznercloud/cli/internal/state" "github.com/spf13/cobra" ) @@ -19,22 +34,22 @@ func NewRootCommand(state *state.State) *cobra.Command { DisableFlagsInUseLine: true, } cmd.AddCommand( - cmds.NewFloatingIPCommand(state), - cmds.NewImageCommand(state), - cmds.NewServerCommand(state), - cmds.NewSSHKeyCommand(state), - cmds.NewVersionCommand(state), - cmds.NewCompletionCommand(state), - cmds.NewServerTypeCommand(state), - cmds.NewContextCommand(state), - cmds.NewDatacenterCommand(state), - cmds.NewLocationCommand(state), - cmds.NewISOCommand(state), - cmds.NewVolumeCommand(state), - cmds.NewNetworkCommand(state), - cmds.NewLoadBalancerCommand(state), - cmds.NewLoadBalancerTypeCommand(state), - cmds.NewCertificatesCommand(state), + floatingip.NewCommand(state), + image.NewCommand(state), + server.NewCommand(state), + sshkey.NewCommand(state), + version.NewCommand(state), + completion.NewCommand(state), + servertype.NewCommand(state), + context.NewCommand(state), + datacenter.NewCommand(state), + location.NewCommand(state), + iso.NewCommand(state), + volume.NewCommand(state), + network.NewCommand(state), + loadbalancer.NewCommand(state), + loadbalancertype.NewCommand(state), + certificate.NewCommand(state), ) cmd.PersistentFlags().Duration("poll-interval", 500*time.Millisecond, "Interval at which to poll information, for example action progress") return cmd diff --git a/internal/cmds/certificate_add_label.go b/internal/cmd/certificate/add_label.go similarity index 95% rename from internal/cmds/certificate_add_label.go rename to internal/cmd/certificate/add_label.go index 2fea9b21..009db54f 100644 --- a/internal/cmds/certificate_add_label.go +++ b/internal/cmd/certificate/add_label.go @@ -1,4 +1,4 @@ -package cmds +package certificate import ( "fmt" @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -func newCertificateAddLabelCommand(cli *state.State) *cobra.Command { +func newAddLabelCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "add-label [FLAGS] CERTIFICATE LABEL", Short: "Add a label to a certificate", diff --git a/internal/cmds/certificate.go b/internal/cmd/certificate/certificate.go similarity index 50% rename from internal/cmds/certificate.go rename to internal/cmd/certificate/certificate.go index d2f7ad0f..aa104785 100644 --- a/internal/cmds/certificate.go +++ b/internal/cmd/certificate/certificate.go @@ -1,11 +1,11 @@ -package cmds +package certificate import ( "github.com/hetznercloud/cli/internal/state" "github.com/spf13/cobra" ) -func NewCertificatesCommand(cli *state.State) *cobra.Command { +func NewCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "certificate", Short: "Manage certificates", @@ -14,13 +14,13 @@ func NewCertificatesCommand(cli *state.State) *cobra.Command { DisableFlagsInUseLine: true, } cmd.AddCommand( - newCertificatesListCommand(cli), - newCertificateCreateCommand(cli), - newCertificateUpdateCommand(cli), - newCertificateAddLabelCommand(cli), - newCertificateRemoveLabelCommand(cli), - newCertificateDeleteCommand(cli), - newCertificateDescribeCommand(cli), + newsListCommand(cli), + newCreateCommand(cli), + newUpdateCommand(cli), + newAddLabelCommand(cli), + newRemoveLabelCommand(cli), + newDeleteCommand(cli), + newDescribeCommand(cli), ) return cmd diff --git a/internal/cmds/certificate_create.go b/internal/cmd/certificate/create.go similarity index 95% rename from internal/cmds/certificate_create.go rename to internal/cmd/certificate/create.go index a98a4ed6..0a45b2de 100644 --- a/internal/cmds/certificate_create.go +++ b/internal/cmd/certificate/create.go @@ -1,4 +1,4 @@ -package cmds +package certificate import ( "fmt" @@ -9,7 +9,7 @@ import ( "github.com/spf13/cobra" ) -func newCertificateCreateCommand(cli *state.State) *cobra.Command { +func newCreateCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "create [FLAGS]", Short: "Create or upload a Certificate", diff --git a/internal/cmds/certificate_delete.go b/internal/cmd/certificate/delete.go similarity index 92% rename from internal/cmds/certificate_delete.go rename to internal/cmd/certificate/delete.go index 6f8c8aa0..99c24af0 100644 --- a/internal/cmds/certificate_delete.go +++ b/internal/cmd/certificate/delete.go @@ -1,4 +1,4 @@ -package cmds +package certificate import ( "fmt" @@ -8,7 +8,7 @@ import ( "github.com/spf13/cobra" ) -func newCertificateDeleteCommand(cli *state.State) *cobra.Command { +func newDeleteCommand(cli *state.State) *cobra.Command { return &cobra.Command{ Use: "delete CERTIFICATE", Short: "Delete a certificate", diff --git a/internal/cmds/certificate_describe.go b/internal/cmd/certificate/describe.go similarity index 96% rename from internal/cmds/certificate_describe.go rename to internal/cmd/certificate/describe.go index ecd6c9c3..8efea20f 100644 --- a/internal/cmds/certificate_describe.go +++ b/internal/cmd/certificate/describe.go @@ -1,4 +1,4 @@ -package cmds +package certificate import ( "encoding/json" @@ -13,7 +13,7 @@ import ( "github.com/spf13/cobra" ) -func newCertificateDescribeCommand(cli *state.State) *cobra.Command { +func newDescribeCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "describe [FLAGS] CERTIFICATE", Short: "Describe a certificate", diff --git a/internal/cmds/certificate_list.go b/internal/cmd/certificate/list.go similarity index 97% rename from internal/cmds/certificate_list.go rename to internal/cmd/certificate/list.go index 6fc0e042..023fa3bd 100644 --- a/internal/cmds/certificate_list.go +++ b/internal/cmd/certificate/list.go @@ -1,4 +1,4 @@ -package cmds +package certificate import ( "strings" @@ -17,7 +17,7 @@ func init() { certificateTableOutput = describeCertificatesTableOutput() } -func newCertificatesListCommand(cli *state.State) *cobra.Command { +func newsListCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "list [FLAGS]", Short: "List Certificates", diff --git a/internal/cmds/certificate_remove_label.go b/internal/cmd/certificate/remove_label.go similarity index 96% rename from internal/cmds/certificate_remove_label.go rename to internal/cmd/certificate/remove_label.go index 61c1bfca..527490ac 100644 --- a/internal/cmds/certificate_remove_label.go +++ b/internal/cmd/certificate/remove_label.go @@ -1,4 +1,4 @@ -package cmds +package certificate import ( "errors" @@ -11,7 +11,7 @@ import ( "github.com/spf13/cobra" ) -func newCertificateRemoveLabelCommand(cli *state.State) *cobra.Command { +func newRemoveLabelCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "remove-label [FLAGS] CERTIFICATE LABELKEY", Short: "Remove a label from a certificate", diff --git a/internal/cmds/certificate_update.go b/internal/cmd/certificate/update.go similarity index 94% rename from internal/cmds/certificate_update.go rename to internal/cmd/certificate/update.go index 2a44a186..81ee518a 100644 --- a/internal/cmds/certificate_update.go +++ b/internal/cmd/certificate/update.go @@ -1,4 +1,4 @@ -package cmds +package certificate import ( "fmt" @@ -9,7 +9,7 @@ import ( "github.com/spf13/cobra" ) -func newCertificateUpdateCommand(cli *state.State) *cobra.Command { +func newUpdateCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "update [FLAGS] CERTIFICATE", Short: "Update an existing Certificate", diff --git a/internal/cmds/completion.go b/internal/cmd/completion/completion.go similarity index 97% rename from internal/cmds/completion.go rename to internal/cmd/completion/completion.go index 6a4389da..bd876e16 100644 --- a/internal/cmds/completion.go +++ b/internal/cmd/completion/completion.go @@ -1,4 +1,4 @@ -package cmds +package completion import ( "fmt" @@ -30,7 +30,7 @@ import ( // source <(hcloud completion zsh)` // ) -func NewCompletionCommand(cli *state.State) *cobra.Command { +func NewCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "completion [FLAGS] SHELL", Short: "Output shell completion code for the specified shell", diff --git a/internal/cmds/context_active.go b/internal/cmd/context/active.go similarity index 87% rename from internal/cmds/context_active.go rename to internal/cmd/context/active.go index 95771c2d..a282b386 100644 --- a/internal/cmds/context_active.go +++ b/internal/cmd/context/active.go @@ -1,4 +1,4 @@ -package cmds +package context import ( "fmt" @@ -7,7 +7,7 @@ import ( "github.com/spf13/cobra" ) -func newContextActiveCommand(cli *state.State) *cobra.Command { +func newActiveCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "active [FLAGS]", Short: "Show active context", diff --git a/internal/cmds/context.go b/internal/cmd/context/context.go similarity index 59% rename from internal/cmds/context.go rename to internal/cmd/context/context.go index 341bf55f..e4fc38fd 100644 --- a/internal/cmds/context.go +++ b/internal/cmd/context/context.go @@ -1,11 +1,11 @@ -package cmds +package context import ( "github.com/hetznercloud/cli/internal/state" "github.com/spf13/cobra" ) -func NewContextCommand(cli *state.State) *cobra.Command { +func NewCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "context [FLAGS]", Short: "Manage contexts", @@ -14,11 +14,11 @@ func NewContextCommand(cli *state.State) *cobra.Command { DisableFlagsInUseLine: true, } cmd.AddCommand( - newContextCreateCommand(cli), - newContextActiveCommand(cli), - newContextUseCommand(cli), - newContextDeleteCommand(cli), - newContextListCommand(cli), + newCreateCommand(cli), + newActiveCommand(cli), + newUseCommand(cli), + newDeleteCommand(cli), + newListCommand(cli), ) return cmd } diff --git a/internal/cmds/context_create.go b/internal/cmd/context/create.go similarity index 95% rename from internal/cmds/context_create.go rename to internal/cmd/context/create.go index 1626b6ea..028e938c 100644 --- a/internal/cmds/context_create.go +++ b/internal/cmd/context/create.go @@ -1,4 +1,4 @@ -package cmds +package context import ( "bytes" @@ -12,7 +12,7 @@ import ( "golang.org/x/crypto/ssh/terminal" ) -func newContextCreateCommand(cli *state.State) *cobra.Command { +func newCreateCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "create [FLAGS] NAME", Short: "Create a new context", diff --git a/internal/cmds/context_delete.go b/internal/cmd/context/delete.go similarity index 92% rename from internal/cmds/context_delete.go rename to internal/cmd/context/delete.go index 72590315..b2297248 100644 --- a/internal/cmds/context_delete.go +++ b/internal/cmd/context/delete.go @@ -1,4 +1,4 @@ -package cmds +package context import ( "fmt" @@ -8,7 +8,7 @@ import ( "github.com/spf13/cobra" ) -func newContextDeleteCommand(cli *state.State) *cobra.Command { +func newDeleteCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "delete [FLAGS] NAME", Short: "Delete a context", diff --git a/internal/cmds/context_list.go b/internal/cmd/context/list.go similarity index 95% rename from internal/cmds/context_list.go rename to internal/cmd/context/list.go index 6ad8dd0d..cf5d4d70 100644 --- a/internal/cmds/context_list.go +++ b/internal/cmd/context/list.go @@ -1,4 +1,4 @@ -package cmds +package context import ( "github.com/hetznercloud/cli/internal/cmd/output" @@ -21,7 +21,7 @@ func init() { RemoveAllowedField("token") } -func newContextListCommand(cli *state.State) *cobra.Command { +func newListCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "list [FLAGS]", Short: "List contexts", diff --git a/internal/cmds/context_use.go b/internal/cmd/context/use.go similarity index 91% rename from internal/cmds/context_use.go rename to internal/cmd/context/use.go index 4763b10b..5e62707e 100644 --- a/internal/cmds/context_use.go +++ b/internal/cmd/context/use.go @@ -1,4 +1,4 @@ -package cmds +package context import ( "fmt" @@ -8,7 +8,7 @@ import ( "github.com/spf13/cobra" ) -func newContextUseCommand(cli *state.State) *cobra.Command { +func newUseCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "use [FLAGS] NAME", Short: "Use a context", diff --git a/internal/cmds/datacenter.go b/internal/cmd/datacenter/datacenter.go similarity index 69% rename from internal/cmds/datacenter.go rename to internal/cmd/datacenter/datacenter.go index 60637dbb..8866f401 100644 --- a/internal/cmds/datacenter.go +++ b/internal/cmd/datacenter/datacenter.go @@ -1,11 +1,11 @@ -package cmds +package datacenter import ( "github.com/hetznercloud/cli/internal/state" "github.com/spf13/cobra" ) -func NewDatacenterCommand(cli *state.State) *cobra.Command { +func NewCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "datacenter", Short: "Manage datacenters", @@ -14,8 +14,8 @@ func NewDatacenterCommand(cli *state.State) *cobra.Command { DisableFlagsInUseLine: true, } cmd.AddCommand( - newDatacenterListCommand(cli), - newDatacenterDescribeCommand(cli), + newListCommand(cli), + newDescribeCommand(cli), ) return cmd } diff --git a/internal/cmds/datacenter_describe.go b/internal/cmd/datacenter/describe.go similarity index 97% rename from internal/cmds/datacenter_describe.go rename to internal/cmd/datacenter/describe.go index dd7088ae..8b6be5d7 100644 --- a/internal/cmds/datacenter_describe.go +++ b/internal/cmd/datacenter/describe.go @@ -1,4 +1,4 @@ -package cmds +package datacenter import ( "encoding/json" @@ -12,7 +12,7 @@ import ( "github.com/spf13/cobra" ) -func newDatacenterDescribeCommand(cli *state.State) *cobra.Command { +func newDescribeCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "describe [FLAGS] DATACENTER", Short: "Describe a datacenter", diff --git a/internal/cmds/datacenter_list.go b/internal/cmd/datacenter/list.go similarity index 96% rename from internal/cmds/datacenter_list.go rename to internal/cmd/datacenter/list.go index 1c9429e0..0c8442d0 100644 --- a/internal/cmds/datacenter_list.go +++ b/internal/cmd/datacenter/list.go @@ -1,4 +1,4 @@ -package cmds +package datacenter import ( "github.com/hetznercloud/cli/internal/cmd/output" @@ -20,7 +20,7 @@ func init() { })) } -func newDatacenterListCommand(cli *state.State) *cobra.Command { +func newListCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "list [FLAGS]", Short: "List datacenters", diff --git a/internal/cmds/floatingip_add_label.go b/internal/cmd/floatingip/add_label.go similarity index 95% rename from internal/cmds/floatingip_add_label.go rename to internal/cmd/floatingip/add_label.go index 8aae78a3..091aa7c6 100644 --- a/internal/cmds/floatingip_add_label.go +++ b/internal/cmd/floatingip/add_label.go @@ -1,4 +1,4 @@ -package cmds +package floatingip import ( "fmt" @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -func newFloatingIPAddLabelCommand(cli *state.State) *cobra.Command { +func newAddLabelCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "add-label [FLAGS] FLOATINGIP LABEL", Short: "Add a label to a Floating IP", diff --git a/internal/cmds/floatingip_assign.go b/internal/cmd/floatingip/assign.go similarity index 94% rename from internal/cmds/floatingip_assign.go rename to internal/cmd/floatingip/assign.go index d23b75cd..cd05bef8 100644 --- a/internal/cmds/floatingip_assign.go +++ b/internal/cmd/floatingip/assign.go @@ -1,4 +1,4 @@ -package cmds +package floatingip import ( "fmt" @@ -8,7 +8,7 @@ import ( "github.com/spf13/cobra" ) -func newFloatingIPAssignCommand(cli *state.State) *cobra.Command { +func newAssignCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "assign [FLAGS] FLOATINGIP SERVER", Short: "Assign a Floating IP to a server", diff --git a/internal/cmds/floatingip_create.go b/internal/cmd/floatingip/create.go similarity index 97% rename from internal/cmds/floatingip_create.go rename to internal/cmd/floatingip/create.go index 8b80c7c9..50bf1e9e 100644 --- a/internal/cmds/floatingip_create.go +++ b/internal/cmd/floatingip/create.go @@ -1,4 +1,4 @@ -package cmds +package floatingip import ( "errors" @@ -11,7 +11,7 @@ import ( "github.com/spf13/cobra" ) -func newFloatingIPCreateCommand(cli *state.State) *cobra.Command { +func newCreateCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "create FLAGS", Short: "Create a Floating IP", diff --git a/internal/cmds/floatingip_delete.go b/internal/cmd/floatingip/delete.go similarity index 92% rename from internal/cmds/floatingip_delete.go rename to internal/cmd/floatingip/delete.go index 4c7e79d5..aeca87ee 100644 --- a/internal/cmds/floatingip_delete.go +++ b/internal/cmd/floatingip/delete.go @@ -1,4 +1,4 @@ -package cmds +package floatingip import ( "fmt" @@ -8,7 +8,7 @@ import ( "github.com/spf13/cobra" ) -func newFloatingIPDeleteCommand(cli *state.State) *cobra.Command { +func newDeleteCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "delete [FLAGS] FLOATINGIP", Short: "Delete a Floating IP", diff --git a/internal/cmds/floatingip_describe.go b/internal/cmd/floatingip/describe.go similarity index 97% rename from internal/cmds/floatingip_describe.go rename to internal/cmd/floatingip/describe.go index 586c690f..92ae2e2e 100644 --- a/internal/cmds/floatingip_describe.go +++ b/internal/cmd/floatingip/describe.go @@ -1,4 +1,4 @@ -package cmds +package floatingip import ( "encoding/json" @@ -13,7 +13,7 @@ import ( "github.com/spf13/cobra" ) -func newFloatingIPDescribeCommand(cli *state.State) *cobra.Command { +func newDescribeCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "describe [FLAGS] FLOATINGIP", Short: "Describe a Floating IP", diff --git a/internal/cmds/floatingip_disable_protection.go b/internal/cmd/floatingip/disable_protection.go similarity index 94% rename from internal/cmds/floatingip_disable_protection.go rename to internal/cmd/floatingip/disable_protection.go index 1c14c5a1..8e1d3e0d 100644 --- a/internal/cmds/floatingip_disable_protection.go +++ b/internal/cmd/floatingip/disable_protection.go @@ -1,4 +1,4 @@ -package cmds +package floatingip import ( "fmt" @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -func newFloatingIPDisableProtectionCommand(cli *state.State) *cobra.Command { +func newDisableProtectionCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "disable-protection [FLAGS] FLOATINGIP PROTECTIONLEVEL [PROTECTIONLEVEL...]", Short: "Disable resource protection for a Floating IP", diff --git a/internal/cmds/floatingip_enable_protection.go b/internal/cmd/floatingip/enable_protection.go similarity index 94% rename from internal/cmds/floatingip_enable_protection.go rename to internal/cmd/floatingip/enable_protection.go index e68e891c..2b070f3a 100644 --- a/internal/cmds/floatingip_enable_protection.go +++ b/internal/cmd/floatingip/enable_protection.go @@ -1,4 +1,4 @@ -package cmds +package floatingip import ( "fmt" @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -func newFloatingIPEnableProtectionCommand(cli *state.State) *cobra.Command { +func newEnableProtectionCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "enable-protection [FLAGS] FLOATINGIP PROTECTIONLEVEL [PROTECTIONLEVEL...]", Short: "Enable resource protection for a Floating IP", diff --git a/internal/cmd/floatingip/floatingip.go b/internal/cmd/floatingip/floatingip.go new file mode 100644 index 00000000..f459e362 --- /dev/null +++ b/internal/cmd/floatingip/floatingip.go @@ -0,0 +1,31 @@ +package floatingip + +import ( + "github.com/hetznercloud/cli/internal/state" + "github.com/spf13/cobra" +) + +func NewCommand(cli *state.State) *cobra.Command { + cmd := &cobra.Command{ + Use: "floating-ip", + Short: "Manage Floating IPs", + Args: cobra.NoArgs, + TraverseChildren: true, + DisableFlagsInUseLine: true, + } + cmd.AddCommand( + newUpdateCommand(cli), + newListCommand(cli), + newCreateCommand(cli), + newDescribeCommand(cli), + newAssignCommand(cli), + newUnassignCommand(cli), + newDeleteCommand(cli), + newEnableProtectionCommand(cli), + newDisableProtectionCommand(cli), + newAddLabelCommand(cli), + newRemoveLabelCommand(cli), + newSetRDNSCommand(cli), + ) + return cmd +} diff --git a/internal/cmds/floatingip_list.go b/internal/cmd/floatingip/list.go similarity index 98% rename from internal/cmds/floatingip_list.go rename to internal/cmd/floatingip/list.go index 9794ab09..492950b6 100644 --- a/internal/cmds/floatingip_list.go +++ b/internal/cmd/floatingip/list.go @@ -1,4 +1,4 @@ -package cmds +package floatingip import ( "fmt" @@ -19,7 +19,7 @@ func init() { floatingIPListTableOutput = describeFloatingIPListTableOutput(nil) } -func newFloatingIPListCommand(cli *state.State) *cobra.Command { +func newListCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "list [FLAGS]", Short: "List Floating IPs", diff --git a/internal/cmds/floatingip_remove_label.go b/internal/cmd/floatingip/remove_label.go similarity index 96% rename from internal/cmds/floatingip_remove_label.go rename to internal/cmd/floatingip/remove_label.go index db841518..f8055a7b 100644 --- a/internal/cmds/floatingip_remove_label.go +++ b/internal/cmd/floatingip/remove_label.go @@ -1,4 +1,4 @@ -package cmds +package floatingip import ( "errors" @@ -11,7 +11,7 @@ import ( "github.com/spf13/cobra" ) -func newFloatingIPRemoveLabelCommand(cli *state.State) *cobra.Command { +func newRemoveLabelCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "remove-label [FLAGS] FLOATINGIP LABELKEY", Short: "Remove a label from a Floating IP", diff --git a/internal/cmds/floatingip_set_rdns.go b/internal/cmd/floatingip/set_rdns.go similarity index 95% rename from internal/cmds/floatingip_set_rdns.go rename to internal/cmd/floatingip/set_rdns.go index e3689b30..4fcb172a 100644 --- a/internal/cmds/floatingip_set_rdns.go +++ b/internal/cmd/floatingip/set_rdns.go @@ -1,4 +1,4 @@ -package cmds +package floatingip import ( "fmt" @@ -9,7 +9,7 @@ import ( "github.com/spf13/cobra" ) -func newFloatingIPSetRDNSCommand(cli *state.State) *cobra.Command { +func newSetRDNSCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "set-rdns [FLAGS] FLOATINGIP", Short: "Change reverse DNS of a Floating IP", diff --git a/internal/cmds/floatingip_unassign.go b/internal/cmd/floatingip/unassign.go similarity index 93% rename from internal/cmds/floatingip_unassign.go rename to internal/cmd/floatingip/unassign.go index b583e59b..213d0510 100644 --- a/internal/cmds/floatingip_unassign.go +++ b/internal/cmd/floatingip/unassign.go @@ -1,4 +1,4 @@ -package cmds +package floatingip import ( "fmt" @@ -8,7 +8,7 @@ import ( "github.com/spf13/cobra" ) -func newFloatingIPUnassignCommand(cli *state.State) *cobra.Command { +func newUnassignCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "unassign [FLAGS] FLOATINGIP", Short: "Unassign a Floating IP", diff --git a/internal/cmds/floatingip_update.go b/internal/cmd/floatingip/update.go similarity index 94% rename from internal/cmds/floatingip_update.go rename to internal/cmd/floatingip/update.go index c62d782c..eaf71be8 100644 --- a/internal/cmds/floatingip_update.go +++ b/internal/cmd/floatingip/update.go @@ -1,4 +1,4 @@ -package cmds +package floatingip import ( "fmt" @@ -9,7 +9,7 @@ import ( "github.com/spf13/cobra" ) -func newFloatingIPUpdateCommand(cli *state.State) *cobra.Command { +func newUpdateCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "update [FLAGS] FLOATINGIP", Short: "Update a Floating IP", diff --git a/internal/cmds/image_add_label.go b/internal/cmd/image/add_label.go similarity index 95% rename from internal/cmds/image_add_label.go rename to internal/cmd/image/add_label.go index 11c18048..6e8bef54 100644 --- a/internal/cmds/image_add_label.go +++ b/internal/cmd/image/add_label.go @@ -1,4 +1,4 @@ -package cmds +package image import ( "fmt" @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -func newImageAddLabelCommand(cli *state.State) *cobra.Command { +func newAddLabelCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "add-label [FLAGS] IMAGE LABEL", Short: "Add a label to an image", diff --git a/internal/cmds/image_delete.go b/internal/cmd/image/delete.go similarity index 92% rename from internal/cmds/image_delete.go rename to internal/cmd/image/delete.go index 72a81e81..d5f0ae5e 100644 --- a/internal/cmds/image_delete.go +++ b/internal/cmd/image/delete.go @@ -1,4 +1,4 @@ -package cmds +package image import ( "fmt" @@ -8,7 +8,7 @@ import ( "github.com/spf13/cobra" ) -func newImageDeleteCommand(cli *state.State) *cobra.Command { +func newDeleteCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "delete [FLAGS] IMAGE", Short: "Delete an image", diff --git a/internal/cmds/image_describe.go b/internal/cmd/image/describe.go similarity index 97% rename from internal/cmds/image_describe.go rename to internal/cmd/image/describe.go index 649c32ac..bd6f0461 100644 --- a/internal/cmds/image_describe.go +++ b/internal/cmd/image/describe.go @@ -1,4 +1,4 @@ -package cmds +package image import ( "encoding/json" @@ -13,7 +13,7 @@ import ( "github.com/spf13/cobra" ) -func newImageDescribeCommand(cli *state.State) *cobra.Command { +func newDescribeCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "describe [FLAGS] IMAGE", Short: "Describe an image", diff --git a/internal/cmds/image_disable_protection.go b/internal/cmd/image/disable_protection.go similarity index 94% rename from internal/cmds/image_disable_protection.go rename to internal/cmd/image/disable_protection.go index cd7750b1..c7000757 100644 --- a/internal/cmds/image_disable_protection.go +++ b/internal/cmd/image/disable_protection.go @@ -1,4 +1,4 @@ -package cmds +package image import ( "errors" @@ -12,7 +12,7 @@ import ( "github.com/spf13/cobra" ) -func newImageDisableProtectionCommand(cli *state.State) *cobra.Command { +func newDisableProtectionCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "disable-protection [FLAGS] IMAGE PROTECTIONLEVEL [PROTECTIONLEVEL...]", Short: "Disable resource protection for an image", diff --git a/internal/cmds/image_enable_protection.go b/internal/cmd/image/enable_protection.go similarity index 94% rename from internal/cmds/image_enable_protection.go rename to internal/cmd/image/enable_protection.go index 324b102b..23601e8d 100644 --- a/internal/cmds/image_enable_protection.go +++ b/internal/cmd/image/enable_protection.go @@ -1,4 +1,4 @@ -package cmds +package image import ( "errors" @@ -12,7 +12,7 @@ import ( "github.com/spf13/cobra" ) -func newImageEnableProtectionCommand(cli *state.State) *cobra.Command { +func newEnableProtectionCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "enable-protection [FLAGS] IMAGE PROTECTIONLEVEL [PROTECTIONLEVEL...]", Short: "Enable resource protection for an image", diff --git a/internal/cmd/image/image.go b/internal/cmd/image/image.go new file mode 100644 index 00000000..9dd8b6fe --- /dev/null +++ b/internal/cmd/image/image.go @@ -0,0 +1,27 @@ +package image + +import ( + "github.com/hetznercloud/cli/internal/state" + "github.com/spf13/cobra" +) + +func NewCommand(cli *state.State) *cobra.Command { + cmd := &cobra.Command{ + Use: "image", + Short: "Manage images", + Args: cobra.NoArgs, + TraverseChildren: true, + DisableFlagsInUseLine: true, + } + cmd.AddCommand( + newListCommand(cli), + newDeleteCommand(cli), + newDescribeCommand(cli), + newUpdateCommand(cli), + newEnableProtectionCommand(cli), + newDisableProtectionCommand(cli), + newAddLabelCommand(cli), + newRemoveLabelCommand(cli), + ) + return cmd +} diff --git a/internal/cmds/image_list.go b/internal/cmd/image/list.go similarity index 98% rename from internal/cmds/image_list.go rename to internal/cmd/image/list.go index 2303c826..be611b3d 100644 --- a/internal/cmds/image_list.go +++ b/internal/cmd/image/list.go @@ -1,4 +1,4 @@ -package cmds +package image import ( "fmt" @@ -21,7 +21,7 @@ func init() { imageListTableOutput = describeImageListTableOutput(nil) } -func newImageListCommand(cli *state.State) *cobra.Command { +func newListCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "list [FLAGS]", Short: "List images", diff --git a/internal/cmds/image_remove_label.go b/internal/cmd/image/remove_label.go similarity index 96% rename from internal/cmds/image_remove_label.go rename to internal/cmd/image/remove_label.go index f647c515..e2c33d2c 100644 --- a/internal/cmds/image_remove_label.go +++ b/internal/cmd/image/remove_label.go @@ -1,4 +1,4 @@ -package cmds +package image import ( "errors" @@ -11,7 +11,7 @@ import ( "github.com/spf13/cobra" ) -func newImageRemoveLabelCommand(cli *state.State) *cobra.Command { +func newRemoveLabelCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "remove-label [FLAGS] IMAGE LABELKEY", Short: "Remove a label from an image", diff --git a/internal/cmds/image_update.go b/internal/cmd/image/update.go similarity index 95% rename from internal/cmds/image_update.go rename to internal/cmd/image/update.go index b1df8488..419bf1ca 100644 --- a/internal/cmds/image_update.go +++ b/internal/cmd/image/update.go @@ -1,4 +1,4 @@ -package cmds +package image import ( "fmt" @@ -9,7 +9,7 @@ import ( "github.com/spf13/cobra" ) -func newImageUpdateCommand(cli *state.State) *cobra.Command { +func newUpdateCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "update [FLAGS] IMAGE", Short: "Update an image", diff --git a/internal/cmds/iso_describe.go b/internal/cmd/iso/describe.go similarity index 96% rename from internal/cmds/iso_describe.go rename to internal/cmd/iso/describe.go index 7ee766d9..551973bb 100644 --- a/internal/cmds/iso_describe.go +++ b/internal/cmd/iso/describe.go @@ -1,4 +1,4 @@ -package cmds +package iso import ( "encoding/json" @@ -12,7 +12,7 @@ import ( "github.com/spf13/cobra" ) -func newISODescribeCommand(cli *state.State) *cobra.Command { +func newDescribeCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "describe [FLAGS] ISO", Short: "Describe an ISO", diff --git a/internal/cmds/iso.go b/internal/cmd/iso/iso.go similarity index 72% rename from internal/cmds/iso.go rename to internal/cmd/iso/iso.go index 3a4d700d..de75f45d 100644 --- a/internal/cmds/iso.go +++ b/internal/cmd/iso/iso.go @@ -1,11 +1,11 @@ -package cmds +package iso import ( "github.com/hetznercloud/cli/internal/state" "github.com/spf13/cobra" ) -func NewISOCommand(cli *state.State) *cobra.Command { +func NewCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "iso", Short: "Manage ISOs", @@ -14,8 +14,8 @@ func NewISOCommand(cli *state.State) *cobra.Command { DisableFlagsInUseLine: true, } cmd.AddCommand( - newISOListCommand(cli), - newISODescribeCommand(cli), + newListCommand(cli), + newDescribeCommand(cli), ) return cmd } diff --git a/internal/cmds/iso_list.go b/internal/cmd/iso/list.go similarity index 95% rename from internal/cmds/iso_list.go rename to internal/cmd/iso/list.go index 3cc3db84..ef3bd1d3 100644 --- a/internal/cmds/iso_list.go +++ b/internal/cmd/iso/list.go @@ -1,4 +1,4 @@ -package cmds +package iso import ( "github.com/hetznercloud/cli/internal/cmd/output" @@ -16,7 +16,7 @@ func init() { AddAllowedFields(hcloud.ISO{}) } -func newISOListCommand(cli *state.State) *cobra.Command { +func newListCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "list [FLAGS]", Short: "List ISOs", diff --git a/internal/cmds/load_balancer_add_label.go b/internal/cmd/loadbalancer/add_label.go similarity index 95% rename from internal/cmds/load_balancer_add_label.go rename to internal/cmd/loadbalancer/add_label.go index 528c402a..e46e5c98 100644 --- a/internal/cmds/load_balancer_add_label.go +++ b/internal/cmd/loadbalancer/add_label.go @@ -1,4 +1,4 @@ -package cmds +package loadbalancer import ( "fmt" @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -func newLoadBalancerAddLabelCommand(cli *state.State) *cobra.Command { +func newAddLabelCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "add-label [FLAGS] LOADBALANCER LABEL", Short: "Add a label to a Load Balancer", diff --git a/internal/cmds/load_balancer_add_service.go b/internal/cmd/loadbalancer/add_service.go similarity index 98% rename from internal/cmds/load_balancer_add_service.go rename to internal/cmd/loadbalancer/add_service.go index f8d11b98..e49f472f 100644 --- a/internal/cmds/load_balancer_add_service.go +++ b/internal/cmd/loadbalancer/add_service.go @@ -1,4 +1,4 @@ -package cmds +package loadbalancer import ( "fmt" @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -func newLoadBalancerAddServiceCommand(cli *state.State) *cobra.Command { +func newAddServiceCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "add-service LOADBALANCER FLAGS", Short: "Add a service from a Load Balancer", diff --git a/internal/cmds/load_balancer_add_target.go b/internal/cmd/loadbalancer/add_target.go similarity index 97% rename from internal/cmds/load_balancer_add_target.go rename to internal/cmd/loadbalancer/add_target.go index 209d36c2..2507d253 100644 --- a/internal/cmds/load_balancer_add_target.go +++ b/internal/cmd/loadbalancer/add_target.go @@ -1,4 +1,4 @@ -package cmds +package loadbalancer import ( "fmt" @@ -11,7 +11,7 @@ import ( "github.com/spf13/cobra" ) -func newLoadBalancerAddTargetCommand(cli *state.State) *cobra.Command { +func newAddTargetCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "add-target LOADBALANCER FLAGS", Short: "Add a target to a Load Balancer", diff --git a/internal/cmds/load_balancer_attach_to_network.go b/internal/cmd/loadbalancer/attach_to_network.go similarity index 95% rename from internal/cmds/load_balancer_attach_to_network.go rename to internal/cmd/loadbalancer/attach_to_network.go index 47ff2746..26faa267 100644 --- a/internal/cmds/load_balancer_attach_to_network.go +++ b/internal/cmd/loadbalancer/attach_to_network.go @@ -1,4 +1,4 @@ -package cmds +package loadbalancer import ( "fmt" @@ -9,7 +9,7 @@ import ( "github.com/spf13/cobra" ) -func newLoadBalancerAttachToNetworkCommand(cli *state.State) *cobra.Command { +func newAttachToNetworkCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "attach-to-network [FLAGS] LOADBALANCER", Short: "Attach a Load Balancer to a Network", diff --git a/internal/cmds/load_balancer_change_algorithm.go b/internal/cmd/loadbalancer/change_algorithm.go similarity index 95% rename from internal/cmds/load_balancer_change_algorithm.go rename to internal/cmd/loadbalancer/change_algorithm.go index b8a7f14c..fd0f37cf 100644 --- a/internal/cmds/load_balancer_change_algorithm.go +++ b/internal/cmd/loadbalancer/change_algorithm.go @@ -1,4 +1,4 @@ -package cmds +package loadbalancer import ( "fmt" @@ -9,7 +9,7 @@ import ( "github.com/spf13/cobra" ) -func newLoadBalancerChangeAlgorithmCommand(cli *state.State) *cobra.Command { +func newChangeAlgorithmCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "change-algorithm LOADBALANCER FLAGS", Short: "Changes the algorithm of a Load Balancer", diff --git a/internal/cmds/load_balancer_change_type.go b/internal/cmd/loadbalancer/change_type.go similarity index 95% rename from internal/cmds/load_balancer_change_type.go rename to internal/cmd/loadbalancer/change_type.go index cd5fffa7..dc4201ef 100644 --- a/internal/cmds/load_balancer_change_type.go +++ b/internal/cmd/loadbalancer/change_type.go @@ -1,4 +1,4 @@ -package cmds +package loadbalancer import ( "fmt" @@ -9,7 +9,7 @@ import ( "github.com/spf13/cobra" ) -func newLoadBalancerChangeTypeCommand(cli *state.State) *cobra.Command { +func newChangeTypeCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "change-type [FLAGS] LOADBALANCER LOADBALANCERTYPE", Short: "Change type of a Load Balancer", diff --git a/internal/cmds/load_balancer_create.go b/internal/cmd/loadbalancer/create.go similarity index 97% rename from internal/cmds/load_balancer_create.go rename to internal/cmd/loadbalancer/create.go index 94231855..4a499864 100644 --- a/internal/cmds/load_balancer_create.go +++ b/internal/cmd/loadbalancer/create.go @@ -1,4 +1,4 @@ -package cmds +package loadbalancer import ( "fmt" @@ -9,7 +9,7 @@ import ( "github.com/spf13/cobra" ) -func newLoadBalancerCreateCommand(cli *state.State) *cobra.Command { +func newCreateCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "create [FLAGS]", Short: "Create a Load Balancer", diff --git a/internal/cmds/load_balancer_delete.go b/internal/cmd/loadbalancer/delete.go similarity index 92% rename from internal/cmds/load_balancer_delete.go rename to internal/cmd/loadbalancer/delete.go index ab8f7a27..dadb6868 100644 --- a/internal/cmds/load_balancer_delete.go +++ b/internal/cmd/loadbalancer/delete.go @@ -1,4 +1,4 @@ -package cmds +package loadbalancer import ( "fmt" @@ -8,7 +8,7 @@ import ( "github.com/spf13/cobra" ) -func newLoadBalancerDeleteCommand(cli *state.State) *cobra.Command { +func newDeleteCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "delete [FLAGS] LOADBALANCER", Short: "Delete a Load Balancer", diff --git a/internal/cmds/load_balancer_delete_service.go b/internal/cmd/loadbalancer/delete_service.go similarity index 94% rename from internal/cmds/load_balancer_delete_service.go rename to internal/cmd/loadbalancer/delete_service.go index 1ce2adac..62bc1ff0 100644 --- a/internal/cmds/load_balancer_delete_service.go +++ b/internal/cmd/loadbalancer/delete_service.go @@ -1,4 +1,4 @@ -package cmds +package loadbalancer import ( "fmt" @@ -9,7 +9,7 @@ import ( "github.com/spf13/cobra" ) -func newLoadBalancerDeleteServiceCommand(cli *state.State) *cobra.Command { +func newDeleteServiceCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "delete-service [FLAGS] LOADBALANCER", Short: "Deletes a service from a Load Balancer", diff --git a/internal/cmds/load_balancer_describe.go b/internal/cmd/loadbalancer/describe.go similarity index 98% rename from internal/cmds/load_balancer_describe.go rename to internal/cmd/loadbalancer/describe.go index 016dc3c9..3841bea6 100644 --- a/internal/cmds/load_balancer_describe.go +++ b/internal/cmd/loadbalancer/describe.go @@ -1,4 +1,4 @@ -package cmds +package loadbalancer import ( "encoding/json" @@ -13,7 +13,7 @@ import ( "github.com/spf13/cobra" ) -func newLoadBalancerDescribeCommand(cli *state.State) *cobra.Command { +func newDescribeCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "describe [FLAGS] LOADBALANCER", Short: "Describe a Load Balancer", diff --git a/internal/cmds/load_balancer_detach_from_network.go b/internal/cmd/loadbalancer/detach_from_network.go similarity index 95% rename from internal/cmds/load_balancer_detach_from_network.go rename to internal/cmd/loadbalancer/detach_from_network.go index 5163b534..36c43f09 100644 --- a/internal/cmds/load_balancer_detach_from_network.go +++ b/internal/cmd/loadbalancer/detach_from_network.go @@ -1,4 +1,4 @@ -package cmds +package loadbalancer import ( "fmt" @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -func newLoadBalancerDetachFromNetworkCommand(cli *state.State) *cobra.Command { +func newDetachFromNetworkCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "detach-from-network [FLAGS] LOADBALANCER", Short: "Detach a Load Balancer from a Network", diff --git a/internal/cmds/load_balancer_disable_protection.go b/internal/cmd/loadbalancer/disable_protection.go similarity index 94% rename from internal/cmds/load_balancer_disable_protection.go rename to internal/cmd/loadbalancer/disable_protection.go index b78e0aee..9fedc94c 100644 --- a/internal/cmds/load_balancer_disable_protection.go +++ b/internal/cmd/loadbalancer/disable_protection.go @@ -1,4 +1,4 @@ -package cmds +package loadbalancer import ( "fmt" @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -func newLoadBalancerDisableProtectionCommand(cli *state.State) *cobra.Command { +func newDisableProtectionCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "disable-protection [FLAGS] LOADBALANCER PROTECTIONLEVEL [PROTECTIONLEVEL...]", Short: "Disable resource protection for a Load Balancer", diff --git a/internal/cmds/load_balancer_disable_public_interface.go b/internal/cmd/loadbalancer/disable_public_interface.go similarity index 93% rename from internal/cmds/load_balancer_disable_public_interface.go rename to internal/cmd/loadbalancer/disable_public_interface.go index 15421c87..538d32f8 100644 --- a/internal/cmds/load_balancer_disable_public_interface.go +++ b/internal/cmd/loadbalancer/disable_public_interface.go @@ -1,4 +1,4 @@ -package cmds +package loadbalancer import ( "fmt" @@ -8,7 +8,7 @@ import ( "github.com/spf13/cobra" ) -func newLoadBalancerDisablePublicInterface(cli *state.State) *cobra.Command { +func newDisablePublicInterfaceCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "disable-public-interface [FLAGS] LOADBALANCER", Short: "Disable the public interface of a Load Balancer", diff --git a/internal/cmds/load_balancer_enable_protection.go b/internal/cmd/loadbalancer/enable_protection.go similarity index 94% rename from internal/cmds/load_balancer_enable_protection.go rename to internal/cmd/loadbalancer/enable_protection.go index 0afa5752..a7042b8a 100644 --- a/internal/cmds/load_balancer_enable_protection.go +++ b/internal/cmd/loadbalancer/enable_protection.go @@ -1,4 +1,4 @@ -package cmds +package loadbalancer import ( "fmt" @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -func newLoadBalancerEnableProtectionCommand(cli *state.State) *cobra.Command { +func newEnableProtectionCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "enable-protection [FLAGS] LOADBALANCER PROTECTIONLEVEL [PROTECTIONLEVEL...]", Short: "Enable resource protection for a Load Balancer", diff --git a/internal/cmds/load_balancer_enable_public_interface.go b/internal/cmd/loadbalancer/enable_public_interface.go similarity index 93% rename from internal/cmds/load_balancer_enable_public_interface.go rename to internal/cmd/loadbalancer/enable_public_interface.go index 77ebf241..e0fd187b 100644 --- a/internal/cmds/load_balancer_enable_public_interface.go +++ b/internal/cmd/loadbalancer/enable_public_interface.go @@ -1,4 +1,4 @@ -package cmds +package loadbalancer import ( "fmt" @@ -8,7 +8,7 @@ import ( "github.com/spf13/cobra" ) -func newLoadBalancerEnablePublicInterface(cli *state.State) *cobra.Command { +func newEnablePublicInterfaceCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "enable-public-interface [FLAGS] LOADBALANCER", Short: "Enable the public interface of a Load Balancer", diff --git a/internal/cmds/load_balancer_list.go b/internal/cmd/loadbalancer/list.go similarity index 98% rename from internal/cmds/load_balancer_list.go rename to internal/cmd/loadbalancer/list.go index 70d375a1..e76d7ab9 100644 --- a/internal/cmds/load_balancer_list.go +++ b/internal/cmd/loadbalancer/list.go @@ -1,4 +1,4 @@ -package cmds +package loadbalancer import ( "strings" @@ -18,7 +18,7 @@ func init() { loadBalancerListTableOutput = describeLoadBalancerListTableOutput(nil) } -func newLoadBalancerListCommand(cli *state.State) *cobra.Command { +func newListCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "list [FLAGS]", Short: "List Load Balancers", diff --git a/internal/cmd/loadbalancer/load_balancer.go b/internal/cmd/loadbalancer/load_balancer.go new file mode 100644 index 00000000..ee2de05b --- /dev/null +++ b/internal/cmd/loadbalancer/load_balancer.go @@ -0,0 +1,40 @@ +package loadbalancer + +import ( + "github.com/hetznercloud/cli/internal/state" + "github.com/spf13/cobra" +) + +func NewCommand(cli *state.State) *cobra.Command { + cmd := &cobra.Command{ + Use: "load-balancer", + Short: "Manage Load Balancers", + Args: cobra.NoArgs, + TraverseChildren: true, + DisableFlagsInUseLine: true, + } + cmd.AddCommand( + newCreateCommand(cli), + newListCommand(cli), + newDescribeCommand(cli), + newDeleteCommand(cli), + newUpdateCommand(cli), + newAddLabelCommand(cli), + newRemoveLabelCommand(cli), + newAddTargetCommand(cli), + newRemoveTargetCommand(cli), + newChangeAlgorithmCommand(cli), + newUpdateServiceCommand(cli), + newDeleteServiceCommand(cli), + newAddServiceCommand(cli), + newEnableProtectionCommand(cli), + newDisableProtectionCommand(cli), + newAttachToNetworkCommand(cli), + newDetachFromNetworkCommand(cli), + newEnablePublicInterfaceCommand(cli), + newDisablePublicInterfaceCommand(cli), + newChangeTypeCommand(cli), + newMetricsCommand(cli), + ) + return cmd +} diff --git a/internal/cmds/load_balancer_metrics.go b/internal/cmd/loadbalancer/metrics.go similarity index 97% rename from internal/cmds/load_balancer_metrics.go rename to internal/cmd/loadbalancer/metrics.go index f23d046b..42e891e7 100644 --- a/internal/cmds/load_balancer_metrics.go +++ b/internal/cmd/loadbalancer/metrics.go @@ -1,4 +1,4 @@ -package cmds +package loadbalancer import ( "encoding/json" @@ -17,7 +17,7 @@ import ( "github.com/spf13/cobra" ) -func newLoadBalancerMetricsCommand(cli *state.State) *cobra.Command { +func newMetricsCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "metrics [FLAGS] LOADBALANCER", Short: "[ALPHA] Metrics from a Load Balancer", diff --git a/internal/cmds/load_balancer_remove_label.go b/internal/cmd/loadbalancer/remove_label.go similarity index 96% rename from internal/cmds/load_balancer_remove_label.go rename to internal/cmd/loadbalancer/remove_label.go index e1e30dd5..64c8ed64 100644 --- a/internal/cmds/load_balancer_remove_label.go +++ b/internal/cmd/loadbalancer/remove_label.go @@ -1,4 +1,4 @@ -package cmds +package loadbalancer import ( "errors" @@ -11,7 +11,7 @@ import ( "github.com/spf13/cobra" ) -func newLoadBalancerRemoveLabelCommand(cli *state.State) *cobra.Command { +func newRemoveLabelCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "remove-label [FLAGS] LOADBALANCER LABELKEY", Short: "Remove a label from a Load Balancer", diff --git a/internal/cmds/load_balancer_remove_target.go b/internal/cmd/loadbalancer/remove_target.go similarity index 96% rename from internal/cmds/load_balancer_remove_target.go rename to internal/cmd/loadbalancer/remove_target.go index 301672c3..2ba3c630 100644 --- a/internal/cmds/load_balancer_remove_target.go +++ b/internal/cmd/loadbalancer/remove_target.go @@ -1,4 +1,4 @@ -package cmds +package loadbalancer import ( "fmt" @@ -11,7 +11,7 @@ import ( "github.com/spf13/cobra" ) -func newLoadBalancerRemoveTargetCommand(cli *state.State) *cobra.Command { +func newRemoveTargetCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "remove-target LOADBALANCER FLAGS", Short: "Remove a target to a Load Balancer", diff --git a/internal/cmds/load_balancer_update.go b/internal/cmd/loadbalancer/update.go similarity index 94% rename from internal/cmds/load_balancer_update.go rename to internal/cmd/loadbalancer/update.go index 09bdeabc..895b1462 100644 --- a/internal/cmds/load_balancer_update.go +++ b/internal/cmd/loadbalancer/update.go @@ -1,4 +1,4 @@ -package cmds +package loadbalancer import ( "errors" @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -func newLoadBalancerUpdateCommand(cli *state.State) *cobra.Command { +func newUpdateCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "update [FLAGS] LOADBALANCER", Short: "Update a Load Balancer", diff --git a/internal/cmds/load_balancer_update_service.go b/internal/cmd/loadbalancer/update_service.go similarity index 98% rename from internal/cmds/load_balancer_update_service.go rename to internal/cmd/loadbalancer/update_service.go index 8ead7be6..22a771b2 100644 --- a/internal/cmds/load_balancer_update_service.go +++ b/internal/cmd/loadbalancer/update_service.go @@ -1,4 +1,4 @@ -package cmds +package loadbalancer import ( "fmt" @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -func newLoadBalancerUpdateServiceCommand(cli *state.State) *cobra.Command { +func newUpdateServiceCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "update-service LOADBALANCER FLAGS", Short: "Updates a service from a Load Balancer", diff --git a/internal/cmds/load_balancer_type_describe.go b/internal/cmd/loadbalancertype/describe.go similarity index 96% rename from internal/cmds/load_balancer_type_describe.go rename to internal/cmd/loadbalancertype/describe.go index 727d73e3..1bd4dcbd 100644 --- a/internal/cmds/load_balancer_type_describe.go +++ b/internal/cmd/loadbalancertype/describe.go @@ -1,4 +1,4 @@ -package cmds +package loadbalancertype import ( "encoding/json" @@ -12,7 +12,7 @@ import ( "github.com/spf13/cobra" ) -func newLoadBalancerTypenDescribeCommand(cli *state.State) *cobra.Command { +func newnDescribeCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "describe [FLAGS] LOADBALANCERTYPE", Short: "Describe a Load Balancer type", diff --git a/internal/cmds/load_balancer_type_list.go b/internal/cmd/loadbalancertype/list.go similarity index 95% rename from internal/cmds/load_balancer_type_list.go rename to internal/cmd/loadbalancertype/list.go index e0cc3cba..4545d114 100644 --- a/internal/cmds/load_balancer_type_list.go +++ b/internal/cmd/loadbalancertype/list.go @@ -1,4 +1,4 @@ -package cmds +package loadbalancertype import ( "github.com/hetznercloud/cli/internal/cmd/output" @@ -16,7 +16,7 @@ func init() { AddAllowedFields(hcloud.LoadBalancerType{}) } -func newLoadBalancerTypeListCommand(cli *state.State) *cobra.Command { +func newListCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "list [FLAGS]", Short: "List Load Balancer types", diff --git a/internal/cmds/load_balancer_type.go b/internal/cmd/loadbalancertype/load_balancer_type.go similarity index 68% rename from internal/cmds/load_balancer_type.go rename to internal/cmd/loadbalancertype/load_balancer_type.go index 2f15ed0f..1985e203 100644 --- a/internal/cmds/load_balancer_type.go +++ b/internal/cmd/loadbalancertype/load_balancer_type.go @@ -1,11 +1,11 @@ -package cmds +package loadbalancertype import ( "github.com/hetznercloud/cli/internal/state" "github.com/spf13/cobra" ) -func NewLoadBalancerTypeCommand(cli *state.State) *cobra.Command { +func NewCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "load-balancer-type", Short: "Manage Load Balancer types", @@ -14,8 +14,8 @@ func NewLoadBalancerTypeCommand(cli *state.State) *cobra.Command { DisableFlagsInUseLine: true, } cmd.AddCommand( - newLoadBalancerTypenDescribeCommand(cli), - newLoadBalancerTypeListCommand(cli), + newnDescribeCommand(cli), + newListCommand(cli), ) return cmd } diff --git a/internal/cmds/location_describe.go b/internal/cmd/location/describe.go similarity index 96% rename from internal/cmds/location_describe.go rename to internal/cmd/location/describe.go index e0ee53a4..4c378693 100644 --- a/internal/cmds/location_describe.go +++ b/internal/cmd/location/describe.go @@ -1,4 +1,4 @@ -package cmds +package location import ( "encoding/json" @@ -12,7 +12,7 @@ import ( "github.com/spf13/cobra" ) -func newLocationDescribeCommand(cli *state.State) *cobra.Command { +func newDescribeCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "describe [FLAGS] LOCATION", Short: "Describe a location", diff --git a/internal/cmds/location_list.go b/internal/cmd/location/list.go similarity index 95% rename from internal/cmds/location_list.go rename to internal/cmd/location/list.go index b86c447b..697f5bd1 100644 --- a/internal/cmds/location_list.go +++ b/internal/cmd/location/list.go @@ -1,4 +1,4 @@ -package cmds +package location import ( "github.com/hetznercloud/cli/internal/cmd/output" @@ -16,7 +16,7 @@ func init() { AddAllowedFields(hcloud.Location{}) } -func newLocationListCommand(cli *state.State) *cobra.Command { +func newListCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "list [FLAGS]", Short: "List locations", diff --git a/internal/cmds/location.go b/internal/cmd/location/location.go similarity index 70% rename from internal/cmds/location.go rename to internal/cmd/location/location.go index b9b1bec1..72d8ee86 100644 --- a/internal/cmds/location.go +++ b/internal/cmd/location/location.go @@ -1,11 +1,11 @@ -package cmds +package location import ( "github.com/hetznercloud/cli/internal/state" "github.com/spf13/cobra" ) -func NewLocationCommand(cli *state.State) *cobra.Command { +func NewCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "location", Short: "Manage locations", @@ -14,8 +14,8 @@ func NewLocationCommand(cli *state.State) *cobra.Command { DisableFlagsInUseLine: true, } cmd.AddCommand( - newLocationListCommand(cli), - newLocationDescribeCommand(cli), + newListCommand(cli), + newDescribeCommand(cli), ) return cmd } diff --git a/internal/cmds/network_add_label.go b/internal/cmd/network/add_label.go similarity index 95% rename from internal/cmds/network_add_label.go rename to internal/cmd/network/add_label.go index 5158b90b..96ef53d3 100644 --- a/internal/cmds/network_add_label.go +++ b/internal/cmd/network/add_label.go @@ -1,4 +1,4 @@ -package cmds +package network import ( "fmt" @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -func newNetworkAddLabelCommand(cli *state.State) *cobra.Command { +func newAddLabelCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "add-label [FLAGS] NETWORK LABEL", Short: "Add a label to a network", diff --git a/internal/cmds/network_add_route.go b/internal/cmd/network/add_route.go similarity index 95% rename from internal/cmds/network_add_route.go rename to internal/cmd/network/add_route.go index bb20b02e..0ccdf5f5 100644 --- a/internal/cmds/network_add_route.go +++ b/internal/cmd/network/add_route.go @@ -1,4 +1,4 @@ -package cmds +package network import ( "fmt" @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -func newNetworkAddRouteCommand(cli *state.State) *cobra.Command { +func newAddRouteCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "add-route NETWORK FLAGS", Short: "Add a route to a network", diff --git a/internal/cmds/network_add_subnet.go b/internal/cmd/network/add_subnet.go similarity index 96% rename from internal/cmds/network_add_subnet.go rename to internal/cmd/network/add_subnet.go index ca8e9e8d..b342510b 100644 --- a/internal/cmds/network_add_subnet.go +++ b/internal/cmd/network/add_subnet.go @@ -1,4 +1,4 @@ -package cmds +package network import ( "fmt" @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -func newNetworkAddSubnetCommand(cli *state.State) *cobra.Command { +func newAddSubnetCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "add-subnet NETWORK FLAGS", Short: "Add a subnet to a network", diff --git a/internal/cmds/network_change_ip_range.go b/internal/cmd/network/change_ip_range.go similarity index 94% rename from internal/cmds/network_change_ip_range.go rename to internal/cmd/network/change_ip_range.go index a1d9bc18..4e5fe925 100644 --- a/internal/cmds/network_change_ip_range.go +++ b/internal/cmd/network/change_ip_range.go @@ -1,4 +1,4 @@ -package cmds +package network import ( "fmt" @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -func newNetworkChangeIPRangeCommand(cli *state.State) *cobra.Command { +func newChangeIPRangeCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "change-ip-range [FLAGS] NETWORK", Short: "Change the IP range of a network", diff --git a/internal/cmds/network_create.go b/internal/cmd/network/create.go similarity index 94% rename from internal/cmds/network_create.go rename to internal/cmd/network/create.go index f73d219c..05bf1029 100644 --- a/internal/cmds/network_create.go +++ b/internal/cmd/network/create.go @@ -1,4 +1,4 @@ -package cmds +package network import ( "fmt" @@ -9,7 +9,7 @@ import ( "github.com/spf13/cobra" ) -func newNetworkCreateCommand(cli *state.State) *cobra.Command { +func newCreateCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "create [FLAGS]", Short: "Create a network", diff --git a/internal/cmds/network_delete.go b/internal/cmd/network/delete.go similarity index 92% rename from internal/cmds/network_delete.go rename to internal/cmd/network/delete.go index 20b56b7f..7c853ef2 100644 --- a/internal/cmds/network_delete.go +++ b/internal/cmd/network/delete.go @@ -1,4 +1,4 @@ -package cmds +package network import ( "fmt" @@ -8,7 +8,7 @@ import ( "github.com/spf13/cobra" ) -func newNetworkDeleteCommand(cli *state.State) *cobra.Command { +func newDeleteCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "delete [FLAGS] NETWORK", Short: "Delete a network", diff --git a/internal/cmds/network_describe.go b/internal/cmd/network/describe.go similarity index 97% rename from internal/cmds/network_describe.go rename to internal/cmd/network/describe.go index 98d07286..cfe4f461 100644 --- a/internal/cmds/network_describe.go +++ b/internal/cmd/network/describe.go @@ -1,4 +1,4 @@ -package cmds +package network import ( "encoding/json" @@ -13,7 +13,7 @@ import ( "github.com/spf13/cobra" ) -func newNetworkDescribeCommand(cli *state.State) *cobra.Command { +func newDescribeCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "describe [FLAGS] NETWORK", Short: "Describe a network", diff --git a/internal/cmds/network_disable_protection.go b/internal/cmd/network/disable_protection.go similarity index 94% rename from internal/cmds/network_disable_protection.go rename to internal/cmd/network/disable_protection.go index fe899ca3..31116534 100644 --- a/internal/cmds/network_disable_protection.go +++ b/internal/cmd/network/disable_protection.go @@ -1,4 +1,4 @@ -package cmds +package network import ( "fmt" @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -func newNetworkDisableProtectionCommand(cli *state.State) *cobra.Command { +func newDisableProtectionCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "disable-protection [FLAGS] NETWORK PROTECTIONLEVEL [PROTECTIONLEVEL...]", Short: "Disable resource protection for a network", diff --git a/internal/cmds/network_enable_protection.go b/internal/cmd/network/enable_protection.go similarity index 94% rename from internal/cmds/network_enable_protection.go rename to internal/cmd/network/enable_protection.go index 9b98ab30..2fde3f02 100644 --- a/internal/cmds/network_enable_protection.go +++ b/internal/cmd/network/enable_protection.go @@ -1,4 +1,4 @@ -package cmds +package network import ( "fmt" @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -func newNetworkEnableProtectionCommand(cli *state.State) *cobra.Command { +func newEnableProtectionCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "enable-protection [FLAGS] NETWORK PROTECTIONLEVEL [PROTECTIONLEVEL...]", Short: "Enable resource protection for a network", diff --git a/internal/cmds/network_list.go b/internal/cmd/network/list.go similarity index 98% rename from internal/cmds/network_list.go rename to internal/cmd/network/list.go index e48b3e4c..50f3ab55 100644 --- a/internal/cmds/network_list.go +++ b/internal/cmd/network/list.go @@ -1,4 +1,4 @@ -package cmds +package network import ( "fmt" @@ -18,7 +18,7 @@ func init() { networkListTableOutput = describeNetworkListTableOutput(nil) } -func newNetworkListCommand(cli *state.State) *cobra.Command { +func newListCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "list [FLAGS]", Short: "List networks", diff --git a/internal/cmd/network/network.go b/internal/cmd/network/network.go new file mode 100644 index 00000000..a960d98e --- /dev/null +++ b/internal/cmd/network/network.go @@ -0,0 +1,33 @@ +package network + +import ( + "github.com/hetznercloud/cli/internal/state" + "github.com/spf13/cobra" +) + +func NewCommand(cli *state.State) *cobra.Command { + cmd := &cobra.Command{ + Use: "network", + Short: "Manage networks", + Args: cobra.NoArgs, + TraverseChildren: true, + DisableFlagsInUseLine: true, + } + cmd.AddCommand( + newListCommand(cli), + newDescribeCommand(cli), + newCreateCommand(cli), + newUpdateCommand(cli), + newDeleteCommand(cli), + newChangeIPRangeCommand(cli), + newAddRouteCommand(cli), + newRemoveRouteCommand(cli), + newAddSubnetCommand(cli), + newRemoveSubnetCommand(cli), + newAddLabelCommand(cli), + newRemoveLabelCommand(cli), + newEnableProtectionCommand(cli), + newDisableProtectionCommand(cli), + ) + return cmd +} diff --git a/internal/cmds/network_remove_label.go b/internal/cmd/network/remove_label.go similarity index 96% rename from internal/cmds/network_remove_label.go rename to internal/cmd/network/remove_label.go index 8471eb48..22c94422 100644 --- a/internal/cmds/network_remove_label.go +++ b/internal/cmd/network/remove_label.go @@ -1,4 +1,4 @@ -package cmds +package network import ( "errors" @@ -11,7 +11,7 @@ import ( "github.com/spf13/cobra" ) -func newNetworkRemoveLabelCommand(cli *state.State) *cobra.Command { +func newRemoveLabelCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "remove-label [FLAGS] NETWORK LABELKEY", Short: "Remove a label from a network", diff --git a/internal/cmds/network_remove_route.go b/internal/cmd/network/remove_route.go similarity index 95% rename from internal/cmds/network_remove_route.go rename to internal/cmd/network/remove_route.go index 9afa8bb8..a7e0ce7d 100644 --- a/internal/cmds/network_remove_route.go +++ b/internal/cmd/network/remove_route.go @@ -1,4 +1,4 @@ -package cmds +package network import ( "fmt" @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -func newNetworkRemoveRouteCommand(cli *state.State) *cobra.Command { +func newRemoveRouteCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "remove-route NETWORK FLAGS", Short: "Remove a route from a network", diff --git a/internal/cmds/network_remove_subnet.go b/internal/cmd/network/remove_subnet.go similarity index 94% rename from internal/cmds/network_remove_subnet.go rename to internal/cmd/network/remove_subnet.go index 9050308e..fb21986f 100644 --- a/internal/cmds/network_remove_subnet.go +++ b/internal/cmd/network/remove_subnet.go @@ -1,4 +1,4 @@ -package cmds +package network import ( "fmt" @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -func newNetworkRemoveSubnetCommand(cli *state.State) *cobra.Command { +func newRemoveSubnetCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "remove-subnet NETWORK FLAGS", Short: "Remove a subnet from a network", diff --git a/internal/cmds/network_update.go b/internal/cmd/network/update.go similarity index 94% rename from internal/cmds/network_update.go rename to internal/cmd/network/update.go index 294bf7c2..13c94cf2 100644 --- a/internal/cmds/network_update.go +++ b/internal/cmd/network/update.go @@ -1,4 +1,4 @@ -package cmds +package network import ( "errors" @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -func newNetworkUpdateCommand(cli *state.State) *cobra.Command { +func newUpdateCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "update [FLAGS] NETWORK", Short: "Update a network", diff --git a/internal/cmds/server_add_label.go b/internal/cmd/server/add_label.go similarity index 95% rename from internal/cmds/server_add_label.go rename to internal/cmd/server/add_label.go index 4a89376c..c06d1b5f 100644 --- a/internal/cmds/server_add_label.go +++ b/internal/cmd/server/add_label.go @@ -1,4 +1,4 @@ -package cmds +package server import ( "fmt" @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -func newServerAddLabelCommand(cli *state.State) *cobra.Command { +func newAddLabelCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "add-label [FLAGS] SERVER LABEL", Short: "Add a label to a server", diff --git a/internal/cmds/server_attach_iso.go b/internal/cmd/server/attach_iso.go similarity index 94% rename from internal/cmds/server_attach_iso.go rename to internal/cmd/server/attach_iso.go index cd79d40e..6ab4f10d 100644 --- a/internal/cmds/server_attach_iso.go +++ b/internal/cmd/server/attach_iso.go @@ -1,4 +1,4 @@ -package cmds +package server import ( "fmt" @@ -8,7 +8,7 @@ import ( "github.com/spf13/cobra" ) -func newServerAttachISOCommand(cli *state.State) *cobra.Command { +func newAttachISOCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "attach-iso [FLAGS] SERVER ISO", Short: "Attach an ISO to a server", diff --git a/internal/cmds/server_attach_to_network.go b/internal/cmd/server/attach_to_network.go similarity index 96% rename from internal/cmds/server_attach_to_network.go rename to internal/cmd/server/attach_to_network.go index c845425e..c34648a9 100644 --- a/internal/cmds/server_attach_to_network.go +++ b/internal/cmd/server/attach_to_network.go @@ -1,4 +1,4 @@ -package cmds +package server import ( "fmt" @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -func newServerAttachToNetworkCommand(cli *state.State) *cobra.Command { +func newAttachToNetworkCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "attach-to-network [FLAGS] SERVER", Short: "Attach a server to a network", diff --git a/internal/cmds/server_change_alias_ips.go b/internal/cmd/server/change_alias_ips.go similarity index 96% rename from internal/cmds/server_change_alias_ips.go rename to internal/cmd/server/change_alias_ips.go index 99b2dc27..fd589bba 100644 --- a/internal/cmds/server_change_alias_ips.go +++ b/internal/cmd/server/change_alias_ips.go @@ -1,4 +1,4 @@ -package cmds +package server import ( "fmt" @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -func newServerChangeAliasIPsCommand(cli *state.State) *cobra.Command { +func newChangeAliasIPsCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "change-alias-ips [FLAGS] SERVER", Short: "Change a server's alias IPs in a network", diff --git a/internal/cmds/server_change_type.go b/internal/cmd/server/change_type.go similarity index 95% rename from internal/cmds/server_change_type.go rename to internal/cmd/server/change_type.go index 707c68cc..6aeb33df 100644 --- a/internal/cmds/server_change_type.go +++ b/internal/cmd/server/change_type.go @@ -1,4 +1,4 @@ -package cmds +package server import ( "fmt" @@ -9,7 +9,7 @@ import ( "github.com/spf13/cobra" ) -func newServerChangeTypeCommand(cli *state.State) *cobra.Command { +func newChangeTypeCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "change-type [FLAGS] SERVER SERVERTYPE", Short: "Change type of a server", diff --git a/internal/cmds/server_create.go b/internal/cmd/server/create.go similarity index 99% rename from internal/cmds/server_create.go rename to internal/cmd/server/create.go index 819e279d..396feab9 100644 --- a/internal/cmds/server_create.go +++ b/internal/cmd/server/create.go @@ -1,4 +1,4 @@ -package cmds +package server import ( "bytes" @@ -18,7 +18,7 @@ import ( "golang.org/x/crypto/ssh" ) -func newServerCreateCommand(cli *state.State) *cobra.Command { +func newCreateCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "create FLAGS", Short: "Create a server", diff --git a/internal/cmds/server_create_image.go b/internal/cmd/server/create_image.go similarity index 96% rename from internal/cmds/server_create_image.go rename to internal/cmd/server/create_image.go index e5dd71f7..a1140397 100644 --- a/internal/cmds/server_create_image.go +++ b/internal/cmd/server/create_image.go @@ -1,4 +1,4 @@ -package cmds +package server import ( "fmt" @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -func newServerCreateImageCommand(cli *state.State) *cobra.Command { +func newCreateImageCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "create-image [FLAGS] SERVER", Short: "Create an image from a server", diff --git a/internal/cmds/server_delete.go b/internal/cmd/server/delete.go similarity index 92% rename from internal/cmds/server_delete.go rename to internal/cmd/server/delete.go index 46c65fc4..a17cc236 100644 --- a/internal/cmds/server_delete.go +++ b/internal/cmd/server/delete.go @@ -1,4 +1,4 @@ -package cmds +package server import ( "fmt" @@ -8,7 +8,7 @@ import ( "github.com/spf13/cobra" ) -func newServerDeleteCommand(cli *state.State) *cobra.Command { +func newDeleteCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "delete SERVER", Short: "Delete a server", diff --git a/internal/cmds/server_describe.go b/internal/cmd/server/describe.go similarity index 98% rename from internal/cmds/server_describe.go rename to internal/cmd/server/describe.go index aacb889b..d609679c 100644 --- a/internal/cmds/server_describe.go +++ b/internal/cmd/server/describe.go @@ -1,4 +1,4 @@ -package cmds +package server import ( "encoding/json" @@ -13,7 +13,7 @@ import ( "github.com/spf13/cobra" ) -func newServerDescribeCommand(cli *state.State) *cobra.Command { +func newDescribeCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "describe [FLAGS] SERVER", Short: "Describe a server", diff --git a/internal/cmds/server_detach_from_network.go b/internal/cmd/server/detach_from_network.go similarity index 95% rename from internal/cmds/server_detach_from_network.go rename to internal/cmd/server/detach_from_network.go index fca60763..70fc3550 100644 --- a/internal/cmds/server_detach_from_network.go +++ b/internal/cmd/server/detach_from_network.go @@ -1,4 +1,4 @@ -package cmds +package server import ( "fmt" @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -func newServerDetachFromNetworkCommand(cli *state.State) *cobra.Command { +func newDetachFromNetworkCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "detach-from-network [FLAGS] SERVER", Short: "Detach a server from a network", diff --git a/internal/cmds/server_detach_iso.go b/internal/cmd/server/detach_iso.go similarity index 93% rename from internal/cmds/server_detach_iso.go rename to internal/cmd/server/detach_iso.go index 928910af..f3c5c3b1 100644 --- a/internal/cmds/server_detach_iso.go +++ b/internal/cmd/server/detach_iso.go @@ -1,4 +1,4 @@ -package cmds +package server import ( "fmt" @@ -8,7 +8,7 @@ import ( "github.com/spf13/cobra" ) -func newServerDetachISOCommand(cli *state.State) *cobra.Command { +func newDetachISOCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "detach-iso [FLAGS] SERVER", Short: "Detach an ISO from a server", diff --git a/internal/cmds/server_disable_backup.go b/internal/cmd/server/disable_backup.go similarity index 93% rename from internal/cmds/server_disable_backup.go rename to internal/cmd/server/disable_backup.go index df2ecaf7..b85376de 100644 --- a/internal/cmds/server_disable_backup.go +++ b/internal/cmd/server/disable_backup.go @@ -1,4 +1,4 @@ -package cmds +package server import ( "fmt" @@ -8,7 +8,7 @@ import ( "github.com/spf13/cobra" ) -func newServerDisableBackupCommand(cli *state.State) *cobra.Command { +func newDisableBackupCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "disable-backup [FLAGS] SERVER", Short: "Disable backup for a server", diff --git a/internal/cmds/server_disable_protection.go b/internal/cmd/server/disable_protection.go similarity index 95% rename from internal/cmds/server_disable_protection.go rename to internal/cmd/server/disable_protection.go index ff33c23c..e520d5f0 100644 --- a/internal/cmds/server_disable_protection.go +++ b/internal/cmd/server/disable_protection.go @@ -1,4 +1,4 @@ -package cmds +package server import ( "fmt" @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -func newServerDisableProtectionCommand(cli *state.State) *cobra.Command { +func newDisableProtectionCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "disable-protection [FLAGS] SERVER PROTECTIONLEVEL [PROTECTIONLEVEL...]", Short: "Disable resource protection for a server", diff --git a/internal/cmds/server_disable_rescue.go b/internal/cmd/server/disable_rescue.go similarity index 93% rename from internal/cmds/server_disable_rescue.go rename to internal/cmd/server/disable_rescue.go index 220efac9..93516720 100644 --- a/internal/cmds/server_disable_rescue.go +++ b/internal/cmd/server/disable_rescue.go @@ -1,4 +1,4 @@ -package cmds +package server import ( "fmt" @@ -8,7 +8,7 @@ import ( "github.com/spf13/cobra" ) -func newServerDisableRescueCommand(cli *state.State) *cobra.Command { +func newDisableRescueCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "disable-rescue [FLAGS] SERVER", Short: "Disable rescue for a server", diff --git a/internal/cmds/server_enable_backup.go b/internal/cmd/server/enable_backup.go similarity index 94% rename from internal/cmds/server_enable_backup.go rename to internal/cmd/server/enable_backup.go index d1e00287..9da7062d 100644 --- a/internal/cmds/server_enable_backup.go +++ b/internal/cmd/server/enable_backup.go @@ -1,4 +1,4 @@ -package cmds +package server import ( "fmt" @@ -8,7 +8,7 @@ import ( "github.com/spf13/cobra" ) -func newServerEnableBackupCommand(cli *state.State) *cobra.Command { +func newEnableBackupCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "enable-backup [FLAGS] SERVER", Short: "Enable backup for a server", diff --git a/internal/cmds/server_enable_protection.go b/internal/cmd/server/enable_protection.go similarity index 95% rename from internal/cmds/server_enable_protection.go rename to internal/cmd/server/enable_protection.go index 197920b8..138bd848 100644 --- a/internal/cmds/server_enable_protection.go +++ b/internal/cmd/server/enable_protection.go @@ -1,4 +1,4 @@ -package cmds +package server import ( "fmt" @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -func newServerEnableProtectionCommand(cli *state.State) *cobra.Command { +func newEnableProtectionCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "enable-protection [FLAGS] SERVER PROTECTIONLEVEL [PROTECTIONLEVEL...]", Short: "Enable resource protection for a server", diff --git a/internal/cmds/server_enable_rescue.go b/internal/cmd/server/enable_rescue.go similarity index 96% rename from internal/cmds/server_enable_rescue.go rename to internal/cmd/server/enable_rescue.go index a77e3da4..7671a36e 100644 --- a/internal/cmds/server_enable_rescue.go +++ b/internal/cmd/server/enable_rescue.go @@ -1,4 +1,4 @@ -package cmds +package server import ( "fmt" @@ -9,7 +9,7 @@ import ( "github.com/spf13/cobra" ) -func newServerEnableRescueCommand(cli *state.State) *cobra.Command { +func newEnableRescueCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "enable-rescue [FLAGS] SERVER", Short: "Enable rescue for a server", diff --git a/internal/cmds/server_ip.go b/internal/cmd/server/ip.go similarity index 93% rename from internal/cmds/server_ip.go rename to internal/cmd/server/ip.go index 4f59d512..670e6fd6 100644 --- a/internal/cmds/server_ip.go +++ b/internal/cmd/server/ip.go @@ -1,4 +1,4 @@ -package cmds +package server import ( "fmt" @@ -8,7 +8,7 @@ import ( "github.com/spf13/cobra" ) -func newServerIPCommand(cli *state.State) *cobra.Command { +func newIPCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "ip SERVER FLAGS", Short: "Print a server's IP address", diff --git a/internal/cmds/server_list.go b/internal/cmd/server/list.go similarity index 98% rename from internal/cmds/server_list.go rename to internal/cmd/server/list.go index 43546c06..5359c608 100644 --- a/internal/cmds/server_list.go +++ b/internal/cmd/server/list.go @@ -1,4 +1,4 @@ -package cmds +package server import ( "strconv" @@ -19,7 +19,7 @@ func init() { serverListTableOutput = describeServerListTableOutput(nil) } -func newServerListCommand(cli *state.State) *cobra.Command { +func newListCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "list [FLAGS]", Short: "List servers", diff --git a/internal/cmds/server_metrics.go b/internal/cmd/server/metrics.go similarity index 97% rename from internal/cmds/server_metrics.go rename to internal/cmd/server/metrics.go index dc45f065..6cf9cf8c 100644 --- a/internal/cmds/server_metrics.go +++ b/internal/cmd/server/metrics.go @@ -1,4 +1,4 @@ -package cmds +package server import ( "encoding/json" @@ -17,7 +17,7 @@ import ( "github.com/spf13/cobra" ) -func newServerMetricsCommand(cli *state.State) *cobra.Command { +func newMetricsCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "metrics [FLAGS] SERVER", Short: "[ALPHA] Metrics from a Server", diff --git a/internal/cmds/server_poweroff.go b/internal/cmd/server/poweroff.go similarity index 93% rename from internal/cmds/server_poweroff.go rename to internal/cmd/server/poweroff.go index c9ff4369..5be57bd5 100644 --- a/internal/cmds/server_poweroff.go +++ b/internal/cmd/server/poweroff.go @@ -1,4 +1,4 @@ -package cmds +package server import ( "fmt" @@ -8,7 +8,7 @@ import ( "github.com/spf13/cobra" ) -func newServerPoweroffCommand(cli *state.State) *cobra.Command { +func newPoweroffCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "poweroff [FLAGS] SERVER", Short: "Poweroff a server", diff --git a/internal/cmds/server_poweron.go b/internal/cmd/server/poweron.go similarity index 93% rename from internal/cmds/server_poweron.go rename to internal/cmd/server/poweron.go index 60ffc4b4..a94ea3c4 100644 --- a/internal/cmds/server_poweron.go +++ b/internal/cmd/server/poweron.go @@ -1,4 +1,4 @@ -package cmds +package server import ( "fmt" @@ -8,7 +8,7 @@ import ( "github.com/spf13/cobra" ) -func newServerPoweronCommand(cli *state.State) *cobra.Command { +func newPoweronCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "poweron [FLAGS] SERVER", Short: "Poweron a server", diff --git a/internal/cmds/server_reboot.go b/internal/cmd/server/reboot.go similarity index 93% rename from internal/cmds/server_reboot.go rename to internal/cmd/server/reboot.go index 42d35029..45311df7 100644 --- a/internal/cmds/server_reboot.go +++ b/internal/cmd/server/reboot.go @@ -1,4 +1,4 @@ -package cmds +package server import ( "fmt" @@ -8,7 +8,7 @@ import ( "github.com/spf13/cobra" ) -func newServerRebootCommand(cli *state.State) *cobra.Command { +func newRebootCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "reboot [FLAGS] SERVER", Short: "Reboot a server", diff --git a/internal/cmds/server_rebuild.go b/internal/cmd/server/rebuild.go similarity index 95% rename from internal/cmds/server_rebuild.go rename to internal/cmd/server/rebuild.go index 207fbe48..94576f86 100644 --- a/internal/cmds/server_rebuild.go +++ b/internal/cmd/server/rebuild.go @@ -1,4 +1,4 @@ -package cmds +package server import ( "fmt" @@ -9,7 +9,7 @@ import ( "github.com/spf13/cobra" ) -func newServerRebuildCommand(cli *state.State) *cobra.Command { +func newRebuildCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "rebuild [FLAGS] SERVER", Short: "Rebuild a server", diff --git a/internal/cmds/server_remove_label.go b/internal/cmd/server/remove_label.go similarity index 96% rename from internal/cmds/server_remove_label.go rename to internal/cmd/server/remove_label.go index 505a66a2..9ce2dd9b 100644 --- a/internal/cmds/server_remove_label.go +++ b/internal/cmd/server/remove_label.go @@ -1,4 +1,4 @@ -package cmds +package server import ( "errors" @@ -11,7 +11,7 @@ import ( "github.com/spf13/cobra" ) -func newServerRemoveLabelCommand(cli *state.State) *cobra.Command { +func newRemoveLabelCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "remove-label [FLAGS] SERVER LABELKEY", Short: "Remove a label from a server", diff --git a/internal/cmds/server_request_console.go b/internal/cmd/server/request_console.go similarity index 95% rename from internal/cmds/server_request_console.go rename to internal/cmd/server/request_console.go index 3078ef98..d2c48a04 100644 --- a/internal/cmds/server_request_console.go +++ b/internal/cmd/server/request_console.go @@ -1,4 +1,4 @@ -package cmds +package server import ( "fmt" @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -func newServerRequestConsoleCommand(cli *state.State) *cobra.Command { +func newRequestConsoleCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "request-console [FLAGS] SERVER", Short: "Request a WebSocket VNC console for a server", diff --git a/internal/cmds/server_reset.go b/internal/cmd/server/reset.go similarity index 93% rename from internal/cmds/server_reset.go rename to internal/cmd/server/reset.go index 8fd45d1b..4cf1cd90 100644 --- a/internal/cmds/server_reset.go +++ b/internal/cmd/server/reset.go @@ -1,4 +1,4 @@ -package cmds +package server import ( "fmt" @@ -8,7 +8,7 @@ import ( "github.com/spf13/cobra" ) -func newServerResetCommand(cli *state.State) *cobra.Command { +func newResetCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "reset [FLAGS] SERVER", Short: "Reset a server", diff --git a/internal/cmds/server_reset_password.go b/internal/cmd/server/reset_password.go similarity index 93% rename from internal/cmds/server_reset_password.go rename to internal/cmd/server/reset_password.go index afbb2424..fa5ae4c2 100644 --- a/internal/cmds/server_reset_password.go +++ b/internal/cmd/server/reset_password.go @@ -1,4 +1,4 @@ -package cmds +package server import ( "fmt" @@ -8,7 +8,7 @@ import ( "github.com/spf13/cobra" ) -func newServerResetPasswordCommand(cli *state.State) *cobra.Command { +func newResetPasswordCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "reset-password [FLAGS] SERVER", Short: "Reset the root password of a server", diff --git a/internal/cmd/server/server.go b/internal/cmd/server/server.go new file mode 100644 index 00000000..464d5a25 --- /dev/null +++ b/internal/cmd/server/server.go @@ -0,0 +1,51 @@ +package server + +import ( + "github.com/hetznercloud/cli/internal/state" + "github.com/spf13/cobra" +) + +func NewCommand(cli *state.State) *cobra.Command { + cmd := &cobra.Command{ + Use: "server", + Short: "Manage servers", + Args: cobra.NoArgs, + TraverseChildren: true, + DisableFlagsInUseLine: true, + } + cmd.AddCommand( + newListCommand(cli), + newDescribeCommand(cli), + newCreateCommand(cli), + newDeleteCommand(cli), + newRebootCommand(cli), + newPoweronCommand(cli), + newPoweroffCommand(cli), + newResetCommand(cli), + newShutdownCommand(cli), + newCreateImageCommand(cli), + newResetPasswordCommand(cli), + newEnableRescueCommand(cli), + newDisableRescueCommand(cli), + newAttachISOCommand(cli), + newDetachISOCommand(cli), + newUpdateCommand(cli), + newChangeTypeCommand(cli), + newRebuildCommand(cli), + newEnableBackupCommand(cli), + newDisableBackupCommand(cli), + newEnableProtectionCommand(cli), + newDisableProtectionCommand(cli), + newSSHCommand(cli), + newAddLabelCommand(cli), + newRemoveLabelCommand(cli), + newSetRDNSCommand(cli), + newAttachToNetworkCommand(cli), + newDetachFromNetworkCommand(cli), + newChangeAliasIPsCommand(cli), + newIPCommand(cli), + newRequestConsoleCommand(cli), + newMetricsCommand(cli), + ) + return cmd +} diff --git a/internal/cmds/server_set_rdns.go b/internal/cmd/server/set_rdns.go similarity index 95% rename from internal/cmds/server_set_rdns.go rename to internal/cmd/server/set_rdns.go index b42c8123..aaf10c30 100644 --- a/internal/cmds/server_set_rdns.go +++ b/internal/cmd/server/set_rdns.go @@ -1,4 +1,4 @@ -package cmds +package server import ( "fmt" @@ -9,7 +9,7 @@ import ( "github.com/spf13/cobra" ) -func newServerSetRDNSCommand(cli *state.State) *cobra.Command { +func newSetRDNSCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "set-rdns [FLAGS] SERVER", Short: "Change reverse DNS of a server", diff --git a/internal/cmds/server_shutdown.go b/internal/cmd/server/shutdown.go similarity index 93% rename from internal/cmds/server_shutdown.go rename to internal/cmd/server/shutdown.go index c27081c2..48abe7c0 100644 --- a/internal/cmds/server_shutdown.go +++ b/internal/cmd/server/shutdown.go @@ -1,4 +1,4 @@ -package cmds +package server import ( "fmt" @@ -8,7 +8,7 @@ import ( "github.com/spf13/cobra" ) -func newServerShutdownCommand(cli *state.State) *cobra.Command { +func newShutdownCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "shutdown [FLAGS] SERVER", Short: "Shutdown a server", diff --git a/internal/cmds/server_ssh.go b/internal/cmd/server/ssh.go similarity index 96% rename from internal/cmds/server_ssh.go rename to internal/cmd/server/ssh.go index 7f6975cb..fd71afe0 100644 --- a/internal/cmds/server_ssh.go +++ b/internal/cmd/server/ssh.go @@ -1,4 +1,4 @@ -package cmds +package server import ( "fmt" @@ -12,7 +12,7 @@ import ( "github.com/spf13/cobra" ) -func newServerSSHCommand(cli *state.State) *cobra.Command { +func newSSHCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "ssh [FLAGS] SERVER [COMMAND...]", Short: "Spawn an SSH connection for the server", diff --git a/internal/cmds/server_update.go b/internal/cmd/server/update.go similarity index 94% rename from internal/cmds/server_update.go rename to internal/cmd/server/update.go index 86742758..90f3d8d4 100644 --- a/internal/cmds/server_update.go +++ b/internal/cmd/server/update.go @@ -1,4 +1,4 @@ -package cmds +package server import ( "errors" @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -func newServerUpdateCommand(cli *state.State) *cobra.Command { +func newUpdateCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "update [FLAGS] SERVER", Short: "Update a server", diff --git a/internal/cmds/servertypes_describe.go b/internal/cmd/servertype/describe.go similarity index 96% rename from internal/cmds/servertypes_describe.go rename to internal/cmd/servertype/describe.go index 2f6e579d..ff7bbaa3 100644 --- a/internal/cmds/servertypes_describe.go +++ b/internal/cmd/servertype/describe.go @@ -1,4 +1,4 @@ -package cmds +package servertype import ( "encoding/json" @@ -12,7 +12,7 @@ import ( "github.com/spf13/cobra" ) -func newServerTypeDescribeCommand(cli *state.State) *cobra.Command { +func newDescribeCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "describe [FLAGS] SERVERTYPE", Short: "Describe a server type", diff --git a/internal/cmds/servertypes_list.go b/internal/cmd/servertype/list.go similarity index 96% rename from internal/cmds/servertypes_list.go rename to internal/cmd/servertype/list.go index 91d7e12b..39b3b4f7 100644 --- a/internal/cmds/servertypes_list.go +++ b/internal/cmd/servertype/list.go @@ -1,4 +1,4 @@ -package cmds +package servertype import ( "fmt" @@ -28,7 +28,7 @@ func init() { })) } -func newServerTypeListCommand(cli *state.State) *cobra.Command { +func newListCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "list [FLAGS]", Short: "List server types", diff --git a/internal/cmds/servertypes.go b/internal/cmd/servertype/server_type.go similarity index 69% rename from internal/cmds/servertypes.go rename to internal/cmd/servertype/server_type.go index b7901125..8961c085 100644 --- a/internal/cmds/servertypes.go +++ b/internal/cmd/servertype/server_type.go @@ -1,11 +1,11 @@ -package cmds +package servertype import ( "github.com/hetznercloud/cli/internal/state" "github.com/spf13/cobra" ) -func NewServerTypeCommand(cli *state.State) *cobra.Command { +func NewCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "server-type", Short: "Manage server types", @@ -14,8 +14,8 @@ func NewServerTypeCommand(cli *state.State) *cobra.Command { DisableFlagsInUseLine: true, } cmd.AddCommand( - newServerTypeListCommand(cli), - newServerTypeDescribeCommand(cli), + newListCommand(cli), + newDescribeCommand(cli), ) return cmd } diff --git a/internal/cmds/sshkey_add_label.go b/internal/cmd/sshkey/add_label.go similarity index 95% rename from internal/cmds/sshkey_add_label.go rename to internal/cmd/sshkey/add_label.go index c9d6843c..cd118ca7 100644 --- a/internal/cmds/sshkey_add_label.go +++ b/internal/cmd/sshkey/add_label.go @@ -1,4 +1,4 @@ -package cmds +package sshkey import ( "fmt" @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -func newSSHKeyAddLabelCommand(cli *state.State) *cobra.Command { +func newAddLabelCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "add-label [FLAGS] SSHKEY LABEL", Short: "Add a label to a SSH key", diff --git a/internal/cmds/sshkey_create.go b/internal/cmd/sshkey/create.go similarity index 96% rename from internal/cmds/sshkey_create.go rename to internal/cmd/sshkey/create.go index b1cb334e..7ef3fe3d 100644 --- a/internal/cmds/sshkey_create.go +++ b/internal/cmd/sshkey/create.go @@ -1,4 +1,4 @@ -package cmds +package sshkey import ( "errors" @@ -12,7 +12,7 @@ import ( "github.com/spf13/cobra" ) -func newSSHKeyCreateCommand(cli *state.State) *cobra.Command { +func newCreateCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "create FLAGS", Short: "Create a SSH key", diff --git a/internal/cmds/sshkey_delete.go b/internal/cmd/sshkey/delete.go similarity index 92% rename from internal/cmds/sshkey_delete.go rename to internal/cmd/sshkey/delete.go index 51328f71..4cda8e4d 100644 --- a/internal/cmds/sshkey_delete.go +++ b/internal/cmd/sshkey/delete.go @@ -1,4 +1,4 @@ -package cmds +package sshkey import ( "fmt" @@ -8,7 +8,7 @@ import ( "github.com/spf13/cobra" ) -func newSSHKeyDeleteCommand(cli *state.State) *cobra.Command { +func newDeleteCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "delete [FLAGS] SSHKEY", Short: "Delete a SSH key", diff --git a/internal/cmds/sshkey_describe.go b/internal/cmd/sshkey/describe.go similarity index 96% rename from internal/cmds/sshkey_describe.go rename to internal/cmd/sshkey/describe.go index 0cba6278..f1040c5c 100644 --- a/internal/cmds/sshkey_describe.go +++ b/internal/cmd/sshkey/describe.go @@ -1,4 +1,4 @@ -package cmds +package sshkey import ( "encoding/json" @@ -15,7 +15,7 @@ import ( "github.com/spf13/cobra" ) -func newSSHKeyDescribeCommand(cli *state.State) *cobra.Command { +func newDescribeCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "describe [FLAGS] SSHKEY", Short: "Describe a SSH key", diff --git a/internal/cmds/sshkey_list.go b/internal/cmd/sshkey/list.go similarity index 97% rename from internal/cmds/sshkey_list.go rename to internal/cmd/sshkey/list.go index f8c44566..74984340 100644 --- a/internal/cmds/sshkey_list.go +++ b/internal/cmd/sshkey/list.go @@ -1,4 +1,4 @@ -package cmds +package sshkey import ( "github.com/hetznercloud/cli/internal/cmd/output" @@ -24,7 +24,7 @@ func init() { })) } -func newSSHKeyListCommand(cli *state.State) *cobra.Command { +func newListCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "list [FLAGS]", Short: "List SSH keys", diff --git a/internal/cmds/sshkey_remove_label.go b/internal/cmd/sshkey/remove_label.go similarity index 96% rename from internal/cmds/sshkey_remove_label.go rename to internal/cmd/sshkey/remove_label.go index 3915cb5b..b382976d 100644 --- a/internal/cmds/sshkey_remove_label.go +++ b/internal/cmd/sshkey/remove_label.go @@ -1,4 +1,4 @@ -package cmds +package sshkey import ( "errors" @@ -11,7 +11,7 @@ import ( "github.com/spf13/cobra" ) -func newSSHKeyRemoveLabelCommand(cli *state.State) *cobra.Command { +func newRemoveLabelCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "remove-label [FLAGS] SSHKEY LABELKEY", Short: "Remove a label from a SSH key", diff --git a/internal/cmds/sshkey.go b/internal/cmd/sshkey/sshkey.go similarity index 52% rename from internal/cmds/sshkey.go rename to internal/cmd/sshkey/sshkey.go index f447cc72..427d1fcc 100644 --- a/internal/cmds/sshkey.go +++ b/internal/cmd/sshkey/sshkey.go @@ -1,11 +1,11 @@ -package cmds +package sshkey import ( "github.com/hetznercloud/cli/internal/state" "github.com/spf13/cobra" ) -func NewSSHKeyCommand(cli *state.State) *cobra.Command { +func NewCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "ssh-key", Short: "Manage SSH keys", @@ -14,13 +14,13 @@ func NewSSHKeyCommand(cli *state.State) *cobra.Command { DisableFlagsInUseLine: true, } cmd.AddCommand( - newSSHKeyListCommand(cli), - newSSHKeyCreateCommand(cli), - newSSHKeyUpdateCommand(cli), - newSSHKeyDeleteCommand(cli), - newSSHKeyDescribeCommand(cli), - newSSHKeyAddLabelCommand(cli), - newSSHKeyRemoveLabelCommand(cli), + newListCommand(cli), + newCreateCommand(cli), + newUpdateCommand(cli), + newDeleteCommand(cli), + newDescribeCommand(cli), + newAddLabelCommand(cli), + newRemoveLabelCommand(cli), ) return cmd } diff --git a/internal/cmds/sshkey_update.go b/internal/cmd/sshkey/update.go similarity index 94% rename from internal/cmds/sshkey_update.go rename to internal/cmd/sshkey/update.go index fc0bc997..f5c9dd5c 100644 --- a/internal/cmds/sshkey_update.go +++ b/internal/cmd/sshkey/update.go @@ -1,4 +1,4 @@ -package cmds +package sshkey import ( "errors" @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -func newSSHKeyUpdateCommand(cli *state.State) *cobra.Command { +func newUpdateCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "update [FLAGS] SSHKEY", Short: "Update a SSH key", diff --git a/internal/cmds/version.go b/internal/cmd/version/version.go similarity index 88% rename from internal/cmds/version.go rename to internal/cmd/version/version.go index ca5e61ca..b0281f2b 100644 --- a/internal/cmds/version.go +++ b/internal/cmd/version/version.go @@ -1,4 +1,4 @@ -package cmds +package version import ( "fmt" @@ -10,7 +10,7 @@ import ( // Version is set via compiler flags (see script/build.bash) var Version = "was not built properly" -func NewVersionCommand(cli *state.State) *cobra.Command { +func NewCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "version", Short: "Print version information", diff --git a/internal/cmds/volume_add_label.go b/internal/cmd/volume/add_label.go similarity index 95% rename from internal/cmds/volume_add_label.go rename to internal/cmd/volume/add_label.go index 94bc4334..24efb4d6 100644 --- a/internal/cmds/volume_add_label.go +++ b/internal/cmd/volume/add_label.go @@ -1,4 +1,4 @@ -package cmds +package volume import ( "fmt" @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -func newVolumeAddLabelCommand(cli *state.State) *cobra.Command { +func newAddLabelCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "add-label [FLAGS] VOLUME LABEL", Short: "Add a label to a volume", diff --git a/internal/cmds/volume_attach.go b/internal/cmd/volume/attach.go similarity index 95% rename from internal/cmds/volume_attach.go rename to internal/cmd/volume/attach.go index 5a9f6aae..8a20d43f 100644 --- a/internal/cmds/volume_attach.go +++ b/internal/cmd/volume/attach.go @@ -1,4 +1,4 @@ -package cmds +package volume import ( "fmt" @@ -9,7 +9,7 @@ import ( "github.com/spf13/cobra" ) -func newVolumeAttachCommand(cli *state.State) *cobra.Command { +func newAttachCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "attach [FLAGS] VOLUME", Short: "Attach a volume to a server", diff --git a/internal/cmds/volume_create.go b/internal/cmd/volume/create.go similarity index 97% rename from internal/cmds/volume_create.go rename to internal/cmd/volume/create.go index 1f4cd362..a99aa11e 100644 --- a/internal/cmds/volume_create.go +++ b/internal/cmd/volume/create.go @@ -1,4 +1,4 @@ -package cmds +package volume import ( "fmt" @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -func newVolumeCreateCommand(cli *state.State) *cobra.Command { +func newCreateCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "create FLAGS", Short: "Create a volume", diff --git a/internal/cmds/volume_delete.go b/internal/cmd/volume/delete.go similarity index 92% rename from internal/cmds/volume_delete.go rename to internal/cmd/volume/delete.go index 58cdab17..7a6dae15 100644 --- a/internal/cmds/volume_delete.go +++ b/internal/cmd/volume/delete.go @@ -1,4 +1,4 @@ -package cmds +package volume import ( "fmt" @@ -8,7 +8,7 @@ import ( "github.com/spf13/cobra" ) -func newVolumeDeleteCommand(cli *state.State) *cobra.Command { +func newDeleteCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "delete [FLAGS] VOLUME", Short: "Delete a volume", diff --git a/internal/cmds/volume_describe.go b/internal/cmd/volume/describe.go similarity index 97% rename from internal/cmds/volume_describe.go rename to internal/cmd/volume/describe.go index ed8773e6..f7f5ed81 100644 --- a/internal/cmds/volume_describe.go +++ b/internal/cmd/volume/describe.go @@ -1,4 +1,4 @@ -package cmds +package volume import ( "encoding/json" @@ -13,7 +13,7 @@ import ( "github.com/spf13/cobra" ) -func newVolumeDescribeCommand(cli *state.State) *cobra.Command { +func newDescribeCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "describe [FLAGS] VOLUME", Short: "Describe a volume", diff --git a/internal/cmds/volume_detach.go b/internal/cmd/volume/detach.go similarity index 93% rename from internal/cmds/volume_detach.go rename to internal/cmd/volume/detach.go index 87a712fa..17383474 100644 --- a/internal/cmds/volume_detach.go +++ b/internal/cmd/volume/detach.go @@ -1,4 +1,4 @@ -package cmds +package volume import ( "fmt" @@ -8,7 +8,7 @@ import ( "github.com/spf13/cobra" ) -func newVolumeDetachCommand(cli *state.State) *cobra.Command { +func newDetachCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "detach [FLAGS] VOLUME", Short: "Detach a volume", diff --git a/internal/cmds/volume_disable_protection.go b/internal/cmd/volume/disable_protection.go similarity index 94% rename from internal/cmds/volume_disable_protection.go rename to internal/cmd/volume/disable_protection.go index cc42185c..177ea7f8 100644 --- a/internal/cmds/volume_disable_protection.go +++ b/internal/cmd/volume/disable_protection.go @@ -1,4 +1,4 @@ -package cmds +package volume import ( "fmt" @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -func newVolumeDisableProtectionCommand(cli *state.State) *cobra.Command { +func newDisableProtectionCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "disable-protection [FLAGS] VOLUME PROTECTIONLEVEL [PROTECTIONLEVEL...]", Short: "Disable resource protection for a volume", diff --git a/internal/cmds/volume_enable_protection.go b/internal/cmd/volume/enable_protection.go similarity index 94% rename from internal/cmds/volume_enable_protection.go rename to internal/cmd/volume/enable_protection.go index 40532a56..37be145e 100644 --- a/internal/cmds/volume_enable_protection.go +++ b/internal/cmd/volume/enable_protection.go @@ -1,4 +1,4 @@ -package cmds +package volume import ( "fmt" @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -func newVolumeEnableProtectionCommand(cli *state.State) *cobra.Command { +func newEnableProtectionCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "enable-protection [FLAGS] VOLUME PROTECTIONLEVEL [PROTECTIONLEVEL...]", Short: "Enable resource protection for a volume", diff --git a/internal/cmds/volume_list.go b/internal/cmd/volume/list.go similarity index 98% rename from internal/cmds/volume_list.go rename to internal/cmd/volume/list.go index a1b2d2ba..6fba6cd2 100644 --- a/internal/cmds/volume_list.go +++ b/internal/cmd/volume/list.go @@ -1,4 +1,4 @@ -package cmds +package volume import ( "strings" @@ -19,7 +19,7 @@ func init() { volumeListTableOutput = describeVolumeListTableOutput(nil) } -func newVolumeListCommand(cli *state.State) *cobra.Command { +func newListCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "list [FLAGS]", Short: "List volumes", diff --git a/internal/cmds/volume_remove_label.go b/internal/cmd/volume/remove_label.go similarity index 96% rename from internal/cmds/volume_remove_label.go rename to internal/cmd/volume/remove_label.go index 2f144176..9d297482 100644 --- a/internal/cmds/volume_remove_label.go +++ b/internal/cmd/volume/remove_label.go @@ -1,4 +1,4 @@ -package cmds +package volume import ( "errors" @@ -11,7 +11,7 @@ import ( "github.com/spf13/cobra" ) -func newVolumeRemoveLabelCommand(cli *state.State) *cobra.Command { +func newRemoveLabelCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "remove-label [FLAGS] VOLUME LABELKEY", Short: "Remove a label from a volume", diff --git a/internal/cmds/volume_resize.go b/internal/cmd/volume/resize.go similarity index 94% rename from internal/cmds/volume_resize.go rename to internal/cmd/volume/resize.go index bf0254d8..a1014aac 100644 --- a/internal/cmds/volume_resize.go +++ b/internal/cmd/volume/resize.go @@ -1,4 +1,4 @@ -package cmds +package volume import ( "fmt" @@ -8,7 +8,7 @@ import ( "github.com/spf13/cobra" ) -func newVolumeResizeCommand(cli *state.State) *cobra.Command { +func newResizeCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "resize [FLAGS] VOLUME", Short: "Resize a volume", diff --git a/internal/cmds/volume_update.go b/internal/cmd/volume/update.go similarity index 94% rename from internal/cmds/volume_update.go rename to internal/cmd/volume/update.go index b7c68ada..84ed51e3 100644 --- a/internal/cmds/volume_update.go +++ b/internal/cmd/volume/update.go @@ -1,4 +1,4 @@ -package cmds +package volume import ( "errors" @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -func newVolumeUpdateCommand(cli *state.State) *cobra.Command { +func newUpdateCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "update [FLAGS] VOLUME", Short: "Update a volume", diff --git a/internal/cmd/volume/volume.go b/internal/cmd/volume/volume.go new file mode 100644 index 00000000..d1dd4085 --- /dev/null +++ b/internal/cmd/volume/volume.go @@ -0,0 +1,31 @@ +package volume + +import ( + "github.com/hetznercloud/cli/internal/state" + "github.com/spf13/cobra" +) + +func NewCommand(cli *state.State) *cobra.Command { + cmd := &cobra.Command{ + Use: "volume", + Short: "Manage Volumes", + Args: cobra.NoArgs, + TraverseChildren: true, + DisableFlagsInUseLine: true, + } + cmd.AddCommand( + newListCommand(cli), + newCreateCommand(cli), + newUpdateCommand(cli), + newDeleteCommand(cli), + newDescribeCommand(cli), + newAttachCommand(cli), + newDetachCommand(cli), + newResizeCommand(cli), + newAddLabelCommand(cli), + newRemoveLabelCommand(cli), + newEnableProtectionCommand(cli), + newDisableProtectionCommand(cli), + ) + return cmd +} diff --git a/internal/cmds/floatingip.go b/internal/cmds/floatingip.go deleted file mode 100644 index d0bb69c1..00000000 --- a/internal/cmds/floatingip.go +++ /dev/null @@ -1,31 +0,0 @@ -package cmds - -import ( - "github.com/hetznercloud/cli/internal/state" - "github.com/spf13/cobra" -) - -func NewFloatingIPCommand(cli *state.State) *cobra.Command { - cmd := &cobra.Command{ - Use: "floating-ip", - Short: "Manage Floating IPs", - Args: cobra.NoArgs, - TraverseChildren: true, - DisableFlagsInUseLine: true, - } - cmd.AddCommand( - newFloatingIPUpdateCommand(cli), - newFloatingIPListCommand(cli), - newFloatingIPCreateCommand(cli), - newFloatingIPDescribeCommand(cli), - newFloatingIPAssignCommand(cli), - newFloatingIPUnassignCommand(cli), - newFloatingIPDeleteCommand(cli), - newFloatingIPEnableProtectionCommand(cli), - newFloatingIPDisableProtectionCommand(cli), - newFloatingIPAddLabelCommand(cli), - newFloatingIPRemoveLabelCommand(cli), - newFloatingIPSetRDNSCommand(cli), - ) - return cmd -} diff --git a/internal/cmds/image.go b/internal/cmds/image.go deleted file mode 100644 index 014f0d11..00000000 --- a/internal/cmds/image.go +++ /dev/null @@ -1,27 +0,0 @@ -package cmds - -import ( - "github.com/hetznercloud/cli/internal/state" - "github.com/spf13/cobra" -) - -func NewImageCommand(cli *state.State) *cobra.Command { - cmd := &cobra.Command{ - Use: "image", - Short: "Manage images", - Args: cobra.NoArgs, - TraverseChildren: true, - DisableFlagsInUseLine: true, - } - cmd.AddCommand( - newImageListCommand(cli), - newImageDeleteCommand(cli), - newImageDescribeCommand(cli), - newImageUpdateCommand(cli), - newImageEnableProtectionCommand(cli), - newImageDisableProtectionCommand(cli), - newImageAddLabelCommand(cli), - newImageRemoveLabelCommand(cli), - ) - return cmd -} diff --git a/internal/cmds/load_balancer.go b/internal/cmds/load_balancer.go deleted file mode 100644 index 0fe04087..00000000 --- a/internal/cmds/load_balancer.go +++ /dev/null @@ -1,40 +0,0 @@ -package cmds - -import ( - "github.com/hetznercloud/cli/internal/state" - "github.com/spf13/cobra" -) - -func NewLoadBalancerCommand(cli *state.State) *cobra.Command { - cmd := &cobra.Command{ - Use: "load-balancer", - Short: "Manage Load Balancers", - Args: cobra.NoArgs, - TraverseChildren: true, - DisableFlagsInUseLine: true, - } - cmd.AddCommand( - newLoadBalancerCreateCommand(cli), - newLoadBalancerListCommand(cli), - newLoadBalancerDescribeCommand(cli), - newLoadBalancerDeleteCommand(cli), - newLoadBalancerUpdateCommand(cli), - newLoadBalancerAddLabelCommand(cli), - newLoadBalancerRemoveLabelCommand(cli), - newLoadBalancerAddTargetCommand(cli), - newLoadBalancerRemoveTargetCommand(cli), - newLoadBalancerChangeAlgorithmCommand(cli), - newLoadBalancerUpdateServiceCommand(cli), - newLoadBalancerDeleteServiceCommand(cli), - newLoadBalancerAddServiceCommand(cli), - newLoadBalancerEnableProtectionCommand(cli), - newLoadBalancerDisableProtectionCommand(cli), - newLoadBalancerAttachToNetworkCommand(cli), - newLoadBalancerDetachFromNetworkCommand(cli), - newLoadBalancerEnablePublicInterface(cli), - newLoadBalancerDisablePublicInterface(cli), - newLoadBalancerChangeTypeCommand(cli), - newLoadBalancerMetricsCommand(cli), - ) - return cmd -} diff --git a/internal/cmds/network.go b/internal/cmds/network.go deleted file mode 100644 index e81e50a1..00000000 --- a/internal/cmds/network.go +++ /dev/null @@ -1,33 +0,0 @@ -package cmds - -import ( - "github.com/hetznercloud/cli/internal/state" - "github.com/spf13/cobra" -) - -func NewNetworkCommand(cli *state.State) *cobra.Command { - cmd := &cobra.Command{ - Use: "network", - Short: "Manage networks", - Args: cobra.NoArgs, - TraverseChildren: true, - DisableFlagsInUseLine: true, - } - cmd.AddCommand( - newNetworkListCommand(cli), - newNetworkDescribeCommand(cli), - newNetworkCreateCommand(cli), - newNetworkUpdateCommand(cli), - newNetworkDeleteCommand(cli), - newNetworkChangeIPRangeCommand(cli), - newNetworkAddRouteCommand(cli), - newNetworkRemoveRouteCommand(cli), - newNetworkAddSubnetCommand(cli), - newNetworkRemoveSubnetCommand(cli), - newNetworkAddLabelCommand(cli), - newNetworkRemoveLabelCommand(cli), - newNetworkEnableProtectionCommand(cli), - newNetworkDisableProtectionCommand(cli), - ) - return cmd -} diff --git a/internal/cmds/server.go b/internal/cmds/server.go deleted file mode 100644 index c53add95..00000000 --- a/internal/cmds/server.go +++ /dev/null @@ -1,51 +0,0 @@ -package cmds - -import ( - "github.com/hetznercloud/cli/internal/state" - "github.com/spf13/cobra" -) - -func NewServerCommand(cli *state.State) *cobra.Command { - cmd := &cobra.Command{ - Use: "server", - Short: "Manage servers", - Args: cobra.NoArgs, - TraverseChildren: true, - DisableFlagsInUseLine: true, - } - cmd.AddCommand( - newServerListCommand(cli), - newServerDescribeCommand(cli), - newServerCreateCommand(cli), - newServerDeleteCommand(cli), - newServerRebootCommand(cli), - newServerPoweronCommand(cli), - newServerPoweroffCommand(cli), - newServerResetCommand(cli), - newServerShutdownCommand(cli), - newServerCreateImageCommand(cli), - newServerResetPasswordCommand(cli), - newServerEnableRescueCommand(cli), - newServerDisableRescueCommand(cli), - newServerAttachISOCommand(cli), - newServerDetachISOCommand(cli), - newServerUpdateCommand(cli), - newServerChangeTypeCommand(cli), - newServerRebuildCommand(cli), - newServerEnableBackupCommand(cli), - newServerDisableBackupCommand(cli), - newServerEnableProtectionCommand(cli), - newServerDisableProtectionCommand(cli), - newServerSSHCommand(cli), - newServerAddLabelCommand(cli), - newServerRemoveLabelCommand(cli), - newServerSetRDNSCommand(cli), - newServerAttachToNetworkCommand(cli), - newServerDetachFromNetworkCommand(cli), - newServerChangeAliasIPsCommand(cli), - newServerIPCommand(cli), - newServerRequestConsoleCommand(cli), - newServerMetricsCommand(cli), - ) - return cmd -} diff --git a/internal/cmds/volume.go b/internal/cmds/volume.go deleted file mode 100644 index 16e05752..00000000 --- a/internal/cmds/volume.go +++ /dev/null @@ -1,31 +0,0 @@ -package cmds - -import ( - "github.com/hetznercloud/cli/internal/state" - "github.com/spf13/cobra" -) - -func NewVolumeCommand(cli *state.State) *cobra.Command { - cmd := &cobra.Command{ - Use: "volume", - Short: "Manage Volumes", - Args: cobra.NoArgs, - TraverseChildren: true, - DisableFlagsInUseLine: true, - } - cmd.AddCommand( - newVolumeListCommand(cli), - newVolumeCreateCommand(cli), - newVolumeUpdateCommand(cli), - newVolumeDeleteCommand(cli), - newVolumeDescribeCommand(cli), - newVolumeAttachCommand(cli), - newVolumeDetachCommand(cli), - newVolumeResizeCommand(cli), - newVolumeAddLabelCommand(cli), - newVolumeRemoveLabelCommand(cli), - newVolumeEnableProtectionCommand(cli), - newVolumeDisableProtectionCommand(cli), - ) - return cmd -} From 8f93602b39d7a4fee652d9e5d0e9f15805e6ccec Mon Sep 17 00:00:00 2001 From: Michael Frister Date: Mon, 11 Jan 2021 11:17:59 +0100 Subject: [PATCH 7/9] Refactor: Simplify CLI main/root --- cmd/hcloud/main.go | 18 ++++++++++-------- internal/cli/cli.go | 20 -------------------- 2 files changed, 10 insertions(+), 28 deletions(-) delete mode 100644 internal/cli/cli.go diff --git a/cmd/hcloud/main.go b/cmd/hcloud/main.go index 6461ff97..dfc7522e 100644 --- a/cmd/hcloud/main.go +++ b/cmd/hcloud/main.go @@ -5,6 +5,7 @@ import ( "os" "github.com/hetznercloud/cli/internal/cli" + "github.com/hetznercloud/cli/internal/state" ) func init() { @@ -14,25 +15,26 @@ func init() { } func main() { - c := cli.NewCLI() + state := state.New() - if c.State.ConfigPath != "" { - _, err := os.Stat(c.State.ConfigPath) + if state.ConfigPath != "" { + _, err := os.Stat(state.ConfigPath) switch { case err == nil: - if err := c.State.ReadConfig(); err != nil { - log.Fatalf("unable to read config file %q: %s\n", c.State.ConfigPath, err) + if err := state.ReadConfig(); err != nil { + log.Fatalf("unable to read config file %q: %s\n", state.ConfigPath, err) } case os.IsNotExist(err): break default: - log.Fatalf("unable to read config file %q: %s\n", c.State.ConfigPath, err) + log.Fatalf("unable to read config file %q: %s\n", state.ConfigPath, err) } } - c.State.ReadEnv() + state.ReadEnv() - if err := c.RootCommand.Execute(); err != nil { + rootCommand := cli.NewRootCommand(state) + if err := rootCommand.Execute(); err != nil { log.Fatalln(err) } } diff --git a/internal/cli/cli.go b/internal/cli/cli.go deleted file mode 100644 index 2f3af873..00000000 --- a/internal/cli/cli.go +++ /dev/null @@ -1,20 +0,0 @@ -package cli - -import ( - "github.com/hetznercloud/cli/internal/state" - "github.com/spf13/cobra" -) - -type CLI struct { - State *state.State - RootCommand *cobra.Command -} - -func NewCLI() *CLI { - cli := &CLI{ - State: state.New(), - } - - cli.RootCommand = NewRootCommand(cli.State) - return cli -} From e38553d06cc56d05b9b3b0797cb112e6df10fa3c Mon Sep 17 00:00:00 2001 From: Michael Frister Date: Mon, 11 Jan 2021 11:37:29 +0100 Subject: [PATCH 8/9] Fix duplicate version variable and build script --- .goreleaser.yml | 4 ++-- internal/cmd/version/version.go | 9 ++++----- internal/state/helpers.go | 6 ++++-- internal/{state => version}/version.go | 4 ++-- script/build.bash | 2 +- 5 files changed, 13 insertions(+), 12 deletions(-) rename internal/{state => version}/version.go (50%) diff --git a/.goreleaser.yml b/.goreleaser.yml index c42dae1c..d5e2fcf7 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -3,7 +3,7 @@ builds: main: ./cmd/hcloud/main.go binary: hcloud ldflags: - - -w -X github.com/hetznercloud/cli/cli.Version={{.Version}} + - -w -X github.com/hetznercloud/cli/internal/version.Version={{.Version}} env: - CGO_ENABLED=0 goos: @@ -23,7 +23,7 @@ builds: main: ./cmd/hcloud/main.go binary: hcloud ldflags: - - -w -X github.com/hetznercloud/cli/cli.Version={{.Version}} + - -w -X github.com/hetznercloud/cli/internal/version.Version={{.Version}} env: - CGO_ENABLED=0 goos: diff --git a/internal/cmd/version/version.go b/internal/cmd/version/version.go index b0281f2b..a56b0302 100644 --- a/internal/cmd/version/version.go +++ b/internal/cmd/version/version.go @@ -3,12 +3,11 @@ package version import ( "fmt" - "github.com/hetznercloud/cli/internal/state" "github.com/spf13/cobra" -) -// Version is set via compiler flags (see script/build.bash) -var Version = "was not built properly" + "github.com/hetznercloud/cli/internal/state" + "github.com/hetznercloud/cli/internal/version" +) func NewCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ @@ -22,6 +21,6 @@ func NewCommand(cli *state.State) *cobra.Command { } func runVersion(cli *state.State, cmd *cobra.Command, args []string) error { - fmt.Printf("hcloud %s\n", Version) + fmt.Printf("hcloud %s\n", version.Version) return nil } diff --git a/internal/state/helpers.go b/internal/state/helpers.go index e0bced42..51ec1e17 100644 --- a/internal/state/helpers.go +++ b/internal/state/helpers.go @@ -8,10 +8,12 @@ import ( "time" "github.com/cheggaaa/pb/v3" - "github.com/hetznercloud/cli/internal/hcapi" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/spf13/cobra" "golang.org/x/crypto/ssh/terminal" + + "github.com/hetznercloud/cli/internal/hcapi" + "github.com/hetznercloud/cli/internal/version" ) const ( @@ -29,7 +31,7 @@ func (c *State) Client() *hcloud.Client { if c.client == nil { opts := []hcloud.ClientOption{ hcloud.WithToken(c.Token), - hcloud.WithApplication("hcloud-cli", Version), + hcloud.WithApplication("hcloud-cli", version.Version), } if c.Endpoint != "" { opts = append(opts, hcloud.WithEndpoint(c.Endpoint)) diff --git a/internal/state/version.go b/internal/version/version.go similarity index 50% rename from internal/state/version.go rename to internal/version/version.go index 2c96a9cb..50019c58 100644 --- a/internal/state/version.go +++ b/internal/version/version.go @@ -1,4 +1,4 @@ -package state +package version // Version is set via compiler flags (see script/build.bash) -var Version = "TODO DUPLICATE, see cli package" +var Version = "was not built properly" diff --git a/script/build.bash b/script/build.bash index a67bd019..ce3d82ee 100755 --- a/script/build.bash +++ b/script/build.bash @@ -14,5 +14,5 @@ arch="$2" release="$3" [ -z "$release" ] && usage -LD_FLAGS="-w -X github.com/hetznercloud/cli/cli.Version=$release" +LD_FLAGS="-w -X github.com/hetznercloud/cli/internal/version.Version=$release" GOOS=$os GOARCH=$arch go build -o ./dist/hcloud-$os-$arch-$release -ldflags "$LD_FLAGS" ./cmd/hcloud From 0110707908f4ee2b76b1c121dc92456a9226fa97 Mon Sep 17 00:00:00 2001 From: Michael Frister Date: Tue, 12 Jan 2021 08:14:51 +0100 Subject: [PATCH 9/9] cmds: Simplify remaining function names Remove the package name from all function names. --- internal/cmd/certificate/add_label.go | 8 ++++---- internal/cmd/certificate/certificate.go | 2 +- internal/cmd/certificate/create.go | 4 ++-- internal/cmd/certificate/delete.go | 4 ++-- internal/cmd/certificate/describe.go | 12 ++++++------ internal/cmd/certificate/list.go | 18 +++++++++--------- internal/cmd/certificate/remove_label.go | 8 ++++---- internal/cmd/certificate/update.go | 4 ++-- internal/cmd/context/active.go | 4 ++-- internal/cmd/context/create.go | 4 ++-- internal/cmd/context/delete.go | 4 ++-- internal/cmd/context/list.go | 14 +++++++------- internal/cmd/context/use.go | 4 ++-- internal/cmd/datacenter/describe.go | 12 ++++++------ internal/cmd/datacenter/list.go | 14 +++++++------- internal/cmd/floatingip/add_label.go | 8 ++++---- internal/cmd/floatingip/assign.go | 4 ++-- internal/cmd/floatingip/create.go | 8 ++++---- internal/cmd/floatingip/delete.go | 4 ++-- internal/cmd/floatingip/describe.go | 12 ++++++------ internal/cmd/floatingip/disable_protection.go | 4 ++-- internal/cmd/floatingip/enable_protection.go | 4 ++-- internal/cmd/floatingip/list.go | 16 ++++++++-------- internal/cmd/floatingip/remove_label.go | 8 ++++---- internal/cmd/floatingip/set_rdns.go | 4 ++-- internal/cmd/floatingip/unassign.go | 4 ++-- internal/cmd/floatingip/update.go | 4 ++-- internal/cmd/image/add_label.go | 8 ++++---- internal/cmd/image/delete.go | 4 ++-- internal/cmd/image/describe.go | 12 ++++++------ internal/cmd/image/disable_protection.go | 4 ++-- internal/cmd/image/enable_protection.go | 4 ++-- internal/cmd/image/list.go | 16 ++++++++-------- internal/cmd/image/remove_label.go | 4 ++-- internal/cmd/image/update.go | 4 ++-- internal/cmd/iso/describe.go | 12 ++++++------ internal/cmd/iso/list.go | 14 +++++++------- internal/cmd/loadbalancer/add_label.go | 8 ++++---- internal/cmd/loadbalancer/add_service.go | 8 ++++---- internal/cmd/loadbalancer/add_target.go | 4 ++-- internal/cmd/loadbalancer/attach_to_network.go | 4 ++-- internal/cmd/loadbalancer/change_algorithm.go | 4 ++-- internal/cmd/loadbalancer/change_type.go | 4 ++-- internal/cmd/loadbalancer/create.go | 4 ++-- internal/cmd/loadbalancer/delete.go | 4 ++-- internal/cmd/loadbalancer/delete_service.go | 4 ++-- internal/cmd/loadbalancer/describe.go | 12 ++++++------ .../cmd/loadbalancer/detach_from_network.go | 4 ++-- .../cmd/loadbalancer/disable_protection.go | 4 ++-- .../loadbalancer/disable_public_interface.go | 4 ++-- internal/cmd/loadbalancer/enable_protection.go | 4 ++-- .../loadbalancer/enable_public_interface.go | 4 ++-- internal/cmd/loadbalancer/list.go | 16 ++++++++-------- internal/cmd/loadbalancer/metrics.go | 8 ++++---- internal/cmd/loadbalancer/remove_label.go | 8 ++++---- internal/cmd/loadbalancer/remove_target.go | 4 ++-- internal/cmd/loadbalancer/update.go | 4 ++-- internal/cmd/loadbalancer/update_service.go | 4 ++-- internal/cmd/loadbalancertype/describe.go | 14 +++++++------- internal/cmd/loadbalancertype/list.go | 14 +++++++------- .../cmd/loadbalancertype/load_balancer_type.go | 2 +- internal/cmd/location/describe.go | 12 ++++++------ internal/cmd/location/list.go | 14 +++++++------- internal/cmd/network/add_label.go | 8 ++++---- internal/cmd/network/add_route.go | 4 ++-- internal/cmd/network/add_subnet.go | 4 ++-- internal/cmd/network/change_ip_range.go | 4 ++-- internal/cmd/network/create.go | 4 ++-- internal/cmd/network/delete.go | 4 ++-- internal/cmd/network/describe.go | 12 ++++++------ internal/cmd/network/disable_protection.go | 4 ++-- internal/cmd/network/enable_protection.go | 4 ++-- internal/cmd/network/list.go | 16 ++++++++-------- internal/cmd/network/remove_label.go | 8 ++++---- internal/cmd/network/remove_route.go | 4 ++-- internal/cmd/network/remove_subnet.go | 4 ++-- internal/cmd/network/update.go | 4 ++-- internal/cmd/server/add_label.go | 8 ++++---- internal/cmd/server/attach_iso.go | 4 ++-- internal/cmd/server/attach_to_network.go | 4 ++-- internal/cmd/server/change_alias_ips.go | 4 ++-- internal/cmd/server/change_type.go | 4 ++-- internal/cmd/server/create.go | 4 ++-- internal/cmd/server/create_image.go | 8 ++++---- internal/cmd/server/delete.go | 4 ++-- internal/cmd/server/describe.go | 12 ++++++------ internal/cmd/server/detach_from_network.go | 4 ++-- internal/cmd/server/detach_iso.go | 4 ++-- internal/cmd/server/disable_backup.go | 4 ++-- internal/cmd/server/disable_protection.go | 4 ++-- internal/cmd/server/disable_rescue.go | 4 ++-- internal/cmd/server/enable_backup.go | 4 ++-- internal/cmd/server/enable_protection.go | 4 ++-- internal/cmd/server/enable_rescue.go | 4 ++-- internal/cmd/server/ip.go | 4 ++-- internal/cmd/server/list.go | 16 ++++++++-------- internal/cmd/server/metrics.go | 8 ++++---- internal/cmd/server/poweroff.go | 4 ++-- internal/cmd/server/poweron.go | 4 ++-- internal/cmd/server/reboot.go | 4 ++-- internal/cmd/server/rebuild.go | 4 ++-- internal/cmd/server/remove_label.go | 8 ++++---- internal/cmd/server/request_console.go | 4 ++-- internal/cmd/server/reset.go | 4 ++-- internal/cmd/server/reset_password.go | 4 ++-- internal/cmd/server/set_rdns.go | 4 ++-- internal/cmd/server/shutdown.go | 4 ++-- internal/cmd/server/ssh.go | 4 ++-- internal/cmd/server/update.go | 4 ++-- internal/cmd/servertype/describe.go | 12 ++++++------ internal/cmd/servertype/list.go | 14 +++++++------- internal/cmd/sshkey/add_label.go | 8 ++++---- internal/cmd/sshkey/create.go | 8 ++++---- internal/cmd/sshkey/delete.go | 4 ++-- internal/cmd/sshkey/describe.go | 12 ++++++------ internal/cmd/sshkey/list.go | 14 +++++++------- internal/cmd/sshkey/remove_label.go | 8 ++++---- internal/cmd/sshkey/update.go | 4 ++-- internal/cmd/volume/add_label.go | 8 ++++---- internal/cmd/volume/attach.go | 4 ++-- internal/cmd/volume/create.go | 4 ++-- internal/cmd/volume/delete.go | 4 ++-- internal/cmd/volume/describe.go | 12 ++++++------ internal/cmd/volume/detach.go | 4 ++-- internal/cmd/volume/disable_protection.go | 4 ++-- internal/cmd/volume/enable_protection.go | 4 ++-- internal/cmd/volume/list.go | 16 ++++++++-------- internal/cmd/volume/remove_label.go | 8 ++++---- internal/cmd/volume/resize.go | 4 ++-- internal/cmd/volume/update.go | 4 ++-- 130 files changed, 431 insertions(+), 431 deletions(-) diff --git a/internal/cmd/certificate/add_label.go b/internal/cmd/certificate/add_label.go index 009db54f..66cc422a 100644 --- a/internal/cmd/certificate/add_label.go +++ b/internal/cmd/certificate/add_label.go @@ -18,15 +18,15 @@ func newAddLabelCommand(cli *state.State) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.CertificateNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: util.ChainRunE(validateCertificateAddLabel, cli.EnsureToken), - RunE: cli.Wrap(runCertificateAddLabel), + PreRunE: util.ChainRunE(validateAddLabel, cli.EnsureToken), + RunE: cli.Wrap(runAddLabel), } cmd.Flags().BoolP("overwrite", "o", false, "Overwrite label if it exists already") return cmd } -func validateCertificateAddLabel(cmd *cobra.Command, args []string) error { +func validateAddLabel(cmd *cobra.Command, args []string) error { label := util.SplitLabel(args[1]) if len(label) != 2 { return fmt.Errorf("invalid label: %s", args[1]) @@ -34,7 +34,7 @@ func validateCertificateAddLabel(cmd *cobra.Command, args []string) error { return nil } -func runCertificateAddLabel(cli *state.State, cmd *cobra.Command, args []string) error { +func runAddLabel(cli *state.State, cmd *cobra.Command, args []string) error { overwrite, err := cmd.Flags().GetBool("overwrite") if err != nil { return err diff --git a/internal/cmd/certificate/certificate.go b/internal/cmd/certificate/certificate.go index aa104785..0aa720df 100644 --- a/internal/cmd/certificate/certificate.go +++ b/internal/cmd/certificate/certificate.go @@ -14,7 +14,7 @@ func NewCommand(cli *state.State) *cobra.Command { DisableFlagsInUseLine: true, } cmd.AddCommand( - newsListCommand(cli), + newListCommand(cli), newCreateCommand(cli), newUpdateCommand(cli), newAddLabelCommand(cli), diff --git a/internal/cmd/certificate/create.go b/internal/cmd/certificate/create.go index 0a45b2de..e0c619b4 100644 --- a/internal/cmd/certificate/create.go +++ b/internal/cmd/certificate/create.go @@ -17,7 +17,7 @@ func newCreateCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runCertificateCreate), + RunE: cli.Wrap(runCreate), } cmd.Flags().String("name", "", "Certificate name (required)") @@ -32,7 +32,7 @@ func newCreateCommand(cli *state.State) *cobra.Command { return cmd } -func runCertificateCreate(cli *state.State, cmd *cobra.Command, args []string) error { +func runCreate(cli *state.State, cmd *cobra.Command, args []string) error { var ( name string diff --git a/internal/cmd/certificate/delete.go b/internal/cmd/certificate/delete.go index 99c24af0..d832e2d0 100644 --- a/internal/cmd/certificate/delete.go +++ b/internal/cmd/certificate/delete.go @@ -17,11 +17,11 @@ func newDeleteCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runCertificateDelete), + RunE: cli.Wrap(runDelete), } } -func runCertificateDelete(cli *state.State, cmd *cobra.Command, args []string) error { +func runDelete(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] cert, _, err := cli.Client().Certificate.Get(cli.Context, idOrName) if err != nil { diff --git a/internal/cmd/certificate/describe.go b/internal/cmd/certificate/describe.go index 8efea20f..f4d5f10d 100644 --- a/internal/cmd/certificate/describe.go +++ b/internal/cmd/certificate/describe.go @@ -22,13 +22,13 @@ func newDescribeCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runCertificateDescribe), + RunE: cli.Wrap(runDescribe), } output.AddFlag(cmd, output.OptionJSON(), output.OptionFormat()) return cmd } -func runCertificateDescribe(cli *state.State, cmd *cobra.Command, args []string) error { +func runDescribe(cli *state.State, cmd *cobra.Command, args []string) error { outputFlags := output.FlagsForCommand(cmd) idOrName := args[0] @@ -42,15 +42,15 @@ func runCertificateDescribe(cli *state.State, cmd *cobra.Command, args []string) switch { case outputFlags.IsSet("json"): - return certificateDescribeJSON(resp) + return describeJSON(resp) case outputFlags.IsSet("format"): return util.DescribeFormat(cert, outputFlags["format"][0]) default: - return certificateDescribeText(cli, cert) + return describeText(cli, cert) } } -func certificateDescribeJSON(resp *hcloud.Response) error { +func describeJSON(resp *hcloud.Response) error { var data map[string]interface{} if err := json.NewDecoder(resp.Body).Decode(&data); err != nil { return err @@ -64,7 +64,7 @@ func certificateDescribeJSON(resp *hcloud.Response) error { return util.DescribeJSON(data) } -func certificateDescribeText(cli *state.State, cert *hcloud.Certificate) error { +func describeText(cli *state.State, cert *hcloud.Certificate) error { fmt.Printf("ID:\t\t\t%d\n", cert.ID) fmt.Printf("Name:\t\t\t%s\n", cert.Name) fmt.Printf("Fingerprint:\t\t%s\n", cert.Fingerprint) diff --git a/internal/cmd/certificate/list.go b/internal/cmd/certificate/list.go index 023fa3bd..5edfb245 100644 --- a/internal/cmd/certificate/list.go +++ b/internal/cmd/certificate/list.go @@ -11,30 +11,30 @@ import ( "github.com/spf13/cobra" ) -var certificateTableOutput *output.Table +var listTableOutput *output.Table func init() { - certificateTableOutput = describeCertificatesTableOutput() + listTableOutput = describeTableOutput() } -func newsListCommand(cli *state.State) *cobra.Command { +func newListCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "list [FLAGS]", Short: "List Certificates", Long: util.ListLongDescription( "Displays a list of certificates", - certificateTableOutput.Columns(), + listTableOutput.Columns(), ), TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runCertificatesList), + RunE: cli.Wrap(runList), } - output.AddFlag(cmd, output.OptionNoHeader(), output.OptionColumns(certificateTableOutput.Columns()), output.OptionJSON()) + output.AddFlag(cmd, output.OptionNoHeader(), output.OptionColumns(listTableOutput.Columns()), output.OptionJSON()) return cmd } -func runCertificatesList(cli *state.State, cmd *cobra.Command, args []string) error { +func runList(cli *state.State, cmd *cobra.Command, args []string) error { outOpts := output.FlagsForCommand(cmd) labelSelector, _ := cmd.Flags().GetString("selector") @@ -76,7 +76,7 @@ func runCertificatesList(cli *state.State, cmd *cobra.Command, args []string) er if outOpts.IsSet("columns") { cols = outOpts["columns"] } - tw := describeCertificatesTableOutput() + tw := describeTableOutput() if err := tw.ValidateColumns(cols); err != nil { return nil } @@ -89,7 +89,7 @@ func runCertificatesList(cli *state.State, cmd *cobra.Command, args []string) er return tw.Flush() } -func describeCertificatesTableOutput() *output.Table { +func describeTableOutput() *output.Table { return output.NewTable(). AddAllowedFields(hcloud.Certificate{}). RemoveAllowedField("certificate", "chain"). diff --git a/internal/cmd/certificate/remove_label.go b/internal/cmd/certificate/remove_label.go index 527490ac..d9ce97f1 100644 --- a/internal/cmd/certificate/remove_label.go +++ b/internal/cmd/certificate/remove_label.go @@ -27,14 +27,14 @@ func newRemoveLabelCommand(cli *state.State) *cobra.Command { ), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: util.ChainRunE(validateCertificateRemoveLabel, cli.EnsureToken), - RunE: cli.Wrap(runCertificateRemoveLabel), + PreRunE: util.ChainRunE(validateRemoveLabel, cli.EnsureToken), + RunE: cli.Wrap(runRemoveLabel), } cmd.Flags().BoolP("all", "a", false, "Remove all labels") return cmd } -func validateCertificateRemoveLabel(cmd *cobra.Command, args []string) error { +func validateRemoveLabel(cmd *cobra.Command, args []string) error { all, err := cmd.Flags().GetBool("all") if err != nil { return err @@ -48,7 +48,7 @@ func validateCertificateRemoveLabel(cmd *cobra.Command, args []string) error { return nil } -func runCertificateRemoveLabel(cli *state.State, cmd *cobra.Command, args []string) error { +func runRemoveLabel(cli *state.State, cmd *cobra.Command, args []string) error { // We ensured the all flag is a valid boolean in // validateCertificateRemoveLabel. No need to handle the error again here. all, _ := cmd.Flags().GetBool("all") diff --git a/internal/cmd/certificate/update.go b/internal/cmd/certificate/update.go index 81ee518a..be7d727f 100644 --- a/internal/cmd/certificate/update.go +++ b/internal/cmd/certificate/update.go @@ -18,14 +18,14 @@ func newUpdateCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runCertificateUpdate), + RunE: cli.Wrap(runUpdate), } cmd.Flags().String("name", "", "Certificate name") return cmd } -func runCertificateUpdate(cli *state.State, cmd *cobra.Command, args []string) error { +func runUpdate(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] cert, _, err := cli.Client().Certificate.Get(cli.Context, idOrName) if err != nil { diff --git a/internal/cmd/context/active.go b/internal/cmd/context/active.go index a282b386..9505dd1a 100644 --- a/internal/cmd/context/active.go +++ b/internal/cmd/context/active.go @@ -14,12 +14,12 @@ func newActiveCommand(cli *state.State) *cobra.Command { Args: cobra.NoArgs, TraverseChildren: true, DisableFlagsInUseLine: true, - RunE: cli.Wrap(runContextActive), + RunE: cli.Wrap(runActive), } return cmd } -func runContextActive(cli *state.State, cmd *cobra.Command, args []string) error { +func runActive(cli *state.State, cmd *cobra.Command, args []string) error { if cli.Config.ActiveContext != nil { fmt.Println(cli.Config.ActiveContext.Name) } diff --git a/internal/cmd/context/create.go b/internal/cmd/context/create.go index 028e938c..fe6a0de4 100644 --- a/internal/cmd/context/create.go +++ b/internal/cmd/context/create.go @@ -19,12 +19,12 @@ func newCreateCommand(cli *state.State) *cobra.Command { Args: cobra.ExactArgs(1), TraverseChildren: true, DisableFlagsInUseLine: true, - RunE: cli.Wrap(runContextCreate), + RunE: cli.Wrap(runCreate), } return cmd } -func runContextCreate(cli *state.State, cmd *cobra.Command, args []string) error { +func runCreate(cli *state.State, cmd *cobra.Command, args []string) error { if !cli.Terminal() { return errors.New("context create is an interactive command") } diff --git a/internal/cmd/context/delete.go b/internal/cmd/context/delete.go index b2297248..fb2b0b17 100644 --- a/internal/cmd/context/delete.go +++ b/internal/cmd/context/delete.go @@ -16,12 +16,12 @@ func newDeleteCommand(cli *state.State) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.Config.ContextNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - RunE: cli.Wrap(runContextDelete), + RunE: cli.Wrap(runDelete), } return cmd } -func runContextDelete(cli *state.State, cmd *cobra.Command, args []string) error { +func runDelete(cli *state.State, cmd *cobra.Command, args []string) error { name := args[0] context := cli.Config.ContextByName(name) if context == nil { diff --git a/internal/cmd/context/list.go b/internal/cmd/context/list.go index cf5d4d70..a40d2122 100644 --- a/internal/cmd/context/list.go +++ b/internal/cmd/context/list.go @@ -7,7 +7,7 @@ import ( "github.com/spf13/cobra" ) -var contextListTableOutput *output.Table +var listTableOutput *output.Table type ContextPresentation struct { Name string @@ -16,7 +16,7 @@ type ContextPresentation struct { } func init() { - contextListTableOutput = output.NewTable(). + listTableOutput = output.NewTable(). AddAllowedFields(ContextPresentation{}). RemoveAllowedField("token") } @@ -27,18 +27,18 @@ func newListCommand(cli *state.State) *cobra.Command { Short: "List contexts", Long: util.ListLongDescription( "Displays a list of contexts.", - contextListTableOutput.Columns(), + listTableOutput.Columns(), ), Args: cobra.NoArgs, TraverseChildren: true, DisableFlagsInUseLine: true, - RunE: cli.Wrap(runContextList), + RunE: cli.Wrap(runList), } - output.AddFlag(cmd, output.OptionNoHeader(), output.OptionColumns(contextListTableOutput.Columns())) + output.AddFlag(cmd, output.OptionNoHeader(), output.OptionColumns(listTableOutput.Columns())) return cmd } -func runContextList(cli *state.State, cmd *cobra.Command, args []string) error { +func runList(cli *state.State, cmd *cobra.Command, args []string) error { outOpts := output.FlagsForCommand(cmd) cols := []string{"active", "name"} @@ -46,7 +46,7 @@ func runContextList(cli *state.State, cmd *cobra.Command, args []string) error { cols = outOpts["columns"] } - tw := contextListTableOutput + tw := listTableOutput if err := tw.ValidateColumns(cols); err != nil { return err } diff --git a/internal/cmd/context/use.go b/internal/cmd/context/use.go index 5e62707e..73b1e56a 100644 --- a/internal/cmd/context/use.go +++ b/internal/cmd/context/use.go @@ -16,12 +16,12 @@ func newUseCommand(cli *state.State) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.Config.ContextNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - RunE: cli.Wrap(runContextUse), + RunE: cli.Wrap(runUse), } return cmd } -func runContextUse(cli *state.State, cmd *cobra.Command, args []string) error { +func runUse(cli *state.State, cmd *cobra.Command, args []string) error { name := args[0] context := cli.Config.ContextByName(name) if context == nil { diff --git a/internal/cmd/datacenter/describe.go b/internal/cmd/datacenter/describe.go index 8b6be5d7..d0aa3201 100644 --- a/internal/cmd/datacenter/describe.go +++ b/internal/cmd/datacenter/describe.go @@ -21,13 +21,13 @@ func newDescribeCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runDatacenterDescribe), + RunE: cli.Wrap(runDescribe), } output.AddFlag(cmd, output.OptionJSON(), output.OptionFormat()) return cmd } -func runDatacenterDescribe(cli *state.State, cmd *cobra.Command, args []string) error { +func runDescribe(cli *state.State, cmd *cobra.Command, args []string) error { outputFlags := output.FlagsForCommand(cmd) idOrName := args[0] @@ -41,15 +41,15 @@ func runDatacenterDescribe(cli *state.State, cmd *cobra.Command, args []string) switch { case outputFlags.IsSet("json"): - return datacenterDescribeJSON(resp) + return describeJSON(resp) case outputFlags.IsSet("format"): return util.DescribeFormat(datacenter, outputFlags["format"][0]) default: - return datacenterDescribeText(cli, datacenter) + return describeText(cli, datacenter) } } -func datacenterDescribeText(cli *state.State, datacenter *hcloud.Datacenter) error { +func describeText(cli *state.State, datacenter *hcloud.Datacenter) error { fmt.Printf("ID:\t\t%d\n", datacenter.ID) fmt.Printf("Name:\t\t%s\n", datacenter.Name) fmt.Printf("Description:\t%s\n", datacenter.Description) @@ -102,7 +102,7 @@ func datacenterDescribeText(cli *state.State, datacenter *hcloud.Datacenter) err return nil } -func datacenterDescribeJSON(resp *hcloud.Response) error { +func describeJSON(resp *hcloud.Response) error { var data map[string]interface{} if err := json.NewDecoder(resp.Body).Decode(&data); err != nil { return err diff --git a/internal/cmd/datacenter/list.go b/internal/cmd/datacenter/list.go index 0c8442d0..6a17a07c 100644 --- a/internal/cmd/datacenter/list.go +++ b/internal/cmd/datacenter/list.go @@ -9,10 +9,10 @@ import ( "github.com/spf13/cobra" ) -var datacenterListTableOutput *output.Table +var listTableOutput *output.Table func init() { - datacenterListTableOutput = output.NewTable(). + listTableOutput = output.NewTable(). AddAllowedFields(hcloud.Datacenter{}). AddFieldFn("location", output.FieldFn(func(obj interface{}) string { datacenter := obj.(*hcloud.Datacenter) @@ -26,18 +26,18 @@ func newListCommand(cli *state.State) *cobra.Command { Short: "List datacenters", Long: util.ListLongDescription( "Displays a list of datacenters.", - datacenterListTableOutput.Columns(), + listTableOutput.Columns(), ), TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runDatacenterList), + RunE: cli.Wrap(runList), } - output.AddFlag(cmd, output.OptionNoHeader(), output.OptionColumns(datacenterListTableOutput.Columns()), output.OptionJSON()) + output.AddFlag(cmd, output.OptionNoHeader(), output.OptionColumns(listTableOutput.Columns()), output.OptionJSON()) return cmd } -func runDatacenterList(cli *state.State, cmd *cobra.Command, args []string) error { +func runList(cli *state.State, cmd *cobra.Command, args []string) error { outOpts := output.FlagsForCommand(cmd) datacenters, err := cli.Client().Datacenter.All(cli.Context) @@ -59,7 +59,7 @@ func runDatacenterList(cli *state.State, cmd *cobra.Command, args []string) erro cols = outOpts["columns"] } - tw := datacenterListTableOutput + tw := listTableOutput if err = tw.ValidateColumns(cols); err != nil { return err } diff --git a/internal/cmd/floatingip/add_label.go b/internal/cmd/floatingip/add_label.go index 091aa7c6..612fb363 100644 --- a/internal/cmd/floatingip/add_label.go +++ b/internal/cmd/floatingip/add_label.go @@ -18,15 +18,15 @@ func newAddLabelCommand(cli *state.State) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.FloatingIPNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: util.ChainRunE(validateFloatingIPAddLabel, cli.EnsureToken), - RunE: cli.Wrap(runFloatingIPAddLabel), + PreRunE: util.ChainRunE(validateAddLabel, cli.EnsureToken), + RunE: cli.Wrap(runAddLabel), } cmd.Flags().BoolP("overwrite", "o", false, "Overwrite label if it exists already") return cmd } -func validateFloatingIPAddLabel(cmd *cobra.Command, args []string) error { +func validateAddLabel(cmd *cobra.Command, args []string) error { label := util.SplitLabel(args[1]) if len(label) != 2 { return fmt.Errorf("invalid label: %s", args[1]) @@ -35,7 +35,7 @@ func validateFloatingIPAddLabel(cmd *cobra.Command, args []string) error { return nil } -func runFloatingIPAddLabel(cli *state.State, cmd *cobra.Command, args []string) error { +func runAddLabel(cli *state.State, cmd *cobra.Command, args []string) error { overwrite, _ := cmd.Flags().GetBool("overwrite") idOrName := args[0] diff --git a/internal/cmd/floatingip/assign.go b/internal/cmd/floatingip/assign.go index cd05bef8..de83f7e1 100644 --- a/internal/cmd/floatingip/assign.go +++ b/internal/cmd/floatingip/assign.go @@ -20,13 +20,13 @@ func newAssignCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runFloatingIPAssign), + RunE: cli.Wrap(runAssign), } cmd.MarkFlagRequired("server") return cmd } -func runFloatingIPAssign(cli *state.State, cmd *cobra.Command, args []string) error { +func runAssign(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] floatingIP, _, err := cli.Client().FloatingIP.Get(cli.Context, idOrName) if err != nil { diff --git a/internal/cmd/floatingip/create.go b/internal/cmd/floatingip/create.go index 50bf1e9e..c3ef90f9 100644 --- a/internal/cmd/floatingip/create.go +++ b/internal/cmd/floatingip/create.go @@ -18,8 +18,8 @@ func newCreateCommand(cli *state.State) *cobra.Command { Args: cobra.NoArgs, TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: util.ChainRunE(validateFloatingIPCreate, cli.EnsureToken), - RunE: cli.Wrap(runFloatingIPCreate), + PreRunE: util.ChainRunE(validateCreate, cli.EnsureToken), + RunE: cli.Wrap(runCreate), } cmd.Flags().String("type", "", "Type (ipv4 or ipv6) (required)") cmd.RegisterFlagCompletionFunc("type", cmpl.SuggestCandidates("ipv4", "ipv6")) @@ -40,7 +40,7 @@ func newCreateCommand(cli *state.State) *cobra.Command { return cmd } -func validateFloatingIPCreate(cmd *cobra.Command, args []string) error { +func validateCreate(cmd *cobra.Command, args []string) error { typ, _ := cmd.Flags().GetString("type") if typ == "" { return errors.New("type is required") @@ -55,7 +55,7 @@ func validateFloatingIPCreate(cmd *cobra.Command, args []string) error { return nil } -func runFloatingIPCreate(cli *state.State, cmd *cobra.Command, args []string) error { +func runCreate(cli *state.State, cmd *cobra.Command, args []string) error { typ, _ := cmd.Flags().GetString("type") name, _ := cmd.Flags().GetString("name") description, _ := cmd.Flags().GetString("description") diff --git a/internal/cmd/floatingip/delete.go b/internal/cmd/floatingip/delete.go index aeca87ee..50f704fe 100644 --- a/internal/cmd/floatingip/delete.go +++ b/internal/cmd/floatingip/delete.go @@ -17,12 +17,12 @@ func newDeleteCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runFloatingIPDelete), + RunE: cli.Wrap(runDelete), } return cmd } -func runFloatingIPDelete(cli *state.State, cmd *cobra.Command, args []string) error { +func runDelete(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] floatingIP, _, err := cli.Client().FloatingIP.Get(cli.Context, idOrName) if err != nil { diff --git a/internal/cmd/floatingip/describe.go b/internal/cmd/floatingip/describe.go index 92ae2e2e..adb0236d 100644 --- a/internal/cmd/floatingip/describe.go +++ b/internal/cmd/floatingip/describe.go @@ -22,13 +22,13 @@ func newDescribeCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runFloatingIPDescribe), + RunE: cli.Wrap(runDescribe), } output.AddFlag(cmd, output.OptionJSON(), output.OptionFormat()) return cmd } -func runFloatingIPDescribe(cli *state.State, cmd *cobra.Command, args []string) error { +func runDescribe(cli *state.State, cmd *cobra.Command, args []string) error { outputFlags := output.FlagsForCommand(cmd) idOrName := args[0] @@ -42,15 +42,15 @@ func runFloatingIPDescribe(cli *state.State, cmd *cobra.Command, args []string) switch { case outputFlags.IsSet("json"): - return floatingIPDescribeJSON(resp) + return describeJSON(resp) case outputFlags.IsSet("format"): return util.DescribeFormat(floatingIP, outputFlags["format"][0]) default: - return floatingIPDescribeText(cli, floatingIP) + return describeText(cli, floatingIP) } } -func floatingIPDescribeText(cli *state.State, floatingIP *hcloud.FloatingIP) error { +func describeText(cli *state.State, floatingIP *hcloud.FloatingIP) error { fmt.Printf("ID:\t\t%d\n", floatingIP.ID) fmt.Printf("Type:\t\t%s\n", floatingIP.Type) fmt.Printf("Name:\t\t%s\n", floatingIP.Name) @@ -100,7 +100,7 @@ func floatingIPDescribeText(cli *state.State, floatingIP *hcloud.FloatingIP) err return nil } -func floatingIPDescribeJSON(resp *hcloud.Response) error { +func describeJSON(resp *hcloud.Response) error { var data map[string]interface{} if err := json.NewDecoder(resp.Body).Decode(&data); err != nil { return err diff --git a/internal/cmd/floatingip/disable_protection.go b/internal/cmd/floatingip/disable_protection.go index 8e1d3e0d..b01ba4ea 100644 --- a/internal/cmd/floatingip/disable_protection.go +++ b/internal/cmd/floatingip/disable_protection.go @@ -22,12 +22,12 @@ func newDisableProtectionCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runFloatingIPDisableProtection), + RunE: cli.Wrap(runDisableProtection), } return cmd } -func runFloatingIPDisableProtection(cli *state.State, cmd *cobra.Command, args []string) error { +func runDisableProtection(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] floatingIP, _, err := cli.Client().FloatingIP.Get(cli.Context, idOrName) if err != nil { diff --git a/internal/cmd/floatingip/enable_protection.go b/internal/cmd/floatingip/enable_protection.go index 2b070f3a..fa362005 100644 --- a/internal/cmd/floatingip/enable_protection.go +++ b/internal/cmd/floatingip/enable_protection.go @@ -22,12 +22,12 @@ func newEnableProtectionCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runFloatingIPEnableProtection), + RunE: cli.Wrap(runEnableProtection), } return cmd } -func runFloatingIPEnableProtection(cli *state.State, cmd *cobra.Command, args []string) error { +func runEnableProtection(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] floatingIP, _, err := cli.Client().FloatingIP.Get(cli.Context, idOrName) if err != nil { diff --git a/internal/cmd/floatingip/list.go b/internal/cmd/floatingip/list.go index 492950b6..5d762d54 100644 --- a/internal/cmd/floatingip/list.go +++ b/internal/cmd/floatingip/list.go @@ -13,10 +13,10 @@ import ( "github.com/spf13/cobra" ) -var floatingIPListTableOutput *output.Table +var listTableOutput *output.Table func init() { - floatingIPListTableOutput = describeFloatingIPListTableOutput(nil) + listTableOutput = describeListTableOutput(nil) } func newListCommand(cli *state.State) *cobra.Command { @@ -25,19 +25,19 @@ func newListCommand(cli *state.State) *cobra.Command { Short: "List Floating IPs", Long: util.ListLongDescription( "Displays a list of Floating IPs.", - floatingIPListTableOutput.Columns(), + listTableOutput.Columns(), ), TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runFloatingIPList), + RunE: cli.Wrap(runList), } - output.AddFlag(cmd, output.OptionNoHeader(), output.OptionColumns(floatingIPListTableOutput.Columns()), output.OptionJSON()) + output.AddFlag(cmd, output.OptionNoHeader(), output.OptionColumns(listTableOutput.Columns()), output.OptionJSON()) cmd.Flags().StringP("selector", "l", "", "Selector to filter by labels") return cmd } -func runFloatingIPList(cli *state.State, cmd *cobra.Command, args []string) error { +func runList(cli *state.State, cmd *cobra.Command, args []string) error { outOpts := output.FlagsForCommand(cmd) labelSelector, _ := cmd.Flags().GetString("selector") @@ -86,7 +86,7 @@ func runFloatingIPList(cli *state.State, cmd *cobra.Command, args []string) erro cols = outOpts["columns"] } - tw := describeFloatingIPListTableOutput(cli) + tw := describeListTableOutput(cli) if err = tw.ValidateColumns(cols); err != nil { return err } @@ -101,7 +101,7 @@ func runFloatingIPList(cli *state.State, cmd *cobra.Command, args []string) erro return nil } -func describeFloatingIPListTableOutput(cli *state.State) *output.Table { +func describeListTableOutput(cli *state.State) *output.Table { return output.NewTable(). AddAllowedFields(hcloud.FloatingIP{}). AddFieldFn("dns", output.FieldFn(func(obj interface{}) string { diff --git a/internal/cmd/floatingip/remove_label.go b/internal/cmd/floatingip/remove_label.go index f8055a7b..7809e22f 100644 --- a/internal/cmd/floatingip/remove_label.go +++ b/internal/cmd/floatingip/remove_label.go @@ -26,15 +26,15 @@ func newRemoveLabelCommand(cli *state.State) *cobra.Command { })), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: util.ChainRunE(validateFloatingIPRemoveLabel, cli.EnsureToken), - RunE: cli.Wrap(runFloatingIPRemoveLabel), + PreRunE: util.ChainRunE(validateRemoveLabel, cli.EnsureToken), + RunE: cli.Wrap(runRemoveLabel), } cmd.Flags().BoolP("all", "a", false, "Remove all labels") return cmd } -func validateFloatingIPRemoveLabel(cmd *cobra.Command, args []string) error { +func validateRemoveLabel(cmd *cobra.Command, args []string) error { all, _ := cmd.Flags().GetBool("all") if all && len(args) == 2 { @@ -47,7 +47,7 @@ func validateFloatingIPRemoveLabel(cmd *cobra.Command, args []string) error { return nil } -func runFloatingIPRemoveLabel(cli *state.State, cmd *cobra.Command, args []string) error { +func runRemoveLabel(cli *state.State, cmd *cobra.Command, args []string) error { all, _ := cmd.Flags().GetBool("all") idOrName := args[0] diff --git a/internal/cmd/floatingip/set_rdns.go b/internal/cmd/floatingip/set_rdns.go index 4fcb172a..69a6fd5a 100644 --- a/internal/cmd/floatingip/set_rdns.go +++ b/internal/cmd/floatingip/set_rdns.go @@ -18,7 +18,7 @@ func newSetRDNSCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runFloatingIPSetRDNS), + RunE: cli.Wrap(runSetRDNS), } cmd.Flags().StringP("hostname", "r", "", "Hostname to set as a reverse DNS PTR entry (required)") @@ -28,7 +28,7 @@ func newSetRDNSCommand(cli *state.State) *cobra.Command { return cmd } -func runFloatingIPSetRDNS(cli *state.State, cmd *cobra.Command, args []string) error { +func runSetRDNS(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] floatingIP, _, err := cli.Client().FloatingIP.Get(cli.Context, idOrName) if err != nil { diff --git a/internal/cmd/floatingip/unassign.go b/internal/cmd/floatingip/unassign.go index 213d0510..b3e2b0b2 100644 --- a/internal/cmd/floatingip/unassign.go +++ b/internal/cmd/floatingip/unassign.go @@ -17,12 +17,12 @@ func newUnassignCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runFloatingIPUnassign), + RunE: cli.Wrap(runUnassign), } return cmd } -func runFloatingIPUnassign(cli *state.State, cmd *cobra.Command, args []string) error { +func runUnassign(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] floatingIP, _, err := cli.Client().FloatingIP.Get(cli.Context, idOrName) if err != nil { diff --git a/internal/cmd/floatingip/update.go b/internal/cmd/floatingip/update.go index eaf71be8..3649c77f 100644 --- a/internal/cmd/floatingip/update.go +++ b/internal/cmd/floatingip/update.go @@ -18,7 +18,7 @@ func newUpdateCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runFloatingIPUpdate), + RunE: cli.Wrap(runUpdate), } cmd.Flags().String("description", "", "Floating IP description") @@ -27,7 +27,7 @@ func newUpdateCommand(cli *state.State) *cobra.Command { return cmd } -func runFloatingIPUpdate(cli *state.State, cmd *cobra.Command, args []string) error { +func runUpdate(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] floatingIP, _, err := cli.Client().FloatingIP.Get(cli.Context, idOrName) if err != nil { diff --git a/internal/cmd/image/add_label.go b/internal/cmd/image/add_label.go index 6e8bef54..3787a12e 100644 --- a/internal/cmd/image/add_label.go +++ b/internal/cmd/image/add_label.go @@ -18,15 +18,15 @@ func newAddLabelCommand(cli *state.State) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.ImageNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: util.ChainRunE(validateImageAddLabel, cli.EnsureToken), - RunE: cli.Wrap(runImageAddLabel), + PreRunE: util.ChainRunE(validateAddLabel, cli.EnsureToken), + RunE: cli.Wrap(runAddLabel), } cmd.Flags().BoolP("overwrite", "o", false, "Overwrite label if it exists already") return cmd } -func validateImageAddLabel(cmd *cobra.Command, args []string) error { +func validateAddLabel(cmd *cobra.Command, args []string) error { label := util.SplitLabel(args[1]) if len(label) != 2 { return fmt.Errorf("invalid label: %s", args[1]) @@ -35,7 +35,7 @@ func validateImageAddLabel(cmd *cobra.Command, args []string) error { return nil } -func runImageAddLabel(cli *state.State, cmd *cobra.Command, args []string) error { +func runAddLabel(cli *state.State, cmd *cobra.Command, args []string) error { overwrite, _ := cmd.Flags().GetBool("overwrite") idOrName := args[0] image, _, err := cli.Client().Image.Get(cli.Context, idOrName) diff --git a/internal/cmd/image/delete.go b/internal/cmd/image/delete.go index d5f0ae5e..f36863f1 100644 --- a/internal/cmd/image/delete.go +++ b/internal/cmd/image/delete.go @@ -17,12 +17,12 @@ func newDeleteCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runImageDelete), + RunE: cli.Wrap(runDelete), } return cmd } -func runImageDelete(cli *state.State, cmd *cobra.Command, args []string) error { +func runDelete(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] image, _, err := cli.Client().Image.Get(cli.Context, idOrName) if err != nil { diff --git a/internal/cmd/image/describe.go b/internal/cmd/image/describe.go index bd6f0461..6a151d4a 100644 --- a/internal/cmd/image/describe.go +++ b/internal/cmd/image/describe.go @@ -22,13 +22,13 @@ func newDescribeCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runImageDescribe), + RunE: cli.Wrap(runDescribe), } output.AddFlag(cmd, output.OptionJSON(), output.OptionFormat()) return cmd } -func runImageDescribe(cli *state.State, cmd *cobra.Command, args []string) error { +func runDescribe(cli *state.State, cmd *cobra.Command, args []string) error { outputFlags := output.FlagsForCommand(cmd) idOrName := args[0] @@ -42,15 +42,15 @@ func runImageDescribe(cli *state.State, cmd *cobra.Command, args []string) error switch { case outputFlags.IsSet("json"): - return imageDescribeJSON(resp) + return describeJSON(resp) case outputFlags.IsSet("format"): return util.DescribeFormat(image, outputFlags["format"][0]) default: - return imageDescribeText(cli, image) + return describeText(cli, image) } } -func imageDescribeText(cli *state.State, image *hcloud.Image) error { +func describeText(cli *state.State, image *hcloud.Image) error { fmt.Printf("ID:\t\t%d\n", image.ID) fmt.Printf("Type:\t\t%s\n", image.Type) fmt.Printf("Status:\t\t%s\n", image.Status) @@ -87,7 +87,7 @@ func imageDescribeText(cli *state.State, image *hcloud.Image) error { return nil } -func imageDescribeJSON(resp *hcloud.Response) error { +func describeJSON(resp *hcloud.Response) error { var data map[string]interface{} if err := json.NewDecoder(resp.Body).Decode(&data); err != nil { return err diff --git a/internal/cmd/image/disable_protection.go b/internal/cmd/image/disable_protection.go index c7000757..361563df 100644 --- a/internal/cmd/image/disable_protection.go +++ b/internal/cmd/image/disable_protection.go @@ -24,12 +24,12 @@ func newDisableProtectionCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runImageDisableProtection), + RunE: cli.Wrap(runDisableProtection), } return cmd } -func runImageDisableProtection(cli *state.State, cmd *cobra.Command, args []string) error { +func runDisableProtection(cli *state.State, cmd *cobra.Command, args []string) error { imageID, err := strconv.Atoi(args[0]) if err != nil { return errors.New("invalid image ID") diff --git a/internal/cmd/image/enable_protection.go b/internal/cmd/image/enable_protection.go index 23601e8d..8ade78e9 100644 --- a/internal/cmd/image/enable_protection.go +++ b/internal/cmd/image/enable_protection.go @@ -24,12 +24,12 @@ func newEnableProtectionCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runImageEnableProtection), + RunE: cli.Wrap(runEnableProtection), } return cmd } -func runImageEnableProtection(cli *state.State, cmd *cobra.Command, args []string) error { +func runEnableProtection(cli *state.State, cmd *cobra.Command, args []string) error { imageID, err := strconv.Atoi(args[0]) if err != nil { return errors.New("invalid image ID") diff --git a/internal/cmd/image/list.go b/internal/cmd/image/list.go index be611b3d..8bd9033c 100644 --- a/internal/cmd/image/list.go +++ b/internal/cmd/image/list.go @@ -14,11 +14,11 @@ import ( "github.com/spf13/cobra" ) -var imageListTableOutput *output.Table +var listTableOutput *output.Table var typeFilter string func init() { - imageListTableOutput = describeImageListTableOutput(nil) + listTableOutput = describeListTableOutput(nil) } func newListCommand(cli *state.State) *cobra.Command { @@ -27,20 +27,20 @@ func newListCommand(cli *state.State) *cobra.Command { Short: "List images", Long: util.ListLongDescription( "Displays a list of images.", - imageListTableOutput.Columns(), + listTableOutput.Columns(), ), TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runImageList), + RunE: cli.Wrap(runList), } - output.AddFlag(cmd, output.OptionNoHeader(), output.OptionColumns(imageListTableOutput.Columns()), output.OptionJSON()) + output.AddFlag(cmd, output.OptionNoHeader(), output.OptionColumns(listTableOutput.Columns()), output.OptionJSON()) cmd.Flags().StringVarP(&typeFilter, "type", "t", "", "Only show images of given type") cmd.Flags().StringP("selector", "l", "", "Selector to filter by labels") return cmd } -func runImageList(cli *state.State, cmd *cobra.Command, args []string) error { +func runList(cli *state.State, cmd *cobra.Command, args []string) error { outOpts := output.FlagsForCommand(cmd) labelSelector, _ := cmd.Flags().GetString("selector") @@ -78,7 +78,7 @@ func runImageList(cli *state.State, cmd *cobra.Command, args []string) error { cols = outOpts["columns"] } - tw := describeImageListTableOutput(cli) + tw := describeListTableOutput(cli) if err = tw.ValidateColumns(cols); err != nil { return err } @@ -94,7 +94,7 @@ func runImageList(cli *state.State, cmd *cobra.Command, args []string) error { return nil } -func describeImageListTableOutput(cli *state.State) *output.Table { +func describeListTableOutput(cli *state.State) *output.Table { return output.NewTable(). AddAllowedFields(hcloud.Image{}). AddFieldAlias("imagesize", "image size"). diff --git a/internal/cmd/image/remove_label.go b/internal/cmd/image/remove_label.go index e2c33d2c..0c17d26f 100644 --- a/internal/cmd/image/remove_label.go +++ b/internal/cmd/image/remove_label.go @@ -28,7 +28,7 @@ func newRemoveLabelCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: util.ChainRunE(validateImageRemoveLabel, cli.EnsureToken), - RunE: cli.Wrap(runImageRemoveLabel), + RunE: cli.Wrap(runRemoveLabel), } cmd.Flags().BoolP("all", "a", false, "Remove all labels") @@ -48,7 +48,7 @@ func validateImageRemoveLabel(cmd *cobra.Command, args []string) error { return nil } -func runImageRemoveLabel(cli *state.State, cmd *cobra.Command, args []string) error { +func runRemoveLabel(cli *state.State, cmd *cobra.Command, args []string) error { all, _ := cmd.Flags().GetBool("all") idOrName := args[0] image, _, err := cli.Client().Image.Get(cli.Context, idOrName) diff --git a/internal/cmd/image/update.go b/internal/cmd/image/update.go index 419bf1ca..82c2376e 100644 --- a/internal/cmd/image/update.go +++ b/internal/cmd/image/update.go @@ -18,7 +18,7 @@ func newUpdateCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runImageUpdate), + RunE: cli.Wrap(runUpdate), } cmd.Flags().String("description", "", "Image description") @@ -29,7 +29,7 @@ func newUpdateCommand(cli *state.State) *cobra.Command { return cmd } -func runImageUpdate(cli *state.State, cmd *cobra.Command, args []string) error { +func runUpdate(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] image, _, err := cli.Client().Image.Get(cli.Context, idOrName) if err != nil { diff --git a/internal/cmd/iso/describe.go b/internal/cmd/iso/describe.go index 551973bb..7c4e738d 100644 --- a/internal/cmd/iso/describe.go +++ b/internal/cmd/iso/describe.go @@ -21,13 +21,13 @@ func newDescribeCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runISODescribe), + RunE: cli.Wrap(runDescribe), } output.AddFlag(cmd, output.OptionJSON(), output.OptionFormat()) return cmd } -func runISODescribe(cli *state.State, cmd *cobra.Command, args []string) error { +func runDescribe(cli *state.State, cmd *cobra.Command, args []string) error { outputFlags := output.FlagsForCommand(cmd) idOrName := args[0] @@ -41,15 +41,15 @@ func runISODescribe(cli *state.State, cmd *cobra.Command, args []string) error { switch { case outputFlags.IsSet("json"): - return isoDescribeJSON(resp) + return describeJSON(resp) case outputFlags.IsSet("format"): return util.DescribeFormat(iso, outputFlags["format"][0]) default: - return isoDescribeText(iso) + return describeText(iso) } } -func isoDescribeText(iso *hcloud.ISO) error { +func describeText(iso *hcloud.ISO) error { fmt.Printf("ID:\t\t%d\n", iso.ID) fmt.Printf("Name:\t\t%s\n", iso.Name) fmt.Printf("Description:\t%s\n", iso.Description) @@ -57,7 +57,7 @@ func isoDescribeText(iso *hcloud.ISO) error { return nil } -func isoDescribeJSON(resp *hcloud.Response) error { +func describeJSON(resp *hcloud.Response) error { var data map[string]interface{} if err := json.NewDecoder(resp.Body).Decode(&data); err != nil { return err diff --git a/internal/cmd/iso/list.go b/internal/cmd/iso/list.go index ef3bd1d3..f47c94ba 100644 --- a/internal/cmd/iso/list.go +++ b/internal/cmd/iso/list.go @@ -9,10 +9,10 @@ import ( "github.com/spf13/cobra" ) -var isoListTableOutput *output.Table +var listTableOutput *output.Table func init() { - isoListTableOutput = output.NewTable(). + listTableOutput = output.NewTable(). AddAllowedFields(hcloud.ISO{}) } @@ -22,18 +22,18 @@ func newListCommand(cli *state.State) *cobra.Command { Short: "List ISOs", Long: util.ListLongDescription( "Displays a list of ISOs.", - isoListTableOutput.Columns(), + listTableOutput.Columns(), ), TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runISOList), + RunE: cli.Wrap(runList), } - output.AddFlag(cmd, output.OptionNoHeader(), output.OptionColumns(isoListTableOutput.Columns()), output.OptionJSON()) + output.AddFlag(cmd, output.OptionNoHeader(), output.OptionColumns(listTableOutput.Columns()), output.OptionJSON()) return cmd } -func runISOList(cli *state.State, cmd *cobra.Command, args []string) error { +func runList(cli *state.State, cmd *cobra.Command, args []string) error { outOpts := output.FlagsForCommand(cmd) isos, err := cli.Client().ISO.All(cli.Context) @@ -54,7 +54,7 @@ func runISOList(cli *state.State, cmd *cobra.Command, args []string) error { cols = outOpts["columns"] } - tw := isoListTableOutput + tw := listTableOutput if err = tw.ValidateColumns(cols); err != nil { return err } diff --git a/internal/cmd/loadbalancer/add_label.go b/internal/cmd/loadbalancer/add_label.go index e46e5c98..61db5a53 100644 --- a/internal/cmd/loadbalancer/add_label.go +++ b/internal/cmd/loadbalancer/add_label.go @@ -18,15 +18,15 @@ func newAddLabelCommand(cli *state.State) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.LoadBalancerNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: util.ChainRunE(validateLoadBalancerAddLabel, cli.EnsureToken), - RunE: cli.Wrap(runLoadBalancerAddLabel), + PreRunE: util.ChainRunE(validateAddLabel, cli.EnsureToken), + RunE: cli.Wrap(runAddLabel), } cmd.Flags().BoolP("overwrite", "o", false, "Overwrite label if it exists already") return cmd } -func validateLoadBalancerAddLabel(cmd *cobra.Command, args []string) error { +func validateAddLabel(cmd *cobra.Command, args []string) error { label := util.SplitLabel(args[1]) if len(label) != 2 { return fmt.Errorf("invalid label: %s", args[1]) @@ -35,7 +35,7 @@ func validateLoadBalancerAddLabel(cmd *cobra.Command, args []string) error { return nil } -func runLoadBalancerAddLabel(cli *state.State, cmd *cobra.Command, args []string) error { +func runAddLabel(cli *state.State, cmd *cobra.Command, args []string) error { overwrite, _ := cmd.Flags().GetBool("overwrite") idOrName := args[0] loadBalancer, _, err := cli.Client().LoadBalancer.Get(cli.Context, idOrName) diff --git a/internal/cmd/loadbalancer/add_service.go b/internal/cmd/loadbalancer/add_service.go index e49f472f..7591ca67 100644 --- a/internal/cmd/loadbalancer/add_service.go +++ b/internal/cmd/loadbalancer/add_service.go @@ -18,8 +18,8 @@ func newAddServiceCommand(cli *state.State) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.LoadBalancerNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: util.ChainRunE(validateLoadBalancerAddService, cli.EnsureToken), - RunE: cli.Wrap(runLoadBalancerAddService), + PreRunE: util.ChainRunE(validateAddService, cli.EnsureToken), + RunE: cli.Wrap(runAddService), } cmd.Flags().String("protocol", "", "Protocol of the service (required)") cmd.MarkFlagRequired("protocol") @@ -37,7 +37,7 @@ func newAddServiceCommand(cli *state.State) *cobra.Command { return cmd } -func validateLoadBalancerAddService(cmd *cobra.Command, args []string) error { +func validateAddService(cmd *cobra.Command, args []string) error { protocol, _ := cmd.Flags().GetString("protocol") listenPort, _ := cmd.Flags().GetInt("listen-port") destinationPort, _ := cmd.Flags().GetInt("destination-port") @@ -76,7 +76,7 @@ func validateLoadBalancerAddService(cmd *cobra.Command, args []string) error { return nil } -func runLoadBalancerAddService(cli *state.State, cmd *cobra.Command, args []string) error { +func runAddService(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] protocol, _ := cmd.Flags().GetString("protocol") listenPort, _ := cmd.Flags().GetInt("listen-port") diff --git a/internal/cmd/loadbalancer/add_target.go b/internal/cmd/loadbalancer/add_target.go index 2507d253..f131ea6a 100644 --- a/internal/cmd/loadbalancer/add_target.go +++ b/internal/cmd/loadbalancer/add_target.go @@ -20,7 +20,7 @@ func newAddTargetCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runLoadBalancerAddTarget), + RunE: cli.Wrap(runAddTarget), } cmd.Flags().String("server", "", "Name or ID of the server") @@ -33,7 +33,7 @@ func newAddTargetCommand(cli *state.State) *cobra.Command { return cmd } -func runLoadBalancerAddTarget(cli *state.State, cmd *cobra.Command, args []string) error { +func runAddTarget(cli *state.State, cmd *cobra.Command, args []string) error { var ( action *hcloud.Action loadBalancer *hcloud.LoadBalancer diff --git a/internal/cmd/loadbalancer/attach_to_network.go b/internal/cmd/loadbalancer/attach_to_network.go index 26faa267..88cae180 100644 --- a/internal/cmd/loadbalancer/attach_to_network.go +++ b/internal/cmd/loadbalancer/attach_to_network.go @@ -18,7 +18,7 @@ func newAttachToNetworkCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runLoadBalancerAttachToNetwork), + RunE: cli.Wrap(runAttachToNetwork), } cmd.Flags().StringP("network", "n", "", "Network (ID or name) (required)") @@ -30,7 +30,7 @@ func newAttachToNetworkCommand(cli *state.State) *cobra.Command { return cmd } -func runLoadBalancerAttachToNetwork(cli *state.State, cmd *cobra.Command, args []string) error { +func runAttachToNetwork(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] loadBalancer, _, err := cli.Client().LoadBalancer.Get(cli.Context, idOrName) if err != nil { diff --git a/internal/cmd/loadbalancer/change_algorithm.go b/internal/cmd/loadbalancer/change_algorithm.go index fd0f37cf..17a4df8a 100644 --- a/internal/cmd/loadbalancer/change_algorithm.go +++ b/internal/cmd/loadbalancer/change_algorithm.go @@ -18,7 +18,7 @@ func newChangeAlgorithmCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runLoadBalancerChangeAlgorithm), + RunE: cli.Wrap(runChangeAlgorithm), } cmd.Flags().String("algorithm-type", "", "The new algorithm of the Load Balancer") @@ -31,7 +31,7 @@ func newChangeAlgorithmCommand(cli *state.State) *cobra.Command { return cmd } -func runLoadBalancerChangeAlgorithm(cli *state.State, cmd *cobra.Command, args []string) error { +func runChangeAlgorithm(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] algorithm, _ := cmd.Flags().GetString("algorithm-type") loadBalancer, _, err := cli.Client().LoadBalancer.Get(cli.Context, idOrName) diff --git a/internal/cmd/loadbalancer/change_type.go b/internal/cmd/loadbalancer/change_type.go index dc4201ef..caddf08a 100644 --- a/internal/cmd/loadbalancer/change_type.go +++ b/internal/cmd/loadbalancer/change_type.go @@ -21,13 +21,13 @@ func newChangeTypeCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runLoadBalancerChangeType), + RunE: cli.Wrap(runChangeType), } return cmd } -func runLoadBalancerChangeType(cli *state.State, cmd *cobra.Command, args []string) error { +func runChangeType(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] loadBalancer, _, err := cli.Client().LoadBalancer.Get(cli.Context, idOrName) if err != nil { diff --git a/internal/cmd/loadbalancer/create.go b/internal/cmd/loadbalancer/create.go index 4a499864..8914edfb 100644 --- a/internal/cmd/loadbalancer/create.go +++ b/internal/cmd/loadbalancer/create.go @@ -17,7 +17,7 @@ func newCreateCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runLoadBalancerCreate), + RunE: cli.Wrap(runCreate), } cmd.Flags().String("name", "", "Load Balancer name (required)") @@ -43,7 +43,7 @@ func newCreateCommand(cli *state.State) *cobra.Command { return cmd } -func runLoadBalancerCreate(cli *state.State, cmd *cobra.Command, args []string) error { +func runCreate(cli *state.State, cmd *cobra.Command, args []string) error { name, _ := cmd.Flags().GetString("name") serverType, _ := cmd.Flags().GetString("type") algorithmType, _ := cmd.Flags().GetString("algorithm-type") diff --git a/internal/cmd/loadbalancer/delete.go b/internal/cmd/loadbalancer/delete.go index dadb6868..2aa5064f 100644 --- a/internal/cmd/loadbalancer/delete.go +++ b/internal/cmd/loadbalancer/delete.go @@ -17,12 +17,12 @@ func newDeleteCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runLoadBalancerDelete), + RunE: cli.Wrap(runDelete), } return cmd } -func runLoadBalancerDelete(cli *state.State, cmd *cobra.Command, args []string) error { +func runDelete(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] loadBalancer, _, err := cli.Client().LoadBalancer.Get(cli.Context, idOrName) if err != nil { diff --git a/internal/cmd/loadbalancer/delete_service.go b/internal/cmd/loadbalancer/delete_service.go index 62bc1ff0..21cd0aff 100644 --- a/internal/cmd/loadbalancer/delete_service.go +++ b/internal/cmd/loadbalancer/delete_service.go @@ -18,7 +18,7 @@ func newDeleteServiceCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: util.ChainRunE(cli.EnsureToken), - RunE: cli.Wrap(runLoadBalancerDeleteService), + RunE: cli.Wrap(runDeleteService), } cmd.Flags().Int("listen-port", 0, "The listen port of the service you want to delete (required)") @@ -26,7 +26,7 @@ func newDeleteServiceCommand(cli *state.State) *cobra.Command { return cmd } -func runLoadBalancerDeleteService(cli *state.State, cmd *cobra.Command, args []string) error { +func runDeleteService(cli *state.State, cmd *cobra.Command, args []string) error { listenPort, _ := cmd.Flags().GetInt("listen-port") idOrName := args[0] loadBalancer, _, err := cli.Client().LoadBalancer.Get(cli.Context, idOrName) diff --git a/internal/cmd/loadbalancer/describe.go b/internal/cmd/loadbalancer/describe.go index 3841bea6..1118ffb4 100644 --- a/internal/cmd/loadbalancer/describe.go +++ b/internal/cmd/loadbalancer/describe.go @@ -22,14 +22,14 @@ func newDescribeCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runLoadBalancerDescribe), + RunE: cli.Wrap(runDescribe), } output.AddFlag(cmd, output.OptionJSON(), output.OptionFormat()) cmd.Flags().Bool("expand-targets", false, "Expand all label_selector targets") return cmd } -func runLoadBalancerDescribe(cli *state.State, cmd *cobra.Command, args []string) error { +func runDescribe(cli *state.State, cmd *cobra.Command, args []string) error { outputFlags := output.FlagsForCommand(cmd) withLabelSelectorTargets, _ := cmd.Flags().GetBool("expand-targets") idOrName := args[0] @@ -43,15 +43,15 @@ func runLoadBalancerDescribe(cli *state.State, cmd *cobra.Command, args []string switch { case outputFlags.IsSet("json"): - return loadBalancerDescribeJSON(resp) + return describeJSON(resp) case outputFlags.IsSet("format"): return util.DescribeFormat(loadBalancer, outputFlags["format"][0]) default: - return loadBalancerDescribeText(cli, loadBalancer, withLabelSelectorTargets) + return describeText(cli, loadBalancer, withLabelSelectorTargets) } } -func loadBalancerDescribeText(cli *state.State, loadBalancer *hcloud.LoadBalancer, withLabelSelectorTargets bool) error { +func describeText(cli *state.State, loadBalancer *hcloud.LoadBalancer, withLabelSelectorTargets bool) error { fmt.Printf("ID:\t\t\t\t%d\n", loadBalancer.ID) fmt.Printf("Name:\t\t\t\t%s\n", loadBalancer.Name) fmt.Printf("Created:\t\t\t%s (%s)\n", util.Datetime(loadBalancer.Created), humanize.Time(loadBalancer.Created)) @@ -188,7 +188,7 @@ func loadBalancerDescribeText(cli *state.State, loadBalancer *hcloud.LoadBalance return nil } -func loadBalancerDescribeJSON(resp *hcloud.Response) error { +func describeJSON(resp *hcloud.Response) error { var data map[string]interface{} if err := json.NewDecoder(resp.Body).Decode(&data); err != nil { return err diff --git a/internal/cmd/loadbalancer/detach_from_network.go b/internal/cmd/loadbalancer/detach_from_network.go index 36c43f09..cf8b275d 100644 --- a/internal/cmd/loadbalancer/detach_from_network.go +++ b/internal/cmd/loadbalancer/detach_from_network.go @@ -19,7 +19,7 @@ func newDetachFromNetworkCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runLoadBalancerDetachFromNetwork), + RunE: cli.Wrap(runDetachFromNetwork), } cmd.Flags().StringP("network", "n", "", "Network (ID or name) (required)") cmd.RegisterFlagCompletionFunc("network", cmpl.SuggestCandidatesF(cli.NetworkNames)) @@ -27,7 +27,7 @@ func newDetachFromNetworkCommand(cli *state.State) *cobra.Command { return cmd } -func runLoadBalancerDetachFromNetwork(cli *state.State, cmd *cobra.Command, args []string) error { +func runDetachFromNetwork(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] loadBalancer, _, err := cli.Client().LoadBalancer.Get(cli.Context, idOrName) if err != nil { diff --git a/internal/cmd/loadbalancer/disable_protection.go b/internal/cmd/loadbalancer/disable_protection.go index 9fedc94c..142c551e 100644 --- a/internal/cmd/loadbalancer/disable_protection.go +++ b/internal/cmd/loadbalancer/disable_protection.go @@ -22,12 +22,12 @@ func newDisableProtectionCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runLoadBalancerDisableProtection), + RunE: cli.Wrap(runDisableProtection), } return cmd } -func runLoadBalancerDisableProtection(cli *state.State, cmd *cobra.Command, args []string) error { +func runDisableProtection(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] loadBalancer, _, err := cli.Client().LoadBalancer.Get(cli.Context, idOrName) if err != nil { diff --git a/internal/cmd/loadbalancer/disable_public_interface.go b/internal/cmd/loadbalancer/disable_public_interface.go index 538d32f8..09c4929b 100644 --- a/internal/cmd/loadbalancer/disable_public_interface.go +++ b/internal/cmd/loadbalancer/disable_public_interface.go @@ -17,13 +17,13 @@ func newDisablePublicInterfaceCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runLoadBalancerDisablePublicInterface), + RunE: cli.Wrap(runDisablePublicInterface), } return cmd } -func runLoadBalancerDisablePublicInterface(cli *state.State, cmd *cobra.Command, args []string) error { +func runDisablePublicInterface(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] loadBalancer, _, err := cli.Client().LoadBalancer.Get(cli.Context, idOrName) if err != nil { diff --git a/internal/cmd/loadbalancer/enable_protection.go b/internal/cmd/loadbalancer/enable_protection.go index a7042b8a..b84d8247 100644 --- a/internal/cmd/loadbalancer/enable_protection.go +++ b/internal/cmd/loadbalancer/enable_protection.go @@ -22,12 +22,12 @@ func newEnableProtectionCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runLoadBalancerEnableProtection), + RunE: cli.Wrap(runEnableProtection), } return cmd } -func runLoadBalancerEnableProtection(cli *state.State, cmd *cobra.Command, args []string) error { +func runEnableProtection(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] LoadBalancer, _, err := cli.Client().LoadBalancer.Get(cli.Context, idOrName) if err != nil { diff --git a/internal/cmd/loadbalancer/enable_public_interface.go b/internal/cmd/loadbalancer/enable_public_interface.go index e0fd187b..b3780db4 100644 --- a/internal/cmd/loadbalancer/enable_public_interface.go +++ b/internal/cmd/loadbalancer/enable_public_interface.go @@ -17,13 +17,13 @@ func newEnablePublicInterfaceCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runLoadBalancerEnablePublicInterface), + RunE: cli.Wrap(runEnablePublicInterface), } return cmd } -func runLoadBalancerEnablePublicInterface(cli *state.State, cmd *cobra.Command, args []string) error { +func runEnablePublicInterface(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] loadBalancer, _, err := cli.Client().LoadBalancer.Get(cli.Context, idOrName) if err != nil { diff --git a/internal/cmd/loadbalancer/list.go b/internal/cmd/loadbalancer/list.go index e76d7ab9..2ee67486 100644 --- a/internal/cmd/loadbalancer/list.go +++ b/internal/cmd/loadbalancer/list.go @@ -12,10 +12,10 @@ import ( "github.com/spf13/cobra" ) -var loadBalancerListTableOutput *output.Table +var listTableOutput *output.Table func init() { - loadBalancerListTableOutput = describeLoadBalancerListTableOutput(nil) + listTableOutput = describeListTableOutput(nil) } func newListCommand(cli *state.State) *cobra.Command { @@ -24,19 +24,19 @@ func newListCommand(cli *state.State) *cobra.Command { Short: "List Load Balancers", Long: util.ListLongDescription( "Displays a list of Load Balancers.", - loadBalancerListTableOutput.Columns(), + listTableOutput.Columns(), ), TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runLoadBalancerList), + RunE: cli.Wrap(runList), } - output.AddFlag(cmd, output.OptionNoHeader(), output.OptionColumns(loadBalancerListTableOutput.Columns()), output.OptionJSON()) + output.AddFlag(cmd, output.OptionNoHeader(), output.OptionColumns(listTableOutput.Columns()), output.OptionJSON()) cmd.Flags().StringP("selector", "l", "", "Selector to filter by labels") return cmd } -func runLoadBalancerList(cli *state.State, cmd *cobra.Command, args []string) error { +func runList(cli *state.State, cmd *cobra.Command, args []string) error { outOpts := output.FlagsForCommand(cmd) labelSelector, _ := cmd.Flags().GetString("selector") @@ -142,7 +142,7 @@ func runLoadBalancerList(cli *state.State, cmd *cobra.Command, args []string) er cols = outOpts["columns"] } - tw := describeLoadBalancerListTableOutput(cli) + tw := describeListTableOutput(cli) if err = tw.ValidateColumns(cols); err != nil { return err } @@ -157,7 +157,7 @@ func runLoadBalancerList(cli *state.State, cmd *cobra.Command, args []string) er return nil } -func describeLoadBalancerListTableOutput(cli *state.State) *output.Table { +func describeListTableOutput(cli *state.State) *output.Table { return output.NewTable(). AddAllowedFields(hcloud.LoadBalancer{}). AddFieldFn("ipv4", output.FieldFn(func(obj interface{}) string { diff --git a/internal/cmd/loadbalancer/metrics.go b/internal/cmd/loadbalancer/metrics.go index 42e891e7..0c7cc09d 100644 --- a/internal/cmd/loadbalancer/metrics.go +++ b/internal/cmd/loadbalancer/metrics.go @@ -26,7 +26,7 @@ func newMetricsCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runLoadBalancerMetrics), + RunE: cli.Wrap(runMetrics), } cmd.Flags().String("type", "", "Type of metrics you want to show") @@ -40,7 +40,7 @@ func newMetricsCommand(cli *state.State) *cobra.Command { return cmd } -func runLoadBalancerMetrics(cli *state.State, cmd *cobra.Command, args []string) error { +func runMetrics(cli *state.State, cmd *cobra.Command, args []string) error { outputFlags := output.FlagsForCommand(cmd) idOrName := args[0] @@ -83,7 +83,7 @@ func runLoadBalancerMetrics(cli *state.State, cmd *cobra.Command, args []string) } switch { case outputFlags.IsSet("json"): - return loadBalancerMetricsJSON(resp) + return metricsJSON(resp) default: var keys []string for k := range m.TimeSeries { @@ -109,7 +109,7 @@ func runLoadBalancerMetrics(cli *state.State, cmd *cobra.Command, args []string) return nil } -func loadBalancerMetricsJSON(resp *hcloud.Response) error { +func metricsJSON(resp *hcloud.Response) error { var data map[string]interface{} if err := json.NewDecoder(resp.Body).Decode(&data); err != nil { return err diff --git a/internal/cmd/loadbalancer/remove_label.go b/internal/cmd/loadbalancer/remove_label.go index 64c8ed64..af55718b 100644 --- a/internal/cmd/loadbalancer/remove_label.go +++ b/internal/cmd/loadbalancer/remove_label.go @@ -27,15 +27,15 @@ func newRemoveLabelCommand(cli *state.State) *cobra.Command { ), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: util.ChainRunE(validateLoadBalancerRemoveLabel, cli.EnsureToken), - RunE: cli.Wrap(runLoadBalancerRemoveLabel), + PreRunE: util.ChainRunE(validateRemoveLabel, cli.EnsureToken), + RunE: cli.Wrap(runRemoveLabel), } cmd.Flags().BoolP("all", "a", false, "Remove all labels") return cmd } -func validateLoadBalancerRemoveLabel(cmd *cobra.Command, args []string) error { +func validateRemoveLabel(cmd *cobra.Command, args []string) error { all, _ := cmd.Flags().GetBool("all") if all && len(args) == 2 { @@ -48,7 +48,7 @@ func validateLoadBalancerRemoveLabel(cmd *cobra.Command, args []string) error { return nil } -func runLoadBalancerRemoveLabel(cli *state.State, cmd *cobra.Command, args []string) error { +func runRemoveLabel(cli *state.State, cmd *cobra.Command, args []string) error { all, _ := cmd.Flags().GetBool("all") idOrName := args[0] loadBalancer, _, err := cli.Client().LoadBalancer.Get(cli.Context, idOrName) diff --git a/internal/cmd/loadbalancer/remove_target.go b/internal/cmd/loadbalancer/remove_target.go index 2ba3c630..3e3b400e 100644 --- a/internal/cmd/loadbalancer/remove_target.go +++ b/internal/cmd/loadbalancer/remove_target.go @@ -20,7 +20,7 @@ func newRemoveTargetCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runLoadBalancerRemoveTarget), + RunE: cli.Wrap(runRemoveTarget), } cmd.Flags().String("server", "", "Name or ID of the server") @@ -33,7 +33,7 @@ func newRemoveTargetCommand(cli *state.State) *cobra.Command { return cmd } -func runLoadBalancerRemoveTarget(cli *state.State, cmd *cobra.Command, args []string) error { +func runRemoveTarget(cli *state.State, cmd *cobra.Command, args []string) error { var ( action *hcloud.Action loadBalancer *hcloud.LoadBalancer diff --git a/internal/cmd/loadbalancer/update.go b/internal/cmd/loadbalancer/update.go index 895b1462..7ec0717e 100644 --- a/internal/cmd/loadbalancer/update.go +++ b/internal/cmd/loadbalancer/update.go @@ -19,7 +19,7 @@ func newUpdateCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runLoadBalancerUpdate), + RunE: cli.Wrap(runUpdate), } cmd.Flags().String("name", "", "Load Balancer name") @@ -27,7 +27,7 @@ func newUpdateCommand(cli *state.State) *cobra.Command { return cmd } -func runLoadBalancerUpdate(cli *state.State, cmd *cobra.Command, args []string) error { +func runUpdate(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] loadBalancer, _, err := cli.Client().LoadBalancer.Get(cli.Context, idOrName) if err != nil { diff --git a/internal/cmd/loadbalancer/update_service.go b/internal/cmd/loadbalancer/update_service.go index 22a771b2..dd6dead2 100644 --- a/internal/cmd/loadbalancer/update_service.go +++ b/internal/cmd/loadbalancer/update_service.go @@ -19,7 +19,7 @@ func newUpdateServiceCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runLoadBalancerUpdateService), + RunE: cli.Wrap(runUpdateService), } cmd.Flags().Int("listen-port", 0, "The listen port of the service that you want to update (required)") @@ -51,7 +51,7 @@ func newUpdateServiceCommand(cli *state.State) *cobra.Command { return cmd } -func runLoadBalancerUpdateService(cli *state.State, cmd *cobra.Command, args []string) error { +func runUpdateService(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] listenPort, _ := cmd.Flags().GetInt("listen-port") diff --git a/internal/cmd/loadbalancertype/describe.go b/internal/cmd/loadbalancertype/describe.go index 1bd4dcbd..35fbcc10 100644 --- a/internal/cmd/loadbalancertype/describe.go +++ b/internal/cmd/loadbalancertype/describe.go @@ -12,7 +12,7 @@ import ( "github.com/spf13/cobra" ) -func newnDescribeCommand(cli *state.State) *cobra.Command { +func newDescribeCommand(cli *state.State) *cobra.Command { cmd := &cobra.Command{ Use: "describe [FLAGS] LOADBALANCERTYPE", Short: "Describe a Load Balancer type", @@ -21,13 +21,13 @@ func newnDescribeCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runLoadBalancerTypeDescribe), + RunE: cli.Wrap(runDescribe), } output.AddFlag(cmd, output.OptionJSON(), output.OptionFormat()) return cmd } -func runLoadBalancerTypeDescribe(cli *state.State, cmd *cobra.Command, args []string) error { +func runDescribe(cli *state.State, cmd *cobra.Command, args []string) error { outputFlags := output.FlagsForCommand(cmd) idOrName := args[0] @@ -41,15 +41,15 @@ func runLoadBalancerTypeDescribe(cli *state.State, cmd *cobra.Command, args []st switch { case outputFlags.IsSet("json"): - return loadBalancerTypeDescribeJSON(resp) + return describeJSON(resp) case outputFlags.IsSet("format"): return util.DescribeFormat(loadBalancerType, outputFlags["format"][0]) default: - return loadBalancerTypeDescribeText(cli, loadBalancerType) + return describeText(cli, loadBalancerType) } } -func loadBalancerTypeDescribeText(cli *state.State, loadBalancerType *hcloud.LoadBalancerType) error { +func describeText(cli *state.State, loadBalancerType *hcloud.LoadBalancerType) error { fmt.Printf("ID:\t\t\t\t%d\n", loadBalancerType.ID) fmt.Printf("Name:\t\t\t\t%s\n", loadBalancerType.Name) fmt.Printf("Description:\t\t\t%s\n", loadBalancerType.Description) @@ -67,7 +67,7 @@ func loadBalancerTypeDescribeText(cli *state.State, loadBalancerType *hcloud.Loa return nil } -func loadBalancerTypeDescribeJSON(resp *hcloud.Response) error { +func describeJSON(resp *hcloud.Response) error { var data map[string]interface{} if err := json.NewDecoder(resp.Body).Decode(&data); err != nil { return err diff --git a/internal/cmd/loadbalancertype/list.go b/internal/cmd/loadbalancertype/list.go index 4545d114..55fba05b 100644 --- a/internal/cmd/loadbalancertype/list.go +++ b/internal/cmd/loadbalancertype/list.go @@ -9,10 +9,10 @@ import ( "github.com/spf13/cobra" ) -var loadBalancerTypeListTableOutput *output.Table +var listTableOutput *output.Table func init() { - loadBalancerTypeListTableOutput = output.NewTable(). + listTableOutput = output.NewTable(). AddAllowedFields(hcloud.LoadBalancerType{}) } @@ -22,18 +22,18 @@ func newListCommand(cli *state.State) *cobra.Command { Short: "List Load Balancer types", Long: util.ListLongDescription( "Displays a list of Load Balancer types.", - loadBalancerTypeListTableOutput.Columns(), + listTableOutput.Columns(), ), TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runLoadBalancerTypeList), + RunE: cli.Wrap(runList), } - output.AddFlag(cmd, output.OptionNoHeader(), output.OptionColumns(loadBalancerTypeListTableOutput.Columns()), output.OptionJSON()) + output.AddFlag(cmd, output.OptionNoHeader(), output.OptionColumns(listTableOutput.Columns()), output.OptionJSON()) return cmd } -func runLoadBalancerTypeList(cli *state.State, cmd *cobra.Command, args []string) error { +func runList(cli *state.State, cmd *cobra.Command, args []string) error { outOpts := output.FlagsForCommand(cmd) loadBalancerTypes, err := cli.Client().LoadBalancerType.All(cli.Context) @@ -54,7 +54,7 @@ func runLoadBalancerTypeList(cli *state.State, cmd *cobra.Command, args []string cols = outOpts["columns"] } - tw := loadBalancerTypeListTableOutput + tw := listTableOutput if err = tw.ValidateColumns(cols); err != nil { return err } diff --git a/internal/cmd/loadbalancertype/load_balancer_type.go b/internal/cmd/loadbalancertype/load_balancer_type.go index 1985e203..81360520 100644 --- a/internal/cmd/loadbalancertype/load_balancer_type.go +++ b/internal/cmd/loadbalancertype/load_balancer_type.go @@ -14,7 +14,7 @@ func NewCommand(cli *state.State) *cobra.Command { DisableFlagsInUseLine: true, } cmd.AddCommand( - newnDescribeCommand(cli), + newDescribeCommand(cli), newListCommand(cli), ) return cmd diff --git a/internal/cmd/location/describe.go b/internal/cmd/location/describe.go index 4c378693..c734e99d 100644 --- a/internal/cmd/location/describe.go +++ b/internal/cmd/location/describe.go @@ -21,13 +21,13 @@ func newDescribeCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runLocationDescribe), + RunE: cli.Wrap(runDescribe), } output.AddFlag(cmd, output.OptionJSON(), output.OptionFormat()) return cmd } -func runLocationDescribe(cli *state.State, cmd *cobra.Command, args []string) error { +func runDescribe(cli *state.State, cmd *cobra.Command, args []string) error { outputFlags := output.FlagsForCommand(cmd) idOrName := args[0] @@ -41,15 +41,15 @@ func runLocationDescribe(cli *state.State, cmd *cobra.Command, args []string) er switch { case outputFlags.IsSet("json"): - return locationDescribeJSON(resp) + return describeJSON(resp) case outputFlags.IsSet("format"): return util.DescribeFormat(location, outputFlags["format"][0]) default: - return locationDescribeText(cli, location) + return describeText(cli, location) } } -func locationDescribeText(cli *state.State, location *hcloud.Location) error { +func describeText(cli *state.State, location *hcloud.Location) error { fmt.Printf("ID:\t\t%d\n", location.ID) fmt.Printf("Name:\t\t%s\n", location.Name) fmt.Printf("Description:\t%s\n", location.Description) @@ -61,7 +61,7 @@ func locationDescribeText(cli *state.State, location *hcloud.Location) error { return nil } -func locationDescribeJSON(resp *hcloud.Response) error { +func describeJSON(resp *hcloud.Response) error { var data map[string]interface{} if err := json.NewDecoder(resp.Body).Decode(&data); err != nil { return err diff --git a/internal/cmd/location/list.go b/internal/cmd/location/list.go index 697f5bd1..9c260422 100644 --- a/internal/cmd/location/list.go +++ b/internal/cmd/location/list.go @@ -9,10 +9,10 @@ import ( "github.com/spf13/cobra" ) -var locationListTableOutput *output.Table +var listTableOutput *output.Table func init() { - locationListTableOutput = output.NewTable(). + listTableOutput = output.NewTable(). AddAllowedFields(hcloud.Location{}) } @@ -22,18 +22,18 @@ func newListCommand(cli *state.State) *cobra.Command { Short: "List locations", Long: util.ListLongDescription( "Displays a list of locations.", - locationListTableOutput.Columns(), + listTableOutput.Columns(), ), TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runLocationList), + RunE: cli.Wrap(runList), } - output.AddFlag(cmd, output.OptionNoHeader(), output.OptionColumns(locationListTableOutput.Columns()), output.OptionJSON()) + output.AddFlag(cmd, output.OptionNoHeader(), output.OptionColumns(listTableOutput.Columns()), output.OptionJSON()) return cmd } -func runLocationList(cli *state.State, cmd *cobra.Command, args []string) error { +func runList(cli *state.State, cmd *cobra.Command, args []string) error { outOpts := output.FlagsForCommand(cmd) locations, err := cli.Client().Location.All(cli.Context) @@ -54,7 +54,7 @@ func runLocationList(cli *state.State, cmd *cobra.Command, args []string) error cols = outOpts["columns"] } - tw := locationListTableOutput + tw := listTableOutput if err = tw.ValidateColumns(cols); err != nil { return err } diff --git a/internal/cmd/network/add_label.go b/internal/cmd/network/add_label.go index 96ef53d3..633c99ea 100644 --- a/internal/cmd/network/add_label.go +++ b/internal/cmd/network/add_label.go @@ -18,15 +18,15 @@ func newAddLabelCommand(cli *state.State) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.NetworkNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: util.ChainRunE(validateNetworkAddLabel, cli.EnsureToken), - RunE: cli.Wrap(runNetworkAddLabel), + PreRunE: util.ChainRunE(validateAddLabel, cli.EnsureToken), + RunE: cli.Wrap(runAddLabel), } cmd.Flags().BoolP("overwrite", "o", false, "Overwrite label if it exists already") return cmd } -func validateNetworkAddLabel(cmd *cobra.Command, args []string) error { +func validateAddLabel(cmd *cobra.Command, args []string) error { label := util.SplitLabel(args[1]) if len(label) != 2 { return fmt.Errorf("invalid label: %s", args[1]) @@ -35,7 +35,7 @@ func validateNetworkAddLabel(cmd *cobra.Command, args []string) error { return nil } -func runNetworkAddLabel(cli *state.State, cmd *cobra.Command, args []string) error { +func runAddLabel(cli *state.State, cmd *cobra.Command, args []string) error { overwrite, _ := cmd.Flags().GetBool("overwrite") idOrName := args[0] network, _, err := cli.Client().Network.Get(cli.Context, idOrName) diff --git a/internal/cmd/network/add_route.go b/internal/cmd/network/add_route.go index 0ccdf5f5..71005c65 100644 --- a/internal/cmd/network/add_route.go +++ b/internal/cmd/network/add_route.go @@ -19,7 +19,7 @@ func newAddRouteCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runNetworkAddRoute), + RunE: cli.Wrap(runAddRoute), } cmd.Flags().IPNet("destination", net.IPNet{}, "Destination network or host (required)") @@ -31,7 +31,7 @@ func newAddRouteCommand(cli *state.State) *cobra.Command { return cmd } -func runNetworkAddRoute(cli *state.State, cmd *cobra.Command, args []string) error { +func runAddRoute(cli *state.State, cmd *cobra.Command, args []string) error { gateway, _ := cmd.Flags().GetIP("gateway") destination, _ := cmd.Flags().GetIPNet("destination") idOrName := args[0] diff --git a/internal/cmd/network/add_subnet.go b/internal/cmd/network/add_subnet.go index b342510b..af4f95d5 100644 --- a/internal/cmd/network/add_subnet.go +++ b/internal/cmd/network/add_subnet.go @@ -19,7 +19,7 @@ func newAddSubnetCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runNetworkAddSubnet), + RunE: cli.Wrap(runAddSubnet), } cmd.Flags().String("type", "", "Type of subnet (required)") @@ -36,7 +36,7 @@ func newAddSubnetCommand(cli *state.State) *cobra.Command { return cmd } -func runNetworkAddSubnet(cli *state.State, cmd *cobra.Command, args []string) error { +func runAddSubnet(cli *state.State, cmd *cobra.Command, args []string) error { subnetType, _ := cmd.Flags().GetString("type") networkZone, _ := cmd.Flags().GetString("network-zone") ipRange, _ := cmd.Flags().GetIPNet("ip-range") diff --git a/internal/cmd/network/change_ip_range.go b/internal/cmd/network/change_ip_range.go index 4e5fe925..00c4974f 100644 --- a/internal/cmd/network/change_ip_range.go +++ b/internal/cmd/network/change_ip_range.go @@ -19,7 +19,7 @@ func newChangeIPRangeCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runNetworkChangeIPRange), + RunE: cli.Wrap(runChangeIPRange), } cmd.Flags().IPNet("ip-range", net.IPNet{}, "New IP range (required)") @@ -28,7 +28,7 @@ func newChangeIPRangeCommand(cli *state.State) *cobra.Command { return cmd } -func runNetworkChangeIPRange(cli *state.State, cmd *cobra.Command, args []string) error { +func runChangeIPRange(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] network, _, err := cli.Client().Network.Get(cli.Context, idOrName) if err != nil { diff --git a/internal/cmd/network/create.go b/internal/cmd/network/create.go index 05bf1029..475d7280 100644 --- a/internal/cmd/network/create.go +++ b/internal/cmd/network/create.go @@ -17,7 +17,7 @@ func newCreateCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runNetworkCreate), + RunE: cli.Wrap(runCreate), } cmd.Flags().String("name", "", "Network name (required)") @@ -31,7 +31,7 @@ func newCreateCommand(cli *state.State) *cobra.Command { return cmd } -func runNetworkCreate(cli *state.State, cmd *cobra.Command, args []string) error { +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") diff --git a/internal/cmd/network/delete.go b/internal/cmd/network/delete.go index 7c853ef2..8d018692 100644 --- a/internal/cmd/network/delete.go +++ b/internal/cmd/network/delete.go @@ -17,12 +17,12 @@ func newDeleteCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runNetworkDelete), + RunE: cli.Wrap(runDelete), } return cmd } -func runNetworkDelete(cli *state.State, cmd *cobra.Command, args []string) error { +func runDelete(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] network, _, err := cli.Client().Network.Get(cli.Context, idOrName) if err != nil { diff --git a/internal/cmd/network/describe.go b/internal/cmd/network/describe.go index cfe4f461..e618898a 100644 --- a/internal/cmd/network/describe.go +++ b/internal/cmd/network/describe.go @@ -22,13 +22,13 @@ func newDescribeCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runNetworkDescribe), + RunE: cli.Wrap(runDescribe), } output.AddFlag(cmd, output.OptionJSON(), output.OptionFormat()) return cmd } -func runNetworkDescribe(cli *state.State, cmd *cobra.Command, args []string) error { +func runDescribe(cli *state.State, cmd *cobra.Command, args []string) error { outputFlags := output.FlagsForCommand(cmd) idOrName := args[0] @@ -42,15 +42,15 @@ func runNetworkDescribe(cli *state.State, cmd *cobra.Command, args []string) err switch { case outputFlags.IsSet("json"): - return networkDescribeJSON(resp) + return describeJSON(resp) case outputFlags.IsSet("format"): return util.DescribeFormat(network, outputFlags["format"][0]) default: - return networkDescribeText(cli, network) + return describeText(cli, network) } } -func networkDescribeText(cli *state.State, network *hcloud.Network) error { +func describeText(cli *state.State, network *hcloud.Network) error { fmt.Printf("ID:\t\t%d\n", network.ID) fmt.Printf("Name:\t\t%s\n", network.Name) fmt.Printf("Created:\t%s (%s)\n", util.Datetime(network.Created), humanize.Time(network.Created)) @@ -96,7 +96,7 @@ func networkDescribeText(cli *state.State, network *hcloud.Network) error { return nil } -func networkDescribeJSON(resp *hcloud.Response) error { +func describeJSON(resp *hcloud.Response) error { var data map[string]interface{} if err := json.NewDecoder(resp.Body).Decode(&data); err != nil { return err diff --git a/internal/cmd/network/disable_protection.go b/internal/cmd/network/disable_protection.go index 31116534..755bc9ff 100644 --- a/internal/cmd/network/disable_protection.go +++ b/internal/cmd/network/disable_protection.go @@ -22,12 +22,12 @@ func newDisableProtectionCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runNetworkDisableProtection), + RunE: cli.Wrap(runDisableProtection), } return cmd } -func runNetworkDisableProtection(cli *state.State, cmd *cobra.Command, args []string) error { +func runDisableProtection(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] network, _, err := cli.Client().Network.Get(cli.Context, idOrName) if err != nil { diff --git a/internal/cmd/network/enable_protection.go b/internal/cmd/network/enable_protection.go index 2fde3f02..b4fd1707 100644 --- a/internal/cmd/network/enable_protection.go +++ b/internal/cmd/network/enable_protection.go @@ -22,12 +22,12 @@ func newEnableProtectionCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runNetworkEnableProtection), + RunE: cli.Wrap(runEnableProtection), } return cmd } -func runNetworkEnableProtection(cli *state.State, cmd *cobra.Command, args []string) error { +func runEnableProtection(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] network, _, err := cli.Client().Network.Get(cli.Context, idOrName) if err != nil { diff --git a/internal/cmd/network/list.go b/internal/cmd/network/list.go index 50f3ab55..2f225002 100644 --- a/internal/cmd/network/list.go +++ b/internal/cmd/network/list.go @@ -12,10 +12,10 @@ import ( "github.com/spf13/cobra" ) -var networkListTableOutput *output.Table +var listTableOutput *output.Table func init() { - networkListTableOutput = describeNetworkListTableOutput(nil) + listTableOutput = describelistTableOutput(nil) } func newListCommand(cli *state.State) *cobra.Command { @@ -24,19 +24,19 @@ func newListCommand(cli *state.State) *cobra.Command { Short: "List networks", Long: util.ListLongDescription( "Displays a list of networks.", - networkListTableOutput.Columns(), + listTableOutput.Columns(), ), TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runNetworkList), + RunE: cli.Wrap(runList), } - output.AddFlag(cmd, output.OptionNoHeader(), output.OptionColumns(networkListTableOutput.Columns()), output.OptionJSON()) + output.AddFlag(cmd, output.OptionNoHeader(), output.OptionColumns(listTableOutput.Columns()), output.OptionJSON()) cmd.Flags().StringP("selector", "l", "", "Selector to filter by labels") return cmd } -func runNetworkList(cli *state.State, cmd *cobra.Command, args []string) error { +func runList(cli *state.State, cmd *cobra.Command, args []string) error { outOpts := output.FlagsForCommand(cmd) labelSelector, _ := cmd.Flags().GetString("selector") @@ -89,7 +89,7 @@ func runNetworkList(cli *state.State, cmd *cobra.Command, args []string) error { cols = outOpts["columns"] } - tw := describeNetworkListTableOutput(cli) + tw := describelistTableOutput(cli) if err = tw.ValidateColumns(cols); err != nil { return err } @@ -104,7 +104,7 @@ func runNetworkList(cli *state.State, cmd *cobra.Command, args []string) error { return nil } -func describeNetworkListTableOutput(cli *state.State) *output.Table { +func describelistTableOutput(cli *state.State) *output.Table { return output.NewTable(). AddAllowedFields(hcloud.Network{}). AddFieldFn("servers", output.FieldFn(func(obj interface{}) string { diff --git a/internal/cmd/network/remove_label.go b/internal/cmd/network/remove_label.go index 22c94422..0dc0404e 100644 --- a/internal/cmd/network/remove_label.go +++ b/internal/cmd/network/remove_label.go @@ -27,15 +27,15 @@ func newRemoveLabelCommand(cli *state.State) *cobra.Command { ), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: util.ChainRunE(validateNetworkRemoveLabel, cli.EnsureToken), - RunE: cli.Wrap(runNetworkRemoveLabel), + PreRunE: util.ChainRunE(validateRemoveLabel, cli.EnsureToken), + RunE: cli.Wrap(runRemoveLabel), } cmd.Flags().BoolP("all", "a", false, "Remove all labels") return cmd } -func validateNetworkRemoveLabel(cmd *cobra.Command, args []string) error { +func validateRemoveLabel(cmd *cobra.Command, args []string) error { all, _ := cmd.Flags().GetBool("all") if all && len(args) == 2 { @@ -48,7 +48,7 @@ func validateNetworkRemoveLabel(cmd *cobra.Command, args []string) error { return nil } -func runNetworkRemoveLabel(cli *state.State, cmd *cobra.Command, args []string) error { +func runRemoveLabel(cli *state.State, cmd *cobra.Command, args []string) error { all, _ := cmd.Flags().GetBool("all") idOrName := args[0] network, _, err := cli.Client().Network.Get(cli.Context, idOrName) diff --git a/internal/cmd/network/remove_route.go b/internal/cmd/network/remove_route.go index a7e0ce7d..3d3b2979 100644 --- a/internal/cmd/network/remove_route.go +++ b/internal/cmd/network/remove_route.go @@ -19,7 +19,7 @@ func newRemoveRouteCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runNetworkRemoveRoute), + RunE: cli.Wrap(runRemoveRoute), } cmd.Flags().IPNet("destination", net.IPNet{}, "Destination network or host (required)") @@ -31,7 +31,7 @@ func newRemoveRouteCommand(cli *state.State) *cobra.Command { return cmd } -func runNetworkRemoveRoute(cli *state.State, cmd *cobra.Command, args []string) error { +func runRemoveRoute(cli *state.State, cmd *cobra.Command, args []string) error { gateway, _ := cmd.Flags().GetIP("gateway") destination, _ := cmd.Flags().GetIPNet("destination") idOrName := args[0] diff --git a/internal/cmd/network/remove_subnet.go b/internal/cmd/network/remove_subnet.go index fb21986f..95c00d56 100644 --- a/internal/cmd/network/remove_subnet.go +++ b/internal/cmd/network/remove_subnet.go @@ -19,14 +19,14 @@ func newRemoveSubnetCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runNetworkRemoveSubnet), + RunE: cli.Wrap(runRemoveSubnet), } cmd.Flags().IPNet("ip-range", net.IPNet{}, "Subnet IP range (required)") cmd.MarkFlagRequired("ip-range") return cmd } -func runNetworkRemoveSubnet(cli *state.State, cmd *cobra.Command, args []string) error { +func runRemoveSubnet(cli *state.State, cmd *cobra.Command, args []string) error { ipRange, _ := cmd.Flags().GetIPNet("ip-range") idOrName := args[0] diff --git a/internal/cmd/network/update.go b/internal/cmd/network/update.go index 13c94cf2..b6162c7a 100644 --- a/internal/cmd/network/update.go +++ b/internal/cmd/network/update.go @@ -19,7 +19,7 @@ func newUpdateCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runNetworkUpdate), + RunE: cli.Wrap(runUpdate), } cmd.Flags().String("name", "", "Network name") @@ -27,7 +27,7 @@ func newUpdateCommand(cli *state.State) *cobra.Command { return cmd } -func runNetworkUpdate(cli *state.State, cmd *cobra.Command, args []string) error { +func runUpdate(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] network, _, err := cli.Client().Network.Get(cli.Context, idOrName) if err != nil { diff --git a/internal/cmd/server/add_label.go b/internal/cmd/server/add_label.go index c06d1b5f..b607ee16 100644 --- a/internal/cmd/server/add_label.go +++ b/internal/cmd/server/add_label.go @@ -18,15 +18,15 @@ func newAddLabelCommand(cli *state.State) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.ServerNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: util.ChainRunE(validateServerAddLabel, cli.EnsureToken), - RunE: cli.Wrap(runServerAddLabel), + PreRunE: util.ChainRunE(validateAddLabel, cli.EnsureToken), + RunE: cli.Wrap(runAddLabel), } cmd.Flags().BoolP("overwrite", "o", false, "Overwrite label if it exists already") return cmd } -func validateServerAddLabel(cmd *cobra.Command, args []string) error { +func validateAddLabel(cmd *cobra.Command, args []string) error { label := util.SplitLabel(args[1]) if len(label) != 2 { return fmt.Errorf("invalid label: %s", args[1]) @@ -35,7 +35,7 @@ func validateServerAddLabel(cmd *cobra.Command, args []string) error { return nil } -func runServerAddLabel(cli *state.State, cmd *cobra.Command, args []string) error { +func runAddLabel(cli *state.State, cmd *cobra.Command, args []string) error { overwrite, _ := cmd.Flags().GetBool("overwrite") idOrName := args[0] server, _, err := cli.Client().Server.Get(cli.Context, idOrName) diff --git a/internal/cmd/server/attach_iso.go b/internal/cmd/server/attach_iso.go index 6ab4f10d..0b66eb4f 100644 --- a/internal/cmd/server/attach_iso.go +++ b/internal/cmd/server/attach_iso.go @@ -20,13 +20,13 @@ func newAttachISOCommand(cli *state.State) *cobra.Command { ), DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runServerAttachISO), + RunE: cli.Wrap(runAttachISO), } return cmd } -func runServerAttachISO(cli *state.State, cmd *cobra.Command, args []string) error { +func runAttachISO(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] server, _, err := cli.Client().Server.Get(cli.Context, idOrName) if err != nil { diff --git a/internal/cmd/server/attach_to_network.go b/internal/cmd/server/attach_to_network.go index c34648a9..c0f3fe6a 100644 --- a/internal/cmd/server/attach_to_network.go +++ b/internal/cmd/server/attach_to_network.go @@ -19,7 +19,7 @@ func newAttachToNetworkCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runServerAttachToNetwork), + RunE: cli.Wrap(runAttachToNetwork), } cmd.Flags().StringP("network", "n", "", "Network (ID or name) (required)") @@ -32,7 +32,7 @@ func newAttachToNetworkCommand(cli *state.State) *cobra.Command { return cmd } -func runServerAttachToNetwork(cli *state.State, cmd *cobra.Command, args []string) error { +func runAttachToNetwork(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] server, _, err := cli.Client().Server.Get(cli.Context, idOrName) if err != nil { diff --git a/internal/cmd/server/change_alias_ips.go b/internal/cmd/server/change_alias_ips.go index fd589bba..e4db0829 100644 --- a/internal/cmd/server/change_alias_ips.go +++ b/internal/cmd/server/change_alias_ips.go @@ -19,7 +19,7 @@ func newChangeAliasIPsCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runServerChangeAliasIPsk), + RunE: cli.Wrap(runChangeAliasIPsk), } cmd.Flags().StringP("network", "n", "", "Network (ID or name) (required)") @@ -32,7 +32,7 @@ func newChangeAliasIPsCommand(cli *state.State) *cobra.Command { return cmd } -func runServerChangeAliasIPsk(cli *state.State, cmd *cobra.Command, args []string) error { +func runChangeAliasIPsk(cli *state.State, cmd *cobra.Command, args []string) error { clear, _ := cmd.Flags().GetBool("clear") idOrName := args[0] server, _, err := cli.Client().Server.Get(cli.Context, idOrName) diff --git a/internal/cmd/server/change_type.go b/internal/cmd/server/change_type.go index 6aeb33df..8eaaf25e 100644 --- a/internal/cmd/server/change_type.go +++ b/internal/cmd/server/change_type.go @@ -21,14 +21,14 @@ func newChangeTypeCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runServerChangeType), + RunE: cli.Wrap(runChangeType), } cmd.Flags().Bool("keep-disk", false, "Keep disk size of current server type. This enables downgrading the server.") return cmd } -func runServerChangeType(cli *state.State, cmd *cobra.Command, args []string) error { +func runChangeType(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] server, _, err := cli.Client().Server.Get(cli.Context, idOrName) if err != nil { diff --git a/internal/cmd/server/create.go b/internal/cmd/server/create.go index 396feab9..ea7f9da6 100644 --- a/internal/cmd/server/create.go +++ b/internal/cmd/server/create.go @@ -26,7 +26,7 @@ func newCreateCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runServerCreate), + RunE: cli.Wrap(runCreate), } cmd.Flags().String("name", "", "Server name (required)") cmd.MarkFlagRequired("name") @@ -65,7 +65,7 @@ func newCreateCommand(cli *state.State) *cobra.Command { return cmd } -func runServerCreate(cli *state.State, cmd *cobra.Command, args []string) error { +func runCreate(cli *state.State, cmd *cobra.Command, args []string) error { opts, err := optsFromFlags(cli, cmd.Flags()) if err != nil { return err diff --git a/internal/cmd/server/create_image.go b/internal/cmd/server/create_image.go index a1140397..735ad2c5 100644 --- a/internal/cmd/server/create_image.go +++ b/internal/cmd/server/create_image.go @@ -17,8 +17,8 @@ func newCreateImageCommand(cli *state.State) *cobra.Command { Args: cobra.ExactArgs(1), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: util.ChainRunE(validateServerCreateImage, cli.EnsureToken), - RunE: cli.Wrap(runServerCreateImage), + PreRunE: util.ChainRunE(validateCreateImage, cli.EnsureToken), + RunE: cli.Wrap(runCreateImage), } cmd.Flags().String("type", "", "Image type (required)") cmd.RegisterFlagCompletionFunc("type", cmpl.SuggestCandidates("backup", "snapshot")) @@ -31,7 +31,7 @@ func newCreateImageCommand(cli *state.State) *cobra.Command { return cmd } -func validateServerCreateImage(cmd *cobra.Command, args []string) error { +func validateCreateImage(cmd *cobra.Command, args []string) error { imageType, _ := cmd.Flags().GetString("type") switch hcloud.ImageType(imageType) { case hcloud.ImageTypeBackup, hcloud.ImageTypeSnapshot: @@ -43,7 +43,7 @@ func validateServerCreateImage(cmd *cobra.Command, args []string) error { return nil } -func runServerCreateImage(cli *state.State, cmd *cobra.Command, args []string) error { +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 { diff --git a/internal/cmd/server/delete.go b/internal/cmd/server/delete.go index a17cc236..73ed33bf 100644 --- a/internal/cmd/server/delete.go +++ b/internal/cmd/server/delete.go @@ -17,12 +17,12 @@ func newDeleteCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runServerDelete), + RunE: cli.Wrap(runDelete), } return cmd } -func runServerDelete(cli *state.State, cmd *cobra.Command, args []string) error { +func runDelete(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] server, _, err := cli.Client().Server.Get(cli.Context, idOrName) if err != nil { diff --git a/internal/cmd/server/describe.go b/internal/cmd/server/describe.go index d609679c..6515b27f 100644 --- a/internal/cmd/server/describe.go +++ b/internal/cmd/server/describe.go @@ -22,13 +22,13 @@ func newDescribeCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runServerDescribe), + RunE: cli.Wrap(runDescribe), } output.AddFlag(cmd, output.OptionJSON(), output.OptionFormat()) return cmd } -func runServerDescribe(cli *state.State, cmd *cobra.Command, args []string) error { +func runDescribe(cli *state.State, cmd *cobra.Command, args []string) error { outputFlags := output.FlagsForCommand(cmd) idOrName := args[0] @@ -42,15 +42,15 @@ func runServerDescribe(cli *state.State, cmd *cobra.Command, args []string) erro switch { case outputFlags.IsSet("json"): - return serverDescribeJSON(resp) + return describeJSON(resp) case outputFlags.IsSet("format"): return util.DescribeFormat(server, outputFlags["format"][0]) default: - return serverDescribeText(cli, server) + return describeText(cli, server) } } -func serverDescribeText(cli *state.State, server *hcloud.Server) error { +func describeText(cli *state.State, server *hcloud.Server) error { fmt.Printf("ID:\t\t%d\n", server.ID) fmt.Printf("Name:\t\t%s\n", server.Name) fmt.Printf("Status:\t\t%s\n", server.Status) @@ -204,7 +204,7 @@ func serverDescribeText(cli *state.State, server *hcloud.Server) error { return nil } -func serverDescribeJSON(resp *hcloud.Response) error { +func describeJSON(resp *hcloud.Response) error { var data map[string]interface{} if err := json.NewDecoder(resp.Body).Decode(&data); err != nil { return err diff --git a/internal/cmd/server/detach_from_network.go b/internal/cmd/server/detach_from_network.go index 70fc3550..107fed96 100644 --- a/internal/cmd/server/detach_from_network.go +++ b/internal/cmd/server/detach_from_network.go @@ -19,7 +19,7 @@ func newDetachFromNetworkCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runServerDetachFromNetwork), + RunE: cli.Wrap(runDetachFromNetwork), } cmd.Flags().StringP("network", "n", "", "Network (ID or name) (required)") cmd.RegisterFlagCompletionFunc("network", cmpl.SuggestCandidatesF(cli.NetworkNames)) @@ -28,7 +28,7 @@ func newDetachFromNetworkCommand(cli *state.State) *cobra.Command { return cmd } -func runServerDetachFromNetwork(cli *state.State, cmd *cobra.Command, args []string) error { +func runDetachFromNetwork(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] server, _, err := cli.Client().Server.Get(cli.Context, idOrName) if err != nil { diff --git a/internal/cmd/server/detach_iso.go b/internal/cmd/server/detach_iso.go index f3c5c3b1..e9ecb7a8 100644 --- a/internal/cmd/server/detach_iso.go +++ b/internal/cmd/server/detach_iso.go @@ -17,13 +17,13 @@ func newDetachISOCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runServerDetachISO), + RunE: cli.Wrap(runDetachISO), } return cmd } -func runServerDetachISO(cli *state.State, cmd *cobra.Command, args []string) error { +func runDetachISO(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] server, _, err := cli.Client().Server.Get(cli.Context, idOrName) if err != nil { diff --git a/internal/cmd/server/disable_backup.go b/internal/cmd/server/disable_backup.go index b85376de..62c875c6 100644 --- a/internal/cmd/server/disable_backup.go +++ b/internal/cmd/server/disable_backup.go @@ -17,12 +17,12 @@ func newDisableBackupCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runServerDisableBackup), + RunE: cli.Wrap(runDisableBackup), } return cmd } -func runServerDisableBackup(cli *state.State, cmd *cobra.Command, args []string) error { +func runDisableBackup(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] server, _, err := cli.Client().Server.Get(cli.Context, idOrName) if err != nil { diff --git a/internal/cmd/server/disable_protection.go b/internal/cmd/server/disable_protection.go index e520d5f0..30fef0a1 100644 --- a/internal/cmd/server/disable_protection.go +++ b/internal/cmd/server/disable_protection.go @@ -22,12 +22,12 @@ func newDisableProtectionCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runServerDisableProtection), + RunE: cli.Wrap(runDisableProtection), } return cmd } -func runServerDisableProtection(cli *state.State, cmd *cobra.Command, args []string) error { +func runDisableProtection(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] server, _, err := cli.Client().Server.Get(cli.Context, idOrName) if err != nil { diff --git a/internal/cmd/server/disable_rescue.go b/internal/cmd/server/disable_rescue.go index 93516720..d2e1debc 100644 --- a/internal/cmd/server/disable_rescue.go +++ b/internal/cmd/server/disable_rescue.go @@ -17,12 +17,12 @@ func newDisableRescueCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runServerDisableRescue), + RunE: cli.Wrap(runDisableRescue), } return cmd } -func runServerDisableRescue(cli *state.State, cmd *cobra.Command, args []string) error { +func runDisableRescue(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] server, _, err := cli.Client().Server.Get(cli.Context, idOrName) if err != nil { diff --git a/internal/cmd/server/enable_backup.go b/internal/cmd/server/enable_backup.go index 9da7062d..f67ce29a 100644 --- a/internal/cmd/server/enable_backup.go +++ b/internal/cmd/server/enable_backup.go @@ -17,7 +17,7 @@ func newEnableBackupCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runServerEnableBackup), + RunE: cli.Wrap(runEnableBackup), } cmd.Flags().String( "window", "", @@ -25,7 +25,7 @@ func newEnableBackupCommand(cli *state.State) *cobra.Command { return cmd } -func runServerEnableBackup(cli *state.State, cmd *cobra.Command, args []string) error { +func runEnableBackup(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] server, _, err := cli.Client().Server.Get(cli.Context, idOrName) if err != nil { diff --git a/internal/cmd/server/enable_protection.go b/internal/cmd/server/enable_protection.go index 138bd848..ec976947 100644 --- a/internal/cmd/server/enable_protection.go +++ b/internal/cmd/server/enable_protection.go @@ -22,12 +22,12 @@ func newEnableProtectionCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runServerEnableProtection), + RunE: cli.Wrap(runEnableProtection), } return cmd } -func runServerEnableProtection(cli *state.State, cmd *cobra.Command, args []string) error { +func runEnableProtection(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] server, _, err := cli.Client().Server.Get(cli.Context, idOrName) if err != nil { diff --git a/internal/cmd/server/enable_rescue.go b/internal/cmd/server/enable_rescue.go index 7671a36e..ae77e64f 100644 --- a/internal/cmd/server/enable_rescue.go +++ b/internal/cmd/server/enable_rescue.go @@ -18,7 +18,7 @@ func newEnableRescueCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runServerEnableRescue), + RunE: cli.Wrap(runEnableRescue), } cmd.Flags().String("type", "linux64", "Rescue type") cmd.RegisterFlagCompletionFunc("type", cmpl.SuggestCandidates("linux64", "linux32", "freebsd64")) @@ -28,7 +28,7 @@ func newEnableRescueCommand(cli *state.State) *cobra.Command { return cmd } -func runServerEnableRescue(cli *state.State, cmd *cobra.Command, args []string) error { +func runEnableRescue(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] server, _, err := cli.Client().Server.Get(cli.Context, idOrName) if err != nil { diff --git a/internal/cmd/server/ip.go b/internal/cmd/server/ip.go index 670e6fd6..92df722c 100644 --- a/internal/cmd/server/ip.go +++ b/internal/cmd/server/ip.go @@ -17,13 +17,13 @@ func newIPCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runServerIP), + RunE: cli.Wrap(runIP), } cmd.Flags().BoolP("ipv6", "6", false, "Print the first address of the IPv6 public server network") return cmd } -func runServerIP(cli *state.State, cmd *cobra.Command, args []string) error { +func runIP(cli *state.State, cmd *cobra.Command, args []string) error { ipv6, err := cmd.Flags().GetBool("ipv6") idOrName := args[0] server, _, err := cli.Client().Server.Get(cli.Context, idOrName) diff --git a/internal/cmd/server/list.go b/internal/cmd/server/list.go index 5359c608..d8e9e98e 100644 --- a/internal/cmd/server/list.go +++ b/internal/cmd/server/list.go @@ -13,10 +13,10 @@ import ( "github.com/spf13/cobra" ) -var serverListTableOutput *output.Table +var listTableOutput *output.Table func init() { - serverListTableOutput = describeServerListTableOutput(nil) + listTableOutput = describeListTableOutput(nil) } func newListCommand(cli *state.State) *cobra.Command { @@ -25,19 +25,19 @@ func newListCommand(cli *state.State) *cobra.Command { Short: "List servers", Long: util.ListLongDescription( "Displays a list of servers.", - serverListTableOutput.Columns(), + listTableOutput.Columns(), ), TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runServerList), + RunE: cli.Wrap(runList), } - output.AddFlag(cmd, output.OptionNoHeader(), output.OptionColumns(serverListTableOutput.Columns()), output.OptionJSON()) + output.AddFlag(cmd, output.OptionNoHeader(), output.OptionColumns(listTableOutput.Columns()), output.OptionJSON()) cmd.Flags().StringP("selector", "l", "", "Selector to filter by labels") return cmd } -func runServerList(cli *state.State, cmd *cobra.Command, args []string) error { +func runList(cli *state.State, cmd *cobra.Command, args []string) error { outOpts := output.FlagsForCommand(cmd) labelSelector, _ := cmd.Flags().GetString("selector") @@ -126,7 +126,7 @@ func runServerList(cli *state.State, cmd *cobra.Command, args []string) error { cols = outOpts["columns"] } - tw := describeServerListTableOutput(cli) + tw := describeListTableOutput(cli) if err = tw.ValidateColumns(cols); err != nil { return err } @@ -141,7 +141,7 @@ func runServerList(cli *state.State, cmd *cobra.Command, args []string) error { return nil } -func describeServerListTableOutput(cli *state.State) *output.Table { +func describeListTableOutput(cli *state.State) *output.Table { return output.NewTable(). AddAllowedFields(hcloud.Server{}). AddFieldFn("ipv4", output.FieldFn(func(obj interface{}) string { diff --git a/internal/cmd/server/metrics.go b/internal/cmd/server/metrics.go index 6cf9cf8c..b1b17bae 100644 --- a/internal/cmd/server/metrics.go +++ b/internal/cmd/server/metrics.go @@ -26,7 +26,7 @@ func newMetricsCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runServerMetrics), + RunE: cli.Wrap(runMetrics), } cmd.Flags().String("type", "", "Type of metrics you want to show") @@ -40,7 +40,7 @@ func newMetricsCommand(cli *state.State) *cobra.Command { return cmd } -func runServerMetrics(cli *state.State, cmd *cobra.Command, args []string) error { +func runMetrics(cli *state.State, cmd *cobra.Command, args []string) error { outputFlags := output.FlagsForCommand(cmd) idOrName := args[0] @@ -82,7 +82,7 @@ func runServerMetrics(cli *state.State, cmd *cobra.Command, args []string) error } switch { case outputFlags.IsSet("json"): - return serverMetricsJSON(resp) + return metricsJSON(resp) default: var keys []string for k := range m.TimeSeries { @@ -108,7 +108,7 @@ func runServerMetrics(cli *state.State, cmd *cobra.Command, args []string) error return nil } -func serverMetricsJSON(resp *hcloud.Response) error { +func metricsJSON(resp *hcloud.Response) error { var data map[string]interface{} if err := json.NewDecoder(resp.Body).Decode(&data); err != nil { return err diff --git a/internal/cmd/server/poweroff.go b/internal/cmd/server/poweroff.go index 5be57bd5..8c0c2016 100644 --- a/internal/cmd/server/poweroff.go +++ b/internal/cmd/server/poweroff.go @@ -17,12 +17,12 @@ func newPoweroffCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runServerPoweroff), + RunE: cli.Wrap(runPoweroff), } return cmd } -func runServerPoweroff(cli *state.State, cmd *cobra.Command, args []string) error { +func runPoweroff(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] server, _, err := cli.Client().Server.Get(cli.Context, idOrName) if err != nil { diff --git a/internal/cmd/server/poweron.go b/internal/cmd/server/poweron.go index a94ea3c4..7b9a6f65 100644 --- a/internal/cmd/server/poweron.go +++ b/internal/cmd/server/poweron.go @@ -17,12 +17,12 @@ func newPoweronCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runServerPoweron), + RunE: cli.Wrap(runPoweron), } return cmd } -func runServerPoweron(cli *state.State, cmd *cobra.Command, args []string) error { +func runPoweron(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] server, _, err := cli.Client().Server.Get(cli.Context, idOrName) if err != nil { diff --git a/internal/cmd/server/reboot.go b/internal/cmd/server/reboot.go index 45311df7..7ed9de78 100644 --- a/internal/cmd/server/reboot.go +++ b/internal/cmd/server/reboot.go @@ -17,12 +17,12 @@ func newRebootCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runServerReboot), + RunE: cli.Wrap(runReboot), } return cmd } -func runServerReboot(cli *state.State, cmd *cobra.Command, args []string) error { +func runReboot(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] server, _, err := cli.Client().Server.Get(cli.Context, idOrName) if err != nil { diff --git a/internal/cmd/server/rebuild.go b/internal/cmd/server/rebuild.go index 94576f86..8fb8b5e9 100644 --- a/internal/cmd/server/rebuild.go +++ b/internal/cmd/server/rebuild.go @@ -18,7 +18,7 @@ func newRebuildCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runServerRebuild), + RunE: cli.Wrap(runRebuild), } cmd.Flags().String("image", "", "ID or name of image to rebuild from (required)") @@ -28,7 +28,7 @@ func newRebuildCommand(cli *state.State) *cobra.Command { return cmd } -func runServerRebuild(cli *state.State, cmd *cobra.Command, args []string) error { +func runRebuild(cli *state.State, cmd *cobra.Command, args []string) error { serverIDOrName := args[0] server, _, err := cli.Client().Server.Get(cli.Context, serverIDOrName) if err != nil { diff --git a/internal/cmd/server/remove_label.go b/internal/cmd/server/remove_label.go index 9ce2dd9b..fdf2148b 100644 --- a/internal/cmd/server/remove_label.go +++ b/internal/cmd/server/remove_label.go @@ -27,15 +27,15 @@ func newRemoveLabelCommand(cli *state.State) *cobra.Command { ), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: util.ChainRunE(validateServerRemoveLabel, cli.EnsureToken), - RunE: cli.Wrap(runServerRemoveLabel), + PreRunE: util.ChainRunE(validateRemoveLabel, cli.EnsureToken), + RunE: cli.Wrap(runRemoveLabel), } cmd.Flags().BoolP("all", "a", false, "Remove all labels") return cmd } -func validateServerRemoveLabel(cmd *cobra.Command, args []string) error { +func validateRemoveLabel(cmd *cobra.Command, args []string) error { all, _ := cmd.Flags().GetBool("all") if all && len(args) == 2 { @@ -48,7 +48,7 @@ func validateServerRemoveLabel(cmd *cobra.Command, args []string) error { return nil } -func runServerRemoveLabel(cli *state.State, cmd *cobra.Command, args []string) error { +func runRemoveLabel(cli *state.State, cmd *cobra.Command, args []string) error { all, _ := cmd.Flags().GetBool("all") idOrName := args[0] server, _, err := cli.Client().Server.Get(cli.Context, idOrName) diff --git a/internal/cmd/server/request_console.go b/internal/cmd/server/request_console.go index d2c48a04..1e2b20cf 100644 --- a/internal/cmd/server/request_console.go +++ b/internal/cmd/server/request_console.go @@ -19,13 +19,13 @@ func newRequestConsoleCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runServerRequestConsole), + RunE: cli.Wrap(runRequestConsole), } output.AddFlag(cmd, output.OptionJSON()) return cmd } -func runServerRequestConsole(cli *state.State, cmd *cobra.Command, args []string) error { +func runRequestConsole(cli *state.State, cmd *cobra.Command, args []string) error { outOpts := output.FlagsForCommand(cmd) idOrName := args[0] server, _, err := cli.Client().Server.Get(cli.Context, idOrName) diff --git a/internal/cmd/server/reset.go b/internal/cmd/server/reset.go index 4cf1cd90..87017836 100644 --- a/internal/cmd/server/reset.go +++ b/internal/cmd/server/reset.go @@ -17,12 +17,12 @@ func newResetCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runServerReset), + RunE: cli.Wrap(runReset), } return cmd } -func runServerReset(cli *state.State, cmd *cobra.Command, args []string) error { +func runReset(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] server, _, err := cli.Client().Server.Get(cli.Context, idOrName) if err != nil { diff --git a/internal/cmd/server/reset_password.go b/internal/cmd/server/reset_password.go index fa5ae4c2..a3d18cf7 100644 --- a/internal/cmd/server/reset_password.go +++ b/internal/cmd/server/reset_password.go @@ -17,12 +17,12 @@ func newResetPasswordCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runServerResetPassword), + RunE: cli.Wrap(runResetPassword), } return cmd } -func runServerResetPassword(cli *state.State, cmd *cobra.Command, args []string) error { +func runResetPassword(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] server, _, err := cli.Client().Server.Get(cli.Context, idOrName) if err != nil { diff --git a/internal/cmd/server/set_rdns.go b/internal/cmd/server/set_rdns.go index aaf10c30..2942849c 100644 --- a/internal/cmd/server/set_rdns.go +++ b/internal/cmd/server/set_rdns.go @@ -18,7 +18,7 @@ func newSetRDNSCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runServerSetRDNS), + RunE: cli.Wrap(runSetRDNS), } cmd.Flags().StringP("hostname", "r", "", "Hostname to set as a reverse DNS PTR entry (required)") @@ -29,7 +29,7 @@ func newSetRDNSCommand(cli *state.State) *cobra.Command { return cmd } -func runServerSetRDNS(cli *state.State, cmd *cobra.Command, args []string) error { +func runSetRDNS(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] server, _, err := cli.Client().Server.Get(cli.Context, idOrName) if err != nil { diff --git a/internal/cmd/server/shutdown.go b/internal/cmd/server/shutdown.go index 48abe7c0..ef725a4f 100644 --- a/internal/cmd/server/shutdown.go +++ b/internal/cmd/server/shutdown.go @@ -17,12 +17,12 @@ func newShutdownCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runServerShutdown), + RunE: cli.Wrap(runShutdown), } return cmd } -func runServerShutdown(cli *state.State, cmd *cobra.Command, args []string) error { +func runShutdown(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] server, _, err := cli.Client().Server.Get(cli.Context, idOrName) if err != nil { diff --git a/internal/cmd/server/ssh.go b/internal/cmd/server/ssh.go index fd71afe0..98b0d9ae 100644 --- a/internal/cmd/server/ssh.go +++ b/internal/cmd/server/ssh.go @@ -21,7 +21,7 @@ func newSSHCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runServerSSH), + RunE: cli.Wrap(runSSH), } cmd.Flags().SetInterspersed(false) // To make "hcloud server ssh uname -a" execute "uname -a" cmd.Flags().Bool("ipv6", false, "Establish SSH connection to IPv6 address") @@ -30,7 +30,7 @@ func newSSHCommand(cli *state.State) *cobra.Command { return cmd } -func runServerSSH(cli *state.State, cmd *cobra.Command, args []string) error { +func runSSH(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] server, _, err := cli.Client().Server.Get(cli.Context, idOrName) if err != nil { diff --git a/internal/cmd/server/update.go b/internal/cmd/server/update.go index 90f3d8d4..12605c4a 100644 --- a/internal/cmd/server/update.go +++ b/internal/cmd/server/update.go @@ -19,7 +19,7 @@ func newUpdateCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runServerUpdate), + RunE: cli.Wrap(runUpdate), } cmd.Flags().String("name", "", "Server name") @@ -27,7 +27,7 @@ func newUpdateCommand(cli *state.State) *cobra.Command { return cmd } -func runServerUpdate(cli *state.State, cmd *cobra.Command, args []string) error { +func runUpdate(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] server, _, err := cli.Client().Server.Get(cli.Context, idOrName) if err != nil { diff --git a/internal/cmd/servertype/describe.go b/internal/cmd/servertype/describe.go index ff7bbaa3..31318029 100644 --- a/internal/cmd/servertype/describe.go +++ b/internal/cmd/servertype/describe.go @@ -21,13 +21,13 @@ func newDescribeCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runServerTypeDescribe), + RunE: cli.Wrap(runDescribe), } output.AddFlag(cmd, output.OptionJSON(), output.OptionFormat()) return cmd } -func runServerTypeDescribe(cli *state.State, cmd *cobra.Command, args []string) error { +func runDescribe(cli *state.State, cmd *cobra.Command, args []string) error { outputFlags := output.FlagsForCommand(cmd) idOrName := args[0] @@ -41,15 +41,15 @@ func runServerTypeDescribe(cli *state.State, cmd *cobra.Command, args []string) switch { case outputFlags.IsSet("json"): - return serverTypeDescribeJSON(resp) + return describeJSON(resp) case outputFlags.IsSet("format"): return util.DescribeFormat(serverType, outputFlags["format"][0]) default: - return serverTypeDescribeText(cli, serverType) + return describeText(cli, serverType) } } -func serverTypeDescribeText(cli *state.State, serverType *hcloud.ServerType) error { +func describeText(cli *state.State, serverType *hcloud.ServerType) error { fmt.Printf("ID:\t\t%d\n", serverType.ID) fmt.Printf("Name:\t\t%s\n", serverType.Name) fmt.Printf("Description:\t%s\n", serverType.Description) @@ -68,7 +68,7 @@ func serverTypeDescribeText(cli *state.State, serverType *hcloud.ServerType) err return nil } -func serverTypeDescribeJSON(resp *hcloud.Response) error { +func describeJSON(resp *hcloud.Response) error { var data map[string]interface{} if err := json.NewDecoder(resp.Body).Decode(&data); err != nil { return err diff --git a/internal/cmd/servertype/list.go b/internal/cmd/servertype/list.go index 39b3b4f7..800b858f 100644 --- a/internal/cmd/servertype/list.go +++ b/internal/cmd/servertype/list.go @@ -12,10 +12,10 @@ import ( "github.com/spf13/cobra" ) -var serverTypeListTableOutput *output.Table +var listTableOutput *output.Table func init() { - serverTypeListTableOutput = output.NewTable(). + listTableOutput = output.NewTable(). AddAllowedFields(hcloud.ServerType{}). AddFieldAlias("storagetype", "storage type"). AddFieldFn("memory", output.FieldFn(func(obj interface{}) string { @@ -34,18 +34,18 @@ func newListCommand(cli *state.State) *cobra.Command { Short: "List server types", Long: util.ListLongDescription( "Displays a list of server types.", - serverTypeListTableOutput.Columns(), + listTableOutput.Columns(), ), TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runServerTypeList), + RunE: cli.Wrap(runList), } - output.AddFlag(cmd, output.OptionNoHeader(), output.OptionColumns(serverTypeListTableOutput.Columns()), output.OptionJSON()) + output.AddFlag(cmd, output.OptionNoHeader(), output.OptionColumns(listTableOutput.Columns()), output.OptionJSON()) return cmd } -func runServerTypeList(cli *state.State, cmd *cobra.Command, args []string) error { +func runList(cli *state.State, cmd *cobra.Command, args []string) error { outOpts := output.FlagsForCommand(cmd) serverTypes, err := cli.Client().ServerType.All(cli.Context) @@ -66,7 +66,7 @@ func runServerTypeList(cli *state.State, cmd *cobra.Command, args []string) erro cols = outOpts["columns"] } - tw := serverTypeListTableOutput + tw := listTableOutput if err = tw.ValidateColumns(cols); err != nil { return err } diff --git a/internal/cmd/sshkey/add_label.go b/internal/cmd/sshkey/add_label.go index cd118ca7..17defbb4 100644 --- a/internal/cmd/sshkey/add_label.go +++ b/internal/cmd/sshkey/add_label.go @@ -18,15 +18,15 @@ func newAddLabelCommand(cli *state.State) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.SSHKeyNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: util.ChainRunE(validateSSHKeyAddLabel, cli.EnsureToken), - RunE: cli.Wrap(runSSHKeyAddLabel), + PreRunE: util.ChainRunE(validateAddLabel, cli.EnsureToken), + RunE: cli.Wrap(runAddLabel), } cmd.Flags().BoolP("overwrite", "o", false, "Overwrite label if it exists already") return cmd } -func validateSSHKeyAddLabel(cmd *cobra.Command, args []string) error { +func validateAddLabel(cmd *cobra.Command, args []string) error { label := util.SplitLabel(args[1]) if len(label) != 2 { return fmt.Errorf("invalid label: %s", args[1]) @@ -35,7 +35,7 @@ func validateSSHKeyAddLabel(cmd *cobra.Command, args []string) error { return nil } -func runSSHKeyAddLabel(cli *state.State, cmd *cobra.Command, args []string) error { +func runAddLabel(cli *state.State, cmd *cobra.Command, args []string) error { overwrite, _ := cmd.Flags().GetBool("overwrite") idOrName := args[0] sshKey, _, err := cli.Client().SSHKey.Get(cli.Context, idOrName) diff --git a/internal/cmd/sshkey/create.go b/internal/cmd/sshkey/create.go index 7ef3fe3d..c2c8107b 100644 --- a/internal/cmd/sshkey/create.go +++ b/internal/cmd/sshkey/create.go @@ -19,8 +19,8 @@ func newCreateCommand(cli *state.State) *cobra.Command { Args: cobra.NoArgs, TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: util.ChainRunE(validateSSHKeyCreate, cli.EnsureToken), - RunE: cli.Wrap(runSSHKeyCreate), + PreRunE: util.ChainRunE(validateCreate, cli.EnsureToken), + RunE: cli.Wrap(runCreate), } cmd.Flags().String("name", "", "Key name (required)") cmd.Flags().String("public-key", "", "Public key") @@ -29,7 +29,7 @@ func newCreateCommand(cli *state.State) *cobra.Command { return cmd } -func validateSSHKeyCreate(cmd *cobra.Command, args []string) error { +func validateCreate(cmd *cobra.Command, args []string) error { if name, _ := cmd.Flags().GetString("name"); name == "" { return errors.New("flag --name is required") } @@ -43,7 +43,7 @@ func validateSSHKeyCreate(cmd *cobra.Command, args []string) error { return nil } -func runSSHKeyCreate(cli *state.State, cmd *cobra.Command, args []string) error { +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") diff --git a/internal/cmd/sshkey/delete.go b/internal/cmd/sshkey/delete.go index 4cda8e4d..7054f7d9 100644 --- a/internal/cmd/sshkey/delete.go +++ b/internal/cmd/sshkey/delete.go @@ -17,12 +17,12 @@ func newDeleteCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runSSHKeyDelete), + RunE: cli.Wrap(runDelete), } return cmd } -func runSSHKeyDelete(cli *state.State, cmd *cobra.Command, args []string) error { +func runDelete(cli *state.State, cmd *cobra.Command, args []string) error { sshKey, _, err := cli.Client().SSHKey.Get(cli.Context, args[0]) if err != nil { return err diff --git a/internal/cmd/sshkey/describe.go b/internal/cmd/sshkey/describe.go index f1040c5c..159383c6 100644 --- a/internal/cmd/sshkey/describe.go +++ b/internal/cmd/sshkey/describe.go @@ -24,13 +24,13 @@ func newDescribeCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runSSHKeyDescribe), + RunE: cli.Wrap(runDescribe), } output.AddFlag(cmd, output.OptionJSON(), output.OptionFormat()) return cmd } -func runSSHKeyDescribe(cli *state.State, cmd *cobra.Command, args []string) error { +func runDescribe(cli *state.State, cmd *cobra.Command, args []string) error { outputFlags := output.FlagsForCommand(cmd) sshKey, resp, err := cli.Client().SSHKey.Get(cli.Context, args[0]) @@ -43,15 +43,15 @@ func runSSHKeyDescribe(cli *state.State, cmd *cobra.Command, args []string) erro switch { case outputFlags.IsSet("json"): - return sshKeyDescribeJSON(resp) + return describeJSON(resp) case outputFlags.IsSet("format"): return util.DescribeFormat(sshKey, outputFlags["format"][0]) default: - return sshKeyDescribeText(cli, sshKey) + return describeText(cli, sshKey) } } -func sshKeyDescribeText(cli *state.State, sshKey *hcloud.SSHKey) error { +func describeText(cli *state.State, sshKey *hcloud.SSHKey) error { fmt.Printf("ID:\t\t%d\n", sshKey.ID) fmt.Printf("Name:\t\t%s\n", sshKey.Name) fmt.Printf("Created:\t%s (%s)\n", util.Datetime(sshKey.Created), humanize.Time(sshKey.Created)) @@ -69,7 +69,7 @@ func sshKeyDescribeText(cli *state.State, sshKey *hcloud.SSHKey) error { return nil } -func sshKeyDescribeJSON(resp *hcloud.Response) error { +func describeJSON(resp *hcloud.Response) error { var data map[string]interface{} if err := json.NewDecoder(resp.Body).Decode(&data); err != nil { return err diff --git a/internal/cmd/sshkey/list.go b/internal/cmd/sshkey/list.go index 74984340..e5fb40ae 100644 --- a/internal/cmd/sshkey/list.go +++ b/internal/cmd/sshkey/list.go @@ -9,10 +9,10 @@ import ( "github.com/spf13/cobra" ) -var sshKeyListTableOutput *output.Table +var listTableOutput *output.Table func init() { - sshKeyListTableOutput = output.NewTable(). + listTableOutput = output.NewTable(). AddAllowedFields(hcloud.SSHKey{}). AddFieldFn("labels", output.FieldFn(func(obj interface{}) string { sshKey := obj.(*hcloud.SSHKey) @@ -30,19 +30,19 @@ func newListCommand(cli *state.State) *cobra.Command { Short: "List SSH keys", Long: util.ListLongDescription( "Displays a list of SSH keys.", - sshKeyListTableOutput.Columns(), + listTableOutput.Columns(), ), TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runSSHKeyList), + RunE: cli.Wrap(runList), } - output.AddFlag(cmd, output.OptionNoHeader(), output.OptionColumns(sshKeyListTableOutput.Columns()), output.OptionJSON()) + output.AddFlag(cmd, output.OptionNoHeader(), output.OptionColumns(listTableOutput.Columns()), output.OptionJSON()) cmd.Flags().StringP("selector", "l", "", "Selector to filter by labels") return cmd } -func runSSHKeyList(cli *state.State, cmd *cobra.Command, args []string) error { +func runList(cli *state.State, cmd *cobra.Command, args []string) error { outOpts := output.FlagsForCommand(cmd) labelSelector, _ := cmd.Flags().GetString("selector") @@ -78,7 +78,7 @@ func runSSHKeyList(cli *state.State, cmd *cobra.Command, args []string) error { cols = outOpts["columns"] } - tw := sshKeyListTableOutput + tw := listTableOutput if err = tw.ValidateColumns(cols); err != nil { return err } diff --git a/internal/cmd/sshkey/remove_label.go b/internal/cmd/sshkey/remove_label.go index b382976d..41e9a83e 100644 --- a/internal/cmd/sshkey/remove_label.go +++ b/internal/cmd/sshkey/remove_label.go @@ -27,15 +27,15 @@ func newRemoveLabelCommand(cli *state.State) *cobra.Command { ), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: util.ChainRunE(validateSSHKeyRemoveLabel, cli.EnsureToken), - RunE: cli.Wrap(runSSHKeyRemoveLabel), + PreRunE: util.ChainRunE(validateRemoveLabel, cli.EnsureToken), + RunE: cli.Wrap(runRemoveLabel), } cmd.Flags().BoolP("all", "a", false, "Remove all labels") return cmd } -func validateSSHKeyRemoveLabel(cmd *cobra.Command, args []string) error { +func validateRemoveLabel(cmd *cobra.Command, args []string) error { all, _ := cmd.Flags().GetBool("all") if all && len(args) == 2 { @@ -48,7 +48,7 @@ func validateSSHKeyRemoveLabel(cmd *cobra.Command, args []string) error { return nil } -func runSSHKeyRemoveLabel(cli *state.State, cmd *cobra.Command, args []string) error { +func runRemoveLabel(cli *state.State, cmd *cobra.Command, args []string) error { all, _ := cmd.Flags().GetBool("all") idOrName := args[0] sshKey, _, err := cli.Client().SSHKey.Get(cli.Context, idOrName) diff --git a/internal/cmd/sshkey/update.go b/internal/cmd/sshkey/update.go index f5c9dd5c..19edf7b8 100644 --- a/internal/cmd/sshkey/update.go +++ b/internal/cmd/sshkey/update.go @@ -19,7 +19,7 @@ func newUpdateCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runSSHKeyUpdate), + RunE: cli.Wrap(runUpdate), } cmd.Flags().String("name", "", "SSH key name") @@ -27,7 +27,7 @@ func newUpdateCommand(cli *state.State) *cobra.Command { return cmd } -func runSSHKeyUpdate(cli *state.State, cmd *cobra.Command, args []string) error { +func runUpdate(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] sshKey, _, err := cli.Client().SSHKey.Get(cli.Context, idOrName) if err != nil { diff --git a/internal/cmd/volume/add_label.go b/internal/cmd/volume/add_label.go index 24efb4d6..b7213d57 100644 --- a/internal/cmd/volume/add_label.go +++ b/internal/cmd/volume/add_label.go @@ -18,15 +18,15 @@ func newAddLabelCommand(cli *state.State) *cobra.Command { ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.VolumeNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: util.ChainRunE(validateVolumeAddLabel, cli.EnsureToken), - RunE: cli.Wrap(runVolumeAddLabel), + PreRunE: util.ChainRunE(validateAddLabel, cli.EnsureToken), + RunE: cli.Wrap(runAddLabel), } cmd.Flags().BoolP("overwrite", "o", false, "Overwrite label if it exists already") return cmd } -func validateVolumeAddLabel(cmd *cobra.Command, args []string) error { +func validateAddLabel(cmd *cobra.Command, args []string) error { label := util.SplitLabel(args[1]) if len(label) != 2 { return fmt.Errorf("invalid label: %s", args[1]) @@ -35,7 +35,7 @@ func validateVolumeAddLabel(cmd *cobra.Command, args []string) error { return nil } -func runVolumeAddLabel(cli *state.State, cmd *cobra.Command, args []string) error { +func runAddLabel(cli *state.State, cmd *cobra.Command, args []string) error { overwrite, _ := cmd.Flags().GetBool("overwrite") volume, _, err := cli.Client().Volume.Get(cli.Context, args[0]) if err != nil { diff --git a/internal/cmd/volume/attach.go b/internal/cmd/volume/attach.go index 8a20d43f..2f06f512 100644 --- a/internal/cmd/volume/attach.go +++ b/internal/cmd/volume/attach.go @@ -18,7 +18,7 @@ func newAttachCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runVolumeAttach), + RunE: cli.Wrap(runAttach), } cmd.Flags().String("server", "", "Server (ID or name) (required)") cmd.RegisterFlagCompletionFunc("server", cmpl.SuggestCandidatesF(cli.ServerNames)) @@ -28,7 +28,7 @@ func newAttachCommand(cli *state.State) *cobra.Command { return cmd } -func runVolumeAttach(cli *state.State, cmd *cobra.Command, args []string) error { +func runAttach(cli *state.State, cmd *cobra.Command, args []string) error { volume, _, err := cli.Client().Volume.Get(cli.Context, args[0]) if err != nil { return err diff --git a/internal/cmd/volume/create.go b/internal/cmd/volume/create.go index a99aa11e..1095e09e 100644 --- a/internal/cmd/volume/create.go +++ b/internal/cmd/volume/create.go @@ -18,7 +18,7 @@ func newCreateCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runVolumeCreate), + RunE: cli.Wrap(runCreate), } cmd.Flags().String("name", "", "Volume name (required)") cmd.MarkFlagRequired("name") @@ -40,7 +40,7 @@ func newCreateCommand(cli *state.State) *cobra.Command { return cmd } -func runVolumeCreate(cli *state.State, cmd *cobra.Command, args []string) error { +func runCreate(cli *state.State, cmd *cobra.Command, args []string) error { name, _ := cmd.Flags().GetString("name") serverIDOrName, _ := cmd.Flags().GetString("server") size, _ := cmd.Flags().GetInt("size") diff --git a/internal/cmd/volume/delete.go b/internal/cmd/volume/delete.go index 7a6dae15..a1ae78ae 100644 --- a/internal/cmd/volume/delete.go +++ b/internal/cmd/volume/delete.go @@ -17,12 +17,12 @@ func newDeleteCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runVolumeDelete), + RunE: cli.Wrap(runDelete), } return cmd } -func runVolumeDelete(cli *state.State, cmd *cobra.Command, args []string) error { +func runDelete(cli *state.State, cmd *cobra.Command, args []string) error { volume, _, err := cli.Client().Volume.Get(cli.Context, args[0]) if err != nil { return err diff --git a/internal/cmd/volume/describe.go b/internal/cmd/volume/describe.go index f7f5ed81..4b93a38e 100644 --- a/internal/cmd/volume/describe.go +++ b/internal/cmd/volume/describe.go @@ -22,13 +22,13 @@ func newDescribeCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runVolumeDescribe), + RunE: cli.Wrap(runDescribe), } output.AddFlag(cmd, output.OptionJSON(), output.OptionFormat()) return cmd } -func runVolumeDescribe(cli *state.State, cmd *cobra.Command, args []string) error { +func runDescribe(cli *state.State, cmd *cobra.Command, args []string) error { outputFlags := output.FlagsForCommand(cmd) volume, resp, err := cli.Client().Volume.Get(cli.Context, args[0]) @@ -41,15 +41,15 @@ func runVolumeDescribe(cli *state.State, cmd *cobra.Command, args []string) erro switch { case outputFlags.IsSet("json"): - return volumeDescribeJSON(resp) + return describeJSON(resp) case outputFlags.IsSet("format"): return util.DescribeFormat(volume, outputFlags["format"][0]) default: - return volumeDescribeText(cli, volume) + return describeText(cli, volume) } } -func volumeDescribeText(cli *state.State, volume *hcloud.Volume) error { +func describeText(cli *state.State, volume *hcloud.Volume) error { fmt.Printf("ID:\t\t%d\n", volume.ID) fmt.Printf("Name:\t\t%s\n", volume.Name) fmt.Printf("Created:\t%s (%s)\n", util.Datetime(volume.Created), humanize.Time(volume.Created)) @@ -91,7 +91,7 @@ func volumeDescribeText(cli *state.State, volume *hcloud.Volume) error { return nil } -func volumeDescribeJSON(resp *hcloud.Response) error { +func describeJSON(resp *hcloud.Response) error { var data map[string]interface{} if err := json.NewDecoder(resp.Body).Decode(&data); err != nil { return err diff --git a/internal/cmd/volume/detach.go b/internal/cmd/volume/detach.go index 17383474..5e7d8883 100644 --- a/internal/cmd/volume/detach.go +++ b/internal/cmd/volume/detach.go @@ -17,12 +17,12 @@ func newDetachCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runVolumeDetach), + RunE: cli.Wrap(runDetach), } return cmd } -func runVolumeDetach(cli *state.State, cmd *cobra.Command, args []string) error { +func runDetach(cli *state.State, cmd *cobra.Command, args []string) error { volume, _, err := cli.Client().Volume.Get(cli.Context, args[0]) if err != nil { return err diff --git a/internal/cmd/volume/disable_protection.go b/internal/cmd/volume/disable_protection.go index 177ea7f8..c72f1094 100644 --- a/internal/cmd/volume/disable_protection.go +++ b/internal/cmd/volume/disable_protection.go @@ -22,12 +22,12 @@ func newDisableProtectionCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runVolumeDisableProtection), + RunE: cli.Wrap(runDisableProtection), } return cmd } -func runVolumeDisableProtection(cli *state.State, cmd *cobra.Command, args []string) error { +func runDisableProtection(cli *state.State, cmd *cobra.Command, args []string) error { volume, _, err := cli.Client().Volume.Get(cli.Context, args[0]) if err != nil { return err diff --git a/internal/cmd/volume/enable_protection.go b/internal/cmd/volume/enable_protection.go index 37be145e..63965244 100644 --- a/internal/cmd/volume/enable_protection.go +++ b/internal/cmd/volume/enable_protection.go @@ -22,12 +22,12 @@ func newEnableProtectionCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runVolumeEnableProtection), + RunE: cli.Wrap(runEnableProtection), } return cmd } -func runVolumeEnableProtection(cli *state.State, cmd *cobra.Command, args []string) error { +func runEnableProtection(cli *state.State, cmd *cobra.Command, args []string) error { volume, _, err := cli.Client().Volume.Get(cli.Context, args[0]) if err != nil { return err diff --git a/internal/cmd/volume/list.go b/internal/cmd/volume/list.go index 6fba6cd2..367f9528 100644 --- a/internal/cmd/volume/list.go +++ b/internal/cmd/volume/list.go @@ -13,10 +13,10 @@ import ( "github.com/spf13/cobra" ) -var volumeListTableOutput *output.Table +var listTableOutput *output.Table func init() { - volumeListTableOutput = describeVolumeListTableOutput(nil) + listTableOutput = describeListTableOutput(nil) } func newListCommand(cli *state.State) *cobra.Command { @@ -25,19 +25,19 @@ func newListCommand(cli *state.State) *cobra.Command { Short: "List volumes", Long: util.ListLongDescription( "Displays a list of volumes.", - volumeListTableOutput.Columns(), + listTableOutput.Columns(), ), TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runVolumeList), + RunE: cli.Wrap(runList), } - output.AddFlag(cmd, output.OptionNoHeader(), output.OptionColumns(volumeListTableOutput.Columns()), output.OptionJSON()) + output.AddFlag(cmd, output.OptionNoHeader(), output.OptionColumns(listTableOutput.Columns()), output.OptionJSON()) cmd.Flags().StringP("selector", "l", "", "Selector to filter by labels") return cmd } -func runVolumeList(cli *state.State, cmd *cobra.Command, args []string) error { +func runList(cli *state.State, cmd *cobra.Command, args []string) error { outOpts := output.FlagsForCommand(cmd) labelSelector, _ := cmd.Flags().GetString("selector") @@ -78,7 +78,7 @@ func runVolumeList(cli *state.State, cmd *cobra.Command, args []string) error { cols = outOpts["columns"] } - tw := describeVolumeListTableOutput(cli) + tw := describeListTableOutput(cli) if err = tw.ValidateColumns(cols); err != nil { return err } @@ -93,7 +93,7 @@ func runVolumeList(cli *state.State, cmd *cobra.Command, args []string) error { return nil } -func describeVolumeListTableOutput(cli *state.State) *output.Table { +func describeListTableOutput(cli *state.State) *output.Table { return output.NewTable(). AddAllowedFields(hcloud.Volume{}). AddFieldFn("server", output.FieldFn(func(obj interface{}) string { diff --git a/internal/cmd/volume/remove_label.go b/internal/cmd/volume/remove_label.go index 9d297482..6488fda4 100644 --- a/internal/cmd/volume/remove_label.go +++ b/internal/cmd/volume/remove_label.go @@ -27,15 +27,15 @@ func newRemoveLabelCommand(cli *state.State) *cobra.Command { ), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: util.ChainRunE(validateVolumeRemoveLabel, cli.EnsureToken), - RunE: cli.Wrap(runVolumeRemoveLabel), + PreRunE: util.ChainRunE(validateRemoveLabel, cli.EnsureToken), + RunE: cli.Wrap(runRemoveLabel), } cmd.Flags().BoolP("all", "a", false, "Remove all labels") return cmd } -func validateVolumeRemoveLabel(cmd *cobra.Command, args []string) error { +func validateRemoveLabel(cmd *cobra.Command, args []string) error { all, _ := cmd.Flags().GetBool("all") if all && len(args) == 2 { @@ -48,7 +48,7 @@ func validateVolumeRemoveLabel(cmd *cobra.Command, args []string) error { return nil } -func runVolumeRemoveLabel(cli *state.State, cmd *cobra.Command, args []string) error { +func runRemoveLabel(cli *state.State, cmd *cobra.Command, args []string) error { all, _ := cmd.Flags().GetBool("all") volume, _, err := cli.Client().Volume.Get(cli.Context, args[0]) if err != nil { diff --git a/internal/cmd/volume/resize.go b/internal/cmd/volume/resize.go index a1014aac..608e79d3 100644 --- a/internal/cmd/volume/resize.go +++ b/internal/cmd/volume/resize.go @@ -17,14 +17,14 @@ func newResizeCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runVolumeResize), + RunE: cli.Wrap(runResize), } cmd.Flags().Int("size", 0, "New size (GB) of the volume (required)") cmd.MarkFlagRequired("size") return cmd } -func runVolumeResize(cli *state.State, cmd *cobra.Command, args []string) error { +func runResize(cli *state.State, cmd *cobra.Command, args []string) error { volume, _, err := cli.Client().Volume.Get(cli.Context, args[0]) if err != nil { return err diff --git a/internal/cmd/volume/update.go b/internal/cmd/volume/update.go index 84ed51e3..ac98ea71 100644 --- a/internal/cmd/volume/update.go +++ b/internal/cmd/volume/update.go @@ -19,7 +19,7 @@ func newUpdateCommand(cli *state.State) *cobra.Command { TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runVolumeUpdate), + RunE: cli.Wrap(runUpdate), } cmd.Flags().String("name", "", "Volume name") @@ -27,7 +27,7 @@ func newUpdateCommand(cli *state.State) *cobra.Command { return cmd } -func runVolumeUpdate(cli *state.State, cmd *cobra.Command, args []string) error { +func runUpdate(cli *state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] volume, _, err := cli.Client().Volume.Get(cli.Context, idOrName) if err != nil {