Skip to content

Commit

Permalink
feat: new action waiting progress (#749)
Browse files Browse the repository at this point in the history
This replaces the 2 previous actions progress display with a single
consistent one, that leverage the new `hcloud.ActionWaiter` functions.

![Screenshot from 2024-05-08
16-03-01](https://github.com/hetznercloud/cli/assets/19195485/4c106811-9aac-4038-a6f7-f8895c2b122d)
![Screenshot from 2024-05-08
16-03-04](https://github.com/hetznercloud/cli/assets/19195485/2909b6e7-c396-48c8-9270-2fbcf37d4d1d)


The progress features have been extracted in a dedicated `ui` package to
split the actions waiting and the progress concerns.
  • Loading branch information
jooola authored May 8, 2024
1 parent 2cd08b2 commit 9e30f3f
Show file tree
Hide file tree
Showing 151 changed files with 773 additions and 334 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/boumenot/gocover-cobertura v1.2.0
github.com/cheggaaa/pb/v3 v3.1.5
github.com/dustin/go-humanize v1.0.1
github.com/fatih/color v1.16.0
github.com/fatih/structs v1.1.0
github.com/goccy/go-yaml v1.11.3
github.com/golang/mock v1.6.0
Expand All @@ -24,7 +25,6 @@ require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fatih/color v1.16.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/base/set_rdns.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (rc *SetRdnsCmd) Run(s state.State, cmd *cobra.Command, args []string) erro
return err
}

if err := s.ActionProgress(cmd, s, action); err != nil {
if err := s.WaitForActions(cmd, s, action); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/certificate/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func createManaged(s state.State, cmd *cobra.Command) (*hcloud.Certificate, erro
if err != nil {
return nil, err
}
if err := s.ActionProgress(cmd, s, res.Action); err != nil {
if err := s.WaitForActions(cmd, s, res.Action); err != nil {
return nil, err
}
defer cmd.Printf("Certificate %d created\n", res.Certificate.ID)
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/certificate/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestCreateManaged(t *testing.T) {
Action: &hcloud.Action{ID: 321},
}, nil, nil)
fx.ActionWaiter.EXPECT().
ActionProgress(gomock.Any(), gomock.Any(), &hcloud.Action{ID: 321})
WaitForActions(gomock.Any(), gomock.Any(), &hcloud.Action{ID: 321})
fx.Client.CertificateClient.EXPECT().
GetByID(gomock.Any(), int64(123)).
Return(&hcloud.Certificate{
Expand Down Expand Up @@ -96,7 +96,7 @@ func TestCreateManagedJSON(t *testing.T) {
Action: &hcloud.Action{ID: 321},
}, nil, nil)
fx.ActionWaiter.EXPECT().
ActionProgress(gomock.Any(), gomock.Any(), &hcloud.Action{ID: 321})
WaitForActions(gomock.Any(), gomock.Any(), &hcloud.Action{ID: 321})
fx.Client.CertificateClient.EXPECT().
GetByID(gomock.Any(), int64(123)).
Return(&hcloud.Certificate{
Expand Down
3 changes: 2 additions & 1 deletion internal/cmd/context/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/hetznercloud/cli/internal/cmd/util"
"github.com/hetznercloud/cli/internal/state"
"github.com/hetznercloud/cli/internal/state/config"
"github.com/hetznercloud/cli/internal/ui"
)

func newCreateCommand(s state.State) *cobra.Command {
Expand All @@ -30,7 +31,7 @@ func newCreateCommand(s state.State) *cobra.Command {

func runCreate(s state.State, cmd *cobra.Command, args []string) error {
cfg := s.Config()
if !state.StdoutIsTerminal() {
if !ui.StdoutIsTerminal() {
return errors.New("context create is an interactive command")
}

Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/firewall/add_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ var AddRuleCmd = base.Cmd{
if err != nil {
return err
}
if err := s.WaitForActions(cmd, s, actions); err != nil {
if err := s.WaitForActions(cmd, s, actions...); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/firewall/apply_to_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ var ApplyToResourceCmd = base.Cmd{
if err != nil {
return err
}
if err := s.WaitForActions(cmd, s, actions); err != nil {
if err := s.WaitForActions(cmd, s, actions...); err != nil {
return err
}
cmd.Printf("Firewall %d applied\n", firewall.ID)
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/firewall/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ var CreateCmd = base.CreateCmd{
return nil, nil, err
}

if err := s.WaitForActions(cmd, s, result.Actions); err != nil {
if err := s.WaitForActions(cmd, s, result.Actions...); err != nil {
return nil, nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/firewall/delete_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ var DeleteRuleCmd = base.Cmd{
if err != nil {
return err
}
if err := s.WaitForActions(cmd, s, actions); err != nil {
if err := s.WaitForActions(cmd, s, actions...); err != nil {
return err
}
cmd.Printf("Firewall Rules for Firewall %d updated\n", firewall.ID)
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/firewall/remove_from_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ var RemoveFromResourceCmd = base.Cmd{
if err != nil {
return err
}
if err := s.WaitForActions(cmd, s, actions); err != nil {
if err := s.WaitForActions(cmd, s, actions...); err != nil {
return err
}
cmd.Printf("Firewall %d applied\n", firewall.ID)
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/firewall/replace_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ var ReplaceRulesCmd = base.Cmd{
if err != nil {
return err
}
if err := s.WaitForActions(cmd, s, actions); err != nil {
if err := s.WaitForActions(cmd, s, actions...); err != nil {
return err
}
cmd.Printf("Firewall Rules for Firewall %d updated\n", firewall.ID)
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/floatingip/assign.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ var AssignCmd = base.Cmd{
return err
}

if err := s.ActionProgress(cmd, s, action); err != nil {
if err := s.WaitForActions(cmd, s, action); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/floatingip/assign_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestAssign(t *testing.T) {
Assign(gomock.Any(), &hcloud.FloatingIP{ID: 123}, &hcloud.Server{ID: 456}).
Return(&hcloud.Action{ID: 123}, nil, nil)
fx.ActionWaiter.EXPECT().
ActionProgress(gomock.Any(), gomock.Any(), &hcloud.Action{ID: 123}).
WaitForActions(gomock.Any(), gomock.Any(), &hcloud.Action{ID: 123}).
Return(nil)

out, errOut, err := fx.Run(cmd, []string{"my-ip", "my-server"})
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/floatingip/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ var CreateCmd = base.CreateCmd{
}

if result.Action != nil {
if err := s.ActionProgress(cmd, s, result.Action); err != nil {
if err := s.WaitForActions(cmd, s, result.Action); err != nil {
return nil, nil, err
}
}
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/floatingip/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,14 @@ func TestCreateProtection(t *testing.T) {
ID: 321,
},
}, nil, nil)
fx.ActionWaiter.EXPECT().ActionProgress(gomock.Any(), gomock.Any(), &hcloud.Action{ID: 321}).Return(nil)
fx.ActionWaiter.EXPECT().WaitForActions(gomock.Any(), gomock.Any(), &hcloud.Action{ID: 321}).Return(nil)

fx.Client.FloatingIPClient.EXPECT().
ChangeProtection(gomock.Any(), floatingIp, hcloud.FloatingIPChangeProtectionOpts{
Delete: hcloud.Ptr(true),
}).
Return(&hcloud.Action{ID: 333}, nil, nil)
fx.ActionWaiter.EXPECT().ActionProgress(gomock.Any(), gomock.Any(), &hcloud.Action{ID: 333}).Return(nil)
fx.ActionWaiter.EXPECT().WaitForActions(gomock.Any(), gomock.Any(), &hcloud.Action{ID: 333}).Return(nil)

out, errOut, err := fx.Run(cmd, []string{"--name", "myFloatingIP", "--type", "ipv4", "--home-location", "fsn1", "--enable-protection", "delete"})

Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/floatingip/disable_protection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestDisableProtection(t *testing.T) {
}).
Return(&hcloud.Action{ID: 123}, nil, nil)
fx.ActionWaiter.EXPECT().
ActionProgress(gomock.Any(), gomock.Any(), &hcloud.Action{ID: 123}).
WaitForActions(gomock.Any(), gomock.Any(), &hcloud.Action{ID: 123}).
Return(nil)

out, errOut, err := fx.Run(cmd, []string{"test", "delete"})
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/floatingip/enable_protection.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func changeProtection(s state.State, cmd *cobra.Command,
return err
}

if err := s.ActionProgress(cmd, s, action); err != nil {
if err := s.WaitForActions(cmd, s, action); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/floatingip/enable_protection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestEnableProtection(t *testing.T) {
}).
Return(&hcloud.Action{ID: 123}, nil, nil)
fx.ActionWaiter.EXPECT().
ActionProgress(gomock.Any(), gomock.Any(), &hcloud.Action{ID: 123}).
WaitForActions(gomock.Any(), gomock.Any(), &hcloud.Action{ID: 123}).
Return(nil)

out, errOut, err := fx.Run(cmd, []string{"test", "delete"})
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/floatingip/set_rdns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestSetRDNS(t *testing.T) {
ChangeDNSPtr(gomock.Any(), floatingIP, floatingIP.IP, hcloud.Ptr("example.com")).
Return(&hcloud.Action{ID: 123}, nil, nil)
fx.ActionWaiter.EXPECT().
ActionProgress(gomock.Any(), gomock.Any(), &hcloud.Action{ID: 123}).
WaitForActions(gomock.Any(), gomock.Any(), &hcloud.Action{ID: 123}).
Return(nil)

out, errOut, err := fx.Run(cmd, []string{"--hostname", "example.com", "test"})
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/floatingip/unassign.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var UnassignCmd = base.Cmd{
return err
}

if err := s.ActionProgress(cmd, s, action); err != nil {
if err := s.WaitForActions(cmd, s, action); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/floatingip/unassign_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestUnassign(t *testing.T) {
Unassign(gomock.Any(), &hcloud.FloatingIP{ID: 123}).
Return(&hcloud.Action{ID: 123}, nil, nil)
fx.ActionWaiter.EXPECT().
ActionProgress(gomock.Any(), gomock.Any(), &hcloud.Action{ID: 123}).
WaitForActions(gomock.Any(), gomock.Any(), &hcloud.Action{ID: 123}).
Return(nil)

out, errOut, err := fx.Run(cmd, []string{"my-ip"})
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/image/disable_protection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestDisableProtection(t *testing.T) {
}).
Return(&hcloud.Action{ID: 123}, nil, nil)
fx.ActionWaiter.EXPECT().
ActionProgress(gomock.Any(), gomock.Any(), &hcloud.Action{ID: 123}).
WaitForActions(gomock.Any(), gomock.Any(), &hcloud.Action{ID: 123}).
Return(nil)

out, errOut, err := fx.Run(cmd, []string{"123", "delete"})
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/image/enable_protection.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func changeProtection(s state.State, cmd *cobra.Command,
return err
}

if err := s.ActionProgress(cmd, s, action); err != nil {
if err := s.WaitForActions(cmd, s, action); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/image/enable_protection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestEnableProtection(t *testing.T) {
}).
Return(&hcloud.Action{ID: 123}, nil, nil)
fx.ActionWaiter.EXPECT().
ActionProgress(gomock.Any(), gomock.Any(), &hcloud.Action{ID: 123}).
WaitForActions(gomock.Any(), gomock.Any(), &hcloud.Action{ID: 123}).
Return(nil)

out, errOut, err := fx.Run(cmd, []string{"123", "delete"})
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/loadbalancer/add_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ var AddServiceCmd = base.Cmd{
if err != nil {
return err
}
if err := s.ActionProgress(cmd, s, action); err != nil {
if err := s.WaitForActions(cmd, s, action); err != nil {
return err
}
cmd.Printf("Service was added to Load Balancer %d\n", loadBalancer.ID)
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/loadbalancer/add_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestAddService(t *testing.T) {
}).
Return(&hcloud.Action{ID: 123}, nil, nil)
fx.ActionWaiter.EXPECT().
ActionProgress(gomock.Any(), gomock.Any(), &hcloud.Action{ID: 123}).
WaitForActions(gomock.Any(), gomock.Any(), &hcloud.Action{ID: 123}).
Return(nil)

out, errOut, err := fx.Run(cmd, []string{"123", "--protocol", "http", "--listen-port", "80", "--destination-port", "8080"})
Expand Down Expand Up @@ -87,7 +87,7 @@ func TestAddServiceWithHealthCheck(t *testing.T) {
}).
Return(&hcloud.Action{ID: 123}, nil, nil)
fx.ActionWaiter.EXPECT().
ActionProgress(gomock.Any(), gomock.Any(), &hcloud.Action{ID: 123}).
WaitForActions(gomock.Any(), gomock.Any(), &hcloud.Action{ID: 123}).
Return(nil)

out, errOut, err := fx.Run(cmd, []string{
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/loadbalancer/add_target.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ var AddTargetCmd = base.Cmd{
return fmt.Errorf("specify one of --server, --label-selector, or --ip")
}

if err := s.ActionProgress(cmd, s, action); err != nil {
if err := s.WaitForActions(cmd, s, action); err != nil {
return err
}
cmd.Printf("Target added to Load Balancer %d\n", loadBalancer.ID)
Expand Down
6 changes: 3 additions & 3 deletions internal/cmd/loadbalancer/add_target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestAddTargetServer(t *testing.T) {
}).
Return(&hcloud.Action{ID: 123}, nil, nil)
fx.ActionWaiter.EXPECT().
ActionProgress(gomock.Any(), gomock.Any(), &hcloud.Action{ID: 123}).
WaitForActions(gomock.Any(), gomock.Any(), &hcloud.Action{ID: 123}).
Return(nil)

out, errOut, err := fx.Run(cmd, []string{"123", "--server", "my-server"})
Expand Down Expand Up @@ -61,7 +61,7 @@ func TestAddTargetLabelSelector(t *testing.T) {
}).
Return(&hcloud.Action{ID: 123}, nil, nil)
fx.ActionWaiter.EXPECT().
ActionProgress(gomock.Any(), gomock.Any(), &hcloud.Action{ID: 123}).
WaitForActions(gomock.Any(), gomock.Any(), &hcloud.Action{ID: 123}).
Return(nil)

out, errOut, err := fx.Run(cmd, []string{"123", "--label-selector", "my-label"})
Expand Down Expand Up @@ -89,7 +89,7 @@ func TestAddTargetIP(t *testing.T) {
}).
Return(&hcloud.Action{ID: 123}, nil, nil)
fx.ActionWaiter.EXPECT().
ActionProgress(gomock.Any(), gomock.Any(), &hcloud.Action{ID: 123}).
WaitForActions(gomock.Any(), gomock.Any(), &hcloud.Action{ID: 123}).
Return(nil)

out, errOut, err := fx.Run(cmd, []string{"123", "--ip", "192.168.2.1"})
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/loadbalancer/attach_to_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ var AttachToNetworkCmd = base.Cmd{
return err
}

if err := s.ActionProgress(cmd, s, action); err != nil {
if err := s.WaitForActions(cmd, s, action); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/loadbalancer/attach_to_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestAttachToNetwork(t *testing.T) {
}).
Return(&hcloud.Action{ID: 123}, nil, nil)
fx.ActionWaiter.EXPECT().
ActionProgress(gomock.Any(), gomock.Any(), &hcloud.Action{ID: 123}).
WaitForActions(gomock.Any(), gomock.Any(), &hcloud.Action{ID: 123}).
Return(nil)

out, errOut, err := fx.Run(cmd, []string{"123", "--network", "my-network"})
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/loadbalancer/change_algorithm.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ var ChangeAlgorithmCmd = base.Cmd{
if err != nil {
return err
}
if err := s.ActionProgress(cmd, s, action); err != nil {
if err := s.WaitForActions(cmd, s, action); err != nil {
return err
}
cmd.Printf("Algorithm for Load Balancer %d was changed\n", loadBalancer.ID)
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/loadbalancer/change_algorithm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestChangeAlgorithm(t *testing.T) {
}).
Return(&hcloud.Action{ID: 123}, nil, nil)
fx.ActionWaiter.EXPECT().
ActionProgress(gomock.Any(), gomock.Any(), &hcloud.Action{ID: 123}).
WaitForActions(gomock.Any(), gomock.Any(), &hcloud.Action{ID: 123}).
Return(nil)

out, errOut, err := fx.Run(cmd, []string{"123", "--algorithm-type", "least_connections"})
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/loadbalancer/change_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ var ChangeTypeCmd = base.Cmd{
return err
}

if err := s.ActionProgress(cmd, s, action); err != nil {
if err := s.WaitForActions(cmd, s, action); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/loadbalancer/change_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestChangeType(t *testing.T) {
}).
Return(&hcloud.Action{ID: 123}, nil, nil)
fx.ActionWaiter.EXPECT().
ActionProgress(gomock.Any(), gomock.Any(), &hcloud.Action{ID: 123}).
WaitForActions(gomock.Any(), gomock.Any(), &hcloud.Action{ID: 123}).
Return(nil)

out, errOut, err := fx.Run(cmd, []string{"123", "lb21"})
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/loadbalancer/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ var CreateCmd = base.CreateCmd{
return nil, nil, err
}

if err := s.ActionProgress(cmd, s, result.Action); err != nil {
if err := s.WaitForActions(cmd, s, result.Action); err != nil {
return nil, nil, err
}
loadBalancer, _, err := s.Client().LoadBalancer().GetByID(s, result.LoadBalancer.ID)
Expand Down
Loading

0 comments on commit 9e30f3f

Please sign in to comment.