RunPluginContext executes a plugin by name with the provided context.
(ctx context.Context, name string)
| 456 | |
| 457 | // RunPluginContext executes a plugin by name with the provided context. |
| 458 | func (cr *Runner) RunPluginContext(ctx context.Context, name string) Output { |
| 459 | cr.mu.Lock() |
| 460 | |
| 461 | var ( |
| 462 | path = cr.path |
| 463 | p = cr.currentProfile |
| 464 | ) |
| 465 | |
| 466 | ctx, span := cr.tracer.Start(ctx, "plugin", trace.WithAttributes( |
| 467 | attribute.String("name", name), |
| 468 | attribute.String("path", path), |
| 469 | )) |
| 470 | defer span.End() |
| 471 | |
| 472 | // Cancel any currently running command. |
| 473 | if cr.cancelFunc != nil { |
| 474 | // Note: The cancel event is broadcast by the canceled goroutine. |
| 475 | cr.cancelFunc() |
| 476 | } |
| 477 | |
| 478 | // Create a new context for this plugin execution. |
| 479 | ctx, cr.cancelFunc = context.WithCancel(ctx) |
| 480 | |
| 481 | cr.mu.Unlock() |
| 482 | |
| 483 | cr.broadcast(NewEventStart(ctx, TypePlugin)) |
| 484 | |
| 485 | co := NewOutput(TypePlugin) |
| 486 | |
| 487 | plugin := p.GetPlugin(name) |
| 488 | if plugin == nil { |
| 489 | co.Error = fmt.Errorf("plugin %q: not found", name) |
| 490 | cr.broadcast(NewEventEnd(ctx, co)) |
| 491 | |
| 492 | return co |
| 493 | } |
| 494 | |
| 495 | _, err := cr.root.Stat(path) |
| 496 | if err != nil { |
| 497 | co.Error = fmt.Errorf("stat path: %w", err) |
| 498 | cr.broadcast(NewEventEnd(ctx, co)) |
| 499 | |
| 500 | return co |
| 501 | } |
| 502 | |
| 503 | result, err := plugin.Exec(ctx, path) |
| 504 | co.Error = err |
| 505 | co.Stdout = result.Stdout |
| 506 | co.Stderr = result.Stderr |
| 507 | |
| 508 | // Check if the command was canceled. |
| 509 | if co.Error != nil && errors.Is(ctx.Err(), context.Canceled) { |
| 510 | cr.broadcast(NewEventCancel(ctx)) |
| 511 | |
| 512 | return co |
| 513 | } else if co.Error != nil { |
| 514 | co.Error = fmt.Errorf("plugin %q: %w", plugin.Description, co.Error) |
| 515 | cr.broadcast(NewEventEnd(ctx, co)) |
no test coverage detected