util

package
v0.0.0-...-a1758a3 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2023 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Overview

Package util contains utility functions and general purpose writers/readers

Index

Constants

View Source
const (
	// EnvHTTPClientTimeout allows overriding the HTTP client timeout (use for tests only)
	EnvHTTPClientTimeout = "PCOPY_HTTP_CLIENT_TIMEOUT"
)

Variables

View Source
var ErrLimitReached = errors.New("limit reached")

ErrLimitReached is the error returned by the Limiter and LimitWriter when the predefined limit has been reached

Functions

func BytesToHuman

func BytesToHuman(b int64) string

BytesToHuman converts bytes to human readable format, e.g. 10 KB or 10.8 MB

func CollapseHome

func CollapseHome(path string) string

CollapseHome shortens a path that contains a user's home directory with "~"

func DurationToHuman

func DurationToHuman(d time.Duration) (str string)

DurationToHuman converts a duration to a human readable format

func ExpandHome

func ExpandHome(path string) string

ExpandHome replaces "~" with the user's home directory

func ExtractZIP

func ExtractZIP(filename string, dir string) error

ExtractZIP extracts the given ZIP archive at filename to a directory dir

func NewHTTPClient

func NewHTTPClient() *http.Client

NewHTTPClient returns a HTTP client

func NewHTTPClientWithInsecureTransport

func NewHTTPClientWithInsecureTransport() *http.Client

NewHTTPClientWithInsecureTransport returns a HTTP client that will accept any TLS certificate. Use this only for testing or unless you know what you're doing.

func NewHTTPClientWithPinnedCert

func NewHTTPClientWithPinnedCert(pinned *x509.Certificate) (*http.Client, error)

NewHTTPClientWithPinnedCert is a helper function to create a HTTP client with a pinned TLS certificate. Communication with a HTTPS server with a different certificate will fail.

func NewZIPReader

func NewZIPReader(paths []string) (io.ReadCloser, error)

NewZIPReader creates a io.ReadCloser that will read the given paths from disk and return a ZIP archive from them. The paths argument supports files and directories and will relativize paths accordingly.

func ParseDuration

func ParseDuration(s string) (time.Duration, error)

ParseDuration is a wrapper around Go's time.ParseDuration to supports days, weeks, months and years ("2y") and values without any unit ("1234"), which are interpreted as seconds. This is obviously inaccurate, but enough for the use case. In this function, the units are defined as follows: - day = 24 hours - week = 7 days - month = 30 days - year = 365 days

func ParseSize

func ParseSize(s string) (int64, error)

ParseSize parses a size string like 2K or 2M into bytes. If no unit is found, e.g. 123, bytes is assumed.

func RandomStringWithCharset

func RandomStringWithCharset(length int, charset string) string

RandomStringWithCharset returns a random string with a given length, using the defined charset

func ReadPassword

func ReadPassword(in io.Reader) ([]byte, error)

ReadPassword will read a password from STDIN. If the terminal supports it, it will not print the input characters to the screen. If not, it'll just read using normal readline semantics (useful for testing).

func WithTimeout

func WithTimeout(client *http.Client) *http.Client

WithTimeout adds a timeout to the given client

Types

type ContentTypeWriter

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

ContentTypeWriter is an implementation of io.Writer that will detect the content type and set the Content-Type and (optionally) Content-Disposition headers accordingly.

It will always set a Content-Type based on http.DetectContentType, but will never send the "text/html" content type.

If "download" is set, the Content-Disposition header will be set to "attachment", and will include a filename based on what is passed into the constructor function.

func NewContentTypeWriter

func NewContentTypeWriter(w http.ResponseWriter, filename string, download bool) *ContentTypeWriter

NewContentTypeWriter creates a new ContentTypeWriter

func (*ContentTypeWriter) Write

func (w *ContentTypeWriter) Write(p []byte) (n int, err error)

type LimitWriter

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

LimitWriter implements an io.Writer that will pass through all Write calls to the underlying writer w until any of the limiter's limit is reached, at which point a Write will return ErrLimitReached. Each limiter's value is increased with every write.

func NewLimitWriter

func NewLimitWriter(w io.Writer, limiters ...*Limiter) *LimitWriter

NewLimitWriter creates a new LimitWriter

func (*LimitWriter) Write

func (w *LimitWriter) Write(p []byte) (n int, err error)

Write passes through all writes to the underlying writer until any of the given limiter's limit is reached

type Limiter

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

Limiter is a helper that allows adding values up to a well-defined limit. Once the limit is reached ErrLimitReached will be returned. Limiter may be used by multiple goroutines.

func NewLimiter

func NewLimiter(limit int64) *Limiter

NewLimiter creates a new Limiter

func (*Limiter) Add

func (l *Limiter) Add(n int64) error

Add adds n to the limiters internal value, but only if the limit has not been reached. If the limit would be exceeded after adding n, ErrLimitReached is returned.

func (*Limiter) Limit

func (l *Limiter) Limit() int64

Limit returns the defined limit

func (*Limiter) Set

func (l *Limiter) Set(n int64)

Set sets the value of the limiter to n. This function ignores the limit. It is meant to set the value based on reality.

func (*Limiter) Sub

func (l *Limiter) Sub(n int64)

Sub subtracts a value from the limiters internal value

func (*Limiter) Value

func (l *Limiter) Value() int64

Value returns the internal value of the limiter

type PeakedReadCloser

type PeakedReadCloser struct {
	PeakedBytes  []byte
	LimitReached bool
	// contains filtered or unexported fields
}

PeakedReadCloser is a ReadCloser that allows peaking into a stream and buffering it in memory. It can be instantiated using the Peak function. After a stream has been peaked, it can still be fully read by reading the PeakedReadCloser. It first drained from the memory buffer, and then from the remaining underlying reader.

func Peak

func Peak(underlying io.ReadCloser, limit int) (*PeakedReadCloser, error)

Peak reads the underlying ReadCloser into memory up until the limit and returns a PeakedReadCloser

func (*PeakedReadCloser) Close

func (r *PeakedReadCloser) Close() error

Close closes the underlying stream

func (*PeakedReadCloser) Read

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

Read reads from the peaked bytes and then from the underlying stream

type ProgressFunc

type ProgressFunc func(processed int64, total int64, done bool)

ProgressFunc is callback that is called during copy/paste operations to indicate progress to the user.

type ProgressReader

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

ProgressReader counts the bytes read through it. Originally from https://github.com/machinebox/progress (Apache License 2.0)

func NewProgressReader

func NewProgressReader(r io.ReadCloser, total int64, fn ProgressFunc) *ProgressReader

NewProgressReader creates a new ProgressReader using fn as the callback function for progress updates, and total as the optional max value that is passed through to fn. This constructor uses the default progress delay and interval.

func NewProgressReaderWithDelay

func NewProgressReaderWithDelay(r io.ReadCloser, total int64, fn ProgressFunc, delay time.Duration, interval time.Duration) *ProgressReader

NewProgressReaderWithDelay creates a new ProgressReader using fn as the callback function for progress updates, and total as the optional max value that is passed through to fn. The progress function is triggered in the given interval, and only after certain delay.

func (*ProgressReader) Close

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

Close closes the underlying reader and stops the progress update ticker. It also calls the callback function one last time, with the "done" flag set.

func (*ProgressReader) Read

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

Read passes reads through to the underlying reader, but also updates the internal state of how many bytes have been processed.

Jump to

Keyboard shortcuts

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