Skip to content

Commit 77d3e64

Browse files
committed
Add WaitForEnvironmentReady utility
1 parent ba5d4b8 commit 77d3e64

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

coder-sdk/env.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ package coder
22

33
import (
44
"context"
5+
"fmt"
56
"net/http"
67
"time"
78

89
"cdr.dev/coder-cli/internal/x/xjson"
910
"nhooyr.io/websocket"
11+
"nhooyr.io/websocket/wsjson"
1012
)
1113

1214
// Environment describes a Coder environment
@@ -135,3 +137,27 @@ func (c Client) DialEnvironmentStats(ctx context.Context, envID string) (*websoc
135137
func (c Client) DialResourceLoad(ctx context.Context, envID string) (*websocket.Conn, error) {
136138
return c.dialWs(ctx, "/api/environments/"+envID+"/watch-resource-load")
137139
}
140+
141+
type buildLogMsg struct {
142+
Type string `json:"type"`
143+
}
144+
145+
// WaitForEnvironmentReady will watch the build log and return when done
146+
func (c Client) WaitForEnvironmentReady(ctx context.Context, env *Environment) error {
147+
conn, err := c.DialEnvironmentBuildLog(ctx, env.ID)
148+
if err != nil {
149+
return fmt.Errorf("%s: dial build log: %w", env.Name, err)
150+
}
151+
152+
for {
153+
msg := buildLogMsg{}
154+
err := wsjson.Read(ctx, conn, &msg)
155+
if err != nil {
156+
return fmt.Errorf("%s: reading build log msg: %w", env.Name, err)
157+
}
158+
159+
if msg.Type == "done" {
160+
return nil
161+
}
162+
}
163+
}

0 commit comments

Comments
 (0)