| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
This package is not in the latest version of its module.
Go to latest Published: Aug 18, 2026 License: Apache-2.0The Go module system was introduced in Go 1.11 and is the official dependency management solution for Go.
Redistributable licenses place minimal restrictions on how software can be used, modified, and redistributed.
Modules with tagged versions give importers more predictable builds.
When a project reaches major version v1 it is considered stable.
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:
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.
IsReadable and IsWritable inspect the file mode, not the effective access of the calling process. That is the portable answer the standard library can give: os.Stat exposes mode bits everywhere, while asking "can I open this?" needs access(2) or an attempted open. A caller that must be certain should open the file and handle the error — the answer can change between any check and the open regardless.
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.
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.
This section is empty.
This section is empty.
Dir returns the directory component of path after expanding it with ExpandPath.
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.
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
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
IsDir reports whether path exists and is a directory.
The path is expanded with ExpandPath before it is checked.
IsFile reports whether path exists and is a regular file.
The path is expanded with ExpandPath before it is checked.
IsReadable reports whether path is a regular file whose owner-read bit is set.
The check is on the file mode, not on the effective access of the calling process. That is the portable answer the standard library can give: os.Stat exposes mode bits on every platform, while asking "can *I* open this?" requires access(2) or an attempted open, neither of which is available portably without cgo or a platform build tag.
So this answers "is this file marked readable by its owner?" — which is what a tool validating a configuration path it created wants to know. A caller that must be certain it can read the file should open it and handle the error; a predicate cannot promise more than the mode bits it read, and the answer can change between the check and the open regardless.
An empty path, a missing path, and a path that is not a regular file all report false.
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
IsWritable reports whether path is a regular file whose owner-write bit is set.
The same mode-bit semantics as IsReadable apply, and for the same reason. The case this exists for is a tool that will later rewrite a file it was handed — a configuration file, a lock file — and would rather refuse at validation time than fail halfway through a write.
An empty path, a missing path, and a path that is not a regular file all report false.
Example ¶
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/slashdevops/fsx"
)
func main() {
dir, err := os.MkdirTemp("", "fsx-example-*")
if err != nil {
fmt.Println("error:", err)
return
}
defer os.RemoveAll(dir)
writable := filepath.Join(dir, "writable.yaml")
if err := os.WriteFile(writable, []byte("a: 1\n"), 0o644); err != nil {
fmt.Println("error:", err)
return
}
readOnly := filepath.Join(dir, "readonly.yaml")
if err := os.WriteFile(readOnly, []byte("a: 1\n"), 0o400); err != nil {
fmt.Println("error:", err)
return
}
fmt.Println(fsx.IsWritable(writable))
fmt.Println(fsx.IsWritable(readOnly))
fmt.Println(fsx.IsReadable(readOnly))
}
Output: true false true
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
This section is empty.
| Back | FazBrowse Home | New Git URL |