Skip to content

Commit a60a216

Browse files
cgoetz-inovexrubenhoenle
authored andcommitted
fix(cli) stop spinner on error
Most usages of spinner did not stop the spinner on error. Introduced helper functions Run and Run2 (result arity 2) to make correct spinner usage easier. Refactored spinner usages to use new helper funcs. STACKITCLI-310
1 parent 5003626 commit a60a216

File tree

103 files changed

+472
-456
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

103 files changed

+472
-456
lines changed

internal/cmd/beta/alb/create/create.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,13 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
8484

8585
// Wait for async operation, if async mode not enabled
8686
if !model.Async {
87-
s := spinner.New(params.Printer)
88-
s.Start("Creating loadbalancer")
89-
_, err = wait.CreateOrUpdateLoadbalancerWaitHandler(ctx, apiClient, model.ProjectId, model.Region, *resp.Name).WaitWithContext(ctx)
87+
err := spinner.Run(params.Printer, "Creating loadbalancer", func() error {
88+
_, err := wait.CreateOrUpdateLoadbalancerWaitHandler(ctx, apiClient, model.ProjectId, model.Region, *resp.Name).WaitWithContext(ctx)
89+
return err
90+
})
9091
if err != nil {
9192
return fmt.Errorf("wait for loadbalancer creation: %w", err)
9293
}
93-
s.Stop()
9494
}
9595

9696
return outputResult(params.Printer, model, projectLabel, resp)

internal/cmd/beta/alb/update/update.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,14 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
9090

9191
// Wait for async operation, if async mode not enabled
9292
if !model.Async {
93-
s := spinner.New(params.Printer)
94-
s.Start("updating loadbalancer")
95-
_, err = wait.CreateOrUpdateLoadbalancerWaitHandler(ctx, apiClient, model.ProjectId, model.Region, *resp.Name).
96-
WaitWithContext(ctx)
93+
err := spinner.Run(params.Printer, "updating loadbalancer", func() error {
94+
_, err = wait.CreateOrUpdateLoadbalancerWaitHandler(ctx, apiClient, model.ProjectId, model.Region, *resp.Name).
95+
WaitWithContext(ctx)
96+
return err
97+
})
9798
if err != nil {
9899
return fmt.Errorf("wait for loadbalancer update: %w", err)
99100
}
100-
s.Stop()
101101
}
102102

103103
return outputResult(params.Printer, model, projectLabel, resp)

internal/cmd/beta/edge/instance/create/create.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,19 +87,18 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
8787

8888
// Wait for async operation, if async mode not enabled
8989
if !model.Async {
90-
s := spinner.New(params.Printer)
91-
s.Start("Creating instance")
92-
// The waiter handler needs a concrete client type. We can safely cast here as the real implementation will always match.
93-
client, ok := apiClient.(*edge.APIClient)
94-
if !ok {
95-
return fmt.Errorf("failed to configure API client")
96-
}
97-
_, err = wait.CreateOrUpdateInstanceWaitHandler(ctx, client, model.ProjectId, model.Region, instanceId).WaitWithContext(ctx)
98-
90+
err := spinner.Run(params.Printer, "Creating instance", func() error {
91+
// The waiter handler needs a concrete concreteClient type. We can safely cast here as the real implementation will always match.
92+
concreteClient, ok := apiClient.(*edge.APIClient)
93+
if !ok {
94+
return fmt.Errorf("failed to configure API concreteClient")
95+
}
96+
_, err = wait.CreateOrUpdateInstanceWaitHandler(ctx, concreteClient, model.ProjectId, model.Region, instanceId).WaitWithContext(ctx)
97+
return err
98+
})
9999
if err != nil {
100100
return fmt.Errorf("wait for edge instance creation: %w", err)
101101
}
102-
s.Stop()
103102
}
104103

105104
// Handle output to printer

internal/cmd/beta/edge/instance/delete/delete.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -110,25 +110,26 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
110110
// Wait for async operation, if async mode not enabled
111111
operationState := "Triggered deletion of"
112112
if !model.Async {
113-
s := spinner.New(params.Printer)
114-
s.Start("Deleting instance")
115-
// Determine identifier and waiter to use
116-
waiterFactory, err := getWaiterFactory(ctx, model)
117-
if err != nil {
113+
err := spinner.Run(params.Printer, "Deleting instance", func() error {
114+
// Determine identifier and waiter to use
115+
waiterFactory, err := getWaiterFactory(ctx, model)
116+
if err != nil {
117+
return err
118+
}
119+
// The waiter factory needs a concrete concreteClient type. We can safely cast here as the real implementation will always match.
120+
concreteClient, ok := apiClient.(*edge.APIClient)
121+
if !ok {
122+
return fmt.Errorf("failed to configure API concreteClient")
123+
}
124+
waiter := waiterFactory(concreteClient)
125+
_, err = waiter.WaitWithContext(ctx)
118126
return err
119-
}
120-
// The waiter factory needs a concrete client type. We can safely cast here as the real implementation will always match.
121-
client, ok := apiClient.(*edge.APIClient)
122-
if !ok {
123-
return fmt.Errorf("failed to configure API client")
124-
}
125-
waiter := waiterFactory(client)
127+
})
126128

127-
if _, err = waiter.WaitWithContext(ctx); err != nil {
129+
if err != nil {
128130
return fmt.Errorf("wait for edge instance deletion: %w", err)
129131
}
130132
operationState = "Deleted"
131-
s.Stop()
132133
}
133134

134135
params.Printer.Info("%s instance with %q %q of project %q.\n", operationState, model.identifier.Flag, model.identifier.Value, projectLabel)

internal/cmd/beta/edge/instance/update/update.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -120,25 +120,26 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
120120
if !model.Async {
121121
// Wait for async operation, if async mode not enabled
122122
// Show spinner while waiting
123-
s := spinner.New(params.Printer)
124-
s.Start("Updating instance")
125-
// Determine identifier and waiter to use
126-
waiterFactory, err := getWaiterFactory(ctx, model)
127-
if err != nil {
123+
err := spinner.Run(params.Printer, "Updating instance", func() error {
124+
// Determine identifier and waiter to use
125+
waiterFactory, err := getWaiterFactory(ctx, model)
126+
if err != nil {
127+
return err
128+
}
129+
// The waiter handler needs a concrete concreteClient type. We can safely cast here as the real implementation will always match.
130+
concreteClient, ok := apiClient.(*edge.APIClient)
131+
if !ok {
132+
return fmt.Errorf("failed to configure API concreteClient")
133+
}
134+
waiter := waiterFactory(concreteClient)
135+
_, err = waiter.WaitWithContext(ctx)
128136
return err
129-
}
130-
// The waiter handler needs a concrete client type. We can safely cast here as the real implementation will always match.
131-
client, ok := apiClient.(*edge.APIClient)
132-
if !ok {
133-
return fmt.Errorf("failed to configure API client")
134-
}
135-
waiter := waiterFactory(client)
137+
})
136138

137-
if _, err = waiter.WaitWithContext(ctx); err != nil {
139+
if err != nil {
138140
return fmt.Errorf("wait for edge instance update: %w", err)
139141
}
140142
operationState = "Updated"
141-
s.Stop()
142143
}
143144

144145
params.Printer.Info("%s instance with %q %q of project %q.\n", operationState, model.identifier.Flag, model.identifier.Value, projectLabel)

internal/cmd/beta/intake/create/create.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,13 @@ func NewCmd(p *types.CmdParams) *cobra.Command {
111111

112112
// Wait for async operation, if async mode not enabled
113113
if !model.Async {
114-
s := spinner.New(p.Printer)
115-
s.Start("Creating STACKIT Intake instance")
116-
_, err = wait.CreateOrUpdateIntakeWaitHandler(ctx, apiClient, model.ProjectId, model.Region, resp.GetId()).WaitWithContext(ctx)
114+
err := spinner.Run(p.Printer, "Creating STACKIT Intake instance", func() error {
115+
_, err = wait.CreateOrUpdateIntakeWaitHandler(ctx, apiClient, model.ProjectId, model.Region, resp.GetId()).WaitWithContext(ctx)
116+
return err
117+
})
117118
if err != nil {
118119
return fmt.Errorf("wait for STACKIT Instance creation: %w", err)
119120
}
120-
s.Stop()
121121
}
122122

123123
return outputResult(p.Printer, model, projectLabel, resp)

internal/cmd/beta/intake/delete/delete.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,13 @@ func NewCmd(p *types.CmdParams) *cobra.Command {
6868

6969
// Wait for async operation, if async mode not enabled
7070
if !model.Async {
71-
s := spinner.New(p.Printer)
72-
s.Start("Deleting STACKIT Intake instance")
73-
_, err = wait.DeleteIntakeWaitHandler(ctx, apiClient, model.ProjectId, model.Region, model.IntakeId).WaitWithContext(ctx)
71+
err := spinner.Run(p.Printer, "Deleting STACKIT Intake instance", func() error {
72+
_, err = wait.DeleteIntakeWaitHandler(ctx, apiClient, model.ProjectId, model.Region, model.IntakeId).WaitWithContext(ctx)
73+
return err
74+
})
7475
if err != nil {
7576
return fmt.Errorf("wait for STACKIT Instance deletion: %w", err)
7677
}
77-
s.Stop()
7878
}
7979

8080
operationState := "Deleted"

internal/cmd/beta/intake/runner/create/create.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,13 @@ func NewCmd(p *types.CmdParams) *cobra.Command {
8888

8989
// Wait for async operation, if async mode not enabled
9090
if !model.Async {
91-
s := spinner.New(p.Printer)
92-
s.Start("Creating STACKIT Intake Runner")
93-
_, err = wait.CreateOrUpdateIntakeRunnerWaitHandler(ctx, apiClient, model.ProjectId, model.Region, resp.GetId()).WaitWithContext(ctx)
91+
err := spinner.Run(p.Printer, "Creating STACKIT Intake Runner", func() error {
92+
_, err = wait.CreateOrUpdateIntakeRunnerWaitHandler(ctx, apiClient, model.ProjectId, model.Region, resp.GetId()).WaitWithContext(ctx)
93+
return err
94+
})
9495
if err != nil {
9596
return fmt.Errorf("wait for STACKIT Intake Runner creation: %w", err)
9697
}
97-
s.Stop()
9898
}
9999

100100
return outputResult(p.Printer, model, projectLabel, resp)

internal/cmd/beta/intake/runner/delete/delete.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,13 @@ func NewCmd(p *types.CmdParams) *cobra.Command {
6868

6969
// Wait for async operation, if async mode not enabled
7070
if !model.Async {
71-
s := spinner.New(p.Printer)
72-
s.Start("Deleting STACKIT Intake Runner")
73-
_, err = wait.DeleteIntakeRunnerWaitHandler(ctx, apiClient, model.ProjectId, model.Region, model.RunnerId).WaitWithContext(ctx)
71+
err := spinner.Run(p.Printer, "Deleting STACKIT Intake Runner", func() error {
72+
_, err = wait.DeleteIntakeRunnerWaitHandler(ctx, apiClient, model.ProjectId, model.Region, model.RunnerId).WaitWithContext(ctx)
73+
return err
74+
})
7475
if err != nil {
7576
return fmt.Errorf("wait for STACKIT Intake Runner deletion: %w", err)
7677
}
77-
s.Stop()
7878
}
7979

8080
operationState := "Deleted"

internal/cmd/beta/intake/runner/update/update.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,13 @@ func NewCmd(p *types.CmdParams) *cobra.Command {
8686

8787
// Wait for async operation, if async mode not enabled
8888
if !model.Async {
89-
s := spinner.New(p.Printer)
90-
s.Start("Updating STACKIT Intake Runner")
91-
_, err = wait.CreateOrUpdateIntakeRunnerWaitHandler(ctx, apiClient, model.ProjectId, model.Region, model.RunnerId).WaitWithContext(ctx)
89+
err := spinner.Run(p.Printer, "Updating STACKIT Intake Runner", func() error {
90+
_, err = wait.CreateOrUpdateIntakeRunnerWaitHandler(ctx, apiClient, model.ProjectId, model.Region, model.RunnerId).WaitWithContext(ctx)
91+
return err
92+
})
9293
if err != nil {
9394
return fmt.Errorf("wait for STACKIT Intake Runner update: %w", err)
9495
}
95-
s.Stop()
9696
}
9797

9898
return outputResult(p.Printer, model, projectLabel, resp)

0 commit comments

Comments
 (0)