detached 0321aca

Refactor deployment command for better error handling

- Add buffered error channel in NewDeployCommand to prevent goroutine leak
- Replace direct error prints with error channel sends in HTTP request, TUI deployment, and progress bar TUI sections
- Implement loop to handle errors from the new error channel
- Adjust message printing format after successful deployment
commit
0321aca94d6de6bbf30d68eebae34b9016c3779c
author
bnema · · 2023-11-13 16:35 UTC
parent
2829d7e
signature
unsigned
1 file · +23 -9
M internal/cli/cmd/deploy.go +23 -9
@@ -65,6 +65,7 @@ func NewDeployCommand(a *cli.App) *cobra.Command {
6565 }
6666
6767 progressCh := make(chan mvu.ProgressMsg)
68+ errCh := make(chan error, 1) // Buffer of 1 to prevent goroutine leak in case of non-blocking send
6869
6970 // Create a progress function to update the progress bar
7071 progressReader := &mvu.ProgressReader{
@@ -87,9 +88,7 @@ func NewDeployCommand(a *cli.App) *cobra.Command {
8788 go func() {
8889 resp, err := handler.SendHTTPRequest(a, &reqPayload, "POST", "/push")
8990 if err != nil {
90- fmt.Println("Error sending HTTP request:", err)
91- // Don't forget to close the channel if there's an error
92- close(progressCh)
91+ errCh <- fmt.Errorf("error sending HTTP request: %w", err)
9392 return
9493 }
9594
@@ -116,28 +115,43 @@ func NewDeployCommand(a *cli.App) *cobra.Command {
116115 // Run the TUI program
117116 finalModel, err := mvu.RunDeploymentTUI(client, imageName, targetDomain, port)
118117 if err != nil {
119- fmt.Println("Error running deployment TUI:", err)
118+ errCh <- fmt.Errorf("error running deployment TUI: %w", err)
120119 return
121120 }
122121
123122 // Check if the deployment was successful
124123 if !finalModel.DeploymentDone {
125- fmt.Println("Deployment failed check your configuration and try again.")
124+ errCh <- fmt.Errorf("deployment failed")
126125 return
127126 }
128127
129128 // Print the final message
130- color.Blue(" Deployment successful!")
131- fmt.Println(" Your application is now available at:", targetDomain)
129+ color.Blue("Deployment successful!")
130+ fmt.Println("Your application is now available at:", targetDomain)
132131 close(progressCh)
133132 }()
134-
135133 // Run the progress bar TUI
136134 m, err := mvu.RunProgressBarTUI(progressCh)
137135 if err != nil {
138- fmt.Println("Error running progress bar TUI:", err)
136+ errCh <- fmt.Errorf("error running progress bar TUI: %w", err)
139137 return
140138 }
139+ done := false
140+
141+ for !done {
142+ select {
143+ case err := <-errCh:
144+ if err != nil {
145+ fmt.Println(err)
146+ return
147+ }
148+ case <-time.After(1 * time.Second):
149+ // Check if the progress bar is done
150+ if m.Done {
151+ done = true
152+ }
153+ }
154+ }
141155
142156 // Close the reader
143157 err = progressReader.Close()
@@ -65,6 +65,7 @@ func NewDeployCommand(a *cli.App) *cobra.Command {
65 }65 }
66 66
67 progressCh := make(chan mvu.ProgressMsg)67 progressCh := make(chan mvu.ProgressMsg)
68+ errCh := make(chan error, 1) // Buffer of 1 to prevent goroutine leak in case of non-blocking send
68 69
69 // Create a progress function to update the progress bar70 // Create a progress function to update the progress bar
70 progressReader := &mvu.ProgressReader{71 progressReader := &mvu.ProgressReader{
@@ -87,9 +88,7 @@ func NewDeployCommand(a *cli.App) *cobra.Command {
87 go func() {88 go func() {
88 resp, err := handler.SendHTTPRequest(a, &reqPayload, "POST", "/push")89 resp, err := handler.SendHTTPRequest(a, &reqPayload, "POST", "/push")
89 if err != nil {90 if err != nil {
90- fmt.Println("Error sending HTTP request:", err)91+ errCh <- fmt.Errorf("error sending HTTP request: %w", err)
91- // Don't forget to close the channel if there's an error
92- close(progressCh)
93 return92 return
94 }93 }
95 94
@@ -116,28 +115,43 @@ func NewDeployCommand(a *cli.App) *cobra.Command {
116 // Run the TUI program115 // Run the TUI program
117 finalModel, err := mvu.RunDeploymentTUI(client, imageName, targetDomain, port)116 finalModel, err := mvu.RunDeploymentTUI(client, imageName, targetDomain, port)
118 if err != nil {117 if err != nil {
119- fmt.Println("Error running deployment TUI:", err)118+ errCh <- fmt.Errorf("error running deployment TUI: %w", err)
120 return119 return
121 }120 }
122 121
123 // Check if the deployment was successful122 // Check if the deployment was successful
124 if !finalModel.DeploymentDone {123 if !finalModel.DeploymentDone {
125- fmt.Println("Deployment failed check your configuration and try again.")124+ errCh <- fmt.Errorf("deployment failed")
126 return125 return
127 }126 }
128 127
129 // Print the final message128 // Print the final message
130- color.Blue(" Deployment successful!")129+ color.Blue("Deployment successful!")
131- fmt.Println(" Your application is now available at:", targetDomain)130+ fmt.Println("Your application is now available at:", targetDomain)
132 close(progressCh)131 close(progressCh)
133 }()132 }()
134-
135 // Run the progress bar TUI133 // Run the progress bar TUI
136 m, err := mvu.RunProgressBarTUI(progressCh)134 m, err := mvu.RunProgressBarTUI(progressCh)
137 if err != nil {135 if err != nil {
138- fmt.Println("Error running progress bar TUI:", err)136+ errCh <- fmt.Errorf("error running progress bar TUI: %w", err)
139 return137 return
140 }138 }
139+ done := false
140+
141+ for !done {
142+ select {
143+ case err := <-errCh:
144+ if err != nil {
145+ fmt.Println(err)
146+ return
147+ }
148+ case <-time.After(1 * time.Second):
149+ // Check if the progress bar is done
150+ if m.Done {
151+ done = true
152+ }
153+ }
154+ }
141 155
142 // Close the reader156 // Close the reader
143 err = progressReader.Close()157 err = progressReader.Close()

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 7

Tags 106

↑↓ moveenter openesc close