Documentation ¶
Overview ¶
Package util contains utility functions and general purpose writers/readers
Index ¶
- Constants
- Variables
- func BytesToHuman(b int64) string
- func CollapseHome(path string) string
- func DurationToHuman(d time.Duration) (str string)
- func ExpandHome(path string) string
- func ExtractZIP(filename string, dir string) error
- func NewHTTPClient() *http.Client
- func NewHTTPClientWithInsecureTransport() *http.Client
- func NewHTTPClientWithPinnedCert(pinned *x509.Certificate) (*http.Client, error)
- func NewZIPReader(paths []string) (io.ReadCloser, error)
- func ParseDuration(s string) (time.Duration, error)
- func ParseSize(s string) (int64, error)
- func RandomStringWithCharset(length int, charset string) string
- func ReadPassword(in io.Reader) ([]byte, error)
- func WithTimeout(client *http.Client) *http.Client
- type ContentTypeWriter
- type LimitWriter
- type Limiter
- type PeakedReadCloser
- type ProgressFunc
- type ProgressReader
Constants ¶
const (
// EnvHTTPClientTimeout allows overriding the HTTP client timeout (use for tests only)
EnvHTTPClientTimeout = "PCOPY_HTTP_CLIENT_TIMEOUT"
)
Variables ¶
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 ¶
BytesToHuman converts bytes to human readable format, e.g. 10 KB or 10.8 MB
func CollapseHome ¶
CollapseHome shortens a path that contains a user's home directory with "~"
func DurationToHuman ¶
DurationToHuman converts a duration to a human readable format
func ExpandHome ¶
ExpandHome replaces "~" with the user's home directory
func ExtractZIP ¶
ExtractZIP extracts the given ZIP archive at filename to a directory dir
func NewHTTPClientWithInsecureTransport ¶
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 ¶
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 ¶
ParseSize parses a size string like 2K or 2M into bytes. If no unit is found, e.g. 123, bytes is assumed.
func RandomStringWithCharset ¶
RandomStringWithCharset returns a random string with a given length, using the defined charset
func ReadPassword ¶
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).
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
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
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 (*Limiter) Add ¶
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) Set ¶
Set sets the value of the limiter to n. This function ignores the limit. It is meant to set the value based on reality.
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
type ProgressFunc ¶
ProgressFunc is callback that is called during copy/paste operations to indicate progress to the user.
type ProgressReader ¶
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.