Documentation
¶
Overview ¶
Package fsx provides small filesystem helpers built only on the Go standard library and targets the latest Go toolchain version declared by the module.
The package focuses on behavior that is useful across command-line tools and services:
- expanding user-facing paths that start with ~ or contain $HOME
- checking whether paths exist and whether they are regular files or directories
- confirming that a path remains inside a base directory after normalization
- matching file extensions case-insensitively
- writing files atomically by replacing the destination with a temporary file
Path Expansion ¶
ExpandPath expands a leading ~ with the current user's home directory and replaces $HOME references with the HOME environment variable. If the home directory cannot be determined, the original path is returned unchanged.
Atomic Writes ¶
WriteFileAtomic writes data to a temporary file in the destination directory, sets the requested permissions, and renames the temporary file over the target. This prevents readers from observing partially written file contents on the same filesystem.
Dependencies ¶
fsx has zero third-party dependencies and no external module requirements. It uses os, path/filepath, strings, and other standard library packages, and the module currently targets Go 1.26.3.
Index ¶
- func Dir(path string) string
- func Exists(path string) bool
- func ExpandPath(path string) string
- func HasExtension(path string, extensions ...string) bool
- func IsDir(path string) bool
- func IsFile(path string) bool
- func IsWithin(base, target string) bool
- func WriteFileAtomic(path string, data []byte, perm os.FileMode) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Dir ¶
Dir returns the directory component of path after expanding it with ExpandPath.
func Exists ¶
Exists reports whether a file, directory, or other filesystem entry exists.
The path is expanded with ExpandPath before it is checked. Empty paths and paths that cannot be statted return false.
func ExpandPath ¶
ExpandPath expands a user-facing filesystem path.
It replaces a leading ~ with the current user's home directory and replaces all $HOME references with the HOME environment variable. Empty paths are returned unchanged. If the home directory cannot be determined, the original path is returned unchanged.
Example ¶
ExampleExpandPath demonstrates expanding a user-facing path.
package main
import (
"fmt"
"path/filepath"
"github.com/slashdevops/fsx"
)
func main() {
expanded := fsx.ExpandPath("~/config.yaml")
fmt.Println(filepath.IsAbs(expanded))
}
Output: true
func HasExtension ¶
HasExtension reports whether path has one of the provided extensions.
Extension matching is case-insensitive. Extensions may be passed with or without a leading dot. The path does not need to exist.
Example ¶
ExampleHasExtension demonstrates extension matching.
package main
import (
"fmt"
"github.com/slashdevops/fsx"
)
func main() {
fmt.Println(fsx.HasExtension("config.YAML", "yaml", "json"))
fmt.Println(fsx.HasExtension("README", "md"))
}
Output: true false
func IsDir ¶
IsDir reports whether path exists and is a directory.
The path is expanded with ExpandPath before it is checked.
func IsFile ¶
IsFile reports whether path exists and is a regular file.
The path is expanded with ExpandPath before it is checked.
func IsWithin ¶
IsWithin reports whether target resolves to a path contained inside base.
Both paths are expanded, cleaned, and made absolute before comparison. The function returns false when either path is empty, either path cannot be resolved, or the relative path from base to target escapes base with "..". A target equal to base is considered within base.
Example ¶
ExampleIsWithin demonstrates a containment check before deleting a file.
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/slashdevops/fsx"
)
func main() {
base := filepath.Join(os.TempDir(), "workspace")
target := filepath.Join(base, "page.md")
fmt.Println(fsx.IsWithin(base, target))
fmt.Println(fsx.IsWithin(base, filepath.Join(base, "..", "escape.md")))
}
Output: true false
func WriteFileAtomic ¶
WriteFileAtomic writes data to path by replacing it with a completed temporary file in the same directory.
The destination directory must already exist. The temporary file is created in that directory, written, closed, chmodded to perm, and renamed to path. If any step fails before the rename, the temporary file is removed.
Example ¶
ExampleWriteFileAtomic demonstrates an atomic file replacement.
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"github.com/slashdevops/fsx"
)
func main() {
dir, err := os.MkdirTemp("", "fsx-example-*")
if err != nil {
log.Fatal(err)
}
defer func() {
if err := os.RemoveAll(dir); err != nil {
log.Fatal(err)
}
}()
path := filepath.Join(dir, "config.txt")
if err := fsx.WriteFileAtomic(path, []byte("ready"), 0o600); err != nil {
log.Fatal(err)
}
data, err := os.ReadFile(path)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(data))
}
Output: ready
Types ¶
This section is empty.