A Go helper package providing utilities for filesystem operations, archive extraction, and file downloads.
go get github.com/gnames/gnsys- File & Directory Operations: Check existence, create directories, copy files, detect file types
- Archive Extraction: Extract zip, tar, gzip, xz, bzip2 archives and combinations
- File Downloads: HTTP downloads with optional progress bars
- Path Utilities: Tilde expansion, path splitting
- Text File Detection: Heuristic-based text file identification
import "github.com/gnames/gnsys"
// Check if file exists
exists, err := gnsys.FileExists("path/to/file.txt")
// Check if directory exists and if it's empty
exists, empty, err := gnsys.DirExists("path/to/dir")
// Create directory (with parent directories)
err := gnsys.MakeDir("path/to/new/dir")
// Clean directory (remove all contents or create if absent)
err := gnsys.CleanDir("path/to/dir")
// Get directory state
state := gnsys.GetDirState("path/to/dir")
// Returns: DirAbsent, DirEmpty, DirNotEmpty, NotDir, or Unknown
// Copy file
bytesCopied, err := gnsys.CopyFile("source.txt", "dest.txt")
// Detect if file is text
isText, err := gnsys.IsTextFile("path/to/file")// Expand tilde in path
fullPath, err := gnsys.ConvertTilda("~/documents/file.txt")
// Split path into directory, base name, and extension
dir, base, ext := gnsys.SplitPath("/path/to/file.tar.gz")
// dir: "/path/to", base: "file.tar", ext: ".gz"// Extract various archive formats
err := gnsys.ExtractZip("archive.zip", "dest/dir")
err := gnsys.ExtractTar("archive.tar", "dest/dir")
err := gnsys.ExtractGz("file.gz", "dest/dir")
err := gnsys.ExtractXz("file.xz", "dest/dir")
err := gnsys.ExtractTarGz("archive.tar.gz", "dest/dir")
err := gnsys.ExtractTarXz("archive.tar.xz", "dest/dir")
err := gnsys.ExtractTarBz2("archive.tar.bz2", "dest/dir")// Get file type based on extension
ft := gnsys.GetFileType("archive.tar.gz")
// Returns: TarGzFT
fmt.Println(ft.String()) // Prints: "tar-gzip"
// Available file types:
// ZipFT, GzFT, XzFT, TarFT, TarGzFT, TarXzFt, TarBzFT, SqlFT, SqliteFT// Download file without progress bar
filePath, err := gnsys.Download("https://example.com/file.zip", "/dest/dir", false)
// Download file with progress bar
filePath, err := gnsys.Download("https://example.com/file.zip", "/dest/dir", true)
// Check if server is reachable
isReachable := gnsys.Ping("example.com:80", 3) // 3 second timeoutThe package provides custom error types for better error handling:
ErrFileMissing: File not found at specified pathErrNotFile: Path is not a regular fileErrNotDir: Path is not a directoryErrExtract: Archive extraction failedErrDownload: File download failed
Run tests using:
just testOr directly with Go:
go test -vLicensed under MIT (see LICENSE file)