utils

package
v0.4.87 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2025 License: MIT Imports: 43 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrEntityInvalid      = errors.New(`common.ENTITY_INVALID`)
	ErrFailedVerification = errors.New(`common.ENTITY_CHECK_FAILED`)
)

Functions

func AppendStringToFile

func AppendStringToFile(filePath, content string) error

func BindJSON

func BindJSON[T any](r *http.Request) (*T, error)

func ByteCountIEC added in v0.1.91

func ByteCountIEC(b uint64) string

func BytesToHexString

func BytesToHexString(bytes []byte) string

func BytesToString

func BytesToString(b []byte, r ...int) string

func CheckBinaryPack

func CheckBinaryPack(data []byte) (byte, byte, bool)

func CheckDir added in v0.1.82

func CheckDir(dirPath string) bool

func CleanExt

func CleanExt(name string) string

func CompareVersions

func CompareVersions(v1, v2 string) int

CompareVersions 0:相等;1:v1>v2;-1:v1<v2

func DecAES

func DecAES(data []byte, key []byte) ([]byte, error)

func Decrypt added in v0.3.38

func Decrypt(cipherHex string, key []byte) string

func Delete

func Delete(filePath string, args ...string)

func DirCheck

func DirCheck(path string) error

func Divide

func Divide(a, b int) int

func DivideAndCeil

func DivideAndCeil(a, b int) int

DivideAndCeil 函数用于进行除法并向上取整

func DownLoadGeneric added in v0.2.28

func DownLoadGeneric(baseUrl, username, password, destPath string) error

func EncAES

func EncAES(data []byte, key []byte) ([]byte, error)

func Encrypt added in v0.3.38

func Encrypt(text []byte, key []byte) string

func EnsureDir

func EnsureDir(path string) error

func Export added in v0.2.28

func Export(obj model.CloudApi) error

func FormatSize

func FormatSize(size int64) string

func GenRandByte

func GenRandByte(n int) []byte

func GetAppSpace added in v0.2.15

func GetAppSpace() (string, string, string)

func GetDataByJson

func GetDataByJson[T any](r *http.Request) (*T, error)

func GetFileNameByTime

func GetFileNameByTime() string

func GetFileNameFromUrl

func GetFileNameFromUrl(rawURL string) string

func GetFilenameFromHeader

func GetFilenameFromHeader(header http.Header) string

func GetFirstPathSegment added in v0.2.6

func GetFirstPathSegment(path string) string

GetFirstPathSegment 获取路径第一个有效段(优化实现)

func GetListeningPorts

func GetListeningPorts()

func GetLocalIp added in v0.2.62

func GetLocalIp() string

func GetLocalMac added in v0.2.70

func GetLocalMac() string

func GetMD5

func GetMD5(data []byte) ([]byte, string)

func GetPointerInstance

func GetPointerInstance[T any](name string, obj interface{}) (t *T, err error)

func GetPrimaryIP added in v0.2.70

func GetPrimaryIP() (string, error)

GetPrimaryIP 获取主IP地址(默认网关所在接口的IP)

func GetSelfSize added in v0.1.68

func GetSelfSize() uint64

func GetSliceChunk

func GetSliceChunk[T any](data *[]T, start, end int) *[]T

func GetSlicePrefix

func GetSlicePrefix[T any](data *[]T, n int) *[]T

func GetSliceSuffix

func GetSliceSuffix[T any](data *[]T, n int) *[]T

func GetStrUUID

func GetStrUUID() string

func GetTime

func GetTime() string

func GetUUID

func GetUUID() []byte

func GetUserDir added in v0.2.37

func GetUserDir() (string, error)

func GetVersion added in v0.3.8

func GetVersion() map[string]interface{}

func GetVersionByFileName added in v0.1.82

func GetVersionByFileName(filename string) string

func HasDiskSpace added in v0.1.83

func HasDiskSpace() bool

func If

func If[T any](b bool, t, f T) T

func Import added in v0.2.28

func Import(obj model.CloudApi) error

func IsClassC added in v0.3.39

func IsClassC(ip net.IP) bool

func IsClassCC added in v0.3.96

func IsClassCC(ip net.IP) bool

func IsDirEmpty added in v0.3.59

func IsDirEmpty(dirPath string) (bool, error)

func IsDirectoryExist

func IsDirectoryExist(dirPath string) bool

func IsExist added in v0.2.6

func IsExist(filePath string) bool

func IsLinux

func IsLinux() bool

func IsMacOs

func IsMacOs() bool

func IsPortOpen

func IsPortOpen(host string, port int, timeout time.Duration) bool

func IsPublicIPv4 added in v0.3.39

func IsPublicIPv4(ip net.IP) bool

func IsURLValidAndAccessible

func IsURLValidAndAccessible(rawURL string) bool

IsURLValidAndAccessible 检查 URL 是否有效并且可访问

func IsWindows

func IsWindows() bool

IsWindows 判断是否为 Windows 系统

func IsZipOrJson

func IsZipOrJson(filename string) (isZip, isJSON bool)

IsZipOrJson 根据后缀判断文件类型(仅后缀匹配)

func MakeDir added in v0.1.91

func MakeDir(path string) error

func Max

func Max[T int | int32 | int64 | uint | uint32 | uint64 | float32 | float64](a, b T) T

func Min

func Min[T int | int32 | int64 | uint | uint32 | uint64 | float32 | float64](a, b T) T

func MoveFileToDir added in v0.1.82

func MoveFileToDir(src string, dstDir string) error

func ObjectToTomlText

func ObjectToTomlText(obj interface{}) []byte

func PingRaw

func PingRaw(ip string) bool

func Read

func Read(filePath string) ([]byte, error)

func ReadToml

func ReadToml(filePath string) ([]byte, error)

func RemoveSlice

func RemoveSlice[T comparable](slice []T, value T) []T

RemoveSlice 泛型删除所有匹配值的元素

func ReplaceNewVersionBinName

func ReplaceNewVersionBinName(filename, v string) string

func RestartProgram

func RestartProgram() error

RestartProgram 重启程序

func SaveFile

func SaveFile(file multipart.File, fileSize int64, saveFilePath string) error

func ScanIP

func ScanIP() []string

func ScanPort

func ScanPort(host string, duration time.Duration, start, end int) []int

func ScanPorts

func ScanPorts(host string, start, end int) []int

func SetFieldValue

func SetFieldValue(obj interface{}, fieldName string, value interface{}) error

SetFieldValue 使用反射给结构体的字段赋值

func ShowUpDirSize added in v0.1.83

func ShowUpDirSize()

func Shutdown

func Shutdown() error

func SplitLastTwoByUnderscore added in v0.1.92

func SplitLastTwoByUnderscore(s string) []string

func SplitVersion

func SplitVersion(v string) []string

func SseHandler added in v0.2.69

func SseHandler(queue *LogQueue) http.HandlerFunc

SseHandler 处理函数

func StringContains

func StringContains(element string, data []string) bool

func StringToBytes

func StringToBytes(s string, r ...int) []byte

func TestToml

func TestToml()

func ToUpperFirst

func ToUpperFirst(s string) string

ToUpperFirst 将字符串的首字母转换为大写

func TomlTextToObject

func TomlTextToObject(tomlBytes []byte, obj interface{}) error

func Unzip

func Unzip(src, dest string) error

Unzip 函数用于解压指定的ZIP文件到指定的目标目录

func UnzipToRoot

func UnzipToRoot(zipFile, destDir string, strich bool) error

func UploadGeneric added in v0.2.28

func UploadGeneric(baseUrl, method, cfgpath string, username, password string) error

func Write

func Write(filePath string, content []byte) error

func WriteAppend

func WriteAppend(filePath string, content []byte) error

func WriteFile

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

func WriteToml

func WriteToml(filePath string, data []byte) error

func XOR

func XOR(data []byte, key []byte) []byte

func Zip

func Zip(dir, dst string) error

func ZipFiles added in v0.2.38

func ZipFiles(zipPath string, files []string) error

Types

type AutoReplaceQueue added in v0.2.69

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

func NewAutoReplaceQueue added in v0.2.69

func NewAutoReplaceQueue(capacity int) *AutoReplaceQueue

func (*AutoReplaceQueue) Pop added in v0.2.69

func (q *AutoReplaceQueue) Pop() interface{}

Pop 取出元素

func (*AutoReplaceQueue) Push added in v0.2.69

func (q *AutoReplaceQueue) Push(item interface{})

Push 添加元素,通道满时自动丢弃旧元素

type FixedQueue added in v0.2.69

type FixedQueue[T any] struct {
	// contains filtered or unexported fields
}

FixedQueue 泛型固定长度队列结构体

func NewFixedQueue added in v0.2.69

func NewFixedQueue[T any](capacity int) *FixedQueue[T]

NewFixedQueue 创建一个新的固定长度队列

func (*FixedQueue[T]) Capacity added in v0.2.69

func (q *FixedQueue[T]) Capacity() int

Capacity 返回队列容量

func (*FixedQueue[T]) Dequeue added in v0.2.69

func (q *FixedQueue[T]) Dequeue() (T, bool)

Dequeue 出队操作

func (*FixedQueue[T]) Enqueue added in v0.2.69

func (q *FixedQueue[T]) Enqueue(item T)

Enqueue 入队操作

func (*FixedQueue[T]) Items added in v0.2.69

func (q *FixedQueue[T]) Items() []T

Items 返回队列所有元素

func (*FixedQueue[T]) Size added in v0.2.69

func (q *FixedQueue[T]) Size() int

Size 返回队列当前大小

type GeneralResponse

type GeneralResponse struct {
	Pid int
	Msg string
}

type LogQueue added in v0.2.69

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

LogQueue 定义日志队列

func NewLogQueue added in v0.2.69

func NewLogQueue() *LogQueue

NewLogQueue 初始化日志队列

func (*LogQueue) AddMessage added in v0.2.69

func (q *LogQueue) AddMessage(message string)

AddMessage 生产者添加日志消息

func (*LogQueue) RegisterClient added in v0.2.69

func (q *LogQueue) RegisterClient(client chan string)

RegisterClient 消费者注册客户端

func (*LogQueue) UnregisterClient added in v0.2.69

func (q *LogQueue) UnregisterClient(client chan string)

UnregisterClient 消费者注销客户端

type NetworkInterface added in v0.2.70

type NetworkInterface struct {
	Name        string   `json:"name"`        // 接口名称
	DisplayName string   `json:"displayName"` // 显示名称
	MacAddress  string   `json:"macAddress"`  // MAC地址
	Ipv4        string   `json:"ipv4"`        // MAC地址
	IPAddresses []string `json:"ipAddresses"` // IP地址列表
}

NetworkInterface 网络接口信息

func GetDeviceInfo added in v0.2.70

func GetDeviceInfo() (*NetworkInterface, error)

GetDeviceInfo 获取主IP地址、Mac地址(默认网关所在接口的IP)

func GetNetworkInterfaces added in v0.2.70

func GetNetworkInterfaces() ([]NetworkInterface, error)

GetNetworkInterfaces 获取所有网络接口信息

type Node

type Node struct {
	Label    string `json:"label"`
	Value    string `json:"value"`
	FilePath string `json:"filePath"`
}

type Option

type Option struct {
	Label    string `json:"label"`
	Value    string `json:"value"`
	Children []Node `json:"children"`
}

func GetNodes

func GetNodes(dir string) []Option

func GetNodes1 added in v0.1.96

func GetNodes1(dir string) []Option

func ToTree added in v0.1.96

func ToTree(dir string, entries []string) []Option

type ProgressWriter

type ProgressWriter struct {
	TotalSize int64
	Written   int64
	Progress  float64
	Title     string
}

ProgressWriter 自定义进度写入器结构体

func (*ProgressWriter) Write

func (pw *ProgressWriter) Write(p []byte) (int, error)

Write 实现 io.Writer 接口的 Write 方法

Jump to

Keyboard shortcuts

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