util

package
v0.0.0-...-1d50e38 Latest Latest
Warning

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

Go to latest
Published: May 30, 2017 License: Apache-2.0 Imports: 28 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultSSHPort = "22"
)

Variables

View Source
var ErrBufferFull = errors.New("buffer full")

ErrBufferFull indicates that a CappedWriter's bytes.Buffer has MaxBytes bytes.

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 BuildFileList

func BuildFileList(startPath string, expressions ...string) ([]string, error)

BuildFileList returns a list of files that match the given list of expressions rooted at the given startPath. The expressions correspond to gitignore ignore expressions: anything that would be matched - and therefore ignored by git - is included in the returned list of file paths. BuildFileList does not follow symlinks as it uses filpath.Walk, which does not follow symlinks.

func CleanForPath

func CleanForPath(name string) string

func CleanName

func CleanName(name string) string

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

func FileExists

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

FileExists returns true if 'path' exists.

func FromNanoseconds

func FromNanoseconds(duration time.Duration) int64

fromNanoSeconds returns milliseconds of a duration for queries in the database.

func FromPythonTime

func FromPythonTime(pyTime float64) time.Time

FromPythonTime returns a time.Time that corresponds to the float style python time which is <seconds>.<fractional_seconds> from unix epoch.

func GetBoolValue

func GetBoolValue(r *http.Request, valueKey string, defaultValue bool) (bool, error)

GetBoolValue returns a form value as an integer

func GetIntValue

func GetIntValue(r *http.Request, valueKey string, defaultValue int) (int, error)

GetIntValue returns a form value as an integer

func GetStringArrayValue

func GetStringArrayValue(r *http.Request, valueKey string, defaultValue []string) []string

GetStringArrayValue returns a form value as a string array

func IndexWhiteSpace

func IndexWhiteSpace(s string) int

IndexWhiteSpace returns the first index of white space in the given string. Returns -1 if no white space exists.

func IsZeroTime

func IsZeroTime(t time.Time) bool

IsZeroTime checks that a time is either equal to golang ZeroTime or UTC ZeroTime.

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 NewRequestReader

func NewRequestReader(req *http.Request) io.ReadCloser

NewRequestReader returns an io.ReadCloser closer for the body of an *http.Request, using a limited reader internally to avoid unbounded reading from the request body. The reader is limited to 16 megabytes.

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 RecoverAndLogStackTrace

func RecoverAndLogStackTrace()

RecoverAndLogStackTrace captures a panic stack trace and writes it to the log file rather than allowing it to be printed to standard error, where it would be lost (in the case of agents.)

func Retry

func Retry(op RetriableFunc, attempts int, sleep time.Duration) (bool, error)

Retry provides a mechanism to retry an operation with exponential backoff (that uses some jitter,) Specify the maximum number of retry attempts that you want to permit as well as the initial period that you want to sleep between attempts.

Retry requires that the starting sleep interval be at least 100 milliseconds, and forces this interval if you attempt to use a shorter period.

If you specify 0 attempts, Retry will use an attempt value of one.

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 StringSliceIntersection

func StringSliceIntersection(a, b []string) []string

StringSliceIntersection returns the intersecting elements of slices a and b.

func ToNanoseconds

func ToNanoseconds(duration time.Duration) time.Duration

fromNanoSeconds returns milliseconds of a duration for queries in the database.

func ToPythonTime

func ToPythonTime(t time.Time) float64

ToPythonTime returns a number in the format that python's time.time() returns as a float with <seconds>.<fractional_seconds>

func Truncate

func Truncate(input string, outputLength int) string

Truncate returns a string of at most the given length.

func UniqueStrings

func UniqueStrings(slice []string) []string

UniqueStrings takes a slice of strings and returns a new slice with duplicates removed. Order is preserved.

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 WriteCSVResponse

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

WriteToCSVResponse takes in an interface that is a slice or an array and converts the struct to csv for fields that have the the csv struct tag.

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 WriteTempFile

func WriteTempFile(prefix string, data []byte) (string, error)

WriteTempFile creates a temp file, writes the data to it, closes it and returns the file name.

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 CappedWriter

type CappedWriter struct {
	Buffer   *bytes.Buffer
	MaxBytes int
}

CappedWriter implements a buffer that stores up to MaxBytes bytes. Returns ErrBufferFull on overflowing writes

func (*CappedWriter) IsFull

func (cw *CappedWriter) IsFull() bool

IsFull indicates whether the buffer is full.

func (*CappedWriter) String

func (cw *CappedWriter) String() string

String return the contents of the buffer as a string.

func (*CappedWriter) Write

func (cw *CappedWriter) Write(in []byte) (int, error)

Write writes to the buffer. An error is returned if the buffer is full.

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 (e 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 SizeTrackingReader

type SizeTrackingReader struct {
	BytesRead uint64
	io.Reader
}

func (*SizeTrackingReader) Read

func (str *SizeTrackingReader) Read(p []byte) (int, error)

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