utils

package
v1.1.7 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2023 License: MIT Imports: 24 Imported by: 0

Documentation

Overview

Package utils

工具包,提供更好用的常用函数和工具类

Package utils

Index

Constants

View Source
const (
	BASH = "bash"
	RUN  = "-c"
)

命令行运行 会开启新的sid 不会和主进程共用终端

View Source
const (
	ArchiveType = ".tar.gz"
	RotateSize  = 20 << 20 // 20mb
)
View Source
const (
	MINPort = 10000
	MaxPort = 20000
)
View Source
const (
	TimeFormat    = "2006/1/2 15:04:05"
	TimeFormatDay = "2006/1/2"
	TimeForLogger = "2006-1-2 15:04:05"
	TimeBetterSep = "2006-1-2-15-04-05"
)
View Source
const (
	// MaxDepth 可以通过环境变量ApolloMaxDepth自定义
	MaxDepth = 5
	KB       = 1024
	MB       = 2 << 20
	GB       = 2 << 30
)

Variables

View Source
var (
	BackBase   = "backup-%s.zip" // 拼接ServiceRoot
	BackupFlag = "/var/.backup"
	BackTmp    = "/tmp/Apollo"
)
View Source
var ApolloMaxDepth int

Functions

func ArchiveFile

func ArchiveFile(fileName string, dst string, autoClear bool) error

ArchiveFile 打包文件 默认为tar.gz fileName 要打包的文件 dst 要生成的文件名 autoClear 自动清空文件 写时复制 会导致writeToolong 将当前文件备份到tmp后压缩并删除

func Backup

func Backup(src, back string) error

Backup 检查是否存在备份文件

func BackupOld added in v1.1.1

func BackupOld(src, back string) error

func CMDAsync

func CMDAsync(envs []string, cmd ...string) error

func CMDRaw

func CMDRaw(envs []string, cmd ...string) *exec.Cmd

func CMDRun

func CMDRun(envs []string, cmd ...string) (string, error)

func CMDWithContext

func CMDWithContext(envs []string, cmd ...string) (string, error)

CMDWithContext 执行结束后会杀死父进程来关闭shell

func CalDir

func CalDir(dirs ...string) string

CalDir calculate dirs

func CalcFileSize

func CalcFileSize(s int64) string

CalcFileSize 统计byte kb mb gb

func CalcPathDepth

func CalcPathDepth(offsetDir, dir string) int

CalcPathDepth 计算路径深度 offsetDir 初始目录需要减去这个长度 eg: offset: /code/gen dir: /code/gen/map depth = 4 - 3 = 1

func CheckAsync

func CheckAsync(query *gin.Context) bool

CheckAsync 通过检查是否异步执行

func ClearFile

func ClearFile(f string) error

ClearFile 清空文件

func CreateFile

func CreateFile(f string) error

CreateFile 多级目录和文件的创建

func CreateFileX

func CreateFileX(f string) error

CreateFileX 可执行文件创建 需要额外的x权限

func CreateOrCoverFile

func CreateOrCoverFile(f string) error

CreateOrCoverFile 覆盖文件或者创建文件

func FileExist

func FileExist(p string) bool

func FileExistOrCreate

func FileExistOrCreate(p string) error

func FileNotExist

func FileNotExist(p string) bool

func GetAppDir

func GetAppDir() string

GetAppDir get application dir

func GetFileSize added in v1.0.4

func GetFileSize(file string) int64

GetFileSize 获取文件大小 不存在时为0

func ParseBson

func ParseBson(file string, r interface{}) error

func ParseJson

func ParseJson(data []byte, m interface{}) error

ParseJson 通过byte读取

func ParseJsonFile

func ParseJsonFile(fileName string, m interface{}) error

ParseJsonFile 解析文件

func PrettyJson

func PrettyJson(v interface{}) string

func RandomPort

func RandomPort() int

func RemoveFile

func RemoveFile(f string) error

RemoveFile 尽力删除 文件不存在 不报错

func Rotate added in v1.0.4

func Rotate(appLog string) error

Rotate 日志绕接 计算大小阈值 超过才会压缩 否则跳过

func Save2Json

func Save2Json(m interface{}) ([]byte, error)

Save2Json 输出配置到byte

func Save2JsonFile

func Save2JsonFile(m interface{}, fileName string) error

Save2JsonFile 保存为json文件

func SaveBson

func SaveBson(data interface{}, file string) error

func TimeCalcString

func TimeCalcString(last time.Time) string

TimeCalcString 计算时间差的string

func TimeCalcUnix

func TimeCalcUnix(last time.Time) int64

TimeCalcUnix 计算ms差

func TimeNowBetterSep

func TimeNowBetterSep() string

TimeNowBetterSep 形如"2006-1-2-15-04-05"

func TimeNowFormat

func TimeNowFormat(layout string) string

TimeNowFormat 返回符合格式的时间戳

func TimeNowString

func TimeNowString() string

TimeNowString 计算当前日志的年月日时分秒

func TimeNowUnix

func TimeNowUnix() int64

TimeNowUnix 计算当前unix时间

func TimeNowYearDay

func TimeNowYearDay() string

TimeNowYearDay 计算当前日期的年月日

func TimeToLocal added in v1.1.4

func TimeToLocal(t time.Time) time.Time

TimeToLocal 时区转换 转换为当地时区 ->东8区

Types

type File

type File struct {
	Type  string `json:"type"`
	Name  string `json:"name"`
	Files []File `json:"files"`
	Extra string `json:"extra"`
}

func GetFileTreeAllDepth

func GetFileTreeAllDepth(offset, dir string) []File

GetFileTreeAllDepth 递归返回路径树 为避免卡顿最大遍历深度为5

Jump to

Keyboard shortcuts

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