utils

package
v0.14.4 Latest Latest
Warning

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

Go to latest
Published: May 9, 2022 License: GPL-3.0 Imports: 14 Imported by: 20

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CopyFileAtomic added in v0.5.3

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 added in v0.5.3

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 added in v0.5.5

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 added in v0.5.5

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 added in v0.3.0

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 RandomUUID added in v0.5.5

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 added in v0.5.3

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 added in v0.12.0

func SafeFirst16Bytes(data []byte) string

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

func SafeFirst16Chars added in v0.12.0

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 added in v0.5.3

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 added in v0.9.6

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

BroadcastFlag is a simple system to broadcast a flag value.

func NewBroadcastFlag added in v0.9.6

func NewBroadcastFlag() *BroadcastFlag

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

func (*BroadcastFlag) NewFlag added in v0.9.6

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 singal triggers. You can call Refresh immediately to get the current state from the broadcasting flag.

func (*BroadcastFlag) NotifyAndReset added in v0.9.6

func (bf *BroadcastFlag) NotifyAndReset()

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

type DirStructure added in v0.3.0

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 added in v0.3.0

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

NewDirStructure returns a new DirStructure.

func (*DirStructure) ChildDir added in v0.3.0

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 added in v0.3.0

func (ds *DirStructure) Ensure() error

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

func (*DirStructure) EnsureAbsPath added in v0.3.0

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 added in v0.3.0

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 added in v0.3.0

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 added in v0.9.6

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 added in v0.9.6

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 added in v0.9.6

func (f *Flag) Refresh()

Refresh fetches the current state from the broadcasting flag.

func (*Flag) Signal added in v0.9.6

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 added in v0.5.5

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.

func (*OnceAgain) Do added in v0.5.5

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 added in v0.7.1

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 added in v0.7.1

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 added in v0.7.1

func (p *StablePool) Max() int

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

func (*StablePool) Put added in v0.7.1

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

Put adds x to the pool.

func (*StablePool) Size added in v0.7.3

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