u

package
v0.0.0-...-ddaf306 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2024 License: MIT Imports: 22 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendNewline

func AppendNewline(s *string) string

func AppendOrReplaceInFileMust

func AppendOrReplaceInFileMust(path string, toAppend string, delim string) bool

func AppendOrReplaceInText

func AppendOrReplaceInText(orig string, toAppend string, delim string) string

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 Capitalize

func Capitalize(s string) string

Capitalize does foo => Foo, BAR => Bar etc.

func CloseNoError

func CloseNoError(f io.Closer)

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

func CollapseMultipleNewlines

func CollapseMultipleNewlines(s string) string

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 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 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 FileLineCount

func FileLineCount(path string) (int, error)

FileLineCount returns number of lines in a 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 FormatDuration

func FormatDuration(d time.Duration) string

Formats duration in a more human friendly way than time.Duration.String()

func FormatSize

func FormatSize(n int64) string

FormatSize formats a number in a human-readable form e.g. 1.24 kB

func FsFileExists

func FsFileExists(fsys fs.FS, path string) bool

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 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 MimeTypeFromFileName

func MimeTypeFromFileName(path string) string

func Must

func Must(err error)

func Must2

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

func NormalizeNewlines

func NormalizeNewlines(d []byte) []byte

NormalizeNewlines is like NormalizeNewlinesInPlace but slower because it makes a copy of data

func NormalizeNewlinesInPlace

func NormalizeNewlinesInPlace(d []byte) []byte

NormalizeNewlinesInPlace changes CRLF (Windows) and CR (Mac) to LF (Unix) Optimized for speed, modifies data in place

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 ...interface{})

func PanicIfErr

func PanicIfErr(err error, args ...interface{})

func ParseEnvMust

func ParseEnvMust(d []byte) map[string]string

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 Percent

func Percent(total, sub int64) float64

Percent returns how many percent of total is sub 100% means total == sub, 50% means sub = total / 2

func PrintLineStats

func PrintLineStats(stats *LineStats)

func Push

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

func ReadFileMaybeCompressed

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

ReadFileMaybeCompressed reads file. Ungzips if it's gzipped.

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 Slug

func Slug(s string) string

Slug generates safe url from string by removing hazardous characters

func SlugNoLowerCase

func SlugNoLowerCase(s string) string

func ToTrimmedLines

func ToTrimmedLines(d []byte) []string

func TrimExt

func TrimExt(s string) string

TrimExt removes extension from s

func TrimPrefix

func TrimPrefix(s string, prefix string) (string, bool)

TrimPrefix is like strings.TrimPrefix but also returns a bool indicating that the string was trimmed

func UnzipDataToDir

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

func WinmergeDiffPreview

func WinmergeDiffPreview()

func WriteFileGzipped

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

WriteFileGzipped writes data to a path, using best gzip compression

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 FilterFunc

type FilterFunc func(string) bool

func MakeAllowedFileFilterForExts

func MakeAllowedFileFilterForExts(exts ...string) FilterFunc

func MakeExcludeDirsFilter

func MakeExcludeDirsFilter(dirs ...string) FilterFunc

func MakeFilterAnd

func MakeFilterAnd(filters ...FilterFunc) FilterFunc

func MakeFilterOr

func MakeFilterOr(filters ...FilterFunc) FilterFunc

type FormattedDuration

type FormattedDuration time.Duration

time.Duration with a better string representation

func (FormattedDuration) String

func (d FormattedDuration) String() string

type LineCount

type LineCount struct {
	Name      string
	Ext       string
	LineCount int
}

LineCount describes line count for a file

func LineStatsPerExt

func LineStatsPerExt(fileToCount map[string]*LineCount) []*LineCount

type LineStats

type LineStats struct {
	FileToCount map[string]*LineCount
}

LineStats gathers line count info for files

func NewLineStats

func NewLineStats() *LineStats

NewLineStats returns new LineStats

func (*LineStats) CalcInDir

func (s *LineStats) CalcInDir(dir string, allowedFileFilter func(name string) bool, recur bool) error

type MemoryFS

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

MemoryFS is a custom file system that uses a map to store file data.

func NewMemoryFS

func NewMemoryFS(m map[string][]byte) *MemoryFS

func NewMemoryFSForZipData

func NewMemoryFSForZipData(zipData []byte) (*MemoryFS, error)

func (MemoryFS) Open

func (m MemoryFS) Open(name string) (fs.File, error)

Open implements the fs.FS interface for MemoryFS.

func (MemoryFS) ReadFile

func (m MemoryFS) ReadFile(name string) ([]byte, error)

optimization for fs.ReadFile https://pkg.go.dev/io/fs#ReadFile

Jump to

Keyboard shortcuts

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