Utility

package module
v0.0.0-...-97aa9a0 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2023 License: Apache-2.0 Imports: 49 Imported by: 52

README

Utility

Documentation

Index

Constants

View Source
const (
	/*
		A JavaScript identifier must start with a letter, underscore (_), or dollar sign ($);
		subsequent characters can also be digits (0-9).
		Because JavaScript is case sensitive, letters include the characters "A"
		through "Z" (uppercase) and the characters "a" through "z" (lowercase).
	*/
	UUID_PATTERN               = "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$"
	VARIABLE_NAME_PATTERN      = "^[a-zA-Z_$][a-zA-Z_$0-9]*$"
	PACKAGE_NAME_PATTERN       = "^[a-zA-Z_$][a-zA-Z_$0-9]*(\\.[a-zA-Z_$][a-zA-Z_$0-9]*)+(\\.[a-zA-Z_$][a-zA-Z_$0-9]*)*$"
	ENTITY_NAME_PATTERN        = "" /* 164-byte string literal not displayed */
	ISO_8601_TIME_PATTERN      = `` /* 143-byte string literal not displayed */
	ISO_8601_DATE_PATTERN      = `^(?P<year>-?(?:[1-9][0-9]*)?[0-9]{4})-(?P<month>1[0-2]|0[1-9])-(?P<day>3[01]|0[1-9]|[12][0-9])$`
	ISO_8601_DATE_TIME_PATTERN = `` /* 237-byte string literal not displayed */
	URI_BASE_64_PATTERN        = `(data:)(\\w+)(\\/)(\\w+)(;base64)`
	STD_BASE_64_PATTERN        = `^(?:[A-Za-z0-9+/]{4})+(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$`
)

Variables

This section is empty.

Functions

func BytesToString

func BytesToString(b []byte) string

func CallFunction

func CallFunction(name string, params ...interface{}) (result []reflect.Value, err error)

func CallMethod

func CallMethod(i interface{}, methodName string, params []interface{}) (interface{}, interface{})

*

  • Make use of reflexion to call the method specified in the message.

func CompressDir

func CompressDir(src string, buf io.Writer) (int, error)

*

  • Here I will made use of tar to compress the file.

func Contains

func Contains(slice []string, item string) bool

* Utility function *

func Copy

func Copy(src, dst string) error

Copy the src file to dst. Any existing file will be overwritten and will not copy file attributes.

func CopyDir

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

*

  • I made use of cp instead of go here...
  • Be sure the command exist in the computer who run that command.

func CopyFile

func CopyFile(source string, dest string) (err error)
func CopySymLink(source, dest string) error

func CreateDataChecksum

func CreateDataChecksum(data []byte) string

func CreateDirIfNotExist

func CreateDirIfNotExist(dir string) error

func CreateFileChecksum

func CreateFileChecksum(path string) string

func CreateIfNotExists

func CreateIfNotExists(dir string, perm os.FileMode) error

func CreateSha1Key

func CreateSha1Key(data []byte) string

func CreateThumbnail

func CreateThumbnail(path string, thumbnailMaxHeight int, thumbnailMaxWidth int) (string, error)

*

  • Create a thumbnail...

func DateTimeFromString

func DateTimeFromString(str string, layout string) (time.Time, error)

func DecodeISO8859_1

func DecodeISO8859_1(val string) (string, error)

ISO8859_1

func DecodeISO8859_10

func DecodeISO8859_10(val string) (string, error)

ISO8859_10

func DecodeISO8859_13

func DecodeISO8859_13(val string) (string, error)

ISO8859_13

func DecodeISO8859_14

func DecodeISO8859_14(val string) (string, error)

ISO8859_14

func DecodeISO8859_15

func DecodeISO8859_15(val string) (string, error)

ISO8859_15

func DecodeISO8859_16

func DecodeISO8859_16(val string) (string, error)

ISO8859_16

func DecodeISO8859_2

func DecodeISO8859_2(val string) (string, error)

ISO8859_2

func DecodeISO8859_3

func DecodeISO8859_3(val string) (string, error)

ISO8859_3

func DecodeISO8859_4

func DecodeISO8859_4(val string) (string, error)

ISO8859_4

func DecodeISO8859_5

func DecodeISO8859_5(val string) (string, error)

ISO8859_5

func DecodeISO8859_6

func DecodeISO8859_6(val string) (string, error)

ISO8859_6

func DecodeISO8859_7

func DecodeISO8859_7(val string) (string, error)

ISO8859_7

func DecodeISO8859_8

func DecodeISO8859_8(val string) (string, error)

ISO8859_8

func DecodeISO8859_9

func DecodeISO8859_9(val string) (string, error)

ISO8859_9

func DecodeKOI8R

func DecodeKOI8R(val string) (string, error)

KOI8R

func DecodeKOI8U

func DecodeKOI8U(val string) (string, error)

KOI8U

func DecodeWindows1250

func DecodeWindows1250(val string) (string, error)

Windows1250

func DecodeWindows1251

func DecodeWindows1251(val string) (string, error)

Windows1251

func DecodeWindows1252

func DecodeWindows1252(val string) (string, error)

Windows1252

func DecodeWindows1253

func DecodeWindows1253(val string) (string, error)

Windows1253

func DecodeWindows1254

func DecodeWindows1254(val string) (string, error)

Windows1254

func DecodeWindows1255

func DecodeWindows1255(val string) (string, error)

Windows1255

func DecodeWindows1256

func DecodeWindows1256(val string) (string, error)

Windows1256

func DecodeWindows1257

func DecodeWindows1257(val string) (string, error)

Windows1257

func DecodeWindows1258

func DecodeWindows1258(val string) (string, error)

Windows1258

func DomainHasIp

func DomainHasIp(domain string, ip string) bool

func DownloadFile

func DownloadFile(URL, fileName string) error

*

  • Download an image from an url...

func Exists

func Exists(filePath string) bool

Exists reports whether the named file or directory exists.

func ExtractTarGz

func ExtractTarGz(r io.Reader) (string, error)

*

  • Extract a tar gz file and return the path where the data is...

func FileLine

func FileLine() string

func FindFileByName

func FindFileByName(path string, name string) ([]string, error)

func FromBytes

func FromBytes(data []byte, typeName string) (interface{}, error)

*

  • Read entity from byte array.

func FunctionName

func FunctionName() string

func GenerateUUID

func GenerateUUID(val string) string

Create a MD5 hash value with UUID format.

func GetChecksum

func GetChecksum(values interface{}) string

*

  • Recursive function that return the checksum value.

func GetEnvironmentVariable

func GetEnvironmentVariable(key string) (string, error)

func GetExecName

func GetExecName(path string) string

func GetFileContentType

func GetFileContentType(out *os.File) (string, error)

*

  • Get the mime type of a file.

func GetFilePathsByExtension

func GetFilePathsByExtension(path string, extension string) []string

*

  • Keep the parent node

func GetFunction

func GetFunction(name string) interface{}

*

  • Get a function from it name.

func GetInstanceOf

func GetInstanceOf(typeName string) interface{}

*

  • Return a new instance of a given typename

func GetIpv4

func GetIpv4(address string) (string, error)

Get the ip from an address

func GetMD5Hash

func GetMD5Hash(text string) string

func GetPrimaryIPAddress

func GetPrimaryIPAddress() (string, error)

*

  • Return the primary ip address of the computer.

func GetProcessIdsByName

func GetProcessIdsByName(name string) ([]int, error)

*

  • Get the list of process id by it name.

func GetProcessRunningStatus

func GetProcessRunningStatus(pid int) (*os.Process, error)

check if the process is actually running However, on Unix systems, os.FindProcess always succeeds and returns a Process for the given pid...regardless of whether the process exists or not.

func GetProperty

func GetProperty(i interface{}, field string) interface{}

func GetTypeOf

func GetTypeOf(typeName string) reflect.Type

func GetVideoDuration

func GetVideoDuration(path string) int

func GetWindowsEnvironmentVariable

func GetWindowsEnvironmentVariable(key string) (string, error)

func InitializeArray

func InitializeArray(data []interface{}) (reflect.Value, error)

*

  • Initialyse an array of values other than structure...

func InitializeBaseTypeValue

func InitializeBaseTypeValue(t reflect.Type, value interface{}) reflect.Value

*

  • Initialyse base type value.

func InitializeStructure

func InitializeStructure(data map[string]interface{}, setEntity func(interface{})) (reflect.Value, error)

*

  • Initialyse a single object from it value.

func InitializeStructureFieldArrayValue

func InitializeStructureFieldArrayValue(slice reflect.Value, fieldName string, fieldType reflect.Type, values reflect.Value, setEntity func(interface{}))

Return an initialyse field value.

func InitializeStructures

func InitializeStructures(data []interface{}, typeName string, setEntity func(interface{})) (reflect.Value, error)

*

  • Initialyse an array of structures, return it as interface (array of the actual
  • objects)

func InsertBoolAt

func InsertBoolAt(pos int, val bool, arr *[]bool)

func InsertInt64At

func InsertInt64At(pos int, val int64, arr *[]int64)

func InsertIntAt

func InsertIntAt(pos int, val int, arr *[]int)

func InsertStringAt

func InsertStringAt(pos int, str string, arr *[]string)

*

  • Insert a new string at a given position.

func IsBool

func IsBool(value interface{}) bool

func IsCreditCardNumber

func IsCreditCardNumber(number string) bool

func IsEmail

func IsEmail(email string) bool

func IsEmpty

func IsEmpty(name string) (bool, error)

func IsLocal

func IsLocal(hostname string) bool

func IsNumeric

func IsNumeric(value interface{}) bool

func IsPhoneNumber

func IsPhoneNumber(number string) bool

func IsStdBase64

func IsStdBase64(str string) bool

Determine if a string is a valid base64 string

func IsUriBase64

func IsUriBase64(str string) bool

func IsUuid

func IsUuid(uuidStr string) bool

Determine if a string is a UUID or not, a uuid is compose of a TypeName%UUID

func IsValidEntityReferenceName

func IsValidEntityReferenceName(entityReferenceName string) bool

Determine if a string is a valid entity reference name

func IsValidPackageName

func IsValidPackageName(packageName string) bool

Determine if a string is a valid package name

func IsValidVariableName

func IsValidVariableName(variableName string) bool

Determine if a string is a valid variable name

func JsonErrorStr

func JsonErrorStr(functionName string, fileLine string, err error) string

func KillProcessByName

func KillProcessByName(name string) error

*

  • Kill a process with a given name.

func Less

func Less(val0 interface{}, val1 interface{}) bool

func Log

func Log(infos ...interface{})

func MakeInstance

func MakeInstance(typeName string, data map[string]interface{}, setEntity func(interface{})) reflect.Value

*

  • Create an instance of the type with it name.

func MakeTimestamp

func MakeTimestamp() int64

func MatchISO8601_Date

func MatchISO8601_Date(str string) (*time.Time, error)

func MatchISO8601_DateTime

func MatchISO8601_DateTime(str string) (*time.Time, error)

*

  • Parse and return a time object from a 8601 iso string, the time zone is
  • the UTC.

func MatchISO8601_Time

func MatchISO8601_Time(str string) (*time.Time, error)

*

  • Parse and return a time object from a 8601 iso string, the time zone is
  • the UTC.

func Move

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

*

  • I made use of mv instead of go here...
  • Be sure the command exist in the computer who run that command.

func MoveFile

func MoveFile(source, destination string) (err error)

func MyIP

func MyIP() string

Return the external ip.

func MyIPv6

func MyIPv6() (string, error)

func MyLocalIP

func MyLocalIP(mac string) (string, error)

*

  • Return the ip address with the given mac address.

func MyMacAddr

func MyMacAddr(ip string) (string, error)

getMacAddr gets the MAC hardware address of the host machine

func OpenBrowser

func OpenBrowser(url string)

* Open the browser at given url *

func PidExists

func PidExists(pid int) (bool, error)

func Ping

func Ping(domain string) error

func PrettyPrint

func PrettyPrint(b []byte) ([]byte, error)

Pretty print the result.

func RandomUUID

func RandomUUID() string

Create a random uuid value.

func ReadDir

func ReadDir(dirname string) ([]os.FileInfo, error)

func ReadMetadata

func ReadMetadata(path string) (map[string]interface{}, error)

*

  • Read movie file metadata...

func ReadOutput

func ReadOutput(output chan string, rc io.ReadCloser)

*

  • Read output and send it to a channel.

func RegisterFunction

func RegisterFunction(name string, fct interface{})

*

  • Register a Go function to be able to call it dynamically latter.

func RegisterType

func RegisterType(typedNil interface{})

*

  • Register an instance of the type.

func Remove

func Remove(s []string, index int) ([]string, error)

func RemoveAccent

func RemoveAccent(text string) string

func RemoveContents

func RemoveContents(dir string) error

func RemoveDirContents

func RemoveDirContents(dir string) error

func RemoveString

func RemoveString(s []string, r string) []string

func Round

func Round(x float64, n int) float64

func RunCmd

func RunCmd(name, dir string, args []string, wait chan error)

func SetEnvironmentVariable

func SetEnvironmentVariable(key string, value string) error

func SetMetadata

func SetMetadata(path, key, value string) error

*

  • Store meta data into a file.

func SetProperty

func SetProperty(i interface{}, field string, value interface{}) error

func SetWindowsEnvironmentVariable

func SetWindowsEnvironmentVariable(key string, value string) error

Need a special function to get access to system variables.

func StringToBytes

func StringToBytes(s string) []byte

func SvgToPng

func SvgToPng(input, output string, w, h int) error

func TerminateProcess

func TerminateProcess(pid int, exitcode int) error

Terminate process.

func ToBool

func ToBool(value interface{}) bool

func ToBytes

func ToBytes(val interface{}) ([]byte, error)

*

  • Serialyse the entity to a byte array.

func ToInt

func ToInt(value interface{}) int

func ToJson

func ToJson(obj interface{}) (string, error)

func ToMap

func ToMap(in interface{}) (map[string]interface{}, error)

ToMap converts a struct to a map using the struct's tags.

ToMap uses tags on struct fields to decide which fields to add to the returned map.

func ToNumeric

func ToNumeric(value interface{}) float64

func ToString

func ToString(value interface{}) string

*

  • Convert a numerical value to a string.

func UnsetEnvironmentVariable

func UnsetEnvironmentVariable(key string) error

func UnsetWindowsEnvironmentVariable

func UnsetWindowsEnvironmentVariable(key string) error

func WriteStringToFile

func WriteStringToFile(filepath, s string) error

Write a string to a given file.

Types

type Element

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

type IPInfo

type IPInfo struct {
	// IP holds the described IP address.
	IP string
	// Hostname holds a DNS name associated with the IP address.
	Hostname string
	// City holds the city of the ISP location.
	City string
	// Country holds the two-letter country code.
	Country string
	// Loc holds the latitude and longitude of the
	// ISP location as a comma-separated northing, easting
	// pair of floating point numbers.
	Loc string
	// Org describes the organization that is
	// responsible for the IP address.
	Org string
	// Postal holds the post code or zip code region of the ISP location.
	Postal string
}

IPInfo describes a particular IP address.

func ForeignIP

func ForeignIP(ip string) (*IPInfo, error)

ForeignIP provides information about the given IP address, which should be in dotted-quad form.

type Referenceable

type Referenceable interface {
	GetUUID() string
}

*

  • Use it to get UUID of referenceable object.

type Stack

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

func (*Stack) Len

func (s *Stack) Len() int

Return the stack's length

func (*Stack) Pop

func (s *Stack) Pop() (value map[string]interface{})

Remove the top element from the stack and return it's value If the stack is empty, return nil

func (*Stack) Push

func (s *Stack) Push(value map[string]interface{})

Push a new element onto the stack

type TypeManager

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

type Version

type Version struct {
	Major      int
	Minor      int
	Patch      int
	PreRelease string
}

Base on https://go.dev/doc/modules/version-numbers for version number

func NewVersion

func NewVersion(str string) *Version

func (*Version) Compare

func (v *Version) Compare(to *Version) int

Compare tow version, 1 means v is newer than to, 0 is the same, -1 is older.

func (*Version) Parse

func (v *Version) Parse(str string)

Parse values from string

func (*Version) ToString

func (v *Version) ToString() string

Stringnify the vesion.

Jump to

Keyboard shortcuts

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