{"modulePath":"github.com/osapi-io/osapi","version":"v0.2.0","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package exec\n\nPackage exec provides command execution utilities.\n\n## Constants\n\n```go\nconst DefaultCommandTimeout = 10 * time.Minute\n```\nDefaultCommandTimeout bounds a command whose caller supplied no deadline of its own. It is a backstop rather than a budget: long enough for a package installation over a slow mirror, short enough that a command waiting on a prompt nobody will answer does not outlive the agent.\n\nA caller's own deadline still wins whenever it falls earlier, because the deadline is derived from the context it passed.\n\n## Types\n\n```go\ntype CmdResult struct {\n\t// Stdout contains the standard output of the command.\n\tStdout string\n\t// Stderr contains the standard error output of the command.\n\tStderr string\n\t// ExitCode is the exit code of the command.\n\tExitCode int\n\t// DurationMs is the execution time in milliseconds.\n\tDurationMs int64\n}\n```\nCmdResult contains the full result of a command execution with separate stdout and stderr streams.\n\n```go\ntype CommandExecutor interface {\n\tExecute(\n\t\tctx context.Context,\n\t\tname string,\n\t\targs []string,\n\t\tcwd string,\n\t) (string, error)\n\n\t// ExecuteWithStdin runs the command with stdin written to its standard\n\t// input. Implementations must not log stdin.\n\tExecuteWithStdin(\n\t\tctx context.Context,\n\t\tname string,\n\t\targs []string,\n\t\tcwd string,\n\t\tstdin string,\n\t) (string, error)\n}\n```\nCommandExecutor executes OS commands. The default implementation runs real commands via os/exec. Tests inject a mock to assert commands without executing them.\n\nEvery method takes a context: it is what cancels a command that is still running when the agent shuts down, and what bounds one that would otherwise run forever.\n\n```go\ntype Exec struct {\n\t// contains filtered or unexported fields\n}\n```\nExec disk implementation.\n\n```go\nfunc New(\n\tlogger *slog.Logger,\n\tsudo bool,\n) *Exec\n```\nNew factory to create a new Exec instance.\n\n```go\nfunc (e *Exec) RunCmd(\n\tctx context.Context,\n\tname string,\n\targs []string,\n) (string, error)\n```\nRunCmd executes the provided command with arguments, using the current working directory.\n\n```go\nfunc (e *Exec) RunCmdFull(\n\tctx context.Context,\n\tname string,\n\targs []string,\n\tcwd string,\n\ttimeout int,\n) (*CmdResult, error)\n```\nRunCmdFull executes the provided command with separate stdout and stderr capture, an optional working directory, and a timeout in seconds. A timeout of 0 defaults to 30 seconds.\n\n```go\nfunc (e *Exec) RunCmdImpl(\n\tctx context.Context,\n\tname string,\n\targs []string,\n\tcwd string,\n) (string, error)\n```\nRunCmdImpl executes the provided command with the specified arguments and an optional working directory. It delegates to the CommandExecutor.\n\n```go\nfunc (e *Exec) RunCmdInDir(\n\tctx context.Context,\n\tname string,\n\targs []string,\n\tcwd string,\n) (string, error)\n```\nRunCmdInDir executes the provided command with arguments, using the current provided directory.\n\n```go\nfunc (e *Exec) RunPrivilegedCmd(\n\tctx context.Context,\n\tname string,\n\targs []string,\n) (string, error)\n```\nRunPrivilegedCmd executes the provided command with arguments. When sudo is enabled, the original command is passed as an argument to \"sudo\".\n\n```go\nfunc (e *Exec) RunPrivilegedCmdWithStdin(\n\tctx context.Context,\n\tname string,\n\targs []string,\n\tstdin string,\n) (string, error)\n```\nRunPrivilegedCmdWithStdin executes the provided command with arguments and writes stdin to the command's standard input. When sudo is enabled, the command is prepended with \"sudo\".\n\nPass secrets this way rather than in arguments: stdin is not interpreted by a shell, does not appear in the process list, and is never logged.\n\n```go\ntype Manager interface {\n\t// RunCmd executes the provided command with arguments, using the current\n\t// working directory.\n\tRunCmd(\n\t\tctx context.Context,\n\t\tname string,\n\t\targs []string,\n\t) (string, error)\n\n\t// RunPrivilegedCmd executes the provided command with arguments.\n\t// When sudo is enabled, the command is prepended with \"sudo\".\n\tRunPrivilegedCmd(\n\t\tctx context.Context,\n\t\tname string,\n\t\targs []string,\n\t) (string, error)\n\n\t// RunPrivilegedCmdWithStdin executes the provided command with arguments\n\t// and writes stdin to its standard input. When sudo is enabled, the\n\t// command is prepended with \"sudo\". Use it to pass secrets, which must\n\t// never be placed in arguments.\n\tRunPrivilegedCmdWithStdin(\n\t\tctx context.Context,\n\t\tname string,\n\t\targs []string,\n\t\tstdin string,\n\t) (string, error)\n\n\t// RunCmdFull executes a command with separate stdout/stderr capture,\n\t// an optional working directory, and a timeout in seconds.\n\tRunCmdFull(\n\t\tctx context.Context,\n\t\tname string,\n\t\targs []string,\n\t\tcwd string,\n\t\ttimeout int,\n\t) (*CmdResult, error)\n}\n```\nManager manager responsible for exec operations.\n\nEvery method takes a context. A command with no deadline of its own is bounded by DefaultCommandTimeout, and one still running when the agent shuts down is killed with it rather than outliving it.\n\n","path":"github.com/osapi-io/osapi/internal/exec","name":"exec","synopsis":"Package exec provides command execution utilities.","isRedistributable":true}
