gnsys

package module
v0.4.3 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2025 License: MIT Imports: 19 Imported by: 32

README

gnsys

A Go helper package providing utilities for filesystem operations, archive extraction, and file downloads.

Installation

go get github.com/gnames/gnsys

Features

  • 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

Usage

File and Directory Operations
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")
Path Utilities
// 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"
Archive Extraction
// 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")
File Type Detection
// 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
File Downloads
// 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 timeout

Error Types

The package provides custom error types for better error handling:

  • ErrFileMissing: File not found at specified path
  • ErrNotFile: Path is not a regular file
  • ErrNotDir: Path is not a directory
  • ErrExtract: Archive extraction failed
  • ErrDownload: File download failed

Testing

Run tests using:

just test

Or directly with Go:

go test -v

License

Licensed under MIT (see LICENSE file)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CleanDir

func CleanDir(dir string) error

CleanDir removes all files from a directory or creates the directory if it is absent.

func ConvertTilda

func ConvertTilda(path string) (string, error)

ConvertTilda expands paths with `~/` to an actual home directory.

func CopyFile added in v0.3.9

func CopyFile(src, dst string) (int64, error)

CopyFile copies a file from src to dst. If dst does not exist, it is created. If dst already exists, its contents are truncated.

func DirExists added in v0.2.0

func DirExists(path string) (exists bool, empty bool, err error)

DirExists checks if directory exists and if it is empty

func Download added in v0.3.0

func Download(rawURL, destDir string, showProgress bool) (string, error)

Download copies remote file to local drive. It provides the name of downloaded file and error as output. Supports http://, https://, and file:// URLs.

func ExtractBz2 added in v0.4.2

func ExtractBz2(srcPath, dstDir string) error

ExtractBz2 extracts a bz2 compressed file located at srcPath to the destination directory dstDir.

func ExtractGz added in v0.3.7

func ExtractGz(srcPath, dstDir string) error

ExtractGz extracts a gz compressed file located at srcPath to the destination directory dstDir.

func ExtractTar added in v0.3.5

func ExtractTar(srcPath, dstDir string) error

ExtractTar extracts a tar archive located at srcPath to the destination directory dstDir.

func ExtractTarBz2 added in v0.3.5

func ExtractTarBz2(srcPath, dstDir string) error

ExtractTarBz2 extracts a tar.bz2 archive located at srcPath to the destination directory dstDir.

func ExtractTarGz added in v0.3.5

func ExtractTarGz(srcPath, dstDir string) error

ExtractTarGz extracts a tar.gz archive located at srcPath to the destination directory dstDir.

func ExtractTarXz added in v0.3.5

func ExtractTarXz(srcPath, dstDir string) error

ExtractTarXz extracts a tar.xz archive located at srcPath to the destination directory dstDir.

func ExtractXz added in v0.4.1

func ExtractXz(srcPath, dstDir string) error

ExtractXz extracts an xz compressed file located at srcPath to the destination directory dstDir.

func ExtractZip added in v0.3.5

func ExtractZip(srcPath, dstDir string) error

ExtractZip extracts a zip archive located at srcPath to the destination directory dstDir.

func FileExists

func FileExists(f string) (bool, error)

FileExists checks if a file exists, and that it is a regular file.

func IsDir added in v0.2.0

func IsDir(path string) bool

func IsFile added in v0.2.0

func IsFile(path string) bool

func IsTextFile added in v0.3.8

func IsTextFile(path string) (bool, error)

func MakeDir

func MakeDir(dir string) error

MakeDir a directory out of a given unless it already exists.

func Ping added in v0.3.3

func Ping(host string, seconds int) bool

Ping checks if a server is reachable. Host should be in format "host:port" (eg "google.com:80")

func SplitPath added in v0.3.4

func SplitPath(path string) (dir, base, ext string)

SplitPath breaks path into directory, file name and extension.

Types

type DirState added in v0.2.4

type DirState int

DirState represents the state of a directory.

const (
	Unknown DirState = iota
	NotDir
	DirAbsent
	DirEmpty
	DirNotEmpty
)

func GetDirState added in v0.2.4

func GetDirState(dir string) DirState

GetDirState returns the state of a directory.

func (DirState) String added in v0.2.4

func (d DirState) String() string

String returns a string representation of the DirState.

type ErrDownload added in v0.3.4

type ErrDownload struct {
	URL string
	Err error
}

ErrDownload is returned when a file download operation fails. The URL field specifies the URL that was being downloaded, and the Err field contains the underlying error that caused the download to fail.

func (*ErrDownload) Error added in v0.3.4

func (e *ErrDownload) Error() string

type ErrExtract added in v0.3.5

type ErrExtract struct {
	Path string
	Err  error
}

ErrExtract is returned when the extraction of a file (e.g., Zip, Tar file) fails. The Path field specifies the file that was being extracted, and the Err field contains the underlying error that caused the extraction to fail.

func (*ErrExtract) Error added in v0.3.5

func (e *ErrExtract) Error() string

type ErrFileMissing added in v0.3.5

type ErrFileMissing struct {
	Path string
}

ErrFileMissing indicates that a file at the specified path could not be found.

func (*ErrFileMissing) Error added in v0.3.5

func (e *ErrFileMissing) Error() string

type ErrNotDir added in v0.3.5

type ErrNotDir struct {
	Path string
}

ErrNotDir indicates that the path provided does not refer to a directory. It might be a file, symbolic link, or another type of file system entry.

func (*ErrNotDir) Error added in v0.3.5

func (e *ErrNotDir) Error() string

type ErrNotFile added in v0.3.5

type ErrNotFile struct {
	Path string
}

ErrNotFile indicates that the path provided does not refer to a regular file. It might be a directory, symbolic link, or another type of file system entry.

func (*ErrNotFile) Error added in v0.3.5

func (e *ErrNotFile) Error() string

type Extractor added in v0.3.5

type Extractor func(src, dst string) error

type FileType added in v0.3.5

type FileType int
const (
	UnknownFT FileType = iota
	ZipFT              // .zip
	GzFT               // .gz
	TarFT              // .tar
	TarGzFT            // .tar.gz
	TarXzFt            // .tar.xz
	TarBzFT            // .tar.bz2
	Bz2FT              // .bz2
	XzFT               // .xz
	SqlFT              // .sql
	SqliteFT           // .sqlite
)

func GetFileType added in v0.3.5

func GetFileType(file string) FileType

func (FileType) String added in v0.3.5

func (ft FileType) String() string

Jump to

Keyboard shortcuts

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