| 149 | type echo struct{} |
| 150 | |
| 151 | func 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. |
| 190 | func (*echo) Parse(sess *provisionersdk.Session, _ *proto.ParseRequest, _ <-chan struct{}) *proto.ParseComplete { |