Skip to content

Commit 4c2f2db

Browse files
committed
PR suggestion implemented
1 parent 64bc4d7 commit 4c2f2db

2 files changed

Lines changed: 51 additions & 98 deletions

File tree

cmd/deploy.go

Lines changed: 51 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import (
44
"bytes"
5+
"context"
56
"encoding/json"
67
"fmt"
78
"io"
@@ -186,59 +187,11 @@ func runDeployGithub(cmd *cobra.Command, args []string) error {
186187
return fmt.Errorf("decode deployment response: %w", err)
187188
}
188189

189-
// Follow deployment events via SSE using the same base URL and API key
190-
stream := client.Deployments.FollowStreaming(
191-
cmd.Context(),
192-
depCreated.ID,
193-
kernel.DeploymentFollowParams{},
190+
return followDeployment(cmd.Context(), client, depCreated.ID, startTime,
194191
option.WithBaseURL(baseURL),
195192
option.WithHeader("Authorization", "Bearer "+apiKey),
196193
option.WithMaxRetries(0),
197194
)
198-
for stream.Next() {
199-
data := stream.Current()
200-
switch data.Event {
201-
case "log":
202-
logEv := data.AsLog()
203-
msg := strings.TrimSuffix(logEv.Message, "\n")
204-
pterm.Info.Println(pterm.Gray(msg))
205-
case "deployment_state":
206-
deploymentState := data.AsDeploymentState()
207-
status := deploymentState.Deployment.Status
208-
if status == string(kernel.DeploymentGetResponseStatusFailed) ||
209-
status == string(kernel.DeploymentGetResponseStatusStopped) {
210-
pterm.Error.Println("✖ Deployment failed")
211-
pterm.Error.Printf("Deployment ID: %s\n", depCreated.ID)
212-
pterm.Info.Printf("View logs: kernel deploy logs %s --since 1h\n", depCreated.ID)
213-
return fmt.Errorf("deployment %s: %s", status, deploymentState.Deployment.StatusReason)
214-
}
215-
if status == string(kernel.DeploymentGetResponseStatusRunning) {
216-
duration := time.Since(startTime)
217-
pterm.Success.Printfln("✔ Deployment complete in %s", duration.Round(time.Millisecond))
218-
return nil
219-
}
220-
case "app_version_summary":
221-
appVersionSummary := data.AsDeploymentFollowResponseAppVersionSummaryEvent()
222-
pterm.Info.Printf("App \"%s\" deployed (version: %s)\n", appVersionSummary.AppName, appVersionSummary.Version)
223-
if len(appVersionSummary.Actions) > 0 {
224-
action0Name := appVersionSummary.Actions[0].Name
225-
pterm.Info.Printf("Invoke with: kernel invoke %s %s --payload '{...}'\n", quoteIfNeeded(appVersionSummary.AppName), quoteIfNeeded(action0Name))
226-
}
227-
case "error":
228-
errorEv := data.AsErrorEvent()
229-
pterm.Error.Printf("Deployment ID: %s\n", depCreated.ID)
230-
pterm.Info.Printf("View logs: kernel deploy logs %s --since 1h\n", depCreated.ID)
231-
return fmt.Errorf("%s: %s", errorEv.Error.Code, errorEv.Error.Message)
232-
}
233-
}
234-
235-
if serr := stream.Err(); serr != nil {
236-
pterm.Error.Println("✖ Stream error")
237-
pterm.Error.Printf("Deployment ID: %s\n", depCreated.ID)
238-
pterm.Info.Printf("View logs: kernel deploy logs %s --since 1h\n", depCreated.ID)
239-
return fmt.Errorf("stream error: %w", serr)
240-
}
241-
return nil
242195
}
243196

244197
func runDeploy(cmd *cobra.Command, args []string) (err error) {
@@ -316,54 +269,7 @@ func runDeploy(cmd *cobra.Command, args []string) (err error) {
316269
return util.CleanedUpSdkError{Err: err}
317270
}
318271

319-
// Follow deployment events via SSE
320-
stream := client.Deployments.FollowStreaming(cmd.Context(), resp.ID, kernel.DeploymentFollowParams{}, option.WithMaxRetries(0))
321-
for stream.Next() {
322-
data := stream.Current()
323-
switch data.Event {
324-
case "log":
325-
logEv := data.AsLog()
326-
msg := strings.TrimSuffix(logEv.Message, "\n")
327-
pterm.Info.Println(pterm.Gray(msg))
328-
case "deployment_state":
329-
deploymentState := data.AsDeploymentState()
330-
status := deploymentState.Deployment.Status
331-
if status == string(kernel.DeploymentGetResponseStatusFailed) ||
332-
status == string(kernel.DeploymentGetResponseStatusStopped) {
333-
pterm.Error.Println("✖ Deployment failed")
334-
pterm.Error.Printf("Deployment ID: %s\n", resp.ID)
335-
pterm.Info.Printf("View logs: kernel deploy logs %s --since 1h\n", resp.ID)
336-
err = fmt.Errorf("deployment %s: %s", status, deploymentState.Deployment.StatusReason)
337-
return err
338-
}
339-
if status == string(kernel.DeploymentGetResponseStatusRunning) {
340-
duration := time.Since(startTime)
341-
pterm.Success.Printfln("✔ Deployment complete in %s", duration.Round(time.Millisecond))
342-
return nil
343-
}
344-
case "app_version_summary":
345-
appVersionSummary := data.AsDeploymentFollowResponseAppVersionSummaryEvent()
346-
pterm.Info.Printf("App \"%s\" deployed (version: %s)\n", appVersionSummary.AppName, appVersionSummary.Version)
347-
if len(appVersionSummary.Actions) > 0 {
348-
action0Name := appVersionSummary.Actions[0].Name
349-
pterm.Info.Printf("Invoke with: kernel invoke %s %s --payload '{...}'\n", quoteIfNeeded(appVersionSummary.AppName), quoteIfNeeded(action0Name))
350-
}
351-
case "error":
352-
errorEv := data.AsErrorEvent()
353-
pterm.Error.Printf("Deployment ID: %s\n", resp.ID)
354-
pterm.Info.Printf("View logs: kernel deploy logs %s --since 1h\n", resp.ID)
355-
err = fmt.Errorf("%s: %s", errorEv.Error.Code, errorEv.Error.Message)
356-
return err
357-
}
358-
}
359-
360-
if serr := stream.Err(); serr != nil {
361-
pterm.Error.Println("✖ Stream error")
362-
pterm.Error.Printf("Deployment ID: %s\n", resp.ID)
363-
pterm.Info.Printf("View logs: kernel deploy logs %s --since 1h\n", resp.ID)
364-
return fmt.Errorf("stream error: %w", serr)
365-
}
366-
return nil
272+
return followDeployment(cmd.Context(), client, resp.ID, startTime, option.WithMaxRetries(0))
367273
}
368274

369275
func quoteIfNeeded(s string) string {
@@ -509,3 +415,51 @@ AppsLoop:
509415
pterm.DefaultTable.WithHasHeader().WithData(table).Render()
510416
return nil
511417
}
418+
419+
func followDeployment(ctx context.Context, client kernel.Client, deploymentID string, startTime time.Time, opts ...option.RequestOption) error {
420+
stream := client.Deployments.FollowStreaming(ctx, deploymentID, kernel.DeploymentFollowParams{}, opts...)
421+
for stream.Next() {
422+
data := stream.Current()
423+
switch data.Event {
424+
case "log":
425+
logEv := data.AsLog()
426+
msg := strings.TrimSuffix(logEv.Message, "\n")
427+
pterm.Info.Println(pterm.Gray(msg))
428+
case "deployment_state":
429+
deploymentState := data.AsDeploymentState()
430+
status := deploymentState.Deployment.Status
431+
if status == string(kernel.DeploymentGetResponseStatusFailed) ||
432+
status == string(kernel.DeploymentGetResponseStatusStopped) {
433+
pterm.Error.Println("✖ Deployment failed")
434+
pterm.Error.Printf("Deployment ID: %s\n", deploymentID)
435+
pterm.Info.Printf("View logs: kernel deploy logs %s --since 1h\n", deploymentID)
436+
return fmt.Errorf("deployment %s: %s", status, deploymentState.Deployment.StatusReason)
437+
}
438+
if status == string(kernel.DeploymentGetResponseStatusRunning) {
439+
duration := time.Since(startTime)
440+
pterm.Success.Printfln("✔ Deployment complete in %s", duration.Round(time.Millisecond))
441+
return nil
442+
}
443+
case "app_version_summary":
444+
appVersionSummary := data.AsDeploymentFollowResponseAppVersionSummaryEvent()
445+
pterm.Info.Printf("App \"%s\" deployed (version: %s)\n", appVersionSummary.AppName, appVersionSummary.Version)
446+
if len(appVersionSummary.Actions) > 0 {
447+
action0Name := appVersionSummary.Actions[0].Name
448+
pterm.Info.Printf("Invoke with: kernel invoke %s %s --payload '{...}'\n", quoteIfNeeded(appVersionSummary.AppName), quoteIfNeeded(action0Name))
449+
}
450+
case "error":
451+
errorEv := data.AsErrorEvent()
452+
pterm.Error.Printf("Deployment ID: %s\n", deploymentID)
453+
pterm.Info.Printf("View logs: kernel deploy logs %s --since 1h\n", deploymentID)
454+
return fmt.Errorf("%s: %s", errorEv.Error.Code, errorEv.Error.Message)
455+
}
456+
}
457+
458+
if serr := stream.Err(); serr != nil {
459+
pterm.Error.Println("✖ Stream error")
460+
pterm.Error.Printf("Deployment ID: %s\n", deploymentID)
461+
pterm.Info.Printf("View logs: kernel deploy logs %s --since 1h\n", deploymentID)
462+
return fmt.Errorf("stream error: %w", serr)
463+
}
464+
return nil
465+
}

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,3 @@ require (
5858
golang.org/x/text v0.24.0 // indirect
5959
gopkg.in/yaml.v3 v3.0.1 // indirect
6060
)
61-

0 commit comments

Comments
 (0)