@@ -223,10 +223,7 @@ type runOpts struct {
223
223
224
224
PipelineFunc func (context.Context , context.CancelFunc ) error
225
225
226
- // for debugging purpose only, it indicates how many stack frames to skip to find the caller info
227
- // it's mainly used to find the caller function name for logging
228
- // it should be 0 when the caller is the direct caller of Run/RunStdString/RunStdBytes
229
- LogSkip int
226
+ callerInfo string
230
227
}
231
228
232
229
func commonBaseEnvs () []string {
@@ -270,51 +267,37 @@ func CommonCmdServEnvs() []string {
270
267
var ErrBrokenCommand = errors .New ("git command is broken" )
271
268
272
269
func (c * Command ) WithDir (dir string ) * Command {
273
- if dir != "" {
274
- c .opts .Dir = dir
275
- }
270
+ c .opts .Dir = dir
276
271
return c
277
272
}
278
273
279
274
func (c * Command ) WithEnv (env []string ) * Command {
280
- if env != nil {
281
- c .opts .Env = env
282
- }
275
+ c .opts .Env = env
283
276
return c
284
277
}
285
278
286
279
func (c * Command ) WithTimeout (timeout time.Duration ) * Command {
287
- if timeout > 0 {
288
- c .opts .Timeout = timeout
289
- }
280
+ c .opts .Timeout = timeout
290
281
return c
291
282
}
292
283
293
284
func (c * Command ) WithStdout (stdout io.Writer ) * Command {
294
- if stdout != nil {
295
- c .opts .Stdout = stdout
296
- }
285
+ c .opts .Stdout = stdout
297
286
return c
298
287
}
299
288
300
289
func (c * Command ) WithStderr (stderr io.Writer ) * Command {
301
- if stderr != nil {
302
- c .opts .Stderr = stderr
303
- }
290
+ c .opts .Stderr = stderr
304
291
return c
305
292
}
306
293
307
294
func (c * Command ) WithStdin (stdin io.Reader ) * Command {
308
- if stdin != nil {
309
- c .opts .Stdin = stdin
310
- }
295
+ c .opts .Stdin = stdin
311
296
return c
312
297
}
313
298
314
299
func (c * Command ) WithPipelineFunc (f func (context.Context , context.CancelFunc ) error ) * Command {
315
- if f != nil {
316
- c .opts .PipelineFunc = f
317
- }
300
+ c .opts .PipelineFunc = f
318
301
return c
319
302
}
320
303
@@ -323,8 +306,22 @@ func (c *Command) WithUseContextTimeout(useContextTimeout bool) *Command {
323
306
return c
324
307
}
325
308
326
- func (c * Command ) WithLogSkipStep (stepSkip int ) * Command {
327
- c .opts .LogSkip += stepSkip
309
+ // WithParentCallerInfo can be used to set the caller info (usually function name) of the parent function of the caller.
310
+ // For most cases, "Run" family functions can get its caller info automatically
311
+ // But if you need to call "Run" family functions in a wrapper function: "FeatureFunc -> GeneralWrapperFunc -> RunXxx",
312
+ // then you can to call this function in GeneralWrapperFunc to set the caller info of FeatureFunc.
313
+ func (c * Command ) WithParentCallerInfo (optInfo ... string ) * Command {
314
+ if len (optInfo ) > 0 {
315
+ c .opts .callerInfo = optInfo [0 ]
316
+ return c
317
+ }
318
+ skip := 1 /*parent "wrap/run" functions*/ + 1 /*this function*/
319
+ callerFuncName := util .CallerFuncName (skip )
320
+ callerInfo := callerFuncName
321
+ if pos := strings .LastIndex (callerInfo , "/" ); pos >= 0 {
322
+ callerInfo = callerInfo [pos + 1 :]
323
+ }
324
+ c .opts .callerInfo = callerInfo
328
325
return c
329
326
}
330
327
@@ -342,17 +339,16 @@ func (c *Command) Run(ctx context.Context) error {
342
339
}
343
340
344
341
cmdLogString := c .LogString ()
345
- callerInfo := util .CallerFuncName (1 /* util */ + 1 /* this */ + c .opts .LogSkip /* parent */ )
346
- if pos := strings .LastIndex (callerInfo , "/" ); pos >= 0 {
347
- callerInfo = callerInfo [pos + 1 :]
342
+ if c .opts .callerInfo == "" {
343
+ c .WithParentCallerInfo ()
348
344
}
349
345
// these logs are for debugging purposes only, so no guarantee of correctness or stability
350
- desc := fmt .Sprintf ("git.Run(by:%s, repo:%s): %s" , callerInfo , logArgSanitize (c .opts .Dir ), cmdLogString )
346
+ desc := fmt .Sprintf ("git.Run(by:%s, repo:%s): %s" , c . opts . callerInfo , logArgSanitize (c .opts .Dir ), cmdLogString )
351
347
log .Debug ("git.Command: %s" , desc )
352
348
353
349
_ , span := gtprof .GetTracer ().Start (ctx , gtprof .TraceSpanGitRun )
354
350
defer span .End ()
355
- span .SetAttributeString (gtprof .TraceAttrFuncCaller , callerInfo )
351
+ span .SetAttributeString (gtprof .TraceAttrFuncCaller , c . opts . callerInfo )
356
352
span .SetAttributeString (gtprof .TraceAttrGitCommand , cmdLogString )
357
353
358
354
var cancel context.CancelFunc
@@ -457,7 +453,7 @@ func IsErrorExitCode(err error, code int) bool {
457
453
458
454
// RunStdString runs the command and returns stdout/stderr as string. and store stderr to returned error (err combined with stderr).
459
455
func (c * Command ) RunStdString (ctx context.Context ) (stdout , stderr string , runErr RunStdError ) {
460
- stdoutBytes , stderrBytes , err := c .WithLogSkipStep ( 1 ).RunStdBytes (ctx )
456
+ stdoutBytes , stderrBytes , err := c .WithParentCallerInfo ( ).RunStdBytes (ctx )
461
457
stdout = util .UnsafeBytesToString (stdoutBytes )
462
458
stderr = util .UnsafeBytesToString (stderrBytes )
463
459
if err != nil {
@@ -477,7 +473,7 @@ func (c *Command) RunStdBytes(ctx context.Context) (stdout, stderr []byte, runEr
477
473
stdoutBuf := & bytes.Buffer {}
478
474
stderrBuf := & bytes.Buffer {}
479
475
480
- err := c .WithLogSkipStep ( 1 ).
476
+ err := c .WithParentCallerInfo ( ).
481
477
WithStdout (stdoutBuf ).
482
478
WithStderr (stderrBuf ).
483
479
Run (ctx )
0 commit comments