Documentation

Overview

Package fileutil contains file based utilities and helper functions.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrClosed = errors.New("Config file was closed")
)

Defined error codes for WatchedConfig

View Source
var WatchedConfigErrRetries = 10

WatchedConfigErrRetries is the number of times the code will try to read the disk configuration before overwriting it with the current (working) configuration. Set to -1 if it should never attempt to overwrite.

Functions

func CheckSumFile

func CheckSumFile(path string) (string, error)

CheckSumFile calculates a sha256 checksum of a given file. This function will read in the whole file.

func CheckSumFileFast

func CheckSumFileFast(path string) (string, error)

CheckSumFileFast calculates a 32bit MurmurHash3 checksum from a portion of the given file.

func ConfBool

func ConfBool(config map[string]interface{}, key string) bool

ConfBool reads a config value as a boolean value.

func ConfStr

func ConfStr(config map[string]interface{}, key string) string

ConfStr reads a config value as a string value.

func IsDir

func IsDir(path string) (bool, error)

IsDir returns whether the given path is a directory.

func LoadConfig

func LoadConfig(filename string, defaultConfig map[string]interface{}) (map[string]interface{}, error)

LoadConfig loads or creates a JSON based configuration file. Missing settings from the config file will be filled with default settings. This function provides a simple mechanism for programs to handle user-defined configuration files which should be loaded at start time.

func PathExists

func PathExists(path string) (bool, error)

PathExists returns whether the given file or directory exists.

func UnzipFile

func UnzipFile(name string, dest string, overwrite bool) error

UnzipFile extracts a given zip file into a given output folder.

func UnzipReader

func UnzipReader(reader io.ReaderAt, size int64, dest string, overwrite bool) error

UnzipReader extracts a given zip archive into a given output folder. Size is the size of the archive.

Types

type FilenameIterator

type FilenameIterator interface {

	/*
		Basename decorades the initial file name.
	*/
	Basename(filename string) string

	/*
		NextName returns the next file name based on the current file name.
		An empty string means the end of the iteration.
	*/
	NextName(currentName string) string
}

FilenameIterator is used by the MultiFileBuffer to determine the new file name when rotating the buffer files. Basename is called before doing any calculation. This function should do general filename decoration. If the decoration changes over time then the function needs to also handle the cleanup.

func ConsecutiveNumberIterator

func ConsecutiveNumberIterator(maxNum int) FilenameIterator

ConsecutiveNumberIterator creates a new file name iterator which adds numbers at the end of files. Up to maxNum files will be created. A maxNum parameter < 1 means there is no limit.

func DailyDateIterator

func DailyDateIterator(maxNumPerDay int, maxDays int) FilenameIterator

DailyDateIterator creates a new file name iterator which adds dates at the end of files. The log will be switched at least once every day. Up to maxNumPerDay files will be created per day. A maxNumPerDay parameter < 1 means there is no limit. Up to maxDays different days will be kept (oldest ones are deleted). A maxDays parameter < 1 means everything is kept.

type MultiFileBuffer

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

MultiFileBuffer is a file-persitent buffer which can be split over multiple files.

A specified file is opened and used as backend storage for a byte buffer. By default, the file grows indefinitely. It is possible to specify a rollover condition to allow the file to rollover once the condition is satisfied. If the condition is satisfied, the file is closed and a new file is silently opened for output. The buffer will save old log files by appending the extensions ‘.1’, ‘.2’ etc., to the file name. The rollover condition is only checked once at the beginning of a write operation.

For example, with a base file name of app.log, the buffer would create app.log, app.log.1, app.log.2, etc. The file being written to is always app.log. When this file is filled, it is closed and renamed to app.log.1, and if files app.log.1, app.log.2, etc. exist, then they are renamed to app.log.2, app.log.3 etc. respectively.

func NewMultiFileBuffer

func NewMultiFileBuffer(filename string, it FilenameIterator, cond RolloverCondition) (*MultiFileBuffer, error)

NewMultiFileBuffer creates a new MultiFileBuffer with a given file name iterator and rollover condition.

func (*MultiFileBuffer) Close

func (mfb *MultiFileBuffer) Close() error

Close closes the buffer.

func (*MultiFileBuffer) Write

func (mfb *MultiFileBuffer) Write(output []byte) (int, error)

Write writes len(p) bytes from p to the underlying data stream. It returns the number of bytes written from p (0 <= n <= len(p)) and any error encountered that caused the write to stop early.

type RolloverCondition

type RolloverCondition interface {

	/*
	   CheckRollover checks if the buffer files should be switched.
	*/
	CheckRollover(basename string) bool
}

RolloverCondition is used by the MultiFileBuffer to check if the buffer files should be switched.

func EmptyRolloverCondition

func EmptyRolloverCondition() RolloverCondition

EmptyRolloverCondition creates a rollover condition which is never true.

func SizeBasedRolloverCondition

func SizeBasedRolloverCondition(maxSize int64) RolloverCondition

SizeBasedRolloverCondition creates a new rollover condition based on file size. The condition is satisfied if the base file exceeds a certain file size.

type WatchedConfig

type WatchedConfig struct {
	SyncError error // Synchronization errors
	// contains filtered or unexported fields
}

WatchedConfig is a helper object which continuously watches a given config file. The file and the memory config are kept in sync.

func NewWatchedConfig

func NewWatchedConfig(filename string, defaultConfig map[string]interface{},
	interval time.Duration) (*WatchedConfig, error)

NewWatchedConfig returns a new watcher object for a given config file.

func (*WatchedConfig) Close

func (wc *WatchedConfig) Close() error

Close closes this config watcher.

func (*WatchedConfig) GetConfig

func (wc *WatchedConfig) GetConfig() (map[string]interface{}, error)

GetConfig returns the current config.

func (*WatchedConfig) GetValue

func (wc *WatchedConfig) GetValue(k string) (interface{}, bool, error)

GetValue returns a single config value.