utils

package
v3.0.0-rc.3+incompatible Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2016 License: Apache-2.0 Imports: 21 Imported by: 0

README

Package information

This package contains helpful functions that iris, internally, uses

That's it.

Documentation

Index

Constants

View Source
const (
	// ContentBINARY is the  string of "application/octet-stream response headers
	ContentBINARY = "application/octet-stream"
)

Variables

View Source
var (
	// ErrNoZip returns an error with message: 'While creating file '+filename'. It's not a zip'
	ErrNoZip = errors.New("While installing file '%s'. It's not a zip")
	// ErrFileOpen returns an error with message: 'While opening a file. Trace: +specific error'
	ErrFileOpen = errors.New("While opening a file. Trace: %s")
	// ErrFileCreate returns an error with message: 'While creating a file. Trace: +specific error'
	ErrFileCreate = errors.New("While creating a file. Trace: %s")
	// ErrFileRemove returns an error with message: 'While removing a file. Trace: +specific error'
	ErrFileRemove = errors.New("While removing a file. Trace: %s")
	// ErrFileCopy returns an error with message: 'While copying files. Trace: +specific error'
	ErrFileCopy = errors.New("While copying files. Trace: %s")
	// ErrFileDownload returns an error with message: 'While downloading from +specific url. Trace: +specific error'
	ErrFileDownload = errors.New("While downloading from %s. Trace: %s")
	// ErrDirCreate returns an error with message: 'Unable to create directory on '+root dir'. Trace: +specific error
	ErrDirCreate = errors.New("Unable to create directory on '%s'. Trace: %s")
)
View Source
var (
	// AssetsDirectory the path which iris saves some assets came from the internet ( used in iris control plugin (to download the html,css,js) and for iris command line tool to download the packages)
	AssetsDirectory = ""
)
View Source
var (
	// PathSeparator is the string of os.PathSeparator
	PathSeparator = string(os.PathSeparator)
)

Functions

func BytesToString

func BytesToString(b []byte) string

BytesToString accepts bytes and returns their string presentation instead of string() this method doesn't generate memory allocations, BUT it is not safe to use anywhere because it points this helps on 0 memory allocations

func Command

func Command(command string, a ...string) (output string, err error)

Command executes a command in shell and returns it's output, it's block version

func CopyDir

func CopyDir(source string, dest string) (err error)

CopyDir recursively copies a directory tree, attempting to preserve permissions. Source directory must exist.

Note: the CopyDir function was not written by me, but its working well

func CopyFile

func CopyFile(source string, destination string) error

CopyFile copy a file, accepts full path of the source and full path of destination, if file exists it's overrides it this function doesn't checks for permissions and all that, it returns an error if didn't worked

func Deserialize

func Deserialize(str string, m interface{}) error

Deserialize accepts an encoded string and a data struct which will be filled with the desierialized string using gob decoder

func DeserializeBytes

func DeserializeBytes(b []byte, m interface{}) error

DeserializeBytes converts the bytes to an object using gob decoder

func DirectoryExists

func DirectoryExists(dir string) bool

DirectoryExists returns true if a directory(or file) exists, otherwise false

func DownloadZip

func DownloadZip(zipURL string, newDir string) (string, error)

DownloadZip downloads a zip file returns the downloaded filename and an error.

An indicator is always shown up to the terminal, so the user will know if (a plugin) try to download something

func Exists

func Exists(dir string) bool

Exists returns true if directory||file exists

func FindLower

func FindLower(a, b int) int

FindLower returns the smaller number between a and b

func GetParentDir

func GetParentDir(targetDirectory string) string

GetParentDir returns the parent directory(string) of the passed targetDirectory (string)

func HTMLEscape

func HTMLEscape(s string) string

HTMLEscape returns a string which has no valid html code

func Install

func Install(remoteFileZip string, targetDirectory string) (installedDirectory string, err error)

Install is just the flow of: downloadZip -> unzip -> removeFile(zippedFile) accepts 2 parameters

first parameter is the remote url file zip second parameter is the target directory returns a string(installedDirectory) and an error

(string) installedDirectory is the directory which the zip file had, this is the real installation path, you don't need to know what it's because these things maybe change to the future let's keep it to return the correct path. the installedDirectory is not empty when the installation is succed, the targetDirectory is not already exists and no error happens the installedDirectory is empty when the installation is already done by previous time or an error happens

func MustCommand

func MustCommand(command string, a ...string) (output string)

MustCommand executes a command in shell and returns it's output, it's block version. It panics on an error

func Random

func Random(n int) []byte

Random takes a parameter (int) and returns random slice of byte ex: var randomstrbytes []byte; randomstrbytes = utils.Random(32)

func RandomString

func RandomString(n int) string

RandomString accepts a number(10 for example) and returns a random string using simple but fairly safe random algorithm

func RemoveFile

func RemoveFile(filePath string) error

RemoveFile removes a file or directory and returns an error, if any

func Serialize

func Serialize(m interface{}) (string, error)

Serialize serialize any type to gob bytes and after returns its the base64 encoded string

func SerializeBytes

func SerializeBytes(m interface{}) ([]byte, error)

SerializeBytes serializa bytes using gob encoder and returns them

func StringToBytes

func StringToBytes(s string) []byte

StringToBytes accepts string and returns their []byte presentation instead of byte() this method doesn't generate memory allocations, BUT it is not safe to use anywhere because it points this helps on 0 memory allocations

func TypeByExtension

func TypeByExtension(fullfilename string) (t string)

TypeByExtension returns the MIME type associated with the file extension ext. The extension ext should begin with a leading dot, as in ".html". When ext has no associated type, TypeByExtension returns "".

Extensions are looked up first case-sensitively, then case-insensitively.

The built-in table is small but on unix it is augmented by the local system's mime.types file(s) if available under one or more of these names:

/etc/mime.types
/etc/apache2/mime.types
/etc/apache/mime.types

On Windows, MIME types are extracted from the registry.

Text types have the charset parameter set to "utf-8" by default.

func Unzip

func Unzip(archive string, target string) (string, error)

Unzip extracts a zipped file to the target location

it removes the zipped file after successfully completion returns a string with the path of the created folder (if any) and an error (if any)

func WatchDirectoryChanges

func WatchDirectoryChanges(paths []string, evt func(filename string), logger *logger.Logger)

WatchDirectoryChanges watches a directory and fires the callback with the changed name, receives a logger just to print with red letters any errors, no need for second callback.

Types

type BufferPool

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

BufferPool implements a pool of bytes.Buffers in the form of a bounded channel. Pulled from the github.com/oxtoacart/bpool package (Apache licensed).

func NewBufferPool

func NewBufferPool(size int) (bp *BufferPool)

NewBufferPool creates a new BufferPool bounded to the given size.

func (*BufferPool) Get

func (bp *BufferPool) Get() (b *bytes.Buffer)

Get gets a Buffer from the BufferPool, or creates a new one if none are available in the pool.

func (*BufferPool) Put

func (bp *BufferPool) Put(b *bytes.Buffer)

Put returns the given Buffer to the BufferPool.

type Cmd

type Cmd struct {
	*exec.Cmd
}

Cmd is a custom struch which 'implements' the *exec.Cmd

func CommandBuilder

func CommandBuilder(command string, args ...string) *Cmd

CommandBuilder creates a Cmd object and returns it accepts 2 parameters, one is optionally first parameter is the command (string) second variatic parameter is the argument(s) (slice of string)

the difference from the normal Command function is that you can re-use this Cmd, it doesn't execute until you call its Command function

func (*Cmd) AppendArguments

func (cmd *Cmd) AppendArguments(args ...string) *Cmd

AppendArguments appends the arguments to the exists

func (*Cmd) Arguments

func (cmd *Cmd) Arguments(args ...string) *Cmd

Arguments sets the command line arguments, including the command as Args[0]. If the args parameter is empty or nil, Run uses {Path}.

In typical use, both Path and args are set by calling Command.

func (*Cmd) Directory

func (cmd *Cmd) Directory(workingDirectory string) *Cmd

Directory sets the working directory of the command. If workingDirectory is the empty string, Run runs the command in the calling process's current directory.

func (*Cmd) ResetArguments

func (cmd *Cmd) ResetArguments() *Cmd

ResetArguments resets the arguments

type ITick

type ITick interface {
	OnTick()
}

ITick is the interface which all ticker's listeners must implement

type Ticker

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

Ticker is the timer which is used in cache

func NewTicker

func NewTicker() *Ticker

NewTicker returns a new Ticker

func (*Ticker) OnTick

func (c *Ticker) OnTick(h func())

OnTick add event handlers/ callbacks which are called on each timer's tick

func (*Ticker) Start

func (c *Ticker) Start(duration time.Duration)

Start starts the timer and execute all listener's when tick

func (*Ticker) Stop

func (c *Ticker) Stop()

Stop stops the ticker

Jump to

Keyboard shortcuts

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