4
4
"context"
5
5
"errors"
6
6
"fmt"
7
- "io"
8
7
"io/ioutil"
9
8
"os"
10
9
"os/exec"
@@ -13,8 +12,6 @@ import (
13
12
"time"
14
13
15
14
"github.com/codecrafters-io/cli/internal/utils"
16
- logstream_consumer "github.com/codecrafters-io/logstream/consumer"
17
- "github.com/fatih/color"
18
15
"github.com/getsentry/sentry-go"
19
16
cp "github.com/otiai10/copy"
20
17
"github.com/rs/zerolog"
@@ -115,99 +112,7 @@ func TestCommand(ctx context.Context) (err error) {
115
112
116
113
logger .Debug ().Msgf ("submission created: %v" , createSubmissionResponse .Id )
117
114
118
- for _ , message := range createSubmissionResponse .OnInitMessages {
119
- fmt .Println ("" )
120
- message .Print ()
121
- }
122
-
123
- if createSubmissionResponse .BuildLogstreamURL != "" {
124
- logger .Debug ().Msgf ("streaming build logs from %s" , createSubmissionResponse .BuildLogstreamURL )
125
-
126
- fmt .Println ("" )
127
- err = streamLogs (createSubmissionResponse .BuildLogstreamURL )
128
- if err != nil {
129
- return fmt .Errorf ("stream build logs: %w" , err )
130
- }
131
-
132
- logger .Debug ().Msg ("Finished streaming build logs" )
133
- logger .Debug ().Msg ("fetching build" )
134
-
135
- fetchBuildResponse , err := codecraftersClient .FetchBuild (createSubmissionResponse .BuildID )
136
- if err != nil {
137
- // TODO: Notify sentry
138
- red := color .New (color .FgRed ).SprintFunc ()
139
- fmt .Fprintln (os .Stderr , red (err .Error ()))
140
- fmt .Fprintln (os .Stderr , "" )
141
- fmt .Fprintln (os .Stderr , red ("We couldn't fetch the results of your submission. Please try again?" ))
142
- fmt .
Fprintln (
os .
Stderr ,
red (
"Let us know at [email protected] if this error persists." ))
143
- return err
144
- }
145
-
146
- logger .Debug ().Msgf ("finished fetching build: %v" , fetchBuildResponse )
147
- red := color .New (color .FgRed ).SprintFunc ()
148
-
149
- switch fetchBuildResponse .Status {
150
- case "failure" :
151
- fmt .Fprintln (os .Stderr , red ("" ))
152
- fmt .Fprintln (os .Stderr , red ("Looks like your codebase failed to build." ))
153
- fmt .
Fprintln (
os .
Stderr ,
red (
"If you think this is a CodeCrafters error, please let us know at [email protected] ." ))
154
- fmt .Fprintln (os .Stderr , red ("" ))
155
- os .Exit (0 )
156
- case "success" :
157
- time .Sleep (1 * time .Second ) // The delay in-between build and test logs is usually 5-10 seconds, so let's buy some time
158
- default :
159
- red := color .New (color .FgRed ).SprintFunc ()
160
-
161
- fmt .Fprintln (os .Stderr , red ("We couldn't fetch the results of your build. Please try again?" ))
162
- fmt .
Fprintln (
os .
Stderr ,
red (
"Let us know at [email protected] if this error persists." ))
163
- os .Exit (1 )
164
- }
165
- }
166
-
167
- fmt .Println ("" )
168
- fmt .Println ("Running tests. Logs should appear shortly..." )
169
- fmt .Println ("" )
170
-
171
- err = streamLogs (createSubmissionResponse .LogstreamURL )
172
- if err != nil {
173
- return fmt .Errorf ("stream logs: %w" , err )
174
- }
175
-
176
- logger .Debug ().Msgf ("fetching submission %s" , createSubmissionResponse .Id )
177
-
178
- fetchSubmissionResponse , err := codecraftersClient .FetchSubmission (createSubmissionResponse .Id )
179
- if err != nil {
180
- // TODO: Notify sentry
181
- red := color .New (color .FgRed ).SprintFunc ()
182
- fmt .Fprintln (os .Stderr , red (err .Error ()))
183
- fmt .Fprintln (os .Stderr , "" )
184
- fmt .Fprintln (os .Stderr , red ("We couldn't fetch the results of your submission. Please try again?" ))
185
- fmt .
Fprintln (
os .
Stderr ,
red (
"Let us know at [email protected] if this error persists." ))
186
- return err
187
- }
188
-
189
- logger .Debug ().Msgf ("finished fetching submission, status: %s" , fetchSubmissionResponse .Status )
190
-
191
- switch fetchSubmissionResponse .Status {
192
- case "failure" :
193
- for _ , message := range createSubmissionResponse .OnFailureMessages {
194
- fmt .Println ("" )
195
- message .Print ()
196
- }
197
- case "success" :
198
- for _ , message := range createSubmissionResponse .OnSuccessMessages {
199
- fmt .Println ("" )
200
- message .Print ()
201
- }
202
- default :
203
- fmt .Println ("" )
204
- }
205
-
206
- if fetchSubmissionResponse .IsError {
207
- return fmt .Errorf ("%s" , fetchSubmissionResponse .ErrorMessage )
208
- }
209
-
210
- return nil
115
+ return utils .HandleSubmission (createSubmissionResponse , ctx , codecraftersClient )
211
116
}
212
117
213
118
func copyRepositoryDirToTempDir (repoDir string ) (string , error ) {
@@ -272,20 +177,6 @@ func pushBranchToRemote(tmpDir string, remoteName string) error {
272
177
return nil
273
178
}
274
179
275
- func streamLogs (logstreamUrl string ) error {
276
- consumer , err := logstream_consumer .NewConsumer (logstreamUrl , func (message string ) {})
277
- if err != nil {
278
- return fmt .Errorf ("new log consumer: %w" , err )
279
- }
280
-
281
- _ , err = io .Copy (os .Stdout , consumer )
282
- if err != nil {
283
- return fmt .Errorf ("stream data: %w" , err )
284
- }
285
-
286
- return nil
287
- }
288
-
289
180
func wrapError (err error , output []byte , msg string ) error {
290
181
if _ , ok := err .(* exec.ExitError ); ok {
291
182
return fmt .Errorf ("add all files: %s" , output )
0 commit comments