util

package
Version: v3.0.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2018 License: MIT Imports: 22 Imported by: 0

Documentation

Overview

Packae util includes various utility methods.

These utilities are frequently Martian-specific but do not depend on Martian runtime infrastructure.

Index

Examples

Constants

View Source
const (
	ANSI_BLACK   = 30
	ANSI_RED     = 31
	ANSI_GREEN   = 32
	ANSI_YELLOW  = 33
	ANSI_BLUE    = 34
	ANSI_MAGENTA = 35
	ANSI_CYAN    = 36
	ANSI_WHITE   = 37
)
View Source
const TIMEFMT = "2006-01-02 15:04:05"

Variables

View Source
var ENABLE_LOGGING bool = true

After a call to SetupSignalHandlers, these signals will be handled by waiting for all pending critical sections to complete, running all registered handlers, and then exiting with return code 1

Functions

func ArrayToString

func ArrayToString(data []interface{}) []string

func Colorize

func Colorize(s string, c int) string

Surrounds the given string with ANSI color control characters.

func CreateZip

func CreateZip(zipPath string, filePaths []string) error

func DieIf

func DieIf(err error)

End the process if err is not nil. Because this method waits up to one minute for critical sections to end, it should not be called from inside a critical section.

func EnterCriticalSection

func EnterCriticalSection()

EnterCriticalSection should be called before performing a sequence of operations which should be logically atomic with respect to signals. Handled signals will not terminate the process until ExitCriticalSection is called. If the process has already been signaled, EnterCriticalSection will block.

Note that EnterCriticalSection should never be called when already in a critical section, as it may deadlock if the process is signaled between the first and second call.

func EnvRequire

func EnvRequire(reqs [][]string, log bool) map[string]string

func ExitCriticalSection

func ExitCriticalSection()

ExitCriticalSection should be called after EnterCriticalSection once the logically atomic sequence of operations has completed.

func FormatEnv

func FormatEnv(envs map[string]string) []string

func FormatMroPath

func FormatMroPath(mroPaths []string) string

func GetDirectorySize

func GetDirectorySize(paths []string) (uint, uint64)

func GetFilenameWithSuffix

func GetFilenameWithSuffix(dir string, fname string) string

func GetGitBranch

func GetGitBranch(dir string) (string, error)

Returns the output of running 'git rev-parse --abbrev-ref HEAD' in the given directory, e.g. 'master'.

func GetGitTag

func GetGitTag(dir string) (string, error)

Returns the output of running 'git describe --tags --dirty --always' in the given directory, e.g. 'v2.3.0-rc3-10-gf615588-dirty'

func GetMroVersion

func GetMroVersion(dirs []string) (string, error)

Searches for a file named .version in the parent directory of any of the given directories and returns its content if available. If none is found, attempts to run 'git describe --tags --dirty --always' in those directories to get a version.

func GetSakeVersion

func GetSakeVersion(dir string) (string, error)

Searches for a file name .version in the parent of the given directory name and returns its content.

func GetVersion

func GetVersion() string

Get the build version for this binary, as embedded by the build system.

func IsRelease

func IsRelease() bool

IsRelease returns true if the build system specified that this was a release version.

func Log

func Log(format string, v ...interface{})

Logs the given string to the current log stream. If one has not been initialized with LogTee or LogTeeWriter, the content is buffered until one of those methods is called.

func LogError

func LogError(err error, component string, format string, v ...interface{})

Logs a line in the form "<timestamp> [component] <message>" followed by a line with err.Error(). Component is intended to indicate the source of the message and should be a consistent length. By convention, this length is 7 characters.

func LogInfo

func LogInfo(component string, format string, v ...interface{})

Logs a line in the form "<timestamp> [component] <message>". Component is intended to indicate the source of the message and should be a consistent length. By convention, this length is 7 characters.

func LogTee

func LogTee(filename string)

Sets up the logging methods to log to the given file.

func LogTeeWriter

func LogTeeWriter(writer io.Writer)

Sets up the logging methods to log to the given writer.

func MakeJSON

func MakeJSON(data interface{}) string

func MakeTag

func MakeTag(key string, value string) string

func MergeEnv

func MergeEnv(envs map[string]string) []string

func Mkdir

func Mkdir(p string) error

func MkdirAll

func MkdirAll(p string) error

func ParseMroFlags

func ParseMroFlags(opts map[string]interface{}, doc string, martianOptions []string, martianArguments []string)

func ParseMroPath

func ParseMroPath(mroPath string) []string

func ParseTag

func ParseTag(tag string) (string, string)

func ParseTagsOpt

func ParseTagsOpt(opt string) []string

func Pluralize

func Pluralize(n int) string

func Print

func Print(format string, v ...interface{})

Like Log, but also prints to standard output.

func PrintError

func PrintError(err error, component string, format string, v ...interface{})

Like LogError but also prints to standard output.

func PrintInfo

func PrintInfo(component string, format string, v ...interface{})

Like LogInfo but also prints to standard output.

func Println

func Println(format string, v ...interface{})

func ReadZip

func ReadZip(zipPath string, filePath string) (string, error)

func Readdirnames

func Readdirnames(readPath string) (names []string, err error)

func RegisterSignalHandler

func RegisterSignalHandler(object HandlerObject)

Registers an object as having cleanup work which should be allowed to complete before the process terminates due to a handled signal.

func RelPath

func RelPath(p string) string

func Render

func Render(dir string, tname string, data interface{}) string

func SearchPaths

func SearchPaths(fname string, searchPaths []string) (string, bool)

func SetupSignalHandlers

func SetupSignalHandlers()

Initializes the global signal handler.

func SignalIsIgnored

func SignalIsIgnored(sig syscall.Signal) bool

Test whether the given signal is currently ignored.

func Suicide

func Suicide()

Kill this process cleanly, after waiting for critical sections and handlers to complete.

func Timestamp

func Timestamp() string

func UnregisterSignalHandler

func UnregisterSignalHandler(object HandlerObject)

func Unzip

func Unzip(zipPath string) error

func ValidateID

func ValidateID(id string) error

func Walk

func Walk(root string, walkFn filepath.WalkFunc) error

Faster, Unix-specific implementation of filepath.Walk, which avoids the directory sort and uses openat and fstatat to avoid forcing extra dirent syncs. This can save a lot of time on NFS servers, and also provides more consistent behavior in the face of directory renames or changes to the current process working directory.

Walk walks the file tree rooted at root, calling walkFn for each file or directory in the tree, including root. All errors that arise visiting files and directories are filtered by walkFn. Walk does not follow symbolic links.

Unlike filepath.Walk, Walk makes ABSOLUTELY NO GUARANTEES about the order in which the files are traversed.

Unlike filepath.Walk, the Sys() method of the os.FileInfo object passed to walkFn will be of type golang.org/x/sys/unix.Stat_t.

Example
if root, err := ioutil.TempDir("", "examplewalk"); err != nil {
	fmt.Println("Failed to create tempdir:", err)
	return
} else {
	defer os.RemoveAll(root)
	if err := os.MkdirAll(path.Join(root, "a", "b", "c", "d"), 0777); err != nil {
		fmt.Println(err)
	}
	if err := os.MkdirAll(path.Join(root, "a", "b", "e", "f"), 0777); err != nil {
		fmt.Println(err)
	}
	if err := os.MkdirAll(path.Join(root, "a", "b", "g"), 0777); err != nil {
		fmt.Println(err)
	}
	if err := ioutil.WriteFile(path.Join(root, "a.txt"), []byte("Test string"), 0666); err != nil {
		fmt.Println(err)
	}
	if err := ioutil.WriteFile(path.Join(root, "b.txt"), []byte("Test string2"), 0666); err != nil {
		fmt.Println(err)
	}
	if err := ioutil.WriteFile(path.Join(root, "a", "c.txt"), []byte("Test string33"), 0666); err != nil {
		fmt.Println(err)
	}
	if err := ioutil.WriteFile(path.Join(root, "a", "d.txt"), []byte("Test string444"), 0666); err != nil {
		fmt.Println(err)
	}
	if err := ioutil.WriteFile(path.Join(root, "a", "b", "g", "e.txt"), []byte("Test string5555"), 0666); err != nil {
		fmt.Println(err)
	}
	if err := ioutil.WriteFile(path.Join(root, "a", "b", "g", "f.txt"), []byte("Test string66666"), 0666); err != nil {
		fmt.Println(err)
	}
	if err := ioutil.WriteFile(path.Join(root, "a", "b", "e", "f", "g.txt"), []byte("Test string777777"), 0666); err != nil {
		fmt.Println(err)
	}

	if err := Walk(root, func(p string, info os.FileInfo, err error) error {
		if !strings.HasPrefix(p, root) {
			fmt.Println("Path was not rooted:", p)
		}
		if err != nil {
			fmt.Printf("Error reading %s:\n%v\n", strings.TrimPrefix(p, root), err)
		} else {
			if info.IsDir() {
				fmt.Printf("Directory '%s'\n", strings.TrimPrefix(p, root))
			} else {
				fmt.Printf("File '%s' had %d bytes\n", strings.TrimPrefix(p, root), info.Size())
			}
		}
		if path.Base(p) == "f" {
			fmt.Println("Skipping", path.Base(p))
			return filepath.SkipDir
		} else {
			return err
		}
	}); err != nil {
		fmt.Println(err)
	}

	if err := Walk(path.Join(root, "b"), func(p string, info os.FileInfo, err error) error {
		if err != nil {
			fmt.Printf("Correctly expected error reading %s\n",
				strings.TrimPrefix(p, root))
		} else {
			fmt.Printf("File '%s' had %d bytes\n", p, info.Size())
		}
		return err
	}); err != nil {
		fmt.Println("Correctly expected error")
	}
	
Output:

Directory ''
Directory '/a'
File '/a.txt' had 11 bytes
File '/b.txt' had 12 bytes
Directory '/a/b'
File '/a/c.txt' had 13 bytes
File '/a/d.txt' had 14 bytes
Directory '/a/b/c'
Directory '/a/b/e'
Directory '/a/b/g'
Directory '/a/b/c/d'
Directory '/a/b/e/f'
File '/a/b/g/e.txt' had 15 bytes
File '/a/b/g/f.txt' had 16 bytes
Skipping f
Correctly expected error reading /b
Correctly expected error

func WidthForInt

func WidthForInt(max int) int

Gets the number of digits required to display a given integer in base 10. Optimizes for the common cases.

Types

type HandlerObject

type HandlerObject interface {
	HandleSignal(sig os.Signal)
}

Interface for objects which need to perform cleanup operations when the process is being terminated by a handled signal.

The HandleSignal method is called after all critical sections have completed. Attempting to enter a critical section from within the HandleSignal method will result in a deadlock.

type Logger

type Logger struct {
	// contains filtered or unexported fields
}
var LOGGER *Logger = nil

type MartianError

type MartianError struct {
	Msg string
}

MartianError

func (*MartianError) Error

func (self *MartianError) Error() string

type SignalHandler

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

func (*SignalHandler) Notify

func (self *SignalHandler) Notify()

Notify this handler of signals.

type ZipError

type ZipError struct {
	ZipPath  string
	FilePath string
}

ZipError

func (*ZipError) Error

func (self *ZipError) Error() string

Jump to

Keyboard shortcuts

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