{"modulePath":"github.com/contextvibes/cli","version":"v0.6.0","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package exec\n\nPackage exec provides a client and interface for executing external commands. It abstracts the underlying os/exec calls, allowing for easier testing and consistent command execution logic throughout the application.\n\nThe primary components are:\n\n  - CommandExecutor: An interface defining methods to run commands and capture output.\n  - OSCommandExecutor: The default implementation of CommandExecutor using os/exec.\n  - ExecutorClient: A client that uses a CommandExecutor to provide higher-level methods for command execution.\n\nUsage:\n\n\t// In your application setup (e.g., cmd/root.go or per command)\n\tosExecutor := exec.NewOSCommandExecutor(someLogger) // Pass an *slog.Logger\n\texecClient := exec.NewClient(osExecutor)\n\n\t// Later, to run a command:\n\terr := execClient.Execute(ctx, \"/tmp\", \"ls\", \"-l\")\n\tif err != nil {\n\t\t// handle error\n\t}\n\n\t// To capture output:\n\tstdout, stderr, err := execClient.CaptureOutput(ctx, \".\", \"go\", \"version\")\n\tif err != nil {\n\t\t// handle error\n\t}\n\tfmt.Printf(\"Go version: %s\", stdout)\n\n## Types\n\n```go\ntype CommandExecutor interface {\n\t// Execute runs a command, connecting stdio to the parent process's stdio.\n\t// dir: the working directory for the command.\n\t// commandName: the name or path of the command to run.\n\t// args: arguments for the command.\n\t// Returns an error if execution fails.\n\tExecute(ctx context.Context, dir string, commandName string, args ...string) error\n\n\t// CaptureOutput runs a command, capturing its stdout and stderr.\n\t// dir: the working directory for the command.\n\t// commandName: the name or path of the command to run.\n\t// args: arguments for the command.\n\t// Returns stdout, stderr, and any error (including *exec.PkgExitError).\n\tCaptureOutput(\n\t\tctx context.Context,\n\t\tdir string,\n\t\tcommandName string,\n\t\targs ...string,\n\t) (stdout, stderr string, err error)\n\n\t// CommandExists checks if a command is available in the PATH or at the specified path.\n\tCommandExists(commandName string) bool\n\n\t// Logger returns the logger associated with this executor.\n\tLogger() *slog.Logger\n}\n```\nCommandExecutor defines the interface for running external commands. Implementations of this interface handle the actual execution logic.\n\n```go\nfunc NewOSCommandExecutor(logger *slog.Logger) CommandExecutor\n```\nNewOSCommandExecutor creates a new OSCommandExecutor. If logger is nil, a discard logger will be used.\n\n```go\ntype ExecutorClient struct {\n\t// contains filtered or unexported fields\n}\n```\nExecutorClient provides a high-level interface for running external commands. It uses an underlying CommandExecutor for the actual execution.\n\n```go\nfunc NewClient(executor CommandExecutor) *ExecutorClient\n```\nNewClient creates a new ExecutorClient with the given CommandExecutor.\n\n```go\nfunc (c *ExecutorClient) CaptureOutput(\n\tctx context.Context,\n\tdir string,\n\tcommandName string,\n\targs ...string,\n) (string, string, error)\n```\nCaptureOutput runs a command and captures its stdout and stderr. See CommandExecutor.CaptureOutput.\n\n```go\nfunc (c *ExecutorClient) CommandExists(commandName string) bool\n```\nCommandExists checks if a command is available. See CommandExecutor.CommandExists.\n\n```go\nfunc (c *ExecutorClient) Execute(\n\tctx context.Context,\n\tdir string,\n\tcommandName string,\n\targs ...string,\n) error\n```\nExecute runs a command, typically piping stdio. See CommandExecutor.Execute.\n\n```go\nfunc (c *ExecutorClient) Logger() *slog.Logger\n```\nLogger returns the logger from the underlying executor.\n\n```go\nfunc (c *ExecutorClient) UnderlyingExecutor() CommandExecutor\n```\nUnderlyingExecutor returns the CommandExecutor used by this client. This allows passing the raw executor to other components if needed.\n\n```go\ntype OSCommandExecutor struct {\n\t// contains filtered or unexported fields\n}\n```\nOSCommandExecutor is the default implementation of CommandExecutor using the os/exec package.\n\n```go\nfunc (e *OSCommandExecutor) CaptureOutput(\n\tctx context.Context,\n\tdir string,\n\tcommandName string,\n\targs ...string,\n) (string, string, error)\n```\nCaptureOutput runs a command and captures its output.\n\n```go\nfunc (e *OSCommandExecutor) CommandExists(commandName string) bool\n```\nCommandExists checks if a command exists in the path.\n\n```go\nfunc (e *OSCommandExecutor) Execute(\n\tctx context.Context,\n\tdir string,\n\tcommandName string,\n\targs ...string,\n) error\n```\nExecute runs a command, piping stdio.\n\n```go\nfunc (e *OSCommandExecutor) Logger() *slog.Logger\n```\nLogger returns the logger associated with this executor.\n\n","path":"github.com/contextvibes/cli/internal/exec","name":"exec","synopsis":"Package exec provides a client and interface for executing external commands.","isRedistributable":true}
