@@ -46,7 +46,7 @@ type Command struct {
46
46
brokenArgs []string
47
47
cmd * exec.Cmd // for debug purpose only
48
48
configArgs []string
49
- opts * runOpts
49
+ opts runOpts
50
50
}
51
51
52
52
func logArgSanitize (arg string ) string {
@@ -269,136 +269,61 @@ func CommonCmdServEnvs() []string {
269
269
270
270
var ErrBrokenCommand = errors .New ("git command is broken" )
271
271
272
- type stringWriter struct {
273
- str * string
274
- }
275
-
276
- func (w * stringWriter ) Write (p []byte ) (n int , err error ) {
277
- * w .str += util .UnsafeBytesToString (p )
278
- return len (p ), nil
279
- }
280
-
281
- func (c * Command ) WithStringOutput (output * string ) * Command {
282
- if output != nil {
283
- if c .opts == nil {
284
- c .opts = & runOpts {}
285
- }
286
- c .opts .Stdout = & stringWriter {str : output }
287
- }
288
- return c
289
- }
290
-
291
- func (c * Command ) WithStringErr (stderr * string ) * Command {
292
- if stderr != nil {
293
- if c .opts == nil {
294
- c .opts = & runOpts {}
295
- }
296
- c .opts .Stderr = & stringWriter {str : stderr }
297
- }
298
- return c
299
- }
300
-
301
- type bytesWriter struct {
302
- buf * []byte
303
- }
304
-
305
- func (w * bytesWriter ) Write (p []byte ) (n int , err error ) {
306
- * w .buf = append (* w .buf , p ... )
307
- return len (p ), nil
308
- }
309
-
310
- func (c * Command ) WithBytesOutput (output * []byte ) * Command {
311
- if output != nil {
312
- if c .opts == nil {
313
- c .opts = & runOpts {}
314
- }
315
- c .opts .Stdout = & bytesWriter {buf : output }
316
- }
317
- return c
318
- }
319
-
320
272
func (c * Command ) WithDir (dir string ) * Command {
321
273
if dir != "" {
322
- if c .opts == nil {
323
- c .opts = & runOpts {}
324
- }
325
274
c .opts .Dir = dir
326
275
}
327
276
return c
328
277
}
329
278
330
279
func (c * Command ) WithEnv (env []string ) * Command {
331
280
if env != nil {
332
- if c .opts == nil {
333
- c .opts = & runOpts {}
334
- }
335
281
c .opts .Env = env
336
282
}
337
283
return c
338
284
}
339
285
340
286
func (c * Command ) WithTimeout (timeout time.Duration ) * Command {
341
287
if timeout > 0 {
342
- if c .opts == nil {
343
- c .opts = & runOpts {}
344
- }
345
288
c .opts .Timeout = timeout
346
289
}
347
290
return c
348
291
}
349
292
350
293
func (c * Command ) WithStdout (stdout io.Writer ) * Command {
351
294
if stdout != nil {
352
- if c .opts == nil {
353
- c .opts = & runOpts {}
354
- }
355
295
c .opts .Stdout = stdout
356
296
}
357
297
return c
358
298
}
359
299
360
300
func (c * Command ) WithStderr (stderr io.Writer ) * Command {
361
301
if stderr != nil {
362
- if c .opts == nil {
363
- c .opts = & runOpts {}
364
- }
365
302
c .opts .Stderr = stderr
366
303
}
367
304
return c
368
305
}
369
306
370
307
func (c * Command ) WithStdin (stdin io.Reader ) * Command {
371
308
if stdin != nil {
372
- if c .opts == nil {
373
- c .opts = & runOpts {}
374
- }
375
309
c .opts .Stdin = stdin
376
310
}
377
311
return c
378
312
}
379
313
380
314
func (c * Command ) WithPipelineFunc (f func (context.Context , context.CancelFunc ) error ) * Command {
381
315
if f != nil {
382
- if c .opts == nil {
383
- c .opts = & runOpts {}
384
- }
385
316
c .opts .PipelineFunc = f
386
317
}
387
318
return c
388
319
}
389
320
390
321
func (c * Command ) WithUseContextTimeout (useContextTimeout bool ) * Command {
391
- if c .opts == nil {
392
- c .opts = & runOpts {}
393
- }
394
322
c .opts .UseContextTimeout = useContextTimeout
395
323
return c
396
324
}
397
325
398
326
func (c * Command ) WithLogSkipStep (stepSkip int ) * Command {
399
- if c .opts == nil {
400
- c .opts = & runOpts {}
401
- }
402
327
c .opts .LogSkip += stepSkip
403
328
return c
404
329
}
@@ -409,9 +334,6 @@ func (c *Command) Run(ctx context.Context) error {
409
334
log .Error ("git command is broken: %s, broken args: %s" , c .LogString (), strings .Join (c .brokenArgs , " " ))
410
335
return ErrBrokenCommand
411
336
}
412
- if c .opts == nil {
413
- c .opts = & runOpts {}
414
- }
415
337
416
338
// We must not change the provided options
417
339
timeout := c .opts .Timeout
@@ -547,10 +469,6 @@ func (c *Command) RunStdString(ctx context.Context) (stdout, stderr string, runE
547
469
548
470
// RunStdBytes runs the command and returns stdout/stderr as bytes. and store stderr to returned error (err combined with stderr).
549
471
func (c * Command ) RunStdBytes (ctx context.Context ) (stdout , stderr []byte , runErr RunStdError ) {
550
- if c .opts == nil {
551
- c .opts = & runOpts {}
552
- }
553
-
554
472
if c .opts .Stdout != nil || c .opts .Stderr != nil {
555
473
// we must panic here, otherwise there would be bugs if developers set Stdin/Stderr by mistake, and it would be very difficult to debug
556
474
panic ("stdout and stderr field must be nil when using RunStdBytes" )
0 commit comments