@@ -93,14 +93,14 @@ var ReleaseVersion string
93
93
var BuildVersion string
94
94
95
95
func main () {
96
- err := mainImplementation (os .Args [1 :])
96
+ err := mainImplementation (os .Stdout , os . Stderr , os . Args [1 :])
97
97
if err != nil {
98
98
fmt .Fprintf (os .Stderr , "error: %s\n " , err )
99
99
os .Exit (1 )
100
100
}
101
101
}
102
102
103
- func mainImplementation (args []string ) error {
103
+ func mainImplementation (stdout , stderr io. Writer , args []string ) error {
104
104
var nameStyle sizes.NameStyle = sizes .NameStyleFull
105
105
var cpuprofile string
106
106
var jsonOutput bool
@@ -116,7 +116,7 @@ func mainImplementation(args []string) error {
116
116
117
117
flags := pflag .NewFlagSet ("git-sizer" , pflag .ContinueOnError )
118
118
flags .Usage = func () {
119
- fmt .Print ( usage )
119
+ fmt .Fprint ( stdout , usage )
120
120
}
121
121
122
122
flags .VarP (
@@ -154,11 +154,15 @@ func mainImplementation(args []string) error {
154
154
flags .BoolVarP (& jsonOutput , "json" , "j" , false , "output results in JSON format" )
155
155
flags .IntVar (& jsonVersion , "json-version" , 1 , "JSON format version to output (1 or 2)" )
156
156
157
- atty , err := isatty .Isatty (os .Stderr .Fd ())
158
- if err != nil {
159
- atty = false
157
+ defaultProgress := false
158
+ if f , ok := stderr .(* os.File ); ok {
159
+ atty , err := isatty .Isatty (f .Fd ())
160
+ if err == nil && atty {
161
+ defaultProgress = true
162
+ }
160
163
}
161
- flags .BoolVar (& progress , "progress" , atty , "report progress to stderr" )
164
+
165
+ flags .BoolVar (& progress , "progress" , defaultProgress , "report progress to stderr" )
162
166
flags .BoolVar (& version , "version" , false , "report the git-sizer version number" )
163
167
flags .Var (& NegatedBoolValue {& progress }, "no-progress" , "suppress progress output" )
164
168
flags .Lookup ("no-progress" ).NoOptDefVal = "true"
@@ -205,9 +209,9 @@ func mainImplementation(args []string) error {
205
209
206
210
if version {
207
211
if ReleaseVersion != "" {
208
- fmt .Printf ( "git-sizer release %s\n " , ReleaseVersion )
212
+ fmt .Fprintf ( stdout , "git-sizer release %s\n " , ReleaseVersion )
209
213
} else {
210
- fmt .Printf ( "git-sizer build %s\n " , BuildVersion )
214
+ fmt .Fprintf ( stdout , "git-sizer build %s\n " , BuildVersion )
211
215
}
212
216
return nil
213
217
}
@@ -275,13 +279,13 @@ func mainImplementation(args []string) error {
275
279
}
276
280
277
281
if showRefs {
278
- fmt .Fprintf (os . Stderr , "References (included references marked with '+'):\n " )
279
- rg = refopts .NewShowRefGrouper (rg , os . Stderr )
282
+ fmt .Fprintf (stderr , "References (included references marked with '+'):\n " )
283
+ rg = refopts .NewShowRefGrouper (rg , stderr )
280
284
}
281
285
282
286
var progressMeter meter.Progress = meter .NoProgressMeter
283
287
if progress {
284
- progressMeter = meter .NewProgressMeter (os . Stderr , 100 * time .Millisecond )
288
+ progressMeter = meter .NewProgressMeter (stderr , 100 * time .Millisecond )
285
289
}
286
290
287
291
historySize , err := sizes .ScanRepositoryUsingGraph (repo , rg , nameStyle , progressMeter )
@@ -303,11 +307,10 @@ func mainImplementation(args []string) error {
303
307
if err != nil {
304
308
return fmt .Errorf ("could not convert %v to json: %w" , historySize , err )
305
309
}
306
- fmt .Printf ( "%s\n " , j )
310
+ fmt .Fprintf ( stdout , "%s\n " , j )
307
311
} else {
308
312
if _ , err := io .WriteString (
309
- os .Stdout ,
310
- historySize .TableString (rg .Groups (), threshold , nameStyle ),
313
+ stdout , historySize .TableString (rg .Groups (), threshold , nameStyle ),
311
314
); err != nil {
312
315
return fmt .Errorf ("writing output: %w" , err )
313
316
}
0 commit comments