fsutil

package module
v0.0.0-...-8a6f9c4 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2026 License: Apache-2.0 Imports: 6 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Go Reference Code Coverage License

Take a filesystem, not a path.

fsutil provides file operations written against afero.Fs rather than against the real filesystem, so the code that calls them can be tested without touching a disk.

Status: early. Only copying is here so far. The rest arrives as each operation earns its place.

Introduction

A function that calls os.Create has decided, permanently, that it writes to the real filesystem. Testing it means a temporary directory, cleanup, and the quiet possibility of one test seeing another's leftovers. A function that takes an afero.Fs has decided nothing: production passes afero.NewOsFs(), a test passes afero.NewMemMapFs(), and the same code runs against both.

That is the convention this module exists to make cheap. afero supplies the filesystem abstraction and the operations the standard library has; fsutil adds the ones it does not, in the same shape — filesystem first, paths after.

// Reaches for the disk, whatever the caller wanted.
func writeReport(path string) error

// Writes wherever it is told to.
func writeReport(fs afero.Fs, path string) error

Usage

go get github.com/MarkRosemaker/fsutil
import (
    "github.com/MarkRosemaker/fsutil"
    "github.com/spf13/afero"
)

// Copies within one filesystem, creating the destination directory as needed.
if err := fsutil.Copy(afero.NewOsFs(), "api/openapi.json", "build/openapi.json"); err != nil {
    log.Fatal(err)
}

The copy creates any missing parent directories, preserves the source file's permissions on a best-effort basis, and syncs before returning. Copying a file onto itself, which would empty it, fails with ErrSameFile instead.

In a test, the same call runs entirely in memory:

fs := afero.NewMemMapFs()
afero.WriteFile(fs, "src.txt", []byte("hello"), 0o644)

if err := fsutil.Copy(fs, "src.txt", "nested/dst.txt"); err != nil {
    t.Fatal(err)
}

When the filesystem is not in question

Command-line tools and code generators operate on the real filesystem by definition, and threading afero.NewOsFs() through them buys nothing. The osutil subpackage is the same operations with that argument already applied:

import "github.com/MarkRosemaker/fsutil/osutil"

if err := osutil.Copy("api/openapi.json", "build/openapi.json"); err != nil {
    log.Fatal(err)
}
Package Signature Use when
fsutil Copy(fs afero.Fs, src, dst string) error The caller should be able to choose the filesystem — which is most of the time
fsutil/osutil Copy(src, dst string) error The real filesystem is the whole point, as in a CLI or a generator

Used by

The openapi family reaches for osutil in the generators that build their test fixtures: openapi-flatten, openapi-compress and openapi-codegen.

Additional Information

Contributing

Contributions are welcome — please open an issue or a pull request on GitHub.

License

This project is licensed under the Apache 2.0 License.

Documentation

Overview

Package fsutil provides utilities for working with afero.Fs (and real OS filesystem via the os subpackage).

Index

Constants

This section is empty.

Variables

View Source
var ErrSameFile = errors.New("source and destination are the same file")

ErrSameFile is returned when the source and the destination of a copy are one file, which copying would empty.

Functions

func Copy

func Copy(fs afero.Fs, src, dst string) error

Copy copies a single file within the same filesystem.

func MkdirAll

func MkdirAll(fs afero.Fs, path string, perm os.FileMode) error

MkdirAll creates a directory and all parent directories.

Types

This section is empty.

Directories

Path Synopsis
Package osutil provides convenient functions that operate directly on the real OS filesystem.
Package osutil provides convenient functions that operate directly on the real OS filesystem.

Jump to

Keyboard shortcuts

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