This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
gnsys is a Go helper package for filesystem operations, focusing on file/directory management, archive extraction, and file downloads. It's designed as a utility library to be imported by other Go projects.
# Run all tests
go test
# Run tests with verbose output
go test -v
# Run a specific test
go test -run TestFunctionName
# Run tests with coverage
go test -cover# Build (if needed for validation)
go build
# Install as a module (for use in other projects)
go mod tidy# Format code
go fmt ./...
# Run go vet
go vet ./...gnsys.go: Core filesystem operations
DirStateenum: Represents directory states (Unknown, NotDir, DirAbsent, DirEmpty, DirNotEmpty)- Directory operations:
GetDirState,MakeDir,CleanDir,DirExists - File operations:
FileExists,IsFile,IsDir,IsTextFile,CopyFile - Path utilities:
ConvertTilda(expands~/),SplitPath(breaks path into dir/base/ext)
extract.go: Archive extraction functionality
- Defines
Extractorfunction type:func(src, dst string) error - Extraction functions for multiple formats:
ExtractZip: .zip archivesExtractTar: .tar archivesExtractGz: .gz compressed filesExtractTarGz: .tar.gz archivesExtractTarBz2: .tar.bz2 archivesExtractTarXz: .tar.xz archives
- Internal
untarhelper for tar-based formats
download.go: Network operations
Ping(host, seconds): TCP connectivity check (format: "host:port")Download(url, destDir, showProgress): HTTP file download with optional progress bar usinggithub.com/cheggaaa/pb/v3
filetype.go: File type detection
FileTypeenum with constants: ZipFT, GzFT, TarFT, TarGzFT, TarXzFt, TarBzFT, SqlFT, SqliteFTGetFileType(file): Determines file type based on extension (suffix matching)
errors.go: Custom error types
ErrFileMissing: File not found at pathErrNotFile: Path is not a regular fileErrNotDir: Path is not a directoryErrExtract: Archive extraction failureErrDownload: Download operation failure
- Error Wrapping: Custom error types wrap underlying errors with context (path/URL)
- Functional Types:
Extractortype allows flexibility in extraction implementations - Progress Reporting: Download function supports optional progress bar display
- Path Safety:
ConvertTildaensures paths with~/work correctly
- Tests use
testify/assertandmatryer/isfor assertions - Test data stored in
testdata/directory - Network test downloads from
opendata.globalnames.org(may require internet connection) - Tests create temporary directories for validation
- Progress Bar: The
Downloadfunction withshowProgress=trueusespb.CleanOnFinishto clear the progress bar after completion (see download.go:66) - Tar Extraction: The
untarfunction validates extracted directory is not empty to detect bad tar files (see extract.go:215-221) - Text File Detection:
IsTextFilechecks only first 20 lines and considers null bytes, non-printable character ratio (>30%), and line length (see gnsys.go:129-178) - File Type Order: When detecting file types, check for multi-extension formats (
.tar.gz,.tar.xz,.tar.bz2) before single extensions (.gz) to avoid false positives