util

package
v0.0.0-...-900fa13 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2015 License: Apache-2.0 Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultSSHPort = "22"
)

Variables

View Source
var (
	// ErrTimedOut is returned by a function that timed out.
	ErrTimedOut = errors.New("Function timed out")
)
View Source
var ZeroTime time.Time = time.Unix(0, 0)

ZeroTime represents 0 in epoch time

Functions

func CleanName

func CleanName(name string) string

CleanName returns a name with spaces and dashes replaced with safe underscores

func CurrentGitHash

func CurrentGitHash(repoDir string) (string, error)

CurrentGitHash returns the current git hash of the git repository located at the specified directory.

func DumpStackOnSIGQUIT

func DumpStackOnSIGQUIT(stackOut io.Writer)

DumpStackOnSIGQUIT listens for a SIGQUIT signal and writes stack dump to the given io.Writer when one is received. Blocks, so spawn it as a goroutine.

func FileExists

func FileExists(elem ...string) (bool, error)

FileExists returns true if 'path' exists.

func GetAppendingFile

func GetAppendingFile(path string) (*os.File, error)

GetAppendingFile opens a file for appending. The file will be created if it does not already exist.

func MakeTlsConfig

func MakeTlsConfig(cert string, key string) (*tls.Config, error)

MakeTlsConfig creates a TLS Config from a certificate and key.

func Min

func Min(a ...int) int

min function for ints

func MountHandler

func MountHandler(r *mux.Router, prefix string, h http.Handler) http.Handler

MountHandler routes all requests to the given mux.Router under the prefix to be handled by the http.Handler, which the request's path rooted under that prefix. So for example, if a router configured with the path /foo is given to MountHandler(r, "/bar/baz", newHandler) Then a request to the router at /foo/bar/baz/hello will be handled by newHandler, appearing with the path "/baz/hello"

func RandomString

func RandomString() string

RandomString returns a cryptographically random string.

func ReadJSONInto

func ReadJSONInto(r io.ReadCloser, data interface{}) error

ReadJSONInto reads JSON from an io.ReadCloser into the data pointer.

func ReadYAMLInto

func ReadYAMLInto(r io.ReadCloser, data interface{}) error

ReadYAMLInto reads data for the given io.ReadCloser - until it hits an error or reaches EOF - and attempts to unmarshal the data read into the given interface.

func RemoveSuffix

func RemoveSuffix(s, suffix string) string

RemoveSuffix returns s with 'suffix' removed from end of string if present

func Retry

func Retry(attemptFunc RetriableFunc, maxTries int,
	sleep time.Duration) (bool, error)

Retry will call attemptFunc up to maxTries until it returns nil, sleeping the specified amount of time between each call. The function can return an error to abort the retrying, or return RetriableError to allow the function to be called again.

func RetryArithmeticBackoff

func RetryArithmeticBackoff(attemptFunc RetriableFunc, maxTries int,
	sleep time.Duration) (bool, error)

RetryArithmeticBackoff will call attemptFunc up to maxTries until it returns nil, leeping for an arithmetic progressed duration after each retry attempt. e.g. For if attemptFunc errors out immediately, the sleep duration for a call like RetryArithmeticBackoff(func, 3, 5) would be thus:

1st iteration: Sleep duration 5 seconds 2nd iteration: Sleep duration 10 seconds 3rd iteration: Sleep duration 15 seconds ... The function can return an error to abort the retrying, or return RetriableError to allow the function to be called again.

func RetryGeometricBackoff

func RetryGeometricBackoff(attemptFunc RetriableFunc, maxTries int,
	sleep time.Duration) (bool, error)

RetryGeometricBackoff will call attemptFunc up to maxTries until it returns nil, leeping for an geometric progressed duration after each retry attempt. e.g. For if attemptFunc errors out immediately, the sleep duration for a call like RetryGeometricBackoff(func, 3, 5) would be thus:

1st iteration: Sleep duration 5 seconds 2nd iteration: Sleep duration 25 seconds 3rd iteration: Sleep duration 125 seconds ... The function can return an error to abort the retrying, or return RetriableError to allow the function to be called again.

func RunFunctionWithTimeout

func RunFunctionWithTimeout(f func() error, timeout time.Duration) error

RunFunctionWithTimeout runs a function, timing out after the specified time. The error returned will be the return value of the function if it completes, or ErrTimedOut if it times out.

func SliceContains

func SliceContains(slice, elt interface{}) bool

SliceContains returns true if elt is in slice, panics if slice is not of Kind reflect.Slice

func Truncate

func Truncate(input string, outputLength int) string

Truncate returns a string of at most the given length.

func UnmarshalYAMLFile

func UnmarshalYAMLFile(file string, data interface{}) error

UnmarshalYAMLFile reads in the specified file, and unmarshals it into the given interface. Returns an error if one is encountered in reading the file or if the file does not contain valid YAML.

func WriteJSON

func WriteJSON(w *http.ResponseWriter, data interface{}, status int)

WriteJSON writes a json response with the supplied code on the given writer.

func WriteToTempFile

func WriteToTempFile(data string) (string, error)

WriteToTempFile writes the given string to a temporary file and returns the path to the file.

Types

type BackoffCalc

type BackoffCalc func(minSleep time.Duration, maxTries,
	triesLeft int) time.Duration

BackoffCalc is a custom function signature that can be implemented by any implementation that returns a sleep duration for backoffs

type LineBufferingWriter

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

LineBufferingWriter is an implementation of io.Writer that sends the newline delimited subslices of its inputs to a wrapped io.Writer, buffering as needed.

func NewLineBufferingWriter

func NewLineBufferingWriter(wrapped io.Writer) *LineBufferingWriter

func (*LineBufferingWriter) Flush

func (lbw *LineBufferingWriter) Flush() error

writes whatever is in the buffer out using the wrapped io.Writer

func (*LineBufferingWriter) Write

func (lbw *LineBufferingWriter) Write(p []byte) (n int, err error)

use the wrapped io.Writer to write anything that is delimited with a newline

type RetriableError

type RetriableError struct {
	Failure error
}

RetriableError can be returned by any function called with Retry(), to indicate that it should be retried again after a sleep interval.

func (RetriableError) Error

func (retriable RetriableError) Error() string

type RetriableFunc

type RetriableFunc func() error

RetriableFunc is any function that takes no parameters and returns only an error interface. These functions can be used with util.Retry.

type StaticHostInfo

type StaticHostInfo struct {
	User     string
	Hostname string
	Port     string
}

StaticHostInfo stores the connection parameters for connecting to an SSH host.

func ParseSSHInfo

func ParseSSHInfo(fullHostname string) (*StaticHostInfo, error)

ParseSSHInfo reads in a hostname definition and reads the relevant SSH connection information from it. For example,

"admin@myhostaddress:24"

will return

StaticHostInfo{User: "admin", Hostname:"myhostaddress", Port: 24}

Jump to

Keyboard shortcuts

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