utils

package
v0.5.4 Latest Latest
Warning

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

Go to latest
Published: May 23, 2020 License: GPL-3.0 Imports: 8 Imported by: 5

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 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. If a directory is created, also all missing directories up to the required one are created with the given permissions.

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 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 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 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 [755]
/repo/b/c [750]
/repo/b/d [755]
/repo/b/d/e [755]
/repo/b/d/f [755]
/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.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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