util

package
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2022 License: Apache-2.0 Imports: 10 Imported by: 8

Documentation

Overview

Package util contains useful components for simplifying Go code.

The package contains common error types (errors.go) and functions for converting arrays to pointers.

Index

Constants

This section is empty.

Variables

View Source
var (

	// ErrSkipIntegration indicates integration tests shouldn't be run.
	ErrSkipIntegration = errors.New("skipping integration test")
)

Functions

func AtoiOrPanic added in v0.2.1

func AtoiOrPanic(input string) int

AtoiOrPanic converts a string to an int or it panics. Should only be used in situations where the input MUST be a decimal number.

func ByteSlice added in v0.2.0

func ByteSlice(ptr unsafe.Pointer) []byte

ByteSlice takes a pointer to some data and views it as a slice of bytes. Note, indexing into this slice is unsafe.

func CheckValidLength

func CheckValidLength(expected, actual int) error

CheckValidLength returns an invalid length error if expected != actual

func Chown added in v0.3.2

func Chown(file *os.File, user *user.User) error

Chown changes the owner of a File to a User.

func EffectiveUser added in v0.2.1

func EffectiveUser() (*user.User, error)

EffectiveUser returns the user entry corresponding to the effective user.

func Index

func Index(inVal int64, inArray []int64) (index int, ok bool)

Index returns the first index i such that inVal == inArray[i]. ok is true if we find a match, false otherwise.

func IsKernelVersionAtLeast added in v0.2.7

func IsKernelVersionAtLeast(major, minor int) bool

IsKernelVersionAtLeast returns true if the Linux kernel version is at least major.minor. If something goes wrong it assumes false.

func IsUserRoot added in v0.2.1

func IsUserRoot() bool

IsUserRoot checks if the effective user is root.

func Lookup

func Lookup(inVal int64, inArray, outArray []int64) (outVal int64, ok bool)

Lookup finds inVal in inArray and returns the corresponding element in outArray. Specifically, if inVal == inArray[i], outVal == outArray[i]. ok is true if we find a match, false otherwise.

func MaxInt added in v0.2.0

func MaxInt(a, b int) int

MaxInt returns the greater of a and b.

func MinInt

func MinInt(a, b int) int

MinInt returns the lesser of a and b.

func MinInt64

func MinInt64(a, b int64) int64

MinInt64 returns the lesser of a and b.

func NeverError

func NeverError(err error)

NeverError panics if a non-nil error is passed in. It should be used to check for logic errors, not to handle recoverable errors.

func PointerSlice added in v0.2.0

func PointerSlice(ptr unsafe.Pointer) []unsafe.Pointer

PointerSlice takes a pointer to an array of pointers and views it as a slice of pointers. Note, indexing into this slice is unsafe.

func Ptr

func Ptr(slice []byte) unsafe.Pointer

Ptr converts a Go byte array to a pointer to the start of the array.

func ReadLine added in v0.2.0

func ReadLine() (string, error)

ReadLine returns a line of input from standard input. An empty string is returned if the user didn't insert anything or on error.

func TestRoot added in v0.2.0

func TestRoot() (string, error)

TestRoot returns a the root of a filesystem specified by testEnvVarName. This function is only used for integration tests.

func UserFromUID added in v0.3.2

func UserFromUID(uid int64) (*user.User, error)

UserFromUID returns the User corresponding to the given user id.

Types

type ErrReader

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

ErrReader wraps an io.Reader, passing along calls to Read() until a read fails. Then, the error is stored, and all subsequent calls to Read() do nothing. This allows you to write code which has many subsequent reads and do all of the error checking at the end. For example:

r := NewErrReader(reader)
r.Read(foo)
r.Read(bar)
r.Read(baz)
if r.Err() != nil {
	// Handle error
}

Taken from https://blog.golang.org/errors-are-values by Rob Pike.

func NewErrReader

func NewErrReader(reader io.Reader) *ErrReader

NewErrReader creates an ErrReader which wraps the provided reader.

func (*ErrReader) Err

func (e *ErrReader) Err() error

Err returns the first encountered err (or nil if no errors occurred).

func (*ErrReader) Read

func (e *ErrReader) Read(p []byte) (n int, err error)

Read runs ReadFull on the wrapped reader if no errors have occurred. Otherwise, the previous error is just returned and no reads are attempted.

type ErrWriter

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

ErrWriter works exactly like ErrReader, except with io.Writer.

func NewErrWriter

func NewErrWriter(writer io.Writer) *ErrWriter

NewErrWriter creates an ErrWriter which wraps the provided writer.

func (*ErrWriter) Err

func (e *ErrWriter) Err() error

Err returns the first encountered err (or nil if no errors occurred).

func (*ErrWriter) Write

func (e *ErrWriter) Write(p []byte) (n int, err error)

Write runs the wrapped writer's Write if no errors have occurred. Otherwise, the previous error is just returned and no writes are attempted.

type SystemError

type SystemError string

SystemError is an error that should indicate something has gone wrong in the underlying system (syscall failure, bad ioctl, etc...).

func (SystemError) Error

func (s SystemError) Error() string

Jump to

Keyboard shortcuts

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