MCPcopy Create free account
hub / github.com/coder/coder / readResponses

Function readResponses

provisioner/echo/serve.go:151–187  ·  view source on GitHub ↗
(sess *provisionersdk.Session, trans string, suffix string)

Source from the content-addressed store, hash-verified

149type echo struct{}
150
151func readResponses(sess *provisionersdk.Session, trans string, suffix string) ([]*proto.Response, error) {
152 var responses []*proto.Response
153 for i := 0; ; i++ {
154 paths := []string{
155 // Try more specific path first, then fallback to generic.
156 filepath.Join(sess.Files.WorkDirectory(), fmt.Sprintf("%d.%s.%s", i, trans, suffix)),
157 filepath.Join(sess.Files.WorkDirectory(), fmt.Sprintf("%d.%s", i, suffix)),
158 }
159 for pathIndex, path := range paths {
160 _, err := os.Stat(path)
161 if err != nil && pathIndex == (len(paths)-1) {
162 // If there are zero messages, something is wrong
163 if i == 0 {
164 // Error if nothing is around to enable failed states.
165 return nil, xerrors.Errorf("no state: %w", err)
166 }
167 // Otherwise, we've read all responses
168 return responses, nil
169 }
170 if err != nil {
171 // try next path
172 continue
173 }
174 data, err := os.ReadFile(path)
175 if err != nil {
176 return nil, xerrors.Errorf("read file %q: %w", path, err)
177 }
178 response := new(proto.Response)
179 err = protobuf.Unmarshal(data, response)
180 if err != nil {
181 return nil, xerrors.Errorf("unmarshal: %w", err)
182 }
183 responses = append(responses, response)
184 break
185 }
186 }
187}
188
189// Parse reads requests from the provided directory to stream responses.
190func (*echo) Parse(sess *provisionersdk.Session, _ *proto.ParseRequest, _ <-chan struct{}) *proto.ParseComplete {

Callers 5

ParseMethod · 0.85
InitMethod · 0.85
GraphMethod · 0.85
PlanMethod · 0.85
ApplyMethod · 0.85

Calls 4

WorkDirectoryMethod · 0.80
ReadFileMethod · 0.65
ErrorfMethod · 0.45
UnmarshalMethod · 0.45

Tested by

no test coverage detected