toolbelt

package module
v0.0.0-...-a373633 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2019 License: MIT Imports: 7 Imported by: 0

README

A Go toolbelt

Github Actions Go workflow status godoc.org/github.com/klingtnet/toolbelt

This library contains simple utility functions that I often had to reimplement because they are not part of the standard library.

Please note that this is still a work in progress.

Documentation

Index

Constants

View Source
const DefaultReadBufferSize = 4 * 1024

DefaultReadBufferSize is the buffer size used for the DefaultBufferedReader .

View Source
const DefaultTimeout = 10 * time.Second

Variables

View Source
var DefaultBufferedReader = NewBufferedReader(DefaultReadBufferSize)

DefaultBufferedReader is a BufferedReader initialized with buffers of the DefaultReadBufferSize .

Functions

func CloseAndRemove

func CloseAndRemove(f *os.File) error

CloseAndRemove first closes the given file and secondly deletes it.

func FilesEqual

func FilesEqual(fileA, fileB string) (bool, error)

FilesEqual returns true if both files are equal, i.e. if both store the same bytes.

func NewHTTPClient

func NewHTTPClient(userAgent, version string) *http.Client

NewHTTPClient returns an HTTP client initialized with a DefaultHeaderTransport and with DefaultTimeout.

func ReplaceFile

func ReplaceFile(data io.Reader, dest string) error

ReplaceFile overwrites a file atomically by first creating a temporary file with the given data and afterwards replacing the destination with temporary file. File permissions of the destination are kept. The destination is unchanged if an intermediate error occurs.

func ReplaceFileIfDifferent

func ReplaceFileIfDifferent(data io.Reader, dest string) (bool, error)

ReplaceFileIfDifferent acts like ReplaceFile but first checks if the content of dest and data are different. Note that this requires to buffer the data Reader in memory, unlike ReplaceFile. If dest was replaced the boolean return value is true and otherwise false.

Types

type BufferedReader

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

BufferedReader implements equality checks for io.Reader .

func NewBufferedReader

func NewBufferedReader(bufSize int64) *BufferedReader

NewBufferedReader returns a BufferedReader initialized with the given buffers of the given size. Usually there is no need to instantiate it yourself, use the DefaultBufferedReader instead.

func (*BufferedReader) ReaderEqual

func (br *BufferedReader) ReaderEqual(a, b io.Reader) (bool, error)

ReaderEqual returns true if both readers returned the same bytes.

type DefaultHeaderTransport

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

DefaultHeaderTransport is an http.Transport that inserts user-agent and x-client-version headers.

func NewDefaultHeaderTransport

func NewDefaultHeaderTransport(userAgent, version string) *DefaultHeaderTransport

NewDefaultHeaderTransport returns an initialied DefaultHeaderTransport that uses the http.DefaultTransport .

func (*DefaultHeaderTransport) RoundTrip

func (dht *DefaultHeaderTransport) RoundTrip(r *http.Request) (*http.Response, error)

func (*DefaultHeaderTransport) SetBaseTransport

func (dht *DefaultHeaderTransport) SetBaseTransport(baseTransport http.RoundTripper)

SetBaseTransport allows to replace the base transport.

Jump to

Keyboard shortcuts

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