utils

package
v0.2.0-rc1 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CopyEscapable

func CopyEscapable(dst io.Writer, src io.ReadCloser) (written int64, err error)

Code c/c from io.Copy() modified to handle escape sequence

func CopyFile

func CopyFile(src, dst string) (int64, error)

func CreatePidFile

func CreatePidFile(pidfile string) error

func DisplayJSONMessagesStream

func DisplayJSONMessagesStream(in io.Reader, out io.Writer, terminalFd uintptr, isTerminal bool) error

func DockerInitPath

func DockerInitPath(localCopy string) string

Figure out the path of our dockerinit (which may be SelfPath())

func DoesEnvExist added in v0.2.0

func DoesEnvExist(name string) bool

func Download

func Download(url string) (resp *http.Response, err error)

Request a given URL and return an io.Reader

func GenerateRandomID

func GenerateRandomID() string

GenerateRandomID returns an unique id

func GetCallerName

func GetCallerName(depth int) string

GetCallerName introspects the call stack and returns the name of the function `depth` levels down in the stack.

func GetTotalUsedFds

func GetTotalUsedFds() int

func HashData

func HashData(src io.Reader) (string, error)

func NewHTTPRequestError

func NewHTTPRequestError(msg string, res *http.Response) error

func NewTimeoutConn

func NewTimeoutConn(conn net.Conn, timeout time.Duration) net.Conn

func ParseFlags added in v0.2.0

func ParseFlags(cmd *flag.FlagSet, args []string, withHelp bool) error

ParseFlags is a utility function that adds a help flag if withHelp is true, calls cmd.Parse(args) and prints a relevant error message if there are incorrect number of arguments. It returns error only if error handling is set to ContinueOnError and parsing fails. If error handling is set to ExitOnError, it's safe to ignore the return value. TODO: move this to a better package than utils

func ProgressReader

func ProgressReader(r io.ReadCloser, size int, output io.Writer, sf *StreamFormatter, newline bool, ID, action string) *progressReader

func RandomString

func RandomString() string

func ReadDockerIgnore added in v0.2.0

func ReadDockerIgnore(path string) ([]string, error)

Reads a .dockerignore file and returns the list of file patterns to ignore. Note this will trim whitespace from each line as well as use GO's "clean" func to get the shortest/cleanest path for each.

func ReadSymlinkedDirectory

func ReadSymlinkedDirectory(path string) (string, error)

ReadSymlinkedDirectory returns the target directory of a symlink. The target of the symbolic link may not be a file.

func RemovePidFile

func RemovePidFile(pidfile string)

func ReplaceOrAppendEnvValues

func ReplaceOrAppendEnvValues(defaults, overrides []string) []string

ReplaceOrAppendValues returns the defaults with the overrides either replaced by env key or appended to the list

func ReportError added in v0.2.0

func ReportError(cmd *flag.FlagSet, str string, withHelp bool)

func SelfPath

func SelfPath() string

Figure out the absolute path of our own binary (if it's still around).

func ShellQuoteArguments

func ShellQuoteArguments(args []string) string

Take a list of strings and escape them so they will be handled right when passed as arguments to an program via a shell

func StringsContainsNoCase

func StringsContainsNoCase(slice []string, s string) bool

func TempDir

func TempDir(rootDir string) (string, error)

TempDir returns the default directory to use for temporary files.

func TestDirectory

func TestDirectory(templateDir string) (dir string, err error)

TestDirectory creates a new temporary directory and returns its path. The contents of directory at path `templateDir` is copied into the new directory.

func Trunc

func Trunc(s string, maxlen int) string

func TruncateID

func TruncateID(id string) string

TruncateID returns a shorthand version of a string identifier for convenience. A collision with other shorthands is very unlikely, but possible. In case of a collision a lookup with TruncIndex.Get() will fail, and the caller will need to use a langer prefix, or the full-length Id.

func ValidateContextDirectory

func ValidateContextDirectory(srcPath string, excludes []string) error

ValidateContextDirectory checks if all the contents of the directory can be read and returns an error if some files can't be read symlinks which point to non-existing files don't trigger an error

func ValidateID

func ValidateID(id string) error

Types

type HTTPAuthDecorator

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

func (*HTTPAuthDecorator) ChangeRequest

func (self *HTTPAuthDecorator) ChangeRequest(req *http.Request) (*http.Request, error)

type HTTPMetaHeadersDecorator

type HTTPMetaHeadersDecorator struct {
	Headers map[string][]string
}

func (*HTTPMetaHeadersDecorator) ChangeRequest

func (h *HTTPMetaHeadersDecorator) ChangeRequest(req *http.Request) (newReq *http.Request, err error)

type HTTPRequestDecorator

type HTTPRequestDecorator interface {
	// ChangeRequest() changes the request accordingly.
	// The changed request will be returned or err will be non-nil
	// if an error occur.
	ChangeRequest(req *http.Request) (newReq *http.Request, err error)
}

HTTPRequestDecorator is used to change an instance of http.Request. It could be used to add more header fields, change body, etc.

func NewHTTPAuthDecorator

func NewHTTPAuthDecorator(login, password string) HTTPRequestDecorator

func NewHTTPUserAgentDecorator

func NewHTTPUserAgentDecorator(versions ...VersionInfo) HTTPRequestDecorator

type HTTPRequestFactory

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

HTTPRequestFactory creates an HTTP request and applies a list of decorators on the request.

func NewHTTPRequestFactory

func NewHTTPRequestFactory(d ...HTTPRequestDecorator) *HTTPRequestFactory

func (*HTTPRequestFactory) AddDecorator

func (self *HTTPRequestFactory) AddDecorator(d ...HTTPRequestDecorator)

func (*HTTPRequestFactory) GetDecorators added in v0.2.0

func (self *HTTPRequestFactory) GetDecorators() []HTTPRequestDecorator

func (*HTTPRequestFactory) NewRequest

func (h *HTTPRequestFactory) NewRequest(method, urlStr string, body io.Reader, d ...HTTPRequestDecorator) (*http.Request, error)

NewRequest() creates a new *http.Request, applies all decorators in the HTTPRequestFactory on the request, then applies decorators provided by d on the request.

type HTTPUserAgentDecorator

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

HTTPUserAgentDecorator appends the product/version to the user agent field of a request.

func (*HTTPUserAgentDecorator) ChangeRequest

func (h *HTTPUserAgentDecorator) ChangeRequest(req *http.Request) (newReq *http.Request, err error)

type JSONError

type JSONError struct {
	Code    int    `json:"code,omitempty"`
	Message string `json:"message,omitempty"`
}

func (*JSONError) Error

func (e *JSONError) Error() string

type JSONMessage

type JSONMessage struct {
	Stream          string        `json:"stream,omitempty"`
	Status          string        `json:"status,omitempty"`
	Progress        *JSONProgress `json:"progressDetail,omitempty"`
	ProgressMessage string        `json:"progress,omitempty"` //deprecated
	ID              string        `json:"id,omitempty"`
	From            string        `json:"from,omitempty"`
	Time            int64         `json:"time,omitempty"`
	Error           *JSONError    `json:"errorDetail,omitempty"`
	ErrorMessage    string        `json:"error,omitempty"` //deprecated
}

func (*JSONMessage) Display

func (jm *JSONMessage) Display(out io.Writer, isTerminal bool) error

type JSONProgress

type JSONProgress struct {
	Current int   `json:"current,omitempty"`
	Total   int   `json:"total,omitempty"`
	Start   int64 `json:"start,omitempty"`
	// contains filtered or unexported fields
}

func (*JSONProgress) String

func (p *JSONProgress) String() string

type KeyValuePair

type KeyValuePair struct {
	Key   string
	Value string
}

type StatusError

type StatusError struct {
	Status     string
	StatusCode int
}

An StatusError reports an unsuccessful exit by a command.

func (*StatusError) Error

func (e *StatusError) Error() string

type StderrFormater

type StderrFormater struct {
	io.Writer
	*StreamFormatter
}

func (*StderrFormater) Write

func (sf *StderrFormater) Write(buf []byte) (int, error)

type StdoutFormater

type StdoutFormater struct {
	io.Writer
	*StreamFormatter
}

func (*StdoutFormater) Write

func (sf *StdoutFormater) Write(buf []byte) (int, error)

type StreamFormatter

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

func NewStreamFormatter

func NewStreamFormatter(json bool) *StreamFormatter

func (*StreamFormatter) FormatError

func (sf *StreamFormatter) FormatError(err error) []byte

func (*StreamFormatter) FormatProgress

func (sf *StreamFormatter) FormatProgress(id, action string, progress *JSONProgress) []byte

func (*StreamFormatter) FormatStatus

func (sf *StreamFormatter) FormatStatus(id, format string, a ...interface{}) []byte

func (*StreamFormatter) FormatStream

func (sf *StreamFormatter) FormatStream(str string) []byte

func (*StreamFormatter) Json

func (sf *StreamFormatter) Json() bool

type TimeoutConn

type TimeoutConn struct {
	net.Conn
	// contains filtered or unexported fields
}

A net.Conn that sets a deadline for every Read or Write operation

func (*TimeoutConn) Read

func (c *TimeoutConn) Read(b []byte) (int, error)

type VersionInfo

type VersionInfo interface {
	Name() string
	Version() string
}

VersionInfo is used to model entities which has a version. It is basically a tupple with name and version.

type WriteFlusher

type WriteFlusher struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func NewWriteFlusher

func NewWriteFlusher(w io.Writer) *WriteFlusher

func (*WriteFlusher) Flush

func (wf *WriteFlusher) Flush()

Flush the stream immediately.

func (*WriteFlusher) Write

func (wf *WriteFlusher) Write(b []byte) (n int, err error)

Jump to

Keyboard shortcuts

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