utils

package
v0.0.0-...-2c4dd60 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2022 License: MIT Imports: 30 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CombinedContext

func CombinedContext(signals ...interface{}) (context.Context, context.CancelFunc)

CombinedContext creates a context that finishes when any of the provided signals finish. A signal can be a `context.Context`, a `chan struct{}`, or a `time.Duration` (which is transformed into a `context.WithTimeout`).

func ContextFromChan

func ContextFromChan(chStop <-chan struct{}) (context.Context, context.CancelFunc)

ContextFromChan creates a context that finishes when the provided channel receives or is closed.

func CopyFileWithMaxPerms

func CopyFileWithMaxPerms(srcPath, dstPath string, perms os.FileMode) error

CopyFileWithMaxPerms copies the file at `srcPath` to `dstPath` and ensures that it has permissions that are no more permissive than the given ones.

func DeepCopyJSValue

func DeepCopyJSValue(val interface{}) interface{}

@@TODO: everything about this is horrible

func DeviceIDFromX509Pubkey

func DeviceIDFromX509Pubkey(pubkey interface{}) string

func EnsureDirAndMaxPerms

func EnsureDirAndMaxPerms(path string, perms os.FileMode) error

EnsureDirAndMaxPerms ensures that the given path exists, that it's a directory, and that it has permissions that are no more permissive than the given ones.

- If the path does not exist, it is created - If the path exists, but is not a directory, an error is returned - If the path exists, and is a directory, but has the wrong perms, it is chmod'ed

func EnsureFileMaxPerms

func EnsureFileMaxPerms(file *os.File, perms os.FileMode) error

EnsureFileMaxPerms ensures that the given file has permissions that are no more permissive than the given ones.

func EnsureFilepathMaxPerms

func EnsureFilepathMaxPerms(filepath string, perms os.FileMode) error

EnsureFilepathMaxPerms ensures that the file at the given filepath has permissions that are no more permissive than the given ones.

func Filter

func Filter[T any](in []T, fn func(T) bool) []T

func FilterEmptyBytes

func FilterEmptyBytes(bss [][]byte) [][]byte

func FilterEmptyStrings

func FilterEmptyStrings(s []string) []string

func GetValue

func GetValue(x interface{}, keypath []string) (interface{}, bool)

func GuessContentTypeFromFilename

func GuessContentTypeFromFilename(filename string) string

func IsLocalStateURI

func IsLocalStateURI(stateURI string) bool

func MakeSelfSignedX509Certificate

func MakeSelfSignedX509Certificate() (*tls.Certificate, error)

func Map

func Map[InType any, OutType any](in []InType, fn func(InType) OutType) []OutType

func ParseJWT

func ParseJWT(authHeader string, jwtSecret []byte) (jwt.MapClaims, bool, error)

func PrettyJSON

func PrettyJSON(x interface{}) string

func PrintfDebugPrinter

func PrintfDebugPrinter(msg string, args ...interface{})

func RandomBytes

func RandomBytes(length int) []byte

func RandomNumberString

func RandomNumberString() string

func RandomString

func RandomString(n int) string

func RespondJSON

func RespondJSON(resp http.ResponseWriter, data interface{})

func SniffContentType

func SniffContentType(filename string, data io.ReadCloser) (io.ReadCloser, string, error)

func TrimStringToLen

func TrimStringToLen(s string, length int) string

func UnmarshalHTTPRequest

func UnmarshalHTTPRequest(into interface{}, r *http.Request) error

func UnmarshalHTTPResponse

func UnmarshalHTTPResponse(into interface{}, r *http.Response) error

func UnrestrictedCors

func UnrestrictedCors(handler http.Handler) http.Handler

func WriteFileWithMaxPerms

func WriteFileWithMaxPerms(path string, data []byte, perms os.FileMode) error

WriteFileWithMaxPerms writes `data` to `path` and ensures that the file has permissions that are no more permissive than the given ones.

Types

type ChanContext

type ChanContext chan struct{}

func (ChanContext) Deadline

func (ch ChanContext) Deadline() (deadline time.Time, ok bool)

func (ChanContext) Done

func (ch ChanContext) Done() <-chan struct{}

func (ChanContext) Err

func (ch ChanContext) Err() error

func (ChanContext) Value

func (ch ChanContext) Value(key interface{}) interface{}

type ExponentialBackoff

type ExponentialBackoff struct {
	Min time.Duration
	Max time.Duration
	// contains filtered or unexported fields
}

func (*ExponentialBackoff) Next

func (eb *ExponentialBackoff) Next() time.Duration

func (*ExponentialBackoff) Ready

func (eb *ExponentialBackoff) Ready() (ready bool, until time.Duration)

func (*ExponentialBackoff) Reset

func (eb *ExponentialBackoff) Reset()

func (*ExponentialBackoff) Wait

func (eb *ExponentialBackoff) Wait()

type ExponentialBackoffTicker

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

func NewExponentialBackoffTicker

func NewExponentialBackoffTicker(min, max time.Duration) *ExponentialBackoffTicker

func (*ExponentialBackoffTicker) Close

func (t *ExponentialBackoffTicker) Close()

func (*ExponentialBackoffTicker) Notify

func (t *ExponentialBackoffTicker) Notify() <-chan time.Time

func (*ExponentialBackoffTicker) Reset

func (t *ExponentialBackoffTicker) Reset()

func (*ExponentialBackoffTicker) Start

func (t *ExponentialBackoffTicker) Start()

type FileSize

type FileSize uint64
const (
	KB FileSize = 1000
	MB FileSize = 1000 * KB
	GB FileSize = 1000 * MB
	TB FileSize = 1000 * GB
)

func ParseFileSize

func ParseFileSize(s string) (FileSize, error)

func (FileSize) MarshalText

func (s FileSize) MarshalText() ([]byte, error)

func (FileSize) String

func (s FileSize) String() string

func (*FileSize) UnmarshalText

func (s *FileSize) UnmarshalText(bs []byte) error

type HTTPClient

type HTTPClient struct {
	http.Client
	// contains filtered or unexported fields
}

func MakeHTTPClient

func MakeHTTPClient(requestTimeout, reapIdleConnsInterval time.Duration, cookieJar http.CookieJar, tlsCerts []tls.Certificate) *HTTPClient

func (HTTPClient) Close

func (c HTTPClient) Close()

type HTTPHeaderUnmarshaler

type HTTPHeaderUnmarshaler interface {
	UnmarshalHTTPHeader(header string) error
}

type Mailbox

type Mailbox[T any] struct {
	// contains filtered or unexported fields
}

func NewMailbox

func NewMailbox[T any](capacity uint64) *Mailbox[T]

func (*Mailbox[T]) Clear

func (m *Mailbox[T]) Clear()

func (*Mailbox[T]) Deliver

func (m *Mailbox[T]) Deliver(x T)

func (*Mailbox[T]) Notify

func (m *Mailbox[T]) Notify() chan struct{}

func (*Mailbox[T]) Retrieve

func (m *Mailbox[T]) Retrieve() (T, bool)

func (*Mailbox[T]) RetrieveAll

func (m *Mailbox[T]) RetrieveAll() []T

type StaticTicker

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

func NewStaticTicker

func NewStaticTicker(interval time.Duration) StaticTicker

func (StaticTicker) Close

func (t StaticTicker) Close()

func (StaticTicker) Notify

func (t StaticTicker) Notify() <-chan time.Time

func (StaticTicker) Start

func (t StaticTicker) Start()

type Ticker

type Ticker interface {
	Start()
	Close()
	Notify() <-chan time.Time
}

type URLPathUnmarshaler

type URLPathUnmarshaler interface {
	UnmarshalURLPath(path string) error
}

type WaitGroupChan

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

WaitGroupChan creates a channel that closes when the provided sync.WaitGroup is done.

func NewWaitGroupChan

func NewWaitGroupChan(ctx context.Context) *WaitGroupChan

func (*WaitGroupChan) Add

func (wg *WaitGroupChan) Add(i int)

func (*WaitGroupChan) Close

func (wg *WaitGroupChan) Close()

func (*WaitGroupChan) Done

func (wg *WaitGroupChan) Done()

func (*WaitGroupChan) Wait

func (wg *WaitGroupChan) Wait() <-chan struct{}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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