util

package
v0.0.0-...-ce59a29 Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2023 License: BSD-3-Clause Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrTimeout = errors.New("timeout occurred")

ErrTimeout is returned by TimeoutCloser, TimeoutReader, and TimeoutWriter when the underlying Closer/Reader/Writer does not return within the specified timeout

Functions

func AppendPort

func AppendPort(addr string, defaultPort int) string

AppendPort appends a port to the address e.g. AppendPort(`124.223.81.61:3478?transport=udp`, 3478) => 124.223.81.61:3478?transport=udp e.g. AppendPort(`124.223.81.61?transport=udp`, 3478) => 124.223.81.61:3478?transport=udp e.g. AppendPort(`124.223.81.61`, 3478) => 124.223.81.61:3478

func Fatalf

func Fatalf(format string, v ...interface{})

func FatalfIf

func FatalfIf(condition bool, format string, v ...interface{})

func GetEnv

func GetEnv(key string, defaultVal string) string

func GetEnvBool

func GetEnvBool(key string, defaultVal bool) bool

func If

func If[T any](condition bool, a, b T) T

func Postfix

func Postfix(addr, postfix string) string

func Prefix

func Prefix(prefix, addr string) string

func PrintQRCode

func PrintQRCode(baseURL, code string)

func Printf

func Printf(format string, a ...any)

func RandFull

func RandFull(buf []byte)

func RandIntn

func RandIntn(n int) int

func RandPass

func RandPass(n int) []byte

func TimeoutCloser

func TimeoutCloser(c io.Closer, timeout time.Duration) io.Closer

TimeoutCloser returns an io.Closer that wraps the passed-in io.Closer. If the underlying Closer fails to close within the allotted timeout ErrTimeout is returned.

func TimeoutReadWriter

func TimeoutReadWriter(rw io.ReadWriter, timeout time.Duration) io.ReadWriter

TimeoutReadWriter returns an io.ReadWriter that wraps the passed-in io.ReadWriter. If the underlying ReadWriter fails to read/write within the allotted timeout ErrTimeout is returned.

func TimeoutReader

func TimeoutReader(r io.Reader, timeout time.Duration) io.Reader

TimeoutReader returns an io.Reader that wraps the passed-in io.Reader. If the underlying Reader fails to read within the allotted timeout ErrTimeout is returned.

func TimeoutWriter

func TimeoutWriter(w io.Writer, timeout time.Duration) io.Writer

TimeoutWriter returns an io.Writer that wraps the passed-in io.Writer. If the underlying Writer fails to write within the allotted timeout ErrTimeout is returned.

Types

type CliProgressBar

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

func (*CliProgressBar) Add

func (c *CliProgressBar) Add(n uint64)

func (*CliProgressBar) Finish

func (c *CliProgressBar) Finish()

func (*CliProgressBar) Start

func (c *CliProgressBar) Start(filename string, n uint64)

type Duration

type Duration time.Duration

func (Duration) D

func (d Duration) D() time.Duration

func (Duration) MarshalJSON

func (d Duration) MarshalJSON() (b []byte, err error)

func (*Duration) UnmarshalJSON

func (d *Duration) UnmarshalJSON(b []byte) error

type NoopProgressBar

type NoopProgressBar struct{}

func (*NoopProgressBar) Add

func (n *NoopProgressBar) Add(uint64)

func (*NoopProgressBar) Finish

func (n *NoopProgressBar) Finish()

func (*NoopProgressBar) Start

func (n *NoopProgressBar) Start(string, uint64)

type ProgressBar

type ProgressBar interface {
	Start(filename string, n uint64)
	Add(n uint64)
	Finish()
}

func CreateProgressBar

func CreateProgressBar(pb ProgressBar, progress bool) ProgressBar

type Reader

type Reader struct {
	io.Reader
	ProgressBar
}

Reader it's a wrapper for given reader, but with progress handle

func NewProxyReader

func NewProxyReader(r io.Reader, pb ProgressBar) *Reader

NewProxyReader creates a wrapper for given reader, but with progress handle Takes io.Reader or io.ReadCloser Also, it automatically switches progress bar to handle units as bytes

func (*Reader) Close

func (r *Reader) Close() (err error)

Close the wrapped reader when it implements io.Closer

func (*Reader) Read

func (r *Reader) Read(p []byte) (n int, err error)

Read reads bytes from wrapped reader and add amount of bytes to progress bar

type Writer

type Writer struct {
	io.Writer
	ProgressBar
}

Writer it's a wrapper for given writer, but with progress handle

func NewProxyWriter

func NewProxyWriter(r io.Writer, pb ProgressBar) *Writer

NewProxyWriter creates a wrapper for given writer, but with progress handle Takes io.Writer or io.WriteCloser Also, it automatically switches progress bar to handle units as bytes

func (*Writer) Close

func (r *Writer) Close() (err error)

Close the wrapped reader when it implements io.Closer

func (*Writer) Write

func (r *Writer) Write(p []byte) (n int, err error)

Write writes bytes to wrapped writer and add amount of bytes to progress bar

Jump to

Keyboard shortcuts

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