diff --git a/internal/cmd/server/attach_iso.go b/internal/cmd/server/attach_iso.go index 0b66eb4f..2abaa6a5 100644 --- a/internal/cmd/server/attach_iso.go +++ b/internal/cmd/server/attach_iso.go @@ -1,59 +1,59 @@ package server import ( + "context" "fmt" + "github.com/hetznercloud/cli/internal/cmd/base" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/hcapi2" "github.com/hetznercloud/cli/internal/state" "github.com/spf13/cobra" ) -func newAttachISOCommand(cli *state.State) *cobra.Command { - cmd := &cobra.Command{ - Use: "attach-iso [FLAGS] SERVER ISO", - Short: "Attach an ISO to a server", - Args: cobra.ExactArgs(2), - TraverseChildren: true, - ValidArgsFunction: cmpl.SuggestArgs( - cmpl.SuggestCandidatesF(cli.ServerNames), - cmpl.SuggestCandidatesF(cli.ISONames), - ), - DisableFlagsInUseLine: true, - PreRunE: cli.EnsureToken, - RunE: cli.Wrap(runAttachISO), - } - - return cmd -} - -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 { - return err - } - if server == nil { - return fmt.Errorf("server not found: %s", idOrName) - } - - isoIDOrName := args[1] - iso, _, err := cli.Client().ISO.Get(cli.Context, isoIDOrName) - if err != nil { - return err - } - if iso == nil { - return fmt.Errorf("ISO not found: %s", isoIDOrName) - } - - action, _, err := cli.Client().Server.AttachISO(cli.Context, server, iso) - if err != nil { - return err - } - - if err := cli.ActionProgress(cli.Context, action); err != nil { - return err - } - - fmt.Printf("ISO %s attached to server %d\n", iso.Name, server.ID) - return nil +var AttachISOCommand = base.Cmd{ + BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { + return &cobra.Command{ + Use: "attach-iso [FLAGS] SERVER ISO", + Short: "Attach an ISO to a server", + Args: cobra.ExactArgs(2), + TraverseChildren: true, + ValidArgsFunction: cmpl.SuggestArgs( + cmpl.SuggestCandidatesF(client.Server().Names), + cmpl.SuggestCandidatesF(client.ISO().Names), + ), + DisableFlagsInUseLine: true, + } + }, + Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, command *cobra.Command, args []string) error { + idOrName := args[0] + server, _, err := client.Server().Get(ctx, idOrName) + if err != nil { + return err + } + if server == nil { + return fmt.Errorf("server not found: %s", idOrName) + } + + isoIDOrName := args[1] + iso, _, err := client.ISO().Get(ctx, isoIDOrName) + if err != nil { + return err + } + if iso == nil { + return fmt.Errorf("ISO not found: %s", isoIDOrName) + } + + action, _, err := client.Server().AttachISO(ctx, server, iso) + if err != nil { + return err + } + + if err := waiter.ActionProgress(ctx, action); err != nil { + return err + } + + fmt.Printf("ISO %s attached to server %d\n", iso.Name, server.ID) + return nil + }, } diff --git a/internal/cmd/server/server.go b/internal/cmd/server/server.go index 8db63878..f5d8f5cc 100644 --- a/internal/cmd/server/server.go +++ b/internal/cmd/server/server.go @@ -28,7 +28,7 @@ func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command { newResetPasswordCommand(cli), newEnableRescueCommand(cli), newDisableRescueCommand(cli), - newAttachISOCommand(cli), + AttachISOCommand.CobraCommand(cli.Context, client, cli, cli), newDetachISOCommand(cli), updateCmd.CobraCommand(cli.Context, client, cli), newChangeTypeCommand(cli), diff --git a/internal/hcapi/iso.go b/internal/hcapi/iso.go deleted file mode 100644 index 62919b1f..00000000 --- a/internal/hcapi/iso.go +++ /dev/null @@ -1,33 +0,0 @@ -package hcapi - -import ( - "context" - "strconv" - - "github.com/hetznercloud/hcloud-go/hcloud" -) - -// ISOClient embeds the Hetzner Cloud ISO client and provides some additional -// helper functions. -type ISOClient struct { - *hcloud.ISOClient -} - -// ISONames obtains a list of available ISOs for the current account. It -// returns nil if the current project has no ISOs or the ISO names could not be -// fetched. -func (c *ISOClient) ISONames() []string { - isos, err := c.All(context.Background()) - if err != nil || len(isos) == 0 { - return nil - } - names := make([]string, len(isos)) - for i, iso := range isos { - name := iso.Name - if name == "" { - name = strconv.Itoa(iso.ID) - } - names[i] = name - } - return names -} diff --git a/internal/state/helpers.go b/internal/state/helpers.go index df2f53c0..3753db46 100644 --- a/internal/state/helpers.go +++ b/internal/state/helpers.go @@ -87,14 +87,6 @@ func (c *State) FloatingIPLabelKeys(idOrName string) []string { return c.floatingIPClient.FloatingIPLabelKeys(idOrName) } -func (c *State) ISONames() []string { - if c.isoClient == nil { - client := c.Client() - c.isoClient = &hcapi.ISOClient{ISOClient: &client.ISO} - } - return c.isoClient.ISONames() -} - func (c *State) ImageNames() []string { if c.imageClient == nil { client := c.Client() diff --git a/internal/state/state.go b/internal/state/state.go index a84936a2..946b522b 100644 --- a/internal/state/state.go +++ b/internal/state/state.go @@ -22,7 +22,6 @@ type State struct { DebugFilePath string client *hcloud.Client - isoClient *hcapi.ISOClient imageClient *hcapi.ImageClient locationClient *hcapi.LocationClient sshKeyClient *hcapi.SSHKeyClient