util

package
v0.0.0-...-c6fcb18 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2026 License: 0BSD, Apache-2.0, MIT, + 1 more Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BrCompressData

func BrCompressData(d []byte, level int) ([]byte, error)

func BrCompressDataBest

func BrCompressDataBest(d []byte) ([]byte, error)

func BrCompressDataDefault

func BrCompressDataDefault(d []byte) ([]byte, error)

func BrCompressFile

func BrCompressFile(dstPath string, path string, level int) error

func BrCompressFileBest

func BrCompressFileBest(dstPath string, path string) error

func BrCompressFileDefault

func BrCompressFileDefault(dstPath string, path string) error

func BrDecompressData

func BrDecompressData(d []byte) ([]byte, error)

func BrReadFile

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

func CloseNoError

func CloseNoError(f io.Closer)

CloseNoError is like io.Closer Close() but ignores an error use as: defer CloseNoError(f)

func CopyFile

func CopyFile(dst string, src string) error

CopyFile copies a file from src to dst It'll create destination directory if necessary

func CountFilesInFS

func CountFilesInFS(fsys fs.ReadDirFS) int

func CreateZipFile

func CreateZipFile(dst string, baseDir string, toZip ...string)

toZip is a list of files and directories in baseDir Directories are added recursively

func CreateZipWithDirContent

func CreateZipWithDirContent(zipFilePath, dirToZip string) error

CreateZipWithDirContent creates a zip file with the content of a directory. The names of files inside the zip file are relatitve to dirToZip e.g. if dirToZip is foo and there is a file foo/bar.txt, the name in the zip will be bar.txt

func DataSha1Hex

func DataSha1Hex(d []byte) string

func DirExists

func DirExists(path string) bool

DirExists returns true if path exists and is a directory

func DirFSIter

func DirFSIter(fsys fs.ReadDirFS, startDir string) iter.Seq[*DirEntryWithPath]

DirFSIter iterates over all entries and sub-entries in fsys starting from startDir. For each entry, it yields path relative to the root of fsys. Only provides files, not dirs.

func ExpandTildeInPath

func ExpandTildeInPath(s string) string

func ExtEqualFold

func ExtEqualFold(s string, ext string) bool

ExtEqualFold returns true if s ends with extension (e.g. ".html") case-insensitive

func FileExists

func FileExists(path string) bool

FileExists returns true if path exists and is a regular file

func FileSha1Hex

func FileSha1Hex(path string) (string, error)

func FileSize

func FileSize(path string) int64

FileSize gets file size, -1 if file doesn't exist

func GetCallstack

func GetCallstack(skipFrames int) string

func GetCallstackFrames

func GetCallstackFrames(skipFrames int) []string

func GetErr

func GetErr(errs ...error) error

func GzipCompressData

func GzipCompressData(d []byte) ([]byte, error)

func GzipCompressFile

func GzipCompressFile(dstPath, srcPath string) error

func GzipDecompressData

func GzipDecompressData(d []byte) ([]byte, error)

func GzipReadFile

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

func InlineFlateCompress

func InlineFlateCompress(plain []byte) []byte

func IsLinux

func IsLinux() bool

func IsMac

func IsMac() bool

func IsWinOrMac

func IsWinOrMac() bool

func IsWindows

func IsWindows() bool

func IterDir

func IterDir(dir string, cb func(path string, de fs.DirEntry) (bool, error)) error

call cb on each file in a directory

func IterReadDirFS

func IterReadDirFS(fsys fs.ReadDirFS, startDir string, cb func(string, fs.DirEntry) error) error

call cb on each file in fsys startDir should be ". for root directory, "" and "/" do not work

func IterZipData

func IterZipData(zipData []byte, cb func(f *zip.File, data []byte) error) error

func IterZipReader

func IterZipReader(r *zip.Reader, cb func(f *zip.File, data []byte) error) error

func MaybeSync

func MaybeSync(w io.Writer) error

func Must

func Must(err error)

func Must2

func Must2[T any](x T, err error) T

func OpenFileMaybeCompressed

func OpenFileMaybeCompressed(path string) (io.ReadCloser, error)

OpenFileMaybeCompressed opens a file that might be compressed with gzip or bzip2 or zstd or brotli TODO: could sniff file content instead of checking file extension

func PanicIf

func PanicIf(cond bool, args ...any)

func PanicIfErr

func PanicIfErr(err error, args ...any)

func PathExists

func PathExists(path string) bool

PathExists returns true if path exists

func PathIsDir

func PathIsDir(path string) (isDir bool, err error)

PathIsDir returns true if a path exists and is a directory Returns false, nil if a path exists and is not a directory (e.g. a file) Returns undefined, error if there was an error e.g. because a path doesn't exists

func Push

func Push[S ~[]E, E any](s *S, els ...E)

func ReadBeInt16

func ReadBeInt16(r io.Reader) (val int16)

func ReadBeInt32

func ReadBeInt32(r io.Reader) (val int32)

func ReadBeInt64

func ReadBeInt64(r io.Reader) (val int64)

func ReadByte

func ReadByte(r io.Reader) (val byte)

func ReadFileMaybeCompressed

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

ReadFileMaybeCompressed reads file. Ungzips if it's gzipped.

func ReadLeInt16

func ReadLeInt16(r io.Reader) (val int16)

func ReadLeInt32

func ReadLeInt32(r io.Reader) (val int32)

func ReadLeInt64

func ReadLeInt64(r io.Reader) (val int64)

func ReadLines

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

ReadLines reads file as lines

func ReadLinesFromReader

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

ReadLinesFromReader reads lines from io.Reader

func ReadZipData

func ReadZipData(zipData []byte) (map[string][]byte, error)

func ReadZipFile

func ReadZipFile(path string) (map[string][]byte, error)

func ReadZipFileMust

func ReadZipFileMust(path string) map[string][]byte

func RunLoggedInDir

func RunLoggedInDir(dir string, exe string, args ...string) error

func RunLoggedInDirMust

func RunLoggedInDirMust(dir string, exe string, args ...string)

func RunLoggedMust

func RunLoggedMust(exe string, args ...string) string

func RunMust

func RunMust(exe string, args ...string) string

func SliceLimit

func SliceLimit[S ~[]E, E any](s S, max int) S

func TrimExt

func TrimExt(s string) string

TrimExt removes extension from s

func UnzipDataToDir

func UnzipDataToDir(zipData []byte, dir string) error

func WriteFileGzipped

func WriteFileGzipped(path string, data []byte) error

WriteFileGzipped writes data to a path, using best gzip compression

func WriteToFile

func WriteToFile(path string, content string) error

func ZipDir

func ZipDir(dirToZip string) ([]byte, error)

func ZipDirToWriter

func ZipDirToWriter(w io.Writer, dirToZip string) error

func ZstdCompressData

func ZstdCompressData(d []byte, level zstd.EncoderLevel) ([]byte, error)

func ZstdCompressDataBest

func ZstdCompressDataBest(d []byte) ([]byte, error)

func ZstdCompressDataDefault

func ZstdCompressDataDefault(d []byte) ([]byte, error)

func ZstdCompressFile

func ZstdCompressFile(dst string, src string, level zstd.EncoderLevel) error

func ZstdCompressFileBest

func ZstdCompressFileBest(dst string, src string) error

func ZstdCompressFileDefault

func ZstdCompressFileDefault(dst string, src string) error

func ZstdDecompressData

func ZstdDecompressData(d []byte) ([]byte, error)

func ZstdReadFile

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

Types

type DirEntryWithPath

type DirEntryWithPath struct {
	Path     string
	DirEntry fs.DirEntry
}

Jump to

Keyboard shortcuts

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