command

package
v18.6.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 25, 2025 License: MIT Imports: 29 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// EnvLogConfiguration is the environment variable under which the logging configuration is
	// passed to subprocesses.
	EnvLogConfiguration = "GITALY_LOG_CONFIGURATION"
)

Variables

This section is empty.

Functions

func AllowedEnvironment

func AllowedEnvironment(envs []string) []string

AllowedEnvironment filters the given slice of environment variables and returns all variables which are allowed per the variables defined above. This is useful for constructing a base environment in which a command can be run.

func ExitStatus

func ExitStatus(err error) (int, bool)

ExitStatus will return the exit-code from an error returned by Wait().

func NewSubprocessLogger

func NewSubprocessLogger(ctx context.Context, getEnv func(string) string, component string) (_ log.Logger, _ io.Closer, returnedErr error)

NewSubprocessLogger returns a logger than can be used in a subprocess spawned by Gitaly. It extracts the log directory from the environment using getEnv and outputs logs in the given format log dir under a file defined by logFileName. If `GITALY_LOG_CONFIGURATION` is set, it outputs the logs to the file descriptor using the configuration from the environment variable.

Types

type Command

type Command struct {
	// contains filtered or unexported fields
}

Command encapsulates a running exec.Cmd. The embedded exec.Cmd is terminated and reaped automatically when the context.Context that created it is canceled.

func New

func New(ctx context.Context, logger log.Logger, nameAndArgs []string, opts ...Option) (_ *Command, returnedErr error)

New creates a Command from the given executable name and arguments On success, the Command contains a running subprocess. When ctx is canceled the embedded process will be terminated and reaped automatically.

func (*Command) Args

func (c *Command) Args() []string

Args is an accessor for the command arguments

func (*Command) Env

func (c *Command) Env() []string

Env is an accessor for the environment variables

func (*Command) Pid

func (c *Command) Pid() int

Pid is an accessor for the pid

func (*Command) Read

func (c *Command) Read(p []byte) (int, error)

Read calls Read() on the stdout pipe of the command.

func (*Command) Stdin

func (c *Command) Stdin() io.WriteCloser

Stdin returns the commands stdin pipe.

func (*Command) Wait

func (c *Command) Wait() error

Wait calls Wait() on the exec.Cmd instance inside the command. This blocks until the command has finished and reports the command exit status via the error return value. Use ExitStatus to get the integer exit status from the error returned by Wait().

Wait returns a wrapped context error if the process was reaped due to the context being done.

func (*Command) Write

func (c *Command) Write(p []byte) (int, error)

Write calls Write() on the stdin pipe of the command.

type Option

type Option func(cfg *config)

Option is an option that can be passed to `New()` for controlling how the command is being created.

func WithCgroup

func WithCgroup(cgroupsManager cgroups.Manager, opts ...cgroups.AddCommandOption) Option

WithCgroup adds the spawned command to a Cgroup. The bucket used will be derived from the command's arguments and/or from the repository.

func WithCommandGitVersion

func WithCommandGitVersion(gitCmdVersion string) Option

WithCommandGitVersion overrides the "git_version" label used in metrics.

func WithCommandName

func WithCommandName(commandName, subcommandName string) Option

WithCommandName overrides the "cmd" and "subcmd" label used in metrics.

func WithCompletionErrorLogFilter

func WithCompletionErrorLogFilter(fn func(cmd *Command, stderr string) bool) Option

WithCompletionErrorLogFilter configures a function that should return true if an errored command should not produce logs.

func WithDir

func WithDir(dir string) Option

WithDir will set up the command to be ran in the specific directory.

func WithEnvironment

func WithEnvironment(environment []string) Option

WithEnvironment sets up environment variables that shall be set for the command.

func WithFinalizer

func WithFinalizer(finalizer func(context.Context, *Command)) Option

WithFinalizer sets up the finalizer to be run when the command is being wrapped up. It will be called after `Wait()` has returned.

func WithReferenceBackend

func WithReferenceBackend(refBackend git.ReferenceBackend) Option

WithReferenceBackend overrides the "reference_backend" label used in metrics.

func WithSetupStdin

func WithSetupStdin() Option

WithSetupStdin instructs New() to configure the stdin pipe of the command it is creating. This allows you call Write() on the command as if it is an ordinary io.Writer, sending data directly to the stdin of the process.

func WithSetupStdout

func WithSetupStdout() Option

WithSetupStdout instructs New() to configure the standard output pipe of the command it is creating. This allowsyou to call Read() on the command as if it is an ordinary io.Reader, reading output directly from the stdout of the process.

func WithStderr

func WithStderr(stderr io.Writer) Option

WithStderr sets up the command to write standard error to the given writer.

func WithStdin

func WithStdin(stdin io.Reader) Option

WithStdin sets up the command to read from the given reader.

func WithStdout

func WithStdout(stdout io.Writer) Option

WithStdout sets up the command to write standard output to the given writer.

func WithSubprocessLogger

func WithSubprocessLogger(logConfig log.Config) Option

WithSubprocessLogger sets up a goroutine that consumes logs from the subprocess through a pipe and outputs them in Logger's output.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL