Skip to content

Commit b5ed1d4

Browse files
committed
Address PR comment
1 parent 8de82bc commit b5ed1d4

File tree

1 file changed

+62
-50
lines changed

1 file changed

+62
-50
lines changed

src/cmd/cli/command/compose.go

Lines changed: 62 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package command
22

33
import (
4+
"context"
45
"errors"
56
"fmt"
67
"io"
@@ -153,28 +154,7 @@ func makeComposeUpCmd() *cobra.Command {
153154

154155
deploy, project, err := cli.ComposeUp(ctx, project, client, provider, upload, mode.Value())
155156
if err != nil {
156-
if errors.Is(err, types.ErrComposeFileNotFound) {
157-
printDefangHint("To start a new project, do:", "new")
158-
}
159-
if !nonInteractive {
160-
if strings.Contains(err.Error(), "maximum number of projects") {
161-
if projectName, err2 := provider.RemoteProjectName(cmd.Context()); err2 == nil {
162-
term.Error("Error:", prettyError(err))
163-
if _, err := cli.InteractiveComposeDown(cmd.Context(), provider, projectName); err != nil {
164-
term.Debug("ComposeDown failed:", err)
165-
printDefangHint("To deactivate a project, do:", "compose down --project-name "+projectName)
166-
} else {
167-
printDefangHint("To try deployment again, do:", "compose up")
168-
}
169-
return nil
170-
}
171-
return err
172-
}
173-
term.Error("Error:", prettyError(err))
174-
track.Evt("Debug Prompted", P("composeErr", err))
175-
return cli.InteractiveDebugForClientError(ctx, client, project, err)
176-
}
177-
return err
157+
return handleComposeUpErr(ctx, err, project, provider)
178158
}
179159

180160
if len(deploy.Services) == 0 {
@@ -193,39 +173,18 @@ func makeComposeUpCmd() *cobra.Command {
193173
if deploy.Etag != "" {
194174
tailSource = "deployment ID " + deploy.Etag
195175
}
196-
197176
term.Info("Tailing logs for", tailSource, "; press Ctrl+C to detach:")
198177

199178
tailOptions := newTailOptionsForDeploy(deploy.Etag, since, verbose)
200179
serviceStates, err := cli.TailAndMonitor(ctx, project, provider, time.Duration(waitTimeout)*time.Second, tailOptions)
201180
if err != nil {
202-
var errDeploymentFailed cliClient.ErrDeploymentFailed
203-
if errors.As(err, &errDeploymentFailed) {
204-
// Tail got canceled because of deployment failure: prompt to show the debugger
205-
term.Warn(errDeploymentFailed)
206-
debugConfig := cli.DebugConfig{
207-
Deployment: deploy.Etag,
208-
ModelId: modelId,
209-
Project: project,
210-
Provider: provider,
211-
Since: since,
212-
}
213-
if errDeploymentFailed.Service != "" {
214-
debugConfig.FailedServices = []string{errDeploymentFailed.Service}
215-
}
216-
if nonInteractive {
217-
printDefangHint("To debug the deployment, do:", debugConfig.String())
218-
} else {
219-
track.Evt("Debug Prompted", P("failedServices", debugConfig.FailedServices), P("etag", deploy.Etag), P("reason", errDeploymentFailed))
220-
221-
// Call the AI debug endpoint using the original command context (not the tail ctx which is canceled)
222-
if nil != cli.InteractiveDebugDeployment(ctx, client, debugConfig) {
223-
// don't show this defang hint if debugging was successful
224-
tailOptions := newTailOptionsForDeploy(deploy.Etag, since, true)
225-
printDefangHint("To see the logs of the failed service, do:", tailOptions.String())
226-
}
227-
}
228-
}
181+
handleTailAndMonitorErr(ctx, err, client, cli.DebugConfig{
182+
Deployment: deploy.Etag,
183+
ModelId: modelId,
184+
Project: project,
185+
Provider: provider,
186+
Since: since,
187+
})
229188
return err
230189
}
231190

@@ -258,6 +217,59 @@ func makeComposeUpCmd() *cobra.Command {
258217
return composeUpCmd
259218
}
260219

220+
func handleComposeUpErr(ctx context.Context, err error, project *compose.Project, provider cliClient.Provider) error {
221+
if errors.Is(err, types.ErrComposeFileNotFound) {
222+
// TODO: generate a compose file based on the current project
223+
printDefangHint("To start a new project, do:", "new")
224+
}
225+
226+
if nonInteractive {
227+
return err
228+
}
229+
230+
if strings.Contains(err.Error(), "maximum number of projects") {
231+
if projectName, err2 := provider.RemoteProjectName(ctx); err2 == nil {
232+
term.Error("Error:", prettyError(err))
233+
if _, err := cli.InteractiveComposeDown(ctx, provider, projectName); err != nil {
234+
term.Debug("ComposeDown failed:", err)
235+
printDefangHint("To deactivate a project, do:", "compose down --project-name "+projectName)
236+
} else {
237+
// TODO: actually do the "compose up" (because that's what the user intended in the first place)
238+
printDefangHint("To try deployment again, do:", "compose up")
239+
}
240+
return nil
241+
}
242+
return err
243+
}
244+
245+
term.Error("Error:", prettyError(err))
246+
track.Evt("Debug Prompted", P("composeErr", err))
247+
return cli.InteractiveDebugForClientError(ctx, client, project, err)
248+
}
249+
250+
func handleTailAndMonitorErr(ctx context.Context, err error, client *cliClient.GrpcClient, debugConfig cli.DebugConfig) {
251+
var errDeploymentFailed cliClient.ErrDeploymentFailed
252+
if errors.As(err, &errDeploymentFailed) {
253+
// Tail got canceled because of deployment failure: prompt to show the debugger
254+
term.Warn(errDeploymentFailed)
255+
if errDeploymentFailed.Service != "" {
256+
debugConfig.FailedServices = []string{errDeploymentFailed.Service}
257+
}
258+
if nonInteractive {
259+
printDefangHint("To debug the deployment, do:", debugConfig.String())
260+
} else {
261+
track.Evt("Debug Prompted", P("failedServices", debugConfig.FailedServices), P("etag", debugConfig.Deployment), P("reason", errDeploymentFailed))
262+
263+
// Call the AI debug endpoint using the original command context (not the tail ctx which is canceled)
264+
if nil != cli.InteractiveDebugDeployment(ctx, client, debugConfig) {
265+
// don't show this defang hint if debugging was successful
266+
tailOptions := newTailOptionsForDeploy(debugConfig.Deployment, debugConfig.Since, true)
267+
printDefangHint("To see the logs of the failed service, do:", tailOptions.String())
268+
}
269+
}
270+
}
271+
}
272+
261273
func newTailOptionsForDeploy(deployment string, since time.Time, verbose bool) cli.TailOptions {
262274
return cli.TailOptions{
263275
Deployment: deployment,

0 commit comments

Comments
 (0)