Documentation ¶
Overview ¶
Packae util includes various utility methods.
These utilities are frequently Martian-specific but do not depend on Martian runtime infrastructure.
Index ¶
- Constants
- Variables
- func ArrayToString(data []interface{}) []string
- func Colorize(s string, c int) string
- func CreateZip(zipPath string, filePaths []string) error
- func DieIf(err error)
- func EnterCriticalSection()
- func EnvRequire(reqs [][]string, log bool) map[string]string
- func ExitCriticalSection()
- func FormatEnv(envs map[string]string) []string
- func FormatMroPath(mroPaths []string) string
- func GetDirectorySize(paths []string) (uint, uint64)
- func GetFilenameWithSuffix(dir string, fname string) string
- func GetGitBranch(dir string) (string, error)
- func GetGitTag(dir string) (string, error)
- func GetMroVersion(dirs []string) (string, error)
- func GetSakeVersion(dir string) (string, error)
- func GetVersion() string
- func IsRelease() bool
- func Log(format string, v ...interface{})
- func LogError(err error, component string, format string, v ...interface{})
- func LogInfo(component string, format string, v ...interface{})
- func LogTee(filename string)
- func LogTeeWriter(writer io.Writer)
- func MakeJSON(data interface{}) string
- func MakeTag(key string, value string) string
- func MergeEnv(envs map[string]string) []string
- func Mkdir(p string) error
- func MkdirAll(p string) error
- func ParseMroFlags(opts map[string]interface{}, doc string, martianOptions []string, ...)
- func ParseMroPath(mroPath string) []string
- func ParseTag(tag string) (string, string)
- func ParseTagsOpt(opt string) []string
- func Pluralize(n int) string
- func Print(format string, v ...interface{})
- func PrintError(err error, component string, format string, v ...interface{})
- func PrintInfo(component string, format string, v ...interface{})
- func Println(format string, v ...interface{})
- func ReadZip(zipPath string, filePath string) (string, error)
- func Readdirnames(readPath string) (names []string, err error)
- func RegisterSignalHandler(object HandlerObject)
- func RelPath(p string) string
- func Render(dir string, tname string, data interface{}) string
- func SearchPaths(fname string, searchPaths []string) (string, bool)
- func SetupSignalHandlers()
- func SignalIsIgnored(sig syscall.Signal) bool
- func Suicide()
- func Timestamp() string
- func UnregisterSignalHandler(object HandlerObject)
- func Unzip(zipPath string) error
- func ValidateID(id string) error
- func Walk(root string, walkFn filepath.WalkFunc) error
- func WidthForInt(max int) int
- type HandlerObject
- type Logger
- type MartianError
- type SignalHandler
- type ZipError
Examples ¶
Constants ¶
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 )
const TIMEFMT = "2006-01-02 15:04:05"
Variables ¶
var ENABLE_LOGGING bool = true
var HANDLED_SIGNALS = [...]os.Signal{ os.Interrupt, syscall.SIGHUP, syscall.SIGTERM, syscall.SIGUSR1, syscall.SIGUSR2, }
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 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 ExitCriticalSection ¶
func ExitCriticalSection()
ExitCriticalSection should be called after EnterCriticalSection once the logically atomic sequence of operations has completed.
func FormatMroPath ¶
func GetDirectorySize ¶
func GetFilenameWithSuffix ¶
func GetGitBranch ¶
Returns the output of running 'git rev-parse --abbrev-ref HEAD' in the given directory, e.g. 'master'.
func GetGitTag ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 LogTeeWriter ¶
Sets up the logging methods to log to the given writer.
func ParseMroFlags ¶
func ParseMroPath ¶
func ParseTagsOpt ¶
func Print ¶
func Print(format string, v ...interface{})
Like Log, but also prints to standard output.
func PrintError ¶
Like LogError but also prints to standard output.
func Readdirnames ¶
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 SignalIsIgnored ¶
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 UnregisterSignalHandler ¶
func UnregisterSignalHandler(object HandlerObject)
func ValidateID ¶
func Walk ¶
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 ¶
Gets the number of digits required to display a given integer in base 10. Optimizes for the common cases.
Types ¶
type HandlerObject ¶
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 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
}