Documentation ¶
Overview ¶
Package util provides utility operations used in building sansshell system services.
Index ¶
Constants ¶
const DefRunBufLimit = 10 * 1024 * 1024
DefRunBufLimit is the default limit we'll buffer for stdout/stderr from RunCommand exec'ing a process.
const MaxBuf = 1024
MaxBuf is the maximum we should allow stdout or stderr to be when sending back in an error string. grpc has limits on how large a returned error can be (generally 4-8k depending on language).
Variables ¶
var StreamingChunkSize = 128 * 1024
StreamingChunkSize is the chunk size we use when sending replies on a stream. TODO(jchacon): Make this configurable
Functions ¶
func TrimString ¶
TrimString will return the given string truncated to MAX_BUF size so it can be used in grpc error replies.
Types ¶
type CommandRun ¶
type CommandRun struct { Stdout *LimitedBuffer Stderr *LimitedBuffer Error error ExitCode int }
CommandRun groups all of the status and output from executing a command.
func RunCommand ¶
func RunCommand(ctx context.Context, bin string, args []string, opts ...Option) (*CommandRun, error)
RunCommand will take the given binary and args and execute it returning all relevent state (stdout, stderr, errors, etc). The returned buffers for stdout and stderr are limited by possible options but default limit to DefRunBufLimit.
The binary must be a clean absolute path or an error will result and nothing will be run. Any other errors (starting or from waiting) are recorded in the Error field. Errors returned directly will be a status.Error and Error will be whatever the exec library returns.
type ExecuteState ¶
ExecuteState is used by client packages in services to pass relevant state down to subcommands.
type IntSliceFlags ¶
type IntSliceFlags []int64
IntSliceFlags is a custom flag for a list of ints in a comma separated list.
type KeyValue ¶
KeyValue is used below with KeyValueSliceFlag to construct foo=bar,baz=foo type of flags.
type KeyValueSliceFlag ¶
type KeyValueSliceFlag []*KeyValue
KeyValueSliceFlag is a custom flag for a list of strings in a comma separated list of the form key=value,key=value
func (*KeyValueSliceFlag) Set ¶
func (i *KeyValueSliceFlag) Set(val string) error
Set - see flag.Value
func (*KeyValueSliceFlag) String ¶
func (i *KeyValueSliceFlag) String() string
String - see flag.Value
type LimitedBuffer ¶ added in v1.0.5
type LimitedBuffer struct {
// contains filtered or unexported fields
}
LimitedBuffer is a bytes.Buffer with the added limitation that it will not grow infinitely and will instead stop at a max size limit.
func NewLimitedBuffer ¶ added in v1.0.5
func NewLimitedBuffer(max uint) *LimitedBuffer
NewLimitedBuffer will create a LimitedBuffer with the given maximum size.
func (*LimitedBuffer) Bytes ¶ added in v1.0.5
func (l *LimitedBuffer) Bytes() []byte
Bytes - see bytes.Buffer
func (*LimitedBuffer) Read ¶ added in v1.0.5
func (l *LimitedBuffer) Read(p []byte) (int, error)
Read - see io.Reader
func (*LimitedBuffer) String ¶ added in v1.0.5
func (l *LimitedBuffer) String() string
String - see bytes.Buffer
func (*LimitedBuffer) Truncated ¶ added in v1.0.5
func (l *LimitedBuffer) Truncated() bool
Truncated will return true if the LimitedBuffer has filled and refused to write additional bytes.
func (*LimitedBuffer) Write ¶ added in v1.0.5
func (l *LimitedBuffer) Write(p []byte) (int, error)
Write acts exactly as a bytes.Buffer defines except if the underlying buffer has reached the max size no more bytes will be added. TODO: Implement remaining bytes.Buffer methods if needed. NOTE: This is not an error condition and instead no more bytes
will be written and normal return will happen so writes so not fail. Use the Truncated() method to determine if this has happened.
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
Option will run the apply operation to change required checking/state before executing RunCommand.
func FailOnStderr ¶
func FailOnStderr() Option
FailOnStderr is an option where the command will return an error if any output appears on stderr regardless of exit code. As we're often parsing the text output of specific commands as root this is a sanity check we're getting expected output. i.e. ps never returns anything on stderr so if some run does that's suspect. Up to callers to decide as some tools (yum...) like to emit stderr as debugging as they run.
type StringSliceFlag ¶
type StringSliceFlag struct {
Target *[]string
}
StringSliceFlag is the parsed form of a flag using "foo,bar,baz" style.