utils

package
v0.0.0-...-f4c0d56 Latest Latest
Warning

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

Go to latest
Published: May 27, 2023 License: MIT Imports: 39 Imported by: 1

Documentation

Index

Constants

View Source
const (
	NONE = iota
	UPPER
	DOWN
)
View Source
const (
	BlankConf = `workdir: ""`
	ConfFile  = "conf.yaml"
)
View Source
const (
	CF_DICT = iota
	CF_ARR
	CF_STR
	CF_NUM
	CF_BOOL
	CF_DATA
	CF_UID
	CF_REAL
	CF_NONE
)
View Source
const (
	UTF8 = Charset("UTF-8")
	// GB18030 support Chinese encoding
	GB18030 = Charset("GB18030")

	BigEndian    = true
	LittleEndian = false

	ByteSize    = 1
	Int16Size   = 2 * ByteSize
	Int32Size   = 4 * ByteSize
	Int64Size   = 8 * ByteSize
	Float32Size = 4 * ByteSize
	Float64Size = 8 * ByteSize

	ByteType = iota
	IntType
	Int16Type
	Int32Type
	Int64Type
	Float32Type
	Float64Type
	StringType
)
View Source
const DefaultLogPath = "/log"

Variables

View Source
var (
	JsonTrue  = NewJsonBool(true)
	JsonFalse = NewJsonBool(false)
	JsonNil   = NewJsonNull()
)

Functions

func ConvertByte2String

func ConvertByte2String(byte []byte, charset Charset) string

ConvertByte2String return encoding string, which main effect to support unicode bytes, for example Chinese.

func DecodeAESWithKey

func DecodeAESWithKey(key, str string) string

func EncodeAESWithKey

func EncodeAESWithKey(key, str string) string

func EqualAES

func EqualAES(key, raw, hash string) bool

func Exec

func Exec(arg ...string) ([]byte, error)

Exec automatically fit in os enviroment to execute command. windows 10+ -> powershell, others -> cmd; linux, darwin -> /bin/bash

func ExecStr

func ExecStr(args string) ([]byte, error)

ExecStr automatically split string to string arrary, then call Exec to execute

func FetchFile

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

FetchFile fetch file from remote source to local destination

func Filename

func Filename(fullpath string) string

Filename returns the last element name of fullpath.

func FirstLower

func FirstLower(s string) string

FirstLower return string in which the first letter is lowercase.

func FirstUpper

func FirstUpper(s string) string

FirstUpper return string in which the first letter is uppercase.

func FmtTimestamp

func FmtTimestamp(ts int64) string

FmtTimestamp return a time string whom format is 2006-01-02

func InitLogger

func InitLogger(conf LoggerOpt) error

func InitLoggerWithDefault

func InitLoggerWithDefault() error

func IsExist

func IsExist(path string) bool

func IsURL

func IsURL(urlStr string) bool

IsURL determine whether urlStr is a URL

func JsonStr

func JsonStr(v JsonValue) string

JsonStr return json string according to JsonValue.

func MD5

func MD5(str string) string

MD5 return the MD5 checksum of the data

func Mkdirs

func Mkdirs(path string) error

Mkdirs recurse to create path

func MoveFile

func MoveFile(src, dst string) error

MoveFile move file from src to dst, like mv or move command

func NewEnv

func NewEnv[T any](opt EnvOpt[T]) *T

NewEnv to init BaseEnv and return Payload pointer which will be initialized from specify configure file in opt.

func NowTimestamp

func NowTimestamp() int64

NowTimestamp returm current timestamp

func NowTimestampByString

func NowTimestampByString() string

NowTimestamp returm current timestamp string

func OpenConf

func OpenConf(name, _type, dir string, opts ...viper.Option) *viper.Viper

OpenConf unmarshal file which located in disk to memory according to name, type, dir

func OpenConfFromPath

func OpenConfFromPath(path string, opts ...viper.Option) *viper.Viper

OpenConfFromPath unmarshal file which located in disk to memory according to path

func PathIsExist

func PathIsExist(path string) (bool, error)

PathIsExist judge whether path exist. If exist, return true.

func Prinf

func Prinf(opt PrintfOpt, title []string, rows [][]string)

Printf represent title and rows with tidy

func RandString

func RandString(n int) string

RandString return string of length n

func RandomPort

func RandomPort() (int, error)

RandomPort return an available port with random.

func ReadLineFromFile

func ReadLineFromFile(file string, filter func(string) string) ([]byte, error)

ReadStraemFromFile return data to be filter from specify file

func ReadLineFromString

func ReadLineFromString(str string, filter func(string) string) ([]byte, error)

ReadStraemFromFile return data to be filter from string

func ReadStraemFromFile

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

ReadStraemFromFile return total data from specify file

func SHA256

func SHA256(str string) string

SHA256 return the SHA256 checksum of the data

func SafeBatchMkdirs

func SafeBatchMkdirs(dirs []string) error

SafeBatchMkdirs recurse to create dirs when path isn't exist

func SafeMkdirs

func SafeMkdirs(path string) error

SafeMkdirs recurse to create path when path isn't exist

func SafeWriteFile

func SafeWriteFile(file string, data []byte) error

WriteFile write data or create file to write data according to file when file isn't exist

func SetBytesMode

func SetBytesMode(_type bool)

SetBytesMode set convert mode: BigEndian or LittleEndian

func SimilarText

func SimilarText(first, second string, percent *float64) int

Powered by github.com/syyongx/php2go

func TrimMultiSpace

func TrimMultiSpace(s string) string

TrimMultiSpace trim redundant space

func Unzip

func Unzip(src, dst string) error

Unzip unzip zip of source to specify path

func WriteFile

func WriteFile(file string, data []byte) error

WriteFile write data or create file to write data according to file

func Zip

func Zip(zipPath string, paths ...string) error

Zip to compress zip of source to specify path

Types

type BaseEnv

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

func GetEnv

func GetEnv() *BaseEnv

GetEnv returns global BaseEnv pointer

func NewBaseEnv

func NewBaseEnv() *BaseEnv

func (*BaseEnv) Commit

func (env *BaseEnv) Commit(key string, value any) *BaseEnv

Commit push key to configure file. It'll be write in disk throught Write.

func (*BaseEnv) Dump

func (env *BaseEnv) Dump()

Dump reprensent env on console

func (*BaseEnv) Read

func (env *BaseEnv) Read(path string)

func (*BaseEnv) ReadWithBind

func (env *BaseEnv) ReadWithBind(path string, payload any)

func (*BaseEnv) Workdir

func (env *BaseEnv) Workdir() string

func (*BaseEnv) Write

func (env *BaseEnv) Write()

Write to persist configure file in disk

type ByteWalk

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

ByteWalk can extract bytes by bit.

func NewByteWalk

func NewByteWalk(buf []byte) *ByteWalk

func (*ByteWalk) Buf

func (bw *ByteWalk) Buf() []byte

func (*ByteWalk) Cursor

func (bw *ByteWalk) Cursor() int

func (*ByteWalk) IsEnd

func (bw *ByteWalk) IsEnd() bool

IsEnd judge whether cursor read EOF

func (*ByteWalk) Next

func (bw *ByteWalk) Next(step int) ([]byte, error)

Next move ByteWalk cursor and return bytes from last cursor to current cursor.

func (*ByteWalk) Size

func (bw *ByteWalk) Size() int

Size return buffer length

type BytesDecode

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

BytesDecode is used to transform bytes to basic type.

func NewBytesDecode

func NewBytesDecode(_type bool) BytesDecode

func (BytesDecode) BytesToFloat32

func (decode BytesDecode) BytesToFloat32(buf []byte) float32

BytesToFloat32 return float32 from bytes

func (BytesDecode) BytesToFloat64

func (decode BytesDecode) BytesToFloat64(buf []byte) float64

BytesToFloat64 return float64 from bytes

func (BytesDecode) BytesToInt16

func (decode BytesDecode) BytesToInt16(buf []byte) int16

BytesToInt16 return int16 from bytes

func (BytesDecode) BytesToInt32

func (decode BytesDecode) BytesToInt32(buf []byte) int32

BytesToInt32 return int32 from bytes

func (BytesDecode) BytesToInt64

func (decode BytesDecode) BytesToInt64(buf []byte) int64

BytesToInt64 return int64 from bytes

type BytesEncode

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

BytesEncode is used to transform basic type to bytes.

func NewBytesEncode

func NewBytesEncode(_type bool) BytesEncode

func (BytesEncode) Float32ToBytes

func (encode BytesEncode) Float32ToBytes(f float32) []byte

Float32ToBytes return bytes from float32

func (BytesEncode) Float64ToBytes

func (encode BytesEncode) Float64ToBytes(f float64) []byte

Float64ToBytes return bytes from float64

func (BytesEncode) Int16ToBytes

func (encode BytesEncode) Int16ToBytes(i int16) []byte

Int16ToBytes return bytes from int16

func (BytesEncode) Int32ToBytes

func (encode BytesEncode) Int32ToBytes(i int32) []byte

Int32ToBytes return bytes from int32

func (BytesEncode) Int64ToBytes

func (encode BytesEncode) Int64ToBytes(i int64) []byte

Int64ToBytes return bytes from int64

type BytesTransfomer

type BytesTransfomer struct {
	BytesEncode
	BytesDecode
}

BytesTransfomer is used to convert between basic type and bytes

var ByteTransfomer BytesTransfomer

ByteTransfomer is used to transform basic type to bytes, which is BigEndian convert in default. You can use SetBytesMode to switch LittleEndian.

func (BytesTransfomer) AutoToBytes

func (transfomer BytesTransfomer) AutoToBytes(data any) []byte

AutoToBytes return bytes from referred type automatically.

func (BytesTransfomer) AutoToType

func (transfomer BytesTransfomer) AutoToType(raw []byte, _type int) any

AutoToType return any whom type is referred already from bytes.

type CFArray

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

func (CFArray) Copy

func (c CFArray) Copy() CFArray

func (CFArray) Foreach

func (c CFArray) Foreach(cb func(idx int, v CFValue) bool)

func (CFArray) GetByIdx

func (c CFArray) GetByIdx(idx int) CFValue

func (CFArray) SetByIdx

func (c CFArray) SetByIdx(idx int, v any) error

func (CFArray) Type

func (CFArray) Type() uint8

type CFBool

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

func (CFBool) Type

func (CFBool) Type() uint8

type CFData

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

func (CFData) Type

func (CFData) Type() uint8

type CFDictionary

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

func (CFDictionary) Foreach

func (c CFDictionary) Foreach(cb func(k string, v CFValue) bool)

func (CFDictionary) GetCFValue

func (c CFDictionary) GetCFValue(field string) CFValue

func (CFDictionary) Set

func (c CFDictionary) Set(field string, v any)

func (CFDictionary) Type

func (CFDictionary) Type() uint8

type CFNone

type CFNone struct{}

func (CFNone) Type

func (CFNone) Type() uint8

type CFNumber

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

func (CFNumber) Type

func (CFNumber) Type() uint8

type CFReal

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

func (CFReal) Type

func (CFReal) Type() uint8

type CFString

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

func (CFString) Type

func (CFString) Type() uint8

type CFUID

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

func (CFUID) Type

func (CFUID) Type() uint8

type CFValue

type CFValue interface {
	Type() uint8
}

func GetPlistValue

func GetPlistValue(v CFValue, name string) CFValue

type Charset

type Charset string

type CheckedVersion

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

func NewCheckedVersion

func NewCheckedVersion(version string) *CheckedVersion

func (*CheckedVersion) Compare

func (want *CheckedVersion) Compare(got *CheckedVersion) bool

type EnvOpt

type EnvOpt[T any] struct {
	Payload   T
	Workdir   string
	Subdirs   []string
	BlankConf string
}

type EnvVar

type EnvVar interface {
	// SetPath set operate target:
	// windows: sys or user, it's required in windows.
	// posix: /etc/enviroment, this only is empty method.
	SetPath(string) error

	// set global enviroment variable
	Set(string, any) error
	// set global enviroment variable when key isn't exist
	SafeSet(string, any) error

	// unset (delete) global enviroment variable
	Unset(string) error

	// set local enviroment variable
	SetL(string, string) error
	// set local enviroment variable when key isn't exist
	SafeSetL(string, string) error

	// export current enviroment string into specify file
	Export(string) error
	// load enviroment string to be export from disk
	Load(EnvVarLoadOpt) error

	// Print current enviroment variable
	Print()
}

EnvVar is an interface to abstract different os enviroment variable

type EnvVarLoadOpt

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

type JsonArray

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

func NewJsonArray

func NewJsonArray(v []JsonValue) *JsonArray

func (*JsonArray) Value

func (obj *JsonArray) Value() string

type JsonBool

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

func NewJsonBool

func NewJsonBool(b bool) JsonBool

func (JsonBool) Value

func (obj JsonBool) Value() string

type JsonNull

type JsonNull struct{}

func NewJsonNull

func NewJsonNull() JsonNull

func (JsonNull) Value

func (JsonNull) Value() string

type JsonNumber

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

func NewJsonNumber

func NewJsonNumber(v int64) JsonNumber

func (JsonNumber) Value

func (obj JsonNumber) Value() string

type JsonObject

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

func NewJsonObject

func NewJsonObject(v map[string]JsonValue) *JsonObject

func (*JsonObject) Value

func (obj *JsonObject) Value() string

type JsonString

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

func NewJsonString

func NewJsonString(v string) JsonString

func (JsonString) Value

func (obj JsonString) Value() string

type JsonValue

type JsonValue interface {
	Value() string
}

JsonValue is an interface to abstract object of json type.

type LoggerOpt

type LoggerOpt struct {
	Level          string
	Format         string
	Path           string
	FileName       string
	FileMaxSize    int
	FileMaxBackups int
	MaxAge         int
	Compress       bool
	Stdout         bool
}

type MetaArr

type MetaArr []any

type MetaMap

type MetaMap map[string]any

type MetaTable

type MetaTable interface {
	// SetValue set MetaTable's value and have two different rules:
	// regedit(windows): MetaValue ✔ MetaMap ✔ MetaArr x (MetaArr not work);
	// plist(darwin, posix): MetaValue ✔ MetaMap ✔ MetaArr ✔
	SetValue(MetaValue)
	// SafeSetValue set MetaTable's value when key isn't exist and have two different rules:
	// regedit(windows): MetaValue ✔ MetaMap ✔ MetaArr x (MetaArr not work);
	// plist(darwin, posix): MetaValue ✔ MetaMap ✔ MetaArr ✔
	SafeSetValue(MetaValue)
	// GetValue return MetaValue according to key
	GetValue(string) MetaValue
	// CreateSubTable have two different effect:
	// regedit(windows): create sub key and written file depond on its feture.
	// plist(darwin, posix): create sub element of map or array, but not be saved automatically
	// comparing regedit. It's required to call Write method save manually.
	CreateSubTable(string) MetaTable
	// Write to persist MetaValue in disk.
	// note: The regedit (windows) is written when it is created,
	// and this method is only valid for plist (darwin, posix).
	// The regedit is just an empty method.
	Write() error
	// Backup save a copy which could restore MetaValue
	Backup() error
	// Close to free MetaTable memory
	Close()
}

MetaTable is an interface to abstract plist(darwin) and regedit(windows). In other posix os, uniform use of plist as MetaTable interface implement

func CreateMetaTable

func CreateMetaTable(path string) (MetaTable, error)

CreateMetaTable to create or open MetaTable

func OpenMetaTable

func OpenMetaTable(path string) (MetaTable, error)

CreateMetaTable to open MetaTable

type MetaValue

type MetaValue any

type Platform

type Platform struct {
	OS   string
	Ver  string
	Arch string
}
var CurPlatform Platform

type PlistFile

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

PlistFile manage plist, which serves for PosixMetaTable

func CreatePlistFile

func CreatePlistFile(file string) (*PlistFile, error)

CreatePlistFile create specify plist when plist isn't exist

func OpenPlistFile

func OpenPlistFile(file string) (*PlistFile, error)

OpenPlistFile open specify plist

func (*PlistFile) Append

func (pf *PlistFile) Append(value any) error

func (*PlistFile) Backup

func (pf *PlistFile) Backup() error

func (*PlistFile) Free

func (pf *PlistFile) Free()

func (*PlistFile) GetArr

func (pf *PlistFile) GetArr() CFArray

GetDict returns CFArray. If not exist in plist, it'll return a empty CFArray.

func (*PlistFile) GetArrByField

func (pf *PlistFile) GetArrByField(field string) CFArray

GetArrByField return CFArray according to field

func (*PlistFile) GetBaseValue

func (pf *PlistFile) GetBaseValue() CFValue

GetBaseValue returns CFValue. If not exist in plist, it'll return CFNone.

func (*PlistFile) GetDict

func (pf *PlistFile) GetDict() CFDictionary

GetDict returns CFDictionary. If not exist in plist, it'll return a empty CFDictionary.

func (*PlistFile) GetValue

func (pf *PlistFile) GetValue(key string) CFValue

GetValue returns CFValue according to key

func (*PlistFile) Len

func (pf *PlistFile) Len() int

Len return plist length. Length of array or map depend on elements amount. For other type that include string, date, number and so on, it ever returns 1.

func (*PlistFile) SafeSet

func (pf *PlistFile) SafeSet(key string, value any) error

func (*PlistFile) Set

func (pf *PlistFile) Set(value any)

Set set root element of plist

func (*PlistFile) SetByField

func (pf *PlistFile) SetByField(field string, value CFValue) error

SetByField set element in sprcify field, which support recurse and meant that you can set a.b.c field.

func (*PlistFile) SetByIdx

func (pf *PlistFile) SetByIdx(idx int, value any) error

SetByIdx set element in specify position if position is available. For map, index will be convert string as key to set.

func (*PlistFile) Write

func (pf *PlistFile) Write() error

type Plugin

type Plugin interface {
	// Func return PluginFunc which is an abstract function to be exported dynamic library
	// according to funcName. You can use PluginFunc to call function from dynamic library.
	Func(string) (PluginFunc, error)
}

Plugin is an interface to abstract dynamic library (dll, dylib, so)

func NewPlugin

func NewPlugin(path string) (Plugin, error)

type PluginFunc

type PluginFunc interface {
	// Call return excuted result from dynamic library
	Call(...uintptr) (uintptr, error)
}

PluginFunc is an interface to abstract function to be exported dynamic library

type PosixEnvVar

type PosixEnvVar struct{}

func NewEnvVar

func NewEnvVar() *PosixEnvVar

func (*PosixEnvVar) Export

func (env *PosixEnvVar) Export(file string) error

export current enviroment string into specify file

func (*PosixEnvVar) Load

func (env *PosixEnvVar) Load(opt EnvVarLoadOpt) error

load exported env from disk

func (*PosixEnvVar) Print

func (env *PosixEnvVar) Print()

Print enviroment variable

func (*PosixEnvVar) SafeSet

func (env *PosixEnvVar) SafeSet(k string, v any) error

set global enviroment variable when key isn't exist

func (*PosixEnvVar) SafeSetL

func (env *PosixEnvVar) SafeSetL(k, v string) error

set local enviroment variable when key isn't exist

func (*PosixEnvVar) Set

func (env *PosixEnvVar) Set(k string, v any) error

set global enviroment variable

func (*PosixEnvVar) SetL

func (env *PosixEnvVar) SetL(k, v string) error

set local enviroment variable

func (*PosixEnvVar) SetPath

func (env *PosixEnvVar) SetPath(path string) error

SetPath set operate target: posix: /etc/enviroment, this only is empty method.

func (*PosixEnvVar) Unset

func (env *PosixEnvVar) Unset(k string) error

unset (delete) global enviroment variable

type PosixMetaTable

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

func (*PosixMetaTable) Backup

func (tbl *PosixMetaTable) Backup() error

Backup save a copy which could restore MetaValue

func (*PosixMetaTable) Close

func (tbl *PosixMetaTable) Close()

Close to free MetaTable memory

func (*PosixMetaTable) CreateSubTable

func (tbl *PosixMetaTable) CreateSubTable(name string) MetaTable

CreateSubTable create sub element of map or array, but not be saved automatically

func (*PosixMetaTable) GetValue

func (tbl *PosixMetaTable) GetValue(v string) MetaValue

GetValue return MetaValue according to key

func (*PosixMetaTable) SafeSetValue

func (tbl *PosixMetaTable) SafeSetValue(v MetaValue)

SafeSetValue set MetaTable's value when key isn't exist, plist(darwin, posix): MetaValue ✔ MetaMap ✔ MetaArr ✔

func (*PosixMetaTable) SetValue

func (tbl *PosixMetaTable) SetValue(v MetaValue)

SetValue set MetaTable's value, plist(darwin, posix): MetaValue ✔ MetaMap ✔ MetaArr ✔

func (*PosixMetaTable) Write

func (tbl *PosixMetaTable) Write() error

Write to persist MetaValue in disk. note: The regedit (windows) is written when it is created, and this method is only valid for plist (darwin, posix). The regedit is just an empty method.

type PosixPlugin

type PosixPlugin struct {
	*plugin.Plugin
}

func (*PosixPlugin) Func

func (pp *PosixPlugin) Func(name string) (PluginFunc, error)

Func return PluginFunc which is an abstract function to be exported dynamic library according to funcName. You can use PluginFunc to call function from dynamic library.

type PosixPluginFunc

type PosixPluginFunc struct {
	plugin.Symbol
}

func (*PosixPluginFunc) Call

func (ppf *PosixPluginFunc) Call(params ...uintptr) (uintptr, error)

Call return excuted result from dynamic library

type PrintfOpt

type PrintfOpt struct {
	MaxLen int
}

type ReflectDict

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

ReflectDict is a type dictionary and manage all type by map.

func NewReflectDict

func NewReflectDict() *ReflectDict

func (*ReflectDict) Load

func (dict *ReflectDict) Load(datas ...any)

Load to register type into ReflectDict

func (*ReflectDict) New

func (dict *ReflectDict) New(name string, value any) any

New create a variable according to type name to be specified

type ReflectObject

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

ReflectObject is an assembly on reflect.Value and relfect.Type.

func NewReflectObject

func NewReflectObject(data any) *ReflectObject

func (*ReflectObject) DumpFields

func (re *ReflectObject) DumpFields()

DumpFields print fields and values for current struct

func (*ReflectObject) Fields

func (re *ReflectObject) Fields() map[string]reflect.Value

Fields return all fields for current type

func (*ReflectObject) Get

func (re *ReflectObject) Get(field string) reflect.Value

Get return value of spcify field

func (*ReflectObject) Raw

func (re *ReflectObject) Raw() any

Raw return raw data

func (*ReflectObject) Set

func (re *ReflectObject) Set(field string, value any) error

Set can ignore case to set field value

type TypeMeta

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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