util

package
v0.8.0 Latest Latest
Warning

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

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

Documentation

Overview

copied from github.com/elastic/beats

Index

Constants

View Source
const (
	BYTE     = 1.0
	KILOBYTE = 1024 * BYTE
	MEGABYTE = 1024 * KILOBYTE
	GIGABYTE = 1024 * MEGABYTE
	TERABYTE = 1024 * GIGABYTE
)
View Source
const (
	EventMetadataKey = "_event_metadata"
	FieldsKey        = "fields"
	TagsKey          = "tags"
)

Event metadata constants. These keys are used within libbeat to identify metadata stored in an event.

Variables

View Source
var (
	// ErrKeyNotFound indicates that the specified key was not found.
	ErrKeyNotFound = errors.New("key not found")
)

Functions

func AddTags

func AddTags(ms MapStr, tags []string) error

AddTags appends a tag to the tags field of ms. If the tags field does not exist then it will be created. If the tags field exists and is not a []string then an error will be returned. It does not deduplicate the list of tags.

func AutoGetAddress

func AutoGetAddress(addr string) string

* get valid address to listen, if the specify port is not available, auto choose the next one

func ByteSize

func ByteSize(bytes uint64) string

ByteSize returns a human-readable byte string of the form 10M, 12.5K, and so forth. The following units are available:

T: Terabyte
G: Gigabyte
M: Megabyte
K: Kilobyte
B: Byte

The unit that results in the smallest number greater than or equal to 1 is always chosen.

func BytesToUint32

func BytesToUint32(b []byte) (v uint32)

func BytesToUint64

func BytesToUint64(b []byte) (v uint64)

func CheckInstanceLock

func CheckInstanceLock(p string)

func ClearInstanceLock

func ClearInstanceLock()

func ContainStr

func ContainStr(s, substr string) bool

func CopyFile

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

func CreateFile

func CreateFile(dir string, name string) (string, error)

create file

func DeepCopy

func DeepCopy(value interface{}) interface{}

func FileAppendContentWithByte

func FileAppendContentWithByte(file string, content []byte) (int, error)

func FileAppendNewLine

func FileAppendNewLine(file string, content string) (int, error)

func FileAppendNewLineWithByte

func FileAppendNewLineWithByte(file string, content []byte) (int, error)

func FileExists

func FileExists(path string) bool

func FileGetContent

func FileGetContent(file string) ([]byte, error)

get string from text file

func FileMTime

func FileMTime(file string) (int64, error)

get file modified time

func FilePutContent

func FilePutContent(file string, content string) (int, error)

put string to file

func FilePutContentWithByte

func FilePutContentWithByte(file string, content []byte) (int, error)

put string to file

func FileSize

func FileSize(file string) (int64, error)

get file size as how many bytes

func FilterDirName

func FilterDirName(name string) bool

FilterDirName guess the file or directory is or contains Go source files.

func FormatTime

func FormatTime(date *time.Time) string

func FormatUrlForFilter

func FormatUrlForFilter(url []byte) []byte

format url, normalize url

func GetAvailablePort

func GetAvailablePort(ip string, port int) int

* get valid port to listen, if the specify port is not available, auto choose the next one

func GetHost

func GetHost(url string) string

func GetIncrementID

func GetIncrementID(bucket string) string

func GetRootUrl

func GetRootUrl(source *URL) string

parse to get url root

func GetRuntimeErrorMessage

func GetRuntimeErrorMessage(r runtime.Error) string

func GetUUID

func GetUUID() string

func GetUrlPathFolderWithoutFile

func GetUrlPathFolderWithoutFile(url []byte) []byte

func GetValidAddress

func GetValidAddress(addr string) string

* get valid address, input: :8001 -> output: 127.0.0.1:8001

func HttpGet

func HttpGet(resource string) (msg []byte, err error)

func HttpPostJSON

func HttpPostJSON(url string, cookie string, postStr string) []byte

func Invoke

func Invoke(any interface{}, name string, args ...interface{})

func IsExist

func IsExist(path string) bool

IsExist returns whether a file or directory exists.

func IsFile

func IsFile(file string) bool

it returns false when it's a directory or does not exist.

func IsValidTLD

func IsValidTLD(tld string) bool

returns true if tld is a valid tld, eg: com

func JoinPath

func JoinPath(filenames ...string) string

func MergeFields

func MergeFields(ms, fields MapStr, underRoot bool) error

MergeFields merges the top-level keys and values in each source map (it does not perform a deep merge). If the same key exists in both, the value in fields takes precedence. If underRoot is true then the contents of the fields MapStr is merged with the value of the 'fields' key in ms.

An error is returned if underRoot is true and the value of ms.fields is not a MapStr.

func MergeSpace

func MergeSpace(in string) (out string)

Removes all unnecessary whitespaces

func NoWordBreak

func NoWordBreak(in string) string

func Post

func Post(url string, values url.Values) []byte

func RandomPickName

func RandomPickName() string

func ReadAllLines

func ReadAllLines(file string) []string

func Readln

func Readln(r *bufio.Reader) (string, error)

func Rename

func Rename(file string, to string) error

rename file name

func StringToUTF16

func StringToUTF16(s string) []uint16

func SubString

func SubString(str string, begin, length int) (substr string)

func SubStringWithSuffix

func SubStringWithSuffix(str string, length int, suffix string) string

func TestListenPort

func TestListenPort(ip string, port int) bool

func TestPort

func TestPort(port int) bool

func ToBytes

func ToBytes(s string) (uint64, error)

ToBytes parses a string formatted by ByteSize as bytes.

func ToInt

func ToInt(str string) (int, error)

func ToJson

func ToJson(in interface{}, indent bool) string

func ToMegabytes

func ToMegabytes(s string) (uint64, error)

ToMegabytes parses a string formatted by ByteSize as megabytes.

func Uint32toBytes

func Uint32toBytes(b []byte, v uint32)

func Uint64toBytes

func Uint64toBytes(b []byte, v uint64)

func UnicodeIndex

func UnicodeIndex(str, substr string) int
func Unlink(file string) error

delete file

func XSSHandle

func XSSHandle(src string) string

Types

type EventMetadata

type EventMetadata struct {
	Fields          MapStr
	FieldsUnderRoot bool `config:"fields_under_root"`
	Tags            []string
}

EventMetadata contains fields and tags that can be added to an event via configuration.

type FileRepos

type FileRepos []Repository

func (FileRepos) Len

func (r FileRepos) Len() int

func (FileRepos) Less

func (r FileRepos) Less(i, j int) bool

func (FileRepos) Swap

func (r FileRepos) Swap(i, j int)

type MapStr

type MapStr map[string]interface{}

MapStr is a map[string]interface{} wrapper with utility methods for common map operations like converting to JSON.

func MapStrUnion

func MapStrUnion(dict1 MapStr, dict2 MapStr) MapStr

MapStrUnion creates a new MapStr containing the union of the key-value pairs of the two maps. If the same key is present in both, the key-value pairs from dict2 overwrite the ones from dict1.

func (MapStr) Clone

func (m MapStr) Clone() MapStr

Clone returns a copy of the MapStr. It recursively makes copies of inner maps.

func (MapStr) CopyFieldsTo

func (m MapStr) CopyFieldsTo(to MapStr, key string) error

CopyFieldsTo copies the field specified by key to the given map. It will overwrite the key if it exists. An error is returned if the key does not exist in the source map.

func (MapStr) Delete

func (m MapStr) Delete(key string) error

Delete deletes the given key from the map.

func (MapStr) GetValue

func (m MapStr) GetValue(key string) (interface{}, error)

GetValue gets a value from the map. If the key does not exist then an error is returned.

func (MapStr) HasKey

func (m MapStr) HasKey(key string) (bool, error)

HasKey returns true if the key exist. If an error occurs then false is returned with a non-nil error.

func (MapStr) Put

func (m MapStr) Put(key string, value interface{}) (interface{}, error)

Put associates the specified value with the specified key. If the map previously contained a mapping for the key, the old value is replaced and returned. The key can be expressed in dot-notation (e.g. x.y) to put a value into a nested map.

If you need insert keys containing dots then you must use bracket notation to insert values (e.g. m[key] = value).

func (MapStr) String

func (m MapStr) String() string

String returns the MapStr as JSON.

func (MapStr) StringToPrint

func (m MapStr) StringToPrint() string

StringToPrint returns the MapStr as pretty JSON.

func (MapStr) Update

func (m MapStr) Update(d MapStr)

Update copies all the key-value pairs from d to this map. If the key already exists then it is overwritten. This method does not merge nested maps.

type Repository

type Repository struct {
	Name     string
	FileTime int64
}

type Result

type Result struct {
	Host       string
	Url        string
	Headers    map[string][]string
	Body       []byte
	StatusCode int
	Size       uint64
}

func HttpGetWithCookie

func HttpGetWithCookie(resource string, cookie string, proxy string) (result *Result, err error)

type StringSet

type StringSet map[string]struct{}

func MakeStringSet

func MakeStringSet(strings ...string) StringSet

func (StringSet) Add

func (set StringSet) Add(s string)

func (StringSet) Count

func (set StringSet) Count() int

func (StringSet) Del

func (set StringSet) Del(s string)

func (StringSet) Has

func (set StringSet) Has(s string) (exists bool)

Directories

Path Synopsis
copied from github.com/elastic/beats
copied from github.com/elastic/beats

Jump to

Keyboard shortcuts

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