util

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Dec 7, 2018 License: AGPL-3.0 Imports: 13 Imported by: 0

Documentation

Overview

Package util implements small helper function that should be included in the stdlib in our opinion.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrTimeout = errors.New("I/O Timeout: Operation timed out")

ErrTimeout will be returned by Read/Write in case of a timeout.

Functions

func Clamp

func Clamp(x, lo, hi int) int

Clamp limits x to the range [lo, hi]

func Closer

func Closer(c io.Closer)

Closer closes c. If that fails, it will log the error. The intended usage is for convinient defer calls only! It gives only little knowledge about where the error is, but it's slightly better than a bare defer xyz.Close()

func DeriveKey

func DeriveKey(pwd, salt []byte, keyLen int) []byte

DeriveKey derives a key from password and salt being keyLen bytes long. It uses an established password derivation function.

func LimitWriter

func LimitWriter(w io.Writer, sz int64) io.Writer

LimitWriter is like io.LimitReader but for an io.Writer

func Max

func Max(a, b int) int

Max returns the maximum of a and b.

func Max64

func Max64(a, b int64) int64

Max64 returns the maximum of a and b.

func Min

func Min(a, b int) int

Min returns the minimum of a and b.

func Min64

func Min64(a, b int64) int64

Min64 returns the minimum of a and b.

func NewTimeoutReader

func NewTimeoutReader(r io.Reader, d time.Duration) io.Reader

NewTimeoutReader wraps `r` and returns a io.Reader that times out after `d` elapsed with ErrTimeout if `r` didn't succeed in that time.

func NewTimeoutWriter

func NewTimeoutWriter(w io.Writer, d time.Duration) io.Writer

NewTimeoutWriter wraps `w` and returns a io.Writer that times out after `d` elapsed with ErrTimeout if `w` didn't succeed in that time.

func NopWriteCloser

func NopWriteCloser(w io.Writer) io.WriteCloser

NopWriteCloser returns a WriteCloser with a no-op Close method wrapping the provided Writer w.

func OmitBytes

func OmitBytes(data []byte, lim int) string

OmitBytes converts a byte slice into a string representation that omits data in the middle if necessary. It is useful for testing and printing user information. `lim` is the number of bytes

Example:

OmitBytes([]byte{1,2,3,4}, 2) -> [1 ... 2] OmitBytes([]byte{1,2,3,4}, 4) -> [1, 2, 3, 4]

func PeekHeader added in v0.3.0

func PeekHeader(r io.Reader, size int64) ([]byte, io.Reader, error)

PeekHeader returns a new reader that will yield the very same data as `r`. It reads `size` bytes from `r` and returns it. The underlying implementation uses PrefixReader to prefix the stream with the header again.

func PrefixReader

func PrefixReader(data []byte, r io.Reader) io.Reader

PrefixReader returns an io.Reader that outputs `data` before the rest of `r`.

func SyncedReadWriter

func SyncedReadWriter(w io.ReadWriter) io.ReadWriter

SyncedReadWriter returns a io.ReadWriter that protects each call to Read() and Write() with a sync.Mutex.

func Tar

func Tar(root, archiveName string, w io.Writer) error

Tar packs all files in the directory pointed to by `root` and writes a gzipped and tarred version of it to `w`. The name of the archiv is set to `archiveName`.

func Touch

func Touch(path string) error

Touch works like the unix touch(1)

func UClamp

func UClamp(x, lo, hi uint) uint

UClamp limits x to the range [lo, hi]

func UMax

func UMax(a, b uint) uint

UMax returns the unsigned minimum of a and b

func UMin

func UMin(a, b uint) uint

UMin returns the unsigned minimum of a and b

func Untar

func Untar(r io.Reader, root string) error

Untar reads .tar data (from Tar()) from `r` and writes all files packed in it to `root`.

Types

type Empty

type Empty struct{}

Empty is just an empty struct. Empty{} reads nicer than struct{}{}

type Errors

type Errors []error

Errors is a list of errors that render to one single message

func (Errors) Error

func (es Errors) Error() string

func (Errors) ToErr

func (es Errors) ToErr() error

ToErr combines all errors in the list to a single error. If there were no errors, it returns nil.

type SizeAccumulator

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

SizeAccumulator is a io.Writer that simply counts the amount of bytes that has been written to it. It's useful to count the received bytes from a reader in conjunction with a io.TeeReader

Example usage without error handling:

s := &SizeAccumulator{}
teeR := io.TeeReader(r, s)
io.Copy(os.Stdout, teeR)
fmt.Printf("Wrote %d bytes to stdout\n", s.Size())
Example
s := &SizeAccumulator{}
teeR := io.TeeReader(bytes.NewReader([]byte("Hello, ")), s)
io.Copy(os.Stdout, teeR)
fmt.Printf("wrote %d bytes to stdout\n", s.Size())
Output:

Hello, wrote 7 bytes to stdout

func (*SizeAccumulator) Reset

func (s *SizeAccumulator) Reset()

Reset resets the size counter to 0.

func (*SizeAccumulator) Size

func (s *SizeAccumulator) Size() uint64

Size returns the cumulated written bytes. It can be safely called from any go routine.

func (*SizeAccumulator) Write

func (s *SizeAccumulator) Write(buf []byte) (int, error)

Write simply increments the internal size count without any IO. It can be safely called from any go routine.

type SyncBuffer

type SyncBuffer struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

SyncBuffer is a bytes.Buffer that protects each call to Read() and Write() with a sync.RWMutex, i.e. parallel access to Read() is possible, but blocks when doing a Write().

func (*SyncBuffer) Read

func (b *SyncBuffer) Read(p []byte) (int, error)

func (*SyncBuffer) Write

func (b *SyncBuffer) Write(p []byte) (int, error)

type TimeoutReadWriter

type TimeoutReadWriter struct {
	io.Writer
	io.Reader
	// contains filtered or unexported fields
}

TimeoutReadWriter is io.ReadWriter capable of returning ErrTimeout if there was no result in a certain timeout period.

func NewTimeoutReadWriter

func NewTimeoutReadWriter(rw io.ReadWriter, d time.Duration) *TimeoutReadWriter

NewTimeoutReadWriter wraps `rw` and returns a io.ReadWriter that times out after `d` elapsed with ErrTimeout if `rw` didn't succeed in that time.

func (*TimeoutReadWriter) Read

func (rw *TimeoutReadWriter) Read(p []byte) (n int, err error)

func (*TimeoutReadWriter) SetDeadline

func (rw *TimeoutReadWriter) SetDeadline(d time.Time) error

SetDeadline sets an absolute time in the future where an I/O operation should be canceled.

func (*TimeoutReadWriter) SetReadDeadline

func (rw *TimeoutReadWriter) SetReadDeadline(d time.Time) error

SetReadDeadline sets an absolute time in the future where a read option should be canceled.

func (*TimeoutReadWriter) SetReadTimeout

func (rw *TimeoutReadWriter) SetReadTimeout(d time.Duration) error

SetReadTimeout sets an own timeout for reading.

func (*TimeoutReadWriter) SetTimeout

func (rw *TimeoutReadWriter) SetTimeout(d time.Duration) error

SetTimeout sets both the read and write timeout to `d`.

func (*TimeoutReadWriter) SetWriteDeadline

func (rw *TimeoutReadWriter) SetWriteDeadline(d time.Time) error

SetWriteDeadline sets an absolute time in the future where a write option should be canceled.

func (*TimeoutReadWriter) SetWriteTimeout

func (rw *TimeoutReadWriter) SetWriteTimeout(d time.Duration) error

SetWriteTimeout sets an own timeout for writing.

func (*TimeoutReadWriter) Write

func (rw *TimeoutReadWriter) Write(p []byte) (n int, err error)

Directories

Path Synopsis
Package conductor is a small helper to execute work heavy operations in the backgrounds that deliver partial results ("result streaming").
Package conductor is a small helper to execute work heavy operations in the backgrounds that deliver partial results ("result streaming").
Package log implements utility methods for logging in a colorful manner.
Package log implements utility methods for logging in a colorful manner.
Package registry contains shared utils between server and client regarding the registry opening and modification.
Package registry contains shared utils between server and client regarding the registry opening and modification.
Package trie implements a general purpose Path-*Node.
Package trie implements a general purpose Path-*Node.

Jump to

Keyboard shortcuts

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