Documentation
¶
Overview ¶
Package toolexec runs the external tools Draugr orchestrates, and reports what it ran.
It exists so that every external invocation, scanners today, SBOM generation and whatever comes next, narrates itself the same way. Without that, a run is a black box: you can see that something failed but not the command, the directory, how long it took, or what the tool itself said about it.
Index ¶
- func Divide(commands int) (concurrent int, env []string)
- func Parallelism() int
- func PeakMemory() (uint64, bool)
- func Run(ctx context.Context, dir string, argv []string) ([]byte, error)
- func RunCombined(ctx context.Context, dir string, argv []string) ([]byte, error)
- func RunCombinedWithEnv(ctx context.Context, dir string, argv, env []string) ([]byte, error)
- func RunWithEnv(ctx context.Context, dir string, argv, env []string) ([]byte, error)
- func RunWithStderr(ctx context.Context, dir string, argv, env []string) (stdout, stderr []byte, err error)
- func RunWithStderrShown(ctx context.Context, dir string, argv, env []string, shown func([]byte) []byte) (stdout, stderr []byte, err error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Divide ¶ added in v0.166.3
Divide shares the bound among the `go` commands one tool runs at once, for a tool that runs up to commands of them. It returns how many the tool should run at once, commands or the bound where that is fewer and at least one, and the environment that tool runs with: GOFLAGS with -p at the bound divided by that many, rounded down and at least one, after the caller's other flags. Each command then builds with at most that -p, so together they run at most the bound's compile processes rather than the bound's for each.
The environment is for the tool's run to pass as its own, which the bound is layered over: GOMAXPROCS stays the whole bound, because it sizes the tool's own threads and not its builds. A caller whose GOFLAGS sets -p keeps it, and the environment is then empty: that caller chose each command's parallelism, and the count returned still bounds how many commands run.
func Parallelism ¶ added in v0.166.2
func Parallelism() int
Parallelism is the bound every tool run is given as GOMAXPROCS and as -p in GOFLAGS, where the caller set neither, computed once per process from the machine's cores and memory.
func PeakMemory ¶ added in v0.166.2
PeakMemory is the largest resident memory, in bytes, of any single process this one has run and waited for, including the processes those started and waited for, and false where the operating system does not report it. It is the largest one process reached, not a sum, and it covers the life of this process.
func Run ¶
Run executes argv and returns its stdout. dir sets the working directory; empty inherits the current one. Some tools resolve their target relative to the working directory rather than from an argument (gosec loads Go packages via `./...`), so for those the checkout has to be the cwd, not just a path passed in.
No shell is involved, exec.CommandContext, not "sh -c". And argv is built from typed config by the caller, never from user shell input.
func RunCombined ¶ added in v0.52.0
RunCombined is Run capturing stderr alongside stdout.
For a tool asked a question rather than told to scan. Several write their answer to stderr, `nuclei -templates-version` prints its whole reply there and nothing at all to stdout, so a caller reading only stdout gets an empty string and concludes the wrong thing. Keeping this separate from Run is deliberate: a scanner's stdout is a report to be parsed, and folding stderr into it would corrupt the parse for every tool that logs while it works.
func RunCombinedWithEnv ¶ added in v0.126.0
RunCombinedWithEnv is RunCombined with extra environment variables layered over the parent's.
For a tool that is told where to work through the environment and answers on stderr. notation reads its trust policy from NOTATION_CONFIG and says why verification failed on stderr, with nothing on stdout to parse, so a caller needs both halves at once.
func RunWithEnv ¶ added in v0.45.0
RunWithEnv is Run with extra environment variables layered over the parent's, each "K=V".
A tool that shells out to another tool cannot be told which context to work in through argv, kube-bench invokes kubectl itself, and kubectl takes its cluster from the environment. This is how a scanner points such a tool at the right target rather than whatever the machine happens to be configured for.
func RunWithStderr ¶ added in v0.135.1
func RunWithStderr(ctx context.Context, dir string, argv, env []string) (stdout, stderr []byte, err error)
RunWithStderr is RunWithEnv returning the tool's stderr beside its stdout, kept apart.
For a tool that reports on stdout and logs on stderr what it could not do, then exits 0. Trivy logs each Terraform module it failed to load and scans on without it, and nothing in its report says so. A caller that reads only stdout sees a clean scan of less than was there.
func RunWithStderrShown ¶ added in v0.158.0
func RunWithStderrShown(ctx context.Context, dir string, argv, env []string, shown func([]byte) []byte) (stdout, stderr []byte, err error)
RunWithStderrShown is RunWithStderr for a tool whose log can hold what must not be repeated. shown, when not nil, receives the whole stream and returns the part Draugr may relay into its own log and quote in the error. The caller still receives all of it.
Gitleaks at trace level logs the path of each file it skips, which is the only record of the skip, and in the same stream the value of each candidate secret it set aside. Relaying that stream as it is would copy those values into `--log-level trace` output and any `--log-file`.
Types ¶
This section is empty.