utils

package
v1.6.24 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2024 License: GPL-3.0 Imports: 16 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CopyFileAtomic

func CopyFileAtomic(dest string, src string, opts *AtomicFileOptions) error

CopyFileAtomic is like CreateAtomic but copies content from src to dest. If opts.Mode is 0 CopyFileAtomic tries to set the file mode of src to dest.

func CreateAtomic

func CreateAtomic(dest string, r io.Reader, opts *AtomicFileOptions) error

CreateAtomic creates or overwrites a file at dest atomically using data from r. Atomic means that even in case of a power outage, dest will never be a zero-length file. It will always either contain the previous data (or not exist) or the new data but never anything in between.

func DerivedInstanceUUID

func DerivedInstanceUUID(input string) uuid.UUID

DerivedInstanceUUID returns a new UUID that is derived from the input, but is unique per instance (execution) and therefore is only reproducible with the same process.

func DerivedUUID

func DerivedUUID(input string) uuid.UUID

DerivedUUID returns a new UUID that is derived from the input only, and therefore is always reproducible.

func DuplicateBytes

func DuplicateBytes(a []byte) []byte

DuplicateBytes returns a new copy of the given byte slice.

func DuplicateStrings

func DuplicateStrings(a []string) []string

DuplicateStrings returns a new copy of the given string slice.

func EnsureDirectory

func EnsureDirectory(path string, perm os.FileMode) error

EnsureDirectory ensures that the given directory exists and that is has the given permissions set. If path is a file, it is deleted and a directory created.

func IndexOfString

func IndexOfString(a []string, s string) int

IndexOfString returns the index of given string and -1 if its not part of the slice.

func MimeTypeByExtension

func MimeTypeByExtension(ext string) (mimeType string, ok bool)

MimeTypeByExtension returns a mimetype for the given file name extension, which must including the leading dot. If the extension is not known, the call returns with ok=false and, additionally, a default "application/octet-stream" mime type is returned.

func PathExists

func PathExists(path string) bool

PathExists returns whether the given path (file or dir) exists.

func RandomUUID

func RandomUUID(ns string) uuid.UUID

RandomUUID returns a new random UUID with optionally provided ns.

func RemoveFromStringSlice

func RemoveFromStringSlice(a []string, s string) []string

RemoveFromStringSlice removes the given string from the slice and returns a new slice.

func ReplaceFileAtomic

func ReplaceFileAtomic(dest string, src string, opts *AtomicFileOptions) error

ReplaceFileAtomic replaces the file at dest with the content from src. If dest exists it's file mode copied and used for the replacement. If not, dest will get the same file mode as src. See CopyFileAtomic and CreateAtomic for more information.

func SafeFirst16Bytes

func SafeFirst16Bytes(data []byte) string

SafeFirst16Bytes return the first 16 bytes of the given data in safe form.

func SafeFirst16Chars

func SafeFirst16Chars(s string) string

SafeFirst16Chars return the first 16 characters of the given data in safe form.

func StringInSlice

func StringInSlice(a []string, s string) bool

StringInSlice returns whether the given string is in the string slice.

func StringSliceEqual

func StringSliceEqual(a []string, b []string) bool

StringSliceEqual returns whether the given string slices are equal.

Types

type AtomicFileOptions

type AtomicFileOptions struct {
	// Mode is the file mode for the new file. If
	// 0, the file mode will be set to 0600.
	Mode os.FileMode

	// TempDir is the path to the temp-directory
	// that should be used. If empty, it defaults
	// to the system temp.
	TempDir string
}

AtomicFileOptions holds additional options for manipulating the behavior of CreateAtomic and friends.

type BroadcastFlag

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

BroadcastFlag is a simple system to broadcast a flag value.

func NewBroadcastFlag

func NewBroadcastFlag() *BroadcastFlag

NewBroadcastFlag returns a new BroadcastFlag. In the initial state, the flag is not set and the signal does not trigger.

func (*BroadcastFlag) NewFlag

func (bf *BroadcastFlag) NewFlag() *Flag

NewFlag returns a new Flag that listens to this broadcasting flag. In the initial state, the flag is set and the signal triggers. You can call Refresh immediately to get the current state from the broadcasting flag.

func (*BroadcastFlag) NotifyAndReset

func (bf *BroadcastFlag) NotifyAndReset()

NotifyAndReset notifies all flags of this broadcasting flag and resets the internal broadcast flag state.

type CallLimiter

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

CallLimiter bundles concurrent calls and optionally limits how fast a function is called.

func NewCallLimiter

func NewCallLimiter(minPause time.Duration) *CallLimiter

NewCallLimiter returns a new call limiter. Set minPause to zero to disable the minimum pause between calls.

func (*CallLimiter) Do

func (l *CallLimiter) Do(f func())

Do executes the given function. All concurrent calls to Do are bundled and return when f() finishes. Waits until the minimum pause is over before executing f() again.

type DirStructure

type DirStructure struct {
	sync.Mutex

	Path     string
	Dir      string
	Perm     os.FileMode
	Parent   *DirStructure
	Children map[string]*DirStructure
}

DirStructure represents a directory structure with permissions that should be enforced.

Example

Output:

/ [755]
/repo [777]
/repo/b [707]
/repo/b/c [750]
/repo/b/d [707]
/repo/b/d/e [707]
/repo/b/d/f [707]
/repo/b/d/f/g [707]
/repo/b/d/f/g/h [707]
/secret [700]

func NewDirStructure

func NewDirStructure(path string, perm os.FileMode) *DirStructure

NewDirStructure returns a new DirStructure.

func (*DirStructure) ChildDir

func (ds *DirStructure) ChildDir(dirName string, perm os.FileMode) (child *DirStructure)

ChildDir adds a new child DirStructure and returns it. Should the child already exist, the existing child is returned and the permissions are updated.

func (*DirStructure) Ensure

func (ds *DirStructure) Ensure() error

Ensure ensures that the specified directory structure (from the first parent on) exists.

func (*DirStructure) EnsureAbsPath

func (ds *DirStructure) EnsureAbsPath(dirPath string) error

EnsureAbsPath ensures that the specified directory structure (from the first parent on) and the given absolute path exists. If the given path is outside the DirStructure, an error will be returned.

func (*DirStructure) EnsureRelDir

func (ds *DirStructure) EnsureRelDir(dirNames ...string) error

EnsureRelDir ensures that the specified directory structure (from the first parent on) and the given relative path (to the DirStructure) exists.

func (*DirStructure) EnsureRelPath

func (ds *DirStructure) EnsureRelPath(dirPath string) error

EnsureRelPath ensures that the specified directory structure (from the first parent on) and the given relative path (to the DirStructure) exists.

type Flag

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

Flag receives changes from its broadcasting flag. A Flag must only be used in one goroutine and is not concurrency safe, but fast.

func (*Flag) IsSet

func (f *Flag) IsSet() bool

IsSet returns whether the flag was set since the last Refresh. This does not reset the Flag itself, you'll need to call Refresh for that.

func (*Flag) Refresh

func (f *Flag) Refresh()

Refresh fetches the current state from the broadcasting flag.

func (*Flag) Signal

func (f *Flag) Signal() <-chan struct{}

Signal returns a channel that waits for the flag to be set. This does not reset the Flag itself, you'll need to call Refresh for that.

type OnceAgain

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

OnceAgain is an object that will perform only one action "in flight". It's basically the same as sync.Once, but is automatically reused when the function was executed and everyone who waited has left. Important: This is somewhat racy when used heavily as it only resets _after_ everyone who waited has left. So, while some goroutines are waiting to be activated again to leave the waiting state, other goroutines will call Do() without executing the function again.

func (*OnceAgain) Do

func (o *OnceAgain) Do(f func())

Do calls the function f if and only if Do is being called for the first time for this instance of Once. In other words, given

var once Once

if once.Do(f) is called multiple times, only the first call will invoke f, even if f has a different value in each invocation. A new instance of Once is required for each function to execute.

Do is intended for initialization that must be run exactly once. Since f is niladic, it may be necessary to use a function literal to capture the arguments to a function to be invoked by Do:

config.once.Do(func() { config.init(filename) })

Because no call to Do returns until the one call to f returns, if f causes Do to be called, it will deadlock.

If f panics, Do considers it to have returned; future calls of Do return without calling f.

type StablePool

type StablePool struct {

	// New optionally specifies a function to generate
	// a value when Get would otherwise return nil.
	// It may not be changed concurrently with calls to Get.
	New func() interface{}
	// contains filtered or unexported fields
}

A StablePool is a drop-in replacement for sync.Pool that is slower, but predictable. A StablePool is a set of temporary objects that may be individually saved and retrieved.

In contrast to sync.Pool, items are not removed automatically. Every item will be returned at some point. Items are returned in a FIFO manner in order to evenly distribute usage of a set of items.

A StablePool is safe for use by multiple goroutines simultaneously and must not be copied after first use.

func (*StablePool) Get

func (p *StablePool) Get() interface{}

Get returns the next item from the Pool, removes it from the Pool, and returns it to the caller. In contrast to sync.Pool, Get never ignores the pool. Callers should not assume any relation between values passed to Put and the values returned by Get.

If Get would otherwise return nil and p.New is non-nil, Get returns the result of calling p.New.

func (*StablePool) Max

func (p *StablePool) Max() int

Max returns the amount of items the pool held at maximum.

func (*StablePool) Put

func (p *StablePool) Put(x interface{})

Put adds x to the pool.

func (*StablePool) Size

func (p *StablePool) Size() int

Size returns the amount of items the pool currently holds.

Directories

Path Synopsis
Package renameio provides a way to atomically create or replace a file or symbolic link.
Package renameio provides a way to atomically create or replace a file or symbolic link.

Jump to

Keyboard shortcuts

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