Skip to content

Commit

Permalink
promptui library replaced with tview
Browse files Browse the repository at this point in the history
  • Loading branch information
codinja1188 committed Dec 7, 2023
1 parent ae348c7 commit 0f2859d
Show file tree
Hide file tree
Showing 11 changed files with 204 additions and 163 deletions.
2 changes: 1 addition & 1 deletion docs/metal_virtual-network_delete.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ metal virtual-network delete -i <virtual_network_UUID> [-f] [flags]
metal virtual-network delete -i 77e6d57a-d7a4-4816-b451-cf9b043444e2
>
✔ Are you sure you want to delete virtual network 77e6d57a-d7a4-4816-b451-cf9b043444e2: y
# Deletes a VLAN, skipping confirmation.
metal virtual-network delete -f -i 77e6d57a-d7a4-4816-b451-cf9b043444e2
```
Expand Down
9 changes: 4 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ module github.com/equinix/metal-cli
go 1.19

require (
github.com/c-bata/go-prompt v0.2.6
github.com/equinix-labs/metal-go v0.26.0
github.com/olekukonko/tablewriter v0.0.5
github.com/packethost/packngo v0.30.0
github.com/pkg/errors v0.9.1
github.com/rivo/tview v0.0.0-20231206124440-5f078138442e
github.com/spf13/cobra v1.8.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.17.0
Expand All @@ -20,16 +20,15 @@ require (
github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/gdamore/encoding v1.0.0 // indirect
github.com/gdamore/tcell/v2 v2.6.1-0.20231203215052-2917c3801e73 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mattn/go-tty v0.0.5 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pkg/term v1.2.0-beta.2 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
Expand Down
57 changes: 30 additions & 27 deletions go.sum

Large diffs are not rendered by default.

35 changes: 22 additions & 13 deletions internal/devices/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"fmt"

"github.com/c-bata/go-prompt"
"github.com/rivo/tview"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -34,20 +34,29 @@ func (c *Client) Delete() *cobra.Command {
metal device delete -f -i 7ec86e23-8dcf-48ed-bd9b-c25c20958277`,
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true

if !force {
fmt.Printf("Are you sure you want to delete device %s (y/N): ", deviceID)
userInput := prompt.Input(">", func(d prompt.Document) []prompt.Suggest {
return []prompt.Suggest{
{Text: "y", Description: "Yes"},
{Text: "n", Description: "No"},
}
})
if userInput != "y" && userInput != "Y" {
return nil
app := tview.NewApplication()

modal := tview.NewModal().
SetText(fmt.Sprintf("Are you sure you want to delete Device %s ?", deviceID)).
AddButtons([]string{"Yes", "No"}).
SetDoneFunc(func(buttonIndex int, buttonLabel string) {
if buttonLabel == "Yes" {
if err := deleteDevice(deviceID); err != nil {
fmt.Printf("could not delete Device: %v\n", err)
}
}
app.Stop()
})

if err := app.SetRoot(modal, false).Run(); err != nil {
return fmt.Errorf("prompt failed: %w", err)
}
} else {
if err := deleteDevice(deviceID); err != nil {
return fmt.Errorf("could not delete Device: %w", err)
}
}
if err := deleteDevice(deviceID); err != nil {
return fmt.Errorf("could not delete Device: %w", err)
}
return nil
},
Expand Down
34 changes: 21 additions & 13 deletions internal/gateway/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"context"
"fmt"

"github.com/c-bata/go-prompt"
"github.com/rivo/tview"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -65,19 +65,27 @@ func (c *Client) Delete() *cobra.Command {
cmd.SilenceUsage = true

if !force {
fmt.Printf("Are you sure you want to delete device %s (y/N): ", gwayID)
userInput := prompt.Input(">", func(d prompt.Document) []prompt.Suggest {
return []prompt.Suggest{
{Text: "y", Description: "Yes"},
{Text: "n", Description: "No"},
}
})
if userInput != "y" && userInput != "Y" {
return nil
app := tview.NewApplication()

modal := tview.NewModal().
SetText(fmt.Sprintf("Are you sure you want to delete Gateway %s ?", gwayID)).
AddButtons([]string{"Yes", "No"}).
SetDoneFunc(func(buttonIndex int, buttonLabel string) {
if buttonLabel == "Yes" {
if err := deleteGway(gwayID); err != nil {
fmt.Printf("could not delete Gateways: %v\n", err)
}
}
app.Stop()
})

if err := app.SetRoot(modal, false).Run(); err != nil {
return fmt.Errorf("prompt failed: %w", err)
}
} else {
if err := deleteGway(gwayID); err != nil {
return fmt.Errorf("could not delete Gateways: %w", err)
}
}
if err := deleteGway(gwayID); err != nil {
return fmt.Errorf("could not delete Metal Gateway: %w", err)
}
return nil
},
Expand Down
34 changes: 21 additions & 13 deletions internal/organizations/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"context"
"fmt"

"github.com/c-bata/go-prompt"
"github.com/rivo/tview"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -60,19 +60,27 @@ func (c *Client) Delete() *cobra.Command {
cmd.SilenceUsage = true

if !force {
fmt.Printf("Are you sure you want to delete device %s (y/N): ", organizationID)
userInput := prompt.Input(">", func(d prompt.Document) []prompt.Suggest {
return []prompt.Suggest{
{Text: "y", Description: "Yes"},
{Text: "n", Description: "No"},
}
})
if userInput != "y" && userInput != "Y" {
return nil
app := tview.NewApplication()

modal := tview.NewModal().
SetText(fmt.Sprintf("Are you sure you want to delete Organization %s ?", organizationID)).
AddButtons([]string{"Yes", "No"}).
SetDoneFunc(func(buttonIndex int, buttonLabel string) {
if buttonLabel == "Yes" {
if err := deleteOrganization(organizationID); err != nil {
fmt.Printf("could not delete Organization: %v\n", err)
}
}
app.Stop()
})

if err := app.SetRoot(modal, false).Run(); err != nil {
return fmt.Errorf("prompt failed: %w", err)
}
} else {
if err := deleteOrganization(organizationID); err != nil {
return fmt.Errorf("could not delete Organization: %w", err)
}
}
if err := deleteOrganization(organizationID); err != nil {
return fmt.Errorf("could not delete Organization: %w", err)
}
return nil
},
Expand Down
34 changes: 20 additions & 14 deletions internal/ports/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import (
"net/http"
"strconv"

"github.com/c-bata/go-prompt"
metal "github.com/equinix-labs/metal-go/metal/v1"
"github.com/rivo/tview"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -62,23 +62,29 @@ func (c *Client) Convert() *cobra.Command {
}

convToL2 := func(portID string) (*metal.Port, *http.Response, error) {

var (
port *metal.Port
resp *http.Response
rerr error
)
if !force {
fmt.Printf("Are you sure you want to convert Port %s to Layer2 and remove assigned IP addresses? (y/N): ", portID)
confirmation := prompt.Input(">>> ", func(d prompt.Document) []prompt.Suggest {
return []prompt.Suggest{
{Text: "y", Description: "Yes"},
{Text: "n", Description: "No"},
}
})
if confirmation != "y" && confirmation != "Y" {
app := tview.NewApplication()

confirmModal := tview.NewModal().
SetText(fmt.Sprintf("Are you sure you want to convert Port %s to Layer2 and remove assigned IP addresses?", portID)).
AddButtons([]string{"Yes", "No"}).
SetDoneFunc(func(buttonIndex int, buttonLabel string) {
if buttonLabel == "Yes" {
port, resp, rerr = c.PortService.ConvertLayer2(context.Background(), portID).PortAssignInput(*metal.NewPortAssignInput()).Execute()
}
app.Stop()
})

if err := app.SetRoot(confirmModal, false).Run(); err != nil {
return nil, nil, nil
}
}

return c.PortService.ConvertLayer2(context.Background(), portID).
PortAssignInput(*metal.NewPortAssignInput()).
Execute()
return port, resp, rerr
}

addressFamily := int32(metal.IPADDRESSADDRESSFAMILY__4)
Expand Down
35 changes: 21 additions & 14 deletions internal/projects/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"context"
"fmt"

"github.com/c-bata/go-prompt"
"github.com/rivo/tview"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -58,20 +58,27 @@ func (c *Client) Delete() *cobra.Command {
cmd.SilenceUsage = true

if !force {
fmt.Printf("Are you sure you want to delete device %s (y/N): ", projectID)
userInput := prompt.Input(">", func(d prompt.Document) []prompt.Suggest {
return []prompt.Suggest{
{Text: "y", Description: "Yes"},
{Text: "n", Description: "No"},
}
})
if userInput != "y" && userInput != "Y" {
return nil
}
}
app := tview.NewApplication()

modal := tview.NewModal().
SetText(fmt.Sprintf("Are you sure you want to delete device %s ?", projectID)).
AddButtons([]string{"Yes", "No"}).
SetDoneFunc(func(buttonIndex int, buttonLabel string) {
if buttonLabel == "Yes" {
if err := deleteProject(projectID); err != nil {
fmt.Printf("could not delete Project: %v\n", err)
}
}
app.Stop()
})

if err := deleteProject(projectID); err != nil {
return fmt.Errorf("could not delete Project: %w", err)
if err := app.SetRoot(modal, false).Run(); err != nil {
return fmt.Errorf("prompt failed: %w", err)
}
} else {
if err := deleteProject(projectID); err != nil {
return fmt.Errorf("could not delete Project: %w", err)
}
}
return nil
},
Expand Down
35 changes: 21 additions & 14 deletions internal/ssh/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"context"
"fmt"

"github.com/c-bata/go-prompt"
"github.com/rivo/tview"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -58,20 +58,27 @@ func (c *Client) Delete() *cobra.Command {
cmd.SilenceUsage = true

if !force {
fmt.Printf("Are you sure you want to delete device %s (y/N): ", sshKeyID)
userInput := prompt.Input(">", func(d prompt.Document) []prompt.Suggest {
return []prompt.Suggest{
{Text: "y", Description: "Yes"},
{Text: "n", Description: "No"},
}
})
if userInput != "y" && userInput != "Y" {
return nil
}
}
app := tview.NewApplication()

modal := tview.NewModal().
SetText(fmt.Sprintf("Are you sure you want to delete device %s ?", sshKeyID)).
AddButtons([]string{"Yes", "No"}).
SetDoneFunc(func(buttonIndex int, buttonLabel string) {
if buttonLabel == "Yes" {
if err := deleteSSHKey(sshKeyID); err != nil {
fmt.Printf("could not delete SSH Key: %v\n", err)
}
}
app.Stop()
})

if err := deleteSSHKey(sshKeyID); err != nil {
return fmt.Errorf("could not delete SSH Key: %w", err)
if err := app.SetRoot(modal, false).Run(); err != nil {
return fmt.Errorf("prompt failed: %w", err)
}
} else {
if err := deleteSSHKey(sshKeyID); err != nil {
return fmt.Errorf("could not delete SSH Key:%w", err)
}
}
return nil
},
Expand Down
Loading

0 comments on commit 0f2859d

Please sign in to comment.