11package xcresult3
22
33import (
4+ "bufio"
5+ "bytes"
46 "encoding/json"
57 "fmt"
8+ "io"
9+ "os"
610 "os/exec"
711 "regexp"
812 "strconv"
13+ "strings"
914
1015 "github.com/bitrise-io/go-utils/command"
1116 "github.com/bitrise-io/go-utils/errorutil"
17+ command2 "github.com/bitrise-io/go-utils/v2/command"
18+ "github.com/bitrise-io/go-utils/v2/env"
19+ "github.com/bitrise-io/go-utils/v2/log"
1220)
1321
1422func isXcresulttoolAvailable () bool {
@@ -64,6 +72,9 @@ func supportsNewExtractionMethods() (bool, error) {
6472
6573// xcresulttoolGet performs xcrun xcresulttool get with --id flag defined if id provided and marshals the output into v.
6674func xcresulttoolGet (xcresultPth , id string , useLegacyFlag bool , v interface {}) error {
75+ commandFactory := command2 .NewFactory (env .NewRepository ())
76+ logger := log .NewLogger ()
77+
6778 args := []string {"xcresulttool" , "get" }
6879
6980 supportsNewMethod , err := supportsNewExtractionMethods ()
@@ -87,20 +98,52 @@ func xcresulttoolGet(xcresultPth, id string, useLegacyFlag bool, v interface{})
8798 args = append (args , "--id" , id )
8899 }
89100
90- cmd := command .New ("xcrun" , args ... )
91- out , err := cmd .RunAndReturnTrimmedCombinedOutput ()
92- if err != nil {
101+ var outBuffer , errBuffer , combinedBuffer bytes.Buffer
102+ outWriter := io .MultiWriter (& outBuffer , & combinedBuffer )
103+ errWriter := io .MultiWriter (& errBuffer , & combinedBuffer )
104+
105+ cmd := commandFactory .Create ("xcrun" , args , & command2.Opts {
106+ Stdout : outWriter ,
107+ Stderr : errWriter ,
108+ Stdin : nil ,
109+ Env : os .Environ (),
110+ Dir : "" ,
111+ ErrorFinder : nil ,
112+ })
113+ if err := cmd .Run (); err != nil {
93114 if errorutil .IsExitStatusError (err ) {
94- return fmt .Errorf ("%s failed: %s" , cmd .PrintableCommandArgs (), out )
115+ return fmt .Errorf ("%s failed: %s" , cmd .PrintableCommandArgs (), combinedBuffer . String () )
95116 }
96117 return fmt .Errorf ("%s failed: %s" , cmd .PrintableCommandArgs (), err )
97118 }
98- if err := json .Unmarshal ([]byte (out ), v ); err != nil {
119+ if stdErr := errBuffer .String (); stdErr != "" {
120+ logger .Warnf ("%s: %s" , cmd .PrintableCommandArgs (), stdErr )
121+ }
122+
123+ stdout := outBuffer .Bytes ()
124+ if err := json .Unmarshal (stdout , v ); err != nil {
125+ logger .Warnf ("Failed to parse %s command output, first lines:\n %s" , cmd .PrintableCommandArgs (), firstLines (string (stdout ), 10 ))
99126 return err
100127 }
101128 return nil
102129}
103130
131+ func firstLines (out string , count int ) string {
132+ if count < 1 {
133+ return ""
134+ }
135+
136+ var lines []string
137+ scanner := bufio .NewScanner (strings .NewReader (out ))
138+ for scanner .Scan () {
139+ lines = append (lines , scanner .Text ())
140+ if len (lines ) >= count {
141+ break
142+ }
143+ }
144+ return strings .Join (lines , "\n " )
145+ }
146+
104147// xcresulttoolExport exports a file with the given id at the given output path.
105148func xcresulttoolExport (xcresultPth , id , outputPth string , useLegacyFlag bool ) error {
106149 args := []string {"xcresulttool" , "export" }
0 commit comments