file

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2026 License: MIT Imports: 4 Imported by: 0

README

go-file

Go library providing cross-platform file system utilities: path resolution, existence checks, directory creation, and writability testing.

Installation

go get github.com/eslider/go-file

Features

  • Project root detection (walks up to find etc/ or data/ directories)
  • File/directory existence checks
  • File size queries
  • Directory writability testing
  • Recursive directory creation

Quick Start

package main

import (
    "fmt"

    file "github.com/eslider/go-file"
)

func main() {
    // Check if a file exists
    if file.Exists("/etc/hosts") {
        fmt.Println("File exists, size:", file.Size("/etc/hosts"), "bytes")
    }

    // Ensure a directory exists
    if err := file.PreCreateDirectory("/tmp/my-app/data"); err != nil {
        panic(err)
    }

    // Check if a directory is writable
    if file.IsWritable("/tmp/my-app/data") {
        fmt.Println("Directory is writable")
    }

    // Get project root path
    root, err := file.GetModRootPath()
    if err != nil {
        fmt.Println("Not inside a project:", err)
    } else {
        fmt.Println("Project root:", root)
    }
}

Use Cases

Test Data Loader
func loadTestFixture(name string) []byte {
    root := file.GetRootPath()
    data, _ := os.ReadFile(filepath.Join(root, "testdata", name))
    return data
}
Safe Directory Setup
func initStorage(base string) error {
    dirs := []string{"uploads", "cache", "logs"}
    for _, d := range dirs {
        path := filepath.Join(base, d)
        if err := file.PreCreateDirectory(path); err != nil {
            return fmt.Errorf("failed to create %s: %w", d, err)
        }
        if !file.IsWritable(path) {
            return fmt.Errorf("%s is not writable", d)
        }
    }
    return nil
}

API

Function Description
Exists(path) Check if file or directory exists
IsExist(path) Check if path exists and is a file (not directory)
Size(path) Get file size in bytes (0 if not found)
IsWritable(path) Test if directory is writable
PreCreateDirectory(path) Create directory and parents if needed
GetModRootPath() Find project root by walking up to etc/ or data/
GetRootPath() Cached project root (panics if not found)
GetCmdRootPath() Current dir, stripping /etc/ suffix

License

MIT

Documentation

Overview

Package file provides cross-platform file system utilities for path resolution, existence checks, directory creation, and writability testing.

Designed for use in Go applications and test suites that need to locate project root directories, verify file paths, and manage directories.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Exists

func Exists(path string) bool

Exists returns true if the path exists (file or directory).

func GetCmdRootPath

func GetCmdRootPath() string

GetCmdRootPath returns the current working directory, stripping any /etc/ suffix.

func GetModRootPath

func GetModRootPath() (string, error)

GetModRootPath walks up from the current working directory looking for a project root (a directory containing "etc" or "data" subdirectories). Returns the path and an error if no root marker is found.

func GetRootPath

func GetRootPath() string

GetRootPath returns the cached project root path, panicking if it cannot be found.

func IsExist

func IsExist(path string) bool

IsExist returns true if the path exists and is not a directory.

func IsWritable

func IsWritable(path string) bool

IsWritable tests whether the given directory path is writable by creating and immediately removing a temporary file.

func PreCreateDirectory

func PreCreateDirectory(path string) error

PreCreateDirectory ensures the directory at path exists, creating it (and any parents) if necessary.

func Size

func Size(path string) int64

Size returns the file size in bytes, or 0 if the file does not exist.

Types

This section is empty.

Jump to

Keyboard shortcuts

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