Documentation
¶
Overview ¶
Package utils provides commonly used utility fuctions
Index ¶
- func AbsFile(path string) string
- func AbsFileFromRoot(path string, root string) string
- func EnsurePermissions(file string, maxPermissions int)
- func EnsureSafePermissions(file string)
- func Exit(code int, reason string, reasonArgs ...interface{})
- func FileExists(path string) bool
- func IsFile(path string) bool
- func IsProcessRunning(pid int) bool
- func IsReadable(path string) bool
- func IsReadableFile(path string) bool
- func IsWritable(path string) bool
- func IsWritableFile(path string) bool
- func NewSecureFileAccessor() interface{ ... }
- func OpenFileSecure(name string, flag int, perm os.FileMode) (*os.File, error)
- func ReadPid(file string) (int, error)
- func ReadSecure(file string) (string, error)
- func RoundDuration(duration time.Duration) time.Duration
- func ValidatePidFilePath(path string) (err error)
- func WaitUntil(cb func() bool, args ...time.Duration) (success bool)
- func WritePid(file string, pid int) error
- func WriteSecure(file, data string) error
- type FileAccessor
- type SecureFileAccessor
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AbsFile ¶
AbsFile returns an absolute representation of path. Works like filepath.Abs but do not return a second error return value
func AbsFileFromRoot ¶
AbsFileFromRoot returns an absolute representation of path, using root as the cwd. Setting root to the empty string, uses the CWD
func EnsurePermissions ¶
EnsurePermissions checks that the file permissions are less or equal than maxPermissions, aborting otherwise
func EnsureSafePermissions ¶
func EnsureSafePermissions(file string)
EnsureSafePermissions checks that file permissions are safe (less or equal than 0700), aborting otherwise
func FileExists ¶
FileExists returns true if path exists or false otherwise
func IsFile ¶
IsFile returns true if path exists and is a file (or a link to a file) and false otherwise
func IsProcessRunning ¶
IsProcessRunning returns true if a process is running in the specified pid or false otherwhise
func IsReadable ¶
IsReadable returns true if the path exists and is redable and false otherwise
func IsReadableFile ¶
IsReadableFile returns true if path is a file and redable and false otherwise
func IsWritable ¶
IsWritable returns true if the path exists and is writable or if it can be created, and false otherwise
func IsWritableFile ¶
IsWritableFile returns true if path is a file and writable or does not exists but could be created, and false otherwise
func NewSecureFileAccessor ¶
func NewSecureFileAccessor() interface { FileAccessor }
NewSecureFileAccessor returns a new SecureFileAccessor
func OpenFileSecure ¶
OpenFileSecure allows opening a file ensure secure permissions
func ReadPid ¶
ReadPid returns a parsed numeric pid from the given file. If the file does not exists, its contents do not contain a valid PID in the first line or any other error occurs, it will return -1 as the pid and the detailed error
func ReadSecure ¶
ReadSecure returns a string containing the contents of file The function will cause the process to exit if any user other than the owner has write permissions over it
func RoundDuration ¶
RoundDuration returns a new duration truncated to seconds
func ValidatePidFilePath ¶
ValidatePidFilePath returns an error unless path is valid to write a PID file. Specifically, it will ensure it is writable and, if it already exists, point to a file.
func WritePid ¶
WritePid saves the provided pid into file with safe permissions (644) Returns an error in case of failure
func WriteSecure ¶
WriteSecure writes data into file ensuring the resulting permissions are 0644 (only write permissions for the owner)
Types ¶
type FileAccessor ¶
type FileAccessor interface { ReadFile(file string) (string, error) OpenFile(name string, flag int, perm os.FileMode) (*os.File, error) WriteFile(file, data string) error }
FileAccessor is the interface that defines the operations that file openers must support
type SecureFileAccessor ¶
SecureFileAccessor allows defining the acceptable permission used when reading and writing files
func (*SecureFileAccessor) EnsurePermissions ¶
func (fa *SecureFileAccessor) EnsurePermissions(file string)
EnsurePermissions checks file and aborts the executing if it does not comply with the acceptable configured permissions
func (*SecureFileAccessor) OpenFile ¶
OpenFile allows opening a file and ensuring its permissions are within the acceptable limits
func (*SecureFileAccessor) ReadFile ¶
func (fa *SecureFileAccessor) ReadFile(file string) (string, error)
ReadFile allows reading a file if it complies with the required permissions
func (*SecureFileAccessor) WriteFile ¶
func (fa *SecureFileAccessor) WriteFile(file, data string) error
WriteFile allows writing a file and enforcing its permissions to the configured ones