extensions

package module
v0.0.0-...-a499989 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2023 License: MIT Imports: 29 Imported by: 14

Documentation

Overview

Package extensions provides a set of functions to work with files, directories, and converting data types

Index

Constants

View Source
const (
	UNIX_READ        = 04
	UNIX_WRITE       = 02
	UNIX_EX          = 01
	UNIX_USER_SHIFT  = 6
	UNIX_GROUP_SHIFT = 3
	UNIX_OTH_SHIFT   = 0

	UNIX_USER_R   = UNIX_READ << UNIX_USER_SHIFT
	UNIX_USER_W   = UNIX_WRITE << UNIX_USER_SHIFT
	UNIX_USER_X   = UNIX_EX << UNIX_USER_SHIFT
	UNIX_USER_RW  = UNIX_USER_R | UNIX_USER_W
	UNIX_USER_RWX = UNIX_USER_RW | UNIX_USER_X

	UNIX_GROUP_R   = UNIX_READ << UNIX_GROUP_SHIFT
	UNIX_GROUP_W   = UNIX_WRITE << UNIX_GROUP_SHIFT
	UNIX_GROUP_X   = UNIX_EX << UNIX_GROUP_SHIFT
	UNIX_GROUP_RW  = UNIX_GROUP_R | UNIX_GROUP_W
	UNIX_GROUP_RWX = UNIX_GROUP_RW | UNIX_GROUP_X

	UNIX_OTH_R   = UNIX_READ << UNIX_OTH_SHIFT
	UNIX_OTH_W   = UNIX_WRITE << UNIX_OTH_SHIFT
	UNIX_OTH_X   = UNIX_EX << UNIX_OTH_SHIFT
	UNIX_OTH_RW  = UNIX_OTH_R | UNIX_OTH_W
	UNIX_OTH_RWX = UNIX_OTH_RW | UNIX_OTH_X

	UNIX_ALL_R       = UNIX_USER_R | UNIX_GROUP_R | UNIX_OTH_R
	UNIX_ALL_W       = UNIX_USER_W | UNIX_GROUP_W | UNIX_OTH_W
	UNIX_ALL_X       = UNIX_USER_X | UNIX_GROUP_X | UNIX_OTH_X
	UNIX_ALL_RW      = UNIX_ALL_R | UNIX_ALL_W
	UNIX_ALL_RWX     = UNIX_ALL_RW | UNIX_GROUP_X
	UNIX_COMMON_FILE = (UNIX_USER_RW | UNIX_ALL_R)
	UNIX_COMMON_DIR  = os.ModeDir | (UNIX_USER_RWX | UNIX_ALL_R)
	UNIX_DIR_RWALL   = os.ModeDir | (UNIX_USER_RWX | UNIX_ALL_RW)
)

These constants are to be used when you are passing an os.FileMode uint32 because it's easier to compose exactly what you want and gives you common directory that needs execute.

Variables

This section is empty.

Functions

func Between

func Between(value string, a string, b string) string

Between will return substring between two strings

func BigEndianUInt16

func BigEndianUInt16(value uint16) (value1 byte, value2 byte)

BigEndianUInt16 will convert a uint16 to 2 bytes

func BigEndianUInt32

func BigEndianUInt32(value uint32) (value1 byte, value2 byte, value3 byte, value4 byte)

BigEndianUInt32 will convert a uint32 to 4 bytes

func BoolToString

func BoolToString(val bool) string

BoolToString converts a bool to a string

func CopyFile

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

CopyFile copies a file to another file

func CopyFolder

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

CopyFolder copies a folder to another folder

func DWordToInt

func DWordToInt(data []byte) (num int32)

DWordToInt converts an 4 byte to a int32

func DirSize

func DirSize(path string) (int64, error)

DirSize returns the size of a directory

func DirSizeWithSearch

func DirSizeWithSearch(path string, fileSearch string) (int64, error)

DirSizeWithSearch returns the size of a directory with a search string

func DoesFileExist

func DoesFileExist(path string) bool

DoesFileExist checks if a file exists

func DoesFileNotExist

func DoesFileNotExist(path string) bool

DoesFileNotExist checks if a file does not exist

func ExtractArgsWithinBrackets

func ExtractArgsWithinBrackets(str string) (res []string)

ExtractArgsWithinBrackets extracts the arguments within brackets

func FloatToString

func FloatToString(input_num float64, precision int) string

FloatToString converts a float to a string

func GenPackageImport

func GenPackageImport(name string, imports []string) string

GenPackageImport a utility for initilizing a go package file

func GetAllDirWithExclude

func GetAllDirWithExclude(path string, except string) (files []os.FileInfo, err error)

GetAllDirWithExclude returns all directories in a directory excluding the exclude string

func GetAllDirs

func GetAllDirs(path string) (files []os.FileInfo, err error)

GetAllDirs returns all directories in a directory

func GetAllFiles

func GetAllFiles(path string) (files []os.FileInfo, err error)

GetAllFiles returns all files in a directory

func GetAllFilesDeepWithSearch

func GetAllFilesDeepWithSearch(path string, fileSearch string) (files []os.FileInfo, err error)

GetAllFilesDeepWithSearch recursively returns all files in a directory with a search string

func GetAllFilesRecursively

func GetAllFilesRecursively(fileDir string) (files []string, err error)

GetAllFilesRecursively recurses a directory and returns the path and file names of all the files.

func GetAllFilesRecursivelyByExtension

func GetAllFilesRecursivelyByExtension(fileDir string, fileExtensionFilter []string) (files []string, err error)

GetAllFilesRecursivelyByExtension recurses a directory and returns the path and file names of all the files. Pass []string of fileExtensions as the second parameter (no need for period and cannot have multiple extensions like tar.gz)

func GetAllFilesSortedBy

func GetAllFilesSortedBy(path string, fileSearch string) (files []os.FileInfo, err error)

GetAllFilesSortedBy returns all files in a directory sorted

func GetAllFilesWithSearch

func GetAllFilesWithSearch(path string, fileSearch string) (files []os.FileInfo, err error)

GetAllFilesWithSearch returns all files in a directory with a search string

func GetAllFolders

func GetAllFolders(path string) (files []os.FileInfo, err error)

GetAllFolders returns all folders in a directory

func GetAllFoldersWithSearch

func GetAllFoldersWithSearch(path string, fileSearch string) (files []os.FileInfo, err error)

GetAllFoldersWithSearch returns all folders in a directory with a search string

func GetDecimalAndStringFromHex

func GetDecimalAndStringFromHex(twoDigitHexCode string) (decimalValue int, asciiString string, err error)

GetDecimalAndStringFromHex takes a hex string and returns the decimal value and the string value.

func GetFileSize

func GetFileSize(path string) (size int64, err error)

func GetUserAgentName

func GetUserAgentName(req *http.Request) (name string)

GetUserAgentName will return the name of the user agent.

func Gzipfunc

func Gzipfunc(source string, target string) (err error)

Gzipfunc gzips a file

func HexToInt

func HexToInt(hexStr string) int

GexToInt converts a string to an int.

func Int32ToDWORD

func Int32ToDWORD(val int32) (value []byte)

Int32ToDWORD converts an int32 to DWORD byte array.

func Int32ToInt

func Int32ToInt(val int32) (ret int)

Int32ToInt converts a 32 bit integer to a 32 bit integer

func Int32ToString

func Int32ToString(val int32) string

Int32ToString converts a 32 bit integer to a string

func Int64ToInt32

func Int64ToInt32(val int64) (ret int)

Int64ToInt32 converts a 64 bit integer to a 32 bit integer

func Int64ToString

func Int64ToString(val int64) string

Int64ToString converts a 64 bit integer to a string

func IntToBool

func IntToBool(val int) bool

IntToBool converts an integer to a bool

func IntToString

func IntToString(val int) string

IntToString converts an integer to a string

func InvokeMethodOnAllFilesRecursively

func InvokeMethodOnAllFilesRecursively(fileDir string, function func(*os.File) error) (files []string, fileErrors map[string]error, err error)

InvokeMethodOnAllFilesRecursively recurses a directory and returns the path and file names of all the files. The second parameter is a callback functon which will be called in go routines for each file where you will read the os.File and do something with it

func InvokeMethodOnAllFilesRecursivelyByExtension

func InvokeMethodOnAllFilesRecursivelyByExtension(fileDir string, fileExtensionFilter []string, function func(*os.File) error) (files []string, fileErrors map[string]error, err error)

InvokeMethodOnAllFilesRecursivelyByExtension recurses a directory and returns the path and file names of all the files. Pass []string of fileExtensions as the second parameter (no need for period and cannot have multiple extensions like tar.gz). The third parameter is a callback functon which will be called in go routines for each file where you will read the os.File and do something with it

func IpAddressToUint32

func IpAddressToUint32(ip string) (value uint32)

IpAddressToUint32 will convert a string ip address to UInt32

func IsPrintable

func IsPrintable(s string) bool

IsPrintable checks if a string is printable and returns true or false. If it is an emoji, it returns true even if non printables are also in the string

func LeftPad

func LeftPad(s string, padStr string, pLen int) string

LeftPad pads a string with a specified character to a specified length.

func LeftPad2Len

func LeftPad2Len(s string, padStr string, overallLen int) string

LeftPad2Len functions are generally assumed to be padded with short sequences of strings

func MD5

func MD5(path string) (string, error)

MD returns the md5 hash of a string

func MakeFirstLowerCase

func MakeFirstLowerCase(s string) string

MakeFirstLowerCase makes the first letter of a string lowercase

func MkDir

func MkDir(path string) error

MkDir creates a directory

func MkDirRWAll

func MkDirRWAll(path string) error

MkDirRWAll creates a directory with 0777 permissions

func NewUUID

func NewUUID() (string, error)

NewUUID generates a random UUID according to RFC 4122

func ParseAndWriteFile

func ParseAndWriteFile(path string, v interface{}, perm os.FileMode) (err error)

ParseAndWriteFile parses a file and writes it with json.Marshal

func PrintKiloBytes

func PrintKiloBytes(bytes int64) string

PrintKiloBytes prints a number of bytes in kilobytes as a string

func PrintMegaBytes

func PrintMegaBytes(bytes int64) string

PrintMegaBytes prints a number of bytes in megabytes as a human readable string

func PrintZettaBytes

func PrintZettaBytes(bytes int64) string

PrintZettaBytes prints a number of bytes in zettabytes as a human readable string

func Random

func Random(min, max int) int

Random returns a random number between min and max

func RandomString

func RandomString(strlen int) string

RandomString returns a random string of length

func ReadFile

func ReadFile(path string) ([]byte, error)

ReadFile reads a file

func ReadFileAndParse

func ReadFileAndParse(path string, v interface{}) (err error)

ReadFileAndParse reads a file and parses it with json.Unmarshal

func RemoveDirectory

func RemoveDirectory(dir string) error

RemoveDirectory removes a directory

func RemoveDirectoryShell

func RemoveDirectoryShell(dir string) (err error)

RemoveDirectoryShell removes a directory using the shell

func RightPad

func RightPad(s string, padStr string, pLen int) string

RightPad pads a string with a specified character to a specified length.

func RightPad2Len

func RightPad2Len(s string, padStr string, overallLen int) string
RightPad2Len functions are generally assumed to be padded with short sequences of strings

* in many cases with a single character sequence * * so we assume we can build the string out as if the char seq is 1 char and then * just substr the string if it is longer than needed * * this means we are wasting some cpu and memory work * but this always get us to want we want it to be * * in short not optimized to for massive string work * * If the overallLen is shorter than the original string length * the string will be shortened to this length (substr) *

func Round

func Round(x, unit float64) float64

Round rounds a float to a specified number of decimal places

func StringToBool

func StringToBool(val string) bool

StringToBool converts a string to a bool

func StringToFloat

func StringToFloat(val string, precision int) (r float64)

StringToFloat converts a string to a float

func StringToInt

func StringToInt(val string) int

StringToInteger converts a string to an integer

func StringToUInt16

func StringToUInt16(val string) uint16

StringToUInt16 converts a string to an uin16

func StringToUInt64

func StringToUInt64(val string) uint64

StringToUInt64 converts a string to an uin64

func StringToUInt8

func StringToUInt8(val string) uint8

StringToUInt8 converts a string to an uint8

func SyncMapAny

func SyncMapAny(x *sync.Map) (ok bool)

SyncMapAny will return true if there are any items in the sync.Map

func SyncMapLength

func SyncMapLength(x *sync.Map) (length int)

SyncMapLength will return true the length of items in the sync.Map

func Tar

func Tar(source string, target string) error

Tar takes a source and variable writers and walks 'source' writing each file found to the tar writer; the purpose for accepting multiple writers is to allow for multiple outputs (for example a file, or md5 hash)

func Title

func Title(str string) string

func TrimSuffix

func TrimSuffix(s, suffix string) string

TrimSuffix removes a suffix from a string.

func UnGzipfunc

func UnGzipfunc(source, target string) error

UnGzipfunc ungzips a file

func UnTar

func UnTar(tarball, target string) error

UnTar unzips a tar file

func Write

func Write(value string, path string) error

Write to a file

func WriteAndGoFmt

func WriteAndGoFmt(value string, path string, quiet bool, perm os.FileMode) error

WriteAndGoFmt writes a file and formats it with go

func WriteAndGoFormat

func WriteAndGoFormat(value string, path string) error

WriteAndGoFormat writes a file and formats it with go

func WriteToFile

func WriteToFile(value string, path string, perm os.FileMode) error

WriteToFile writes a file

Types

type ByOldestFile

type ByOldestFile []os.FileInfo

ByOldestFile is a type for sorting files by oldest first

func (ByOldestFile) Len

func (a ByOldestFile) Len() int

func (ByOldestFile) Less

func (a ByOldestFile) Less(i, j int) bool

func (ByOldestFile) Swap

func (a ByOldestFile) Swap(i, j int)

type FilePath

type FilePath struct {
	Name string `json:"Name"`
	Path string `json:"Path"`
	Type string `json:"Type"`
}

FilePath is a type that represents a file path.

func GetAllFilesSearchWithPath

func GetAllFilesSearchWithPath(path string, fileSearch string) (files []FilePath, err error)

GetAllFilesSearchWithPath returns all files in a directory with a search string

func (*FilePath) ToString

func (obj *FilePath) ToString() (str string)

ToString converts a FilePath to a string.

type Version

type Version struct {
	Major          int
	Minor          int
	Revision       int
	MajorString    string
	MinorString    string
	RevisionString string
	Value          string
}

Version is a type that represents a version number.

func (*Version) Init

func (obj *Version) Init(value string)

Init initilizes the version

Jump to

Keyboard shortcuts

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