@@ -2,8 +2,8 @@ package replpkg
2
2
3
3
import (
4
4
"bytes"
5
+ "errors"
5
6
"fmt"
6
- "io"
7
7
"io/ioutil"
8
8
"os"
9
9
"os/exec"
@@ -206,8 +206,7 @@ func (s *Session) evalStmt(in string, noPrint bool) error {
206
206
cmd .Stderr = b
207
207
err = cmd .Run ()
208
208
if err != nil {
209
- os .Stderr .WriteString ("Error running goimports:\n " )
210
- io .Copy (os .Stderr , b )
209
+ err = errors .New (b .String ())
211
210
return err
212
211
}
213
212
@@ -217,7 +216,7 @@ func (s *Session) evalStmt(in string, noPrint bool) error {
217
216
}
218
217
219
218
if err = s .importFile (functproxy ); err != nil {
220
- errorf ("%s" , err )
219
+ errorf ("%s" , err . Error () )
221
220
if _ , ok := err .(scanner.ErrorList ); ok {
222
221
return ErrContinue
223
222
}
@@ -369,7 +368,7 @@ func (s *Session) Eval(in string) (string, bytes.Buffer, error) {
369
368
if err == ErrQuit {
370
369
return "" , bytes.Buffer {}, err
371
370
}
372
- errorf ("%s: %s" , command .name , err )
371
+ errorf ("%s: %s" , command .name , err . Error () )
373
372
}
374
373
}
375
374
}
@@ -386,14 +385,14 @@ func (s *Session) Eval(in string) (string, bytes.Buffer, error) {
386
385
// Extract statements.
387
386
priorListLength := len (s .mainBody .List )
388
387
if err := s .separateEvalStmt (in ); err != nil {
389
- return "" , bytes.Buffer {} , err
388
+ return "" , * bytes .NewBuffer ([] byte ( err . Error ())) , err
390
389
}
391
390
392
391
s .doQuickFix ()
393
392
394
- output , strerr , err := s .Run ()
395
- if err != nil {
396
- if exitErr , ok := err .(* exec.ExitError ); ok {
393
+ output , stderr , runErr := s .Run ()
394
+ if runErr != nil {
395
+ if exitErr , ok := runErr .(* exec.ExitError ); ok {
397
396
// if failed with status 2, remove the last statement
398
397
if st , ok := exitErr .ProcessState .Sys ().(syscall.WaitStatus ); ok {
399
398
if st .ExitStatus () == 2 {
@@ -402,24 +401,24 @@ func (s *Session) Eval(in string) (string, bytes.Buffer, error) {
402
401
}
403
402
}
404
403
}
405
- errorf ("%s" , err )
404
+ errorf ("%s" , runErr . Error () )
406
405
}
407
406
408
407
// Cleanup the session file.
409
408
s .mainBody .List = s .mainBody .List [0 :priorListLength ]
410
409
if err := s .cleanEvalStmt (in ); err != nil {
411
- return "" , bytes. Buffer {} , err
410
+ return string ( output ), stderr , err
412
411
}
413
412
f , err := os .Create (s .FilePath )
414
413
if err != nil {
415
- return "" , bytes. Buffer {} , err
414
+ return string ( output ), stderr , err
416
415
}
417
416
err = printer .Fprint (f , s .Fset , s .File )
418
417
if err != nil {
419
- return "" , bytes. Buffer {} , err
418
+ return string ( output ), stderr , err
420
419
}
421
420
422
- return string (output ), strerr , err
421
+ return string (output ), stderr , runErr
423
422
}
424
423
425
424
// separateEvalStmt separates what can be evaluated via evalExpr from what cannot.
@@ -445,7 +444,9 @@ func (s *Session) separateEvalStmt(in string) error {
445
444
}
446
445
447
446
if strings .LastIndex (line , "}" ) == len (line )- 1 {
448
- bracketCount --
447
+ if ! strings .HasSuffix (line , "{}" ) {
448
+ bracketCount --
449
+ }
449
450
}
450
451
if strings .LastIndex (line , "{" ) == len (line )- 1 {
451
452
bracketCount ++
@@ -525,17 +526,17 @@ func (s *Session) includeFiles(files []string) {
525
526
func (s * Session ) includeFile (file string ) {
526
527
content , err := ioutil .ReadFile (file )
527
528
if err != nil {
528
- errorf ("%s" , err )
529
+ errorf ("%s" , err . Error () )
529
530
return
530
531
}
531
532
532
533
if err = s .importPackages (content ); err != nil {
533
- errorf ("%s" , err )
534
+ errorf ("%s" , err . Error () )
534
535
return
535
536
}
536
537
537
538
if err = s .importFile (content ); err != nil {
538
- errorf ("%s" , err )
539
+ errorf ("%s" , err . Error () )
539
540
}
540
541
541
542
infof ("added file %s" , file )
0 commit comments