utils

package module
v0.0.0-...-0c5965e Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2024 License: MIT Imports: 31 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	GOOS       = runtime.GOARCH
	AppName    string
	AppVersion string
	NEWLINE    = "\n"
	Ipv4Regex  = `([0-9]+\.){3}[0-9]+`
)

Functions

func ArgsGet

func ArgsGet(index int, args []string) string

func Cat

func Cat(filename ...string) error

func CheckErr

func CheckErr(msg interface{})

CheckErr prints the msg with the prefix 'Error:' and exits with error code 1. If the msg is nil, it does nothing.

func Cp

func Cp(sourceFile, destinationFile string) bool
func CreateSymLink(src, dest string) error

use for both file and dir

func DirAllChild

func DirAllChild(directory string) (files []string, err error)

list all file and dir int level1 of dir

func DirCopy

func DirCopy(srcDir, dstDir string) error

Copy dir, change ownner (as user execute), change datetime modify Notes: Care full with recursive can't overload the File decriptor limit

func DirCopyKeepOwner

func DirCopyKeepOwner(srcDir, dstDir string) error

Notes: Care full with recursive can't overload the File decriptor limit

func DirCreate

func DirCreate(dirPath string, permission fs.FileMode) error

Dir

func DirIsEmptyorNotexist

func DirIsEmptyorNotexist(directory string) bool

func DirMove

func DirMove(src, dst string) error

func DirMoveKeepOwner

func DirMoveKeepOwner(src, dst string) error

func DirRemove

func DirRemove(dirPath string) error

func DirRemoveContents

func DirRemoveContents(dir string) error

Remove all content of dir

func File2lines

func File2lines(filePath string) ([]string, error)

func FileCloneDate

func FileCloneDate(dst, src string) bool

cp date-time modify from dst file to src file

func FileCopy

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

cp source to dir. If dest exist, overwrite it

func FileCopyKeepOwner

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

cp source to dir, keep modify time, owner. If dest exist, overwrite it

func FileCreate

func FileCreate(fullPath string) error

File

func FileCreateOverwrite

func FileCreateOverwrite(fullPath string) error

func FileCreateWithContent

func FileCreateWithContent(fullPath string, data []byte) (bytewrite int, err error)

func FileGetSize

func FileGetSize(filepath string) (int64, error)

only read filesize, not dir

func FileHashMd5

func FileHashMd5(filePath string) (string, error)

get Md5 string of file

func FileInsertStringAtLine

func FileInsertStringAtLine(filePath, str string, index int) error

insert string at index lines of file, if this line have text, push it one line down.

func FileIsWriteable

func FileIsWriteable(path string) (isWritable bool)

func FileMove

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

func FileMoveSameFileSys

func FileMoveSameFileSys(src, dst string) error

func FileOpen2Write

func FileOpen2Write(fullPath string) (*os.File, error)

open and attend to file

func FileReadAll

func FileReadAll(fullPath string) (string, error)

func FileRemoveFile

func FileRemoveFile(path string) error

removeFile removes the specified file. Errors are ignored.

func FileTempCreateInNewTemDir

func FileTempCreateInNewTemDir(filename string) string

create tempdir and return tempfile in tempdir (not create yet)

func FileTempCreateInNewTemDirWithContent

func FileTempCreateInNewTemDirWithContent(filename string, data []byte) string

func FileToURL

func FileToURL(in string) string

return C:/path/to/file.txt on windows(fileToURL("C:\\path\\to\\file.txt")) and path/to/file.txt on Linux (fileToURL("/path/to/file.txt")).

func FileWaitContentsAndRead

func FileWaitContentsAndRead(path string, timeoutms int) (string, error)
readFile waits for the specified file to contain contents, and then returns

those contents as a string. If an error occurs while reading the file, the error is returned. If the file has no content after 100 attempts, an error is returned.

func FileWaitForFileExist

func FileWaitForFileExist(path string, timeoutms int) error
waitForFile waits for the specified file to exist before returning. If the an

error, other than the file not existing, occurs, the error is returned. If, after 100 attempts, the file does not exist, an error is returned.

func FileWriteStringIfChange

func FileWriteStringIfChange(pathfile string, contents []byte) (bool, error)

write content to file if it different with exist file content

func GenerateID

func GenerateID() string

func GenerateIdv4

func GenerateIdv4(preString string) (string, error)

return Prestring + _uuidv4

func GenerateRandomBytes

func GenerateRandomBytes(n int) ([]byte, error)

Get n random bytes

func GenerateRandomString

func GenerateRandomString(s int) (string, error)

Get random string with n character

func GenerateSha1Bytes

func GenerateSha1Bytes(data []byte) string

creat sha1 of data

func GenerateSha1String

func GenerateSha1String(data string) string

func GenerateTokenSha1

func GenerateTokenSha1(key int) string

func GetCurrentContainerID

func GetCurrentContainerID(filepaths ...string) (id string)

GetCurrentContainerID attempts to extract the current container ID from the provided file paths. (call inside container)

	If no files paths are provided, it will default to /proc/1/cpuset, /proc/self/cgroup and /proc/self/mountinfo.
 It attempts to match the HOSTNAME first then use the fallback method, and returns with the first valid match.

func InitLogger

func InitLogger(filePath string, logger *log.Logger, level log.Level) error

func InitLoggerStdout

func InitLoggerStdout(logger *log.Logger, level log.Level)

func Int64ToBytes

func Int64ToBytes(number int64) []byte

func Int64ToString

func Int64ToString(number int64) string

func IsContainer

func IsContainer() bool

func JsonGetPathNode

func JsonGetPathNode(node *jsonquery.Node) (retstr string)

func JsonParser

func JsonParser(str string) gjson.Result

func JsonParserBytes

func JsonParserBytes(bs []byte) gjson.Result

func JsonSet

func JsonSet(jsonstring string, elementPath string, val any) (string, error)

func JsonStringFindElement

func JsonStringFindElement(strjson *string, pathSearch string) (string, error)

func JsonStringFindElements

func JsonStringFindElements(strjson *string, pathSearch string, valueflag ...bool) (map[string]string, error)

func JsonStringFindElementsSlide

func JsonStringFindElementsSlide(strjson *string, pathSearch string) ([]string, error)

func PATHArr

func PATHArr() []string

return PATH as array (:)

func PATHGetEnvPathValue

func PATHGetEnvPathValue() string

func PATHJointList

func PATHJointList(PATH, data string) string

add data to PATH , vd: /home/mannk:path

func PATHRemove

func PATHRemove(PATH, addpath string) string

remove addpath from path

func ParserShellPath

func ParserShellPath(shellPath string) string

parser shell path include ${shelvariale}

func PathBaseName

func PathBaseName(filePath string) string

Path

func PathGetEnvPathKey

func PathGetEnvPathKey() string

retur path or PATH

func PathIsDir

func PathIsDir(path string) bool

func PathIsExist

func PathIsExist(path string) bool

func PathIsFile

func PathIsFile(path string) bool

func Reader2lines

func Reader2lines(r io.Reader) ([]string, error)

func SliceStringRemoveDuplicates

func SliceStringRemoveDuplicates(slice []string) []string

func String2Int64

func String2Int64(str string) int64

func String2lines

func String2lines(str string) []string

return []string, sperate by endline

func StringGetFirstFields

func StringGetFirstFields(str string, delimiter string, firstNField uint16) string

get n field of string seprate by delimiter string

func StringInSlice

func StringInSlice(a string, list []string) bool

func SymWalk

func SymWalk(path string, walkFn filepath.WalkFunc) error

Walk extends filepath.Walk to also follow symlinks

func SysGetGroupId

func SysGetGroupId() string

func SysGetHomeDir

func SysGetHomeDir() (home string)

func SysGetUserId

func SysGetUserId() string

func SysGetUsername

func SysGetUsername() string

func TokenSha1IsMatch

func TokenSha1IsMatch(key int, token string) bool

compare token (15s limit between create and check) (ex: http post and response confrim)

func Touch

func Touch(name string) error

func Which

func Which(filePath, PATH string) (string, bool)

func WindowsIsAdmin

func WindowsIsAdmin() bool

func WriteStringAndCheck

func WriteStringAndCheck(b io.StringWriter, s string)

WriteStringAndCheck writes a string into a buffer, and checks if the error is not nil.

Types

This section is empty.

Jump to

Keyboard shortcuts

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