Skip to content

Commit

Permalink
chore: migrate hcapi.ISOClient usage to hcapi2 (#444)
Browse files Browse the repository at this point in the history
  • Loading branch information
samcday authored Feb 16, 2023
1 parent 1ffdcf5 commit df03020
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 91 deletions.
96 changes: 48 additions & 48 deletions internal/cmd/server/attach_iso.go
Original file line number Diff line number Diff line change
@@ -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
},
}
2 changes: 1 addition & 1 deletion internal/cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
33 changes: 0 additions & 33 deletions internal/hcapi/iso.go

This file was deleted.

8 changes: 0 additions & 8 deletions internal/state/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 0 additions & 1 deletion internal/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ type State struct {
DebugFilePath string

client *hcloud.Client
isoClient *hcapi.ISOClient
imageClient *hcapi.ImageClient
locationClient *hcapi.LocationClient
sshKeyClient *hcapi.SSHKeyClient
Expand Down

0 comments on commit df03020

Please sign in to comment.