util

package
v0.1.0-beta Latest Latest
Warning

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

Go to latest
Published: Apr 19, 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")

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

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

func NewTimeoutWriter

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

TimeoutReadWriter 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

NopCloser 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 PrefixReader

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

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

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
}

func NewTimeoutReadWriter

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

func (*TimeoutReadWriter) Read

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

func (*TimeoutReadWriter) SetDeadline

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

func (*TimeoutReadWriter) SetReadDeadline

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

func (*TimeoutReadWriter) SetReadTimeout

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

func (*TimeoutReadWriter) SetTimeout

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

func (*TimeoutReadWriter) SetWriteDeadline

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

func (*TimeoutReadWriter) SetWriteTimeout

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

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 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