Path existence checking with configurable logging modes for robust file system operations.
π― Path Existence Validation: Comprehensive checking for paths, files, and directories β‘ Configurable Logging Modes: Multiple log levels (Noisy, Sweet, Quiet, Might) π Type-Safe Validation: Ensures paths match expected types (file vs DIR) π Error Context Enhancement: Rich error wrapping with detailed location information π Convenience Functions: Simple one-line existence checks for common use cases
go get github.com/yyle88/osexistpathpackage main
import (
"os"
"path/filepath"
"github.com/yyle88/must"
"github.com/yyle88/osexistpath/osmustexist"
"github.com/yyle88/rese"
"github.com/yyle88/zaplog"
"go.uber.org/zap"
)
func main() {
tempDIR := rese.V1(os.MkdirTemp("", "osexistpath-demo-*"))
defer func() {
must.Done(os.RemoveAll(tempDIR))
}()
zaplog.SUG.Debug("working in:", tempDIR)
root := osmustexist.ROOT(tempDIR)
zaplog.LOG.Debug("must exist DIR:", zap.String("root", root))
path := filepath.Join(tempDIR, "test.txt")
must.Done(os.WriteFile(path, []byte("hello world"), 0644))
osmustexist.FILE(path)
zaplog.LOG.Debug("must exist FILE:", zap.String("path", path))
data := rese.A1(os.ReadFile(path))
zaplog.LOG.Debug("read data:", zap.ByteString("data", data))
}β¬οΈ Source: Source
package main
import (
"os"
"path/filepath"
"github.com/yyle88/must"
"github.com/yyle88/osexistpath/osmustexist"
"github.com/yyle88/rese"
"github.com/yyle88/zaplog"
"go.uber.org/zap"
)
func main() {
tempDIR := osmustexist.ROOT(rese.V1(os.MkdirTemp("", "osexistpath-demo-*")))
defer func() {
must.Done(os.RemoveAll(tempDIR))
}()
zaplog.SUG.Debug("working in:", tempDIR)
path := filepath.Join(tempDIR, "test.txt")
must.False(osmustexist.IsPath(path))
zaplog.LOG.Debug("must not exist FILE:", zap.String("path", path))
must.Done(os.WriteFile(path, []byte("hello world"), 0644))
must.True(osmustexist.IsFile(path))
zaplog.LOG.Debug("must exist FILE:", zap.String("path", path))
data := rese.A1(os.ReadFile(osmustexist.FILE(path)))
zaplog.LOG.Info("read data:", zap.ByteString("data", data))
}β¬οΈ Source: Source
| Package | Function | Description |
|---|---|---|
| osexistpath | IsPathExists(path, verb) (bool, error) |
Check if path exists with configurable logging |
| osexistpath | IsFileExists(path, verb) (bool, error) |
Check if file exists with type validation |
| osexistpath | IsRootExists(path, verb) (bool, error) |
Check if DIR exists with type validation |
| Package | Function | Description |
|---|---|---|
| osexistpath | IsPathExist(path) (bool, error) |
Check path existence in Quiet mode |
| osexistpath | IsFileExist(path) (bool, error) |
Check file existence in Quiet mode |
| osexistpath | IsRootExist(path) (bool, error) |
Check DIR existence in Quiet mode |
| Package | Function | Description |
|---|---|---|
| osexistpath | IsPath(path) (bool, error) |
Check path existence using Might mode |
| osexistpath | IsFile(path) (bool, error) |
Check file existence using Might mode |
| osexistpath | IsRoot(path) (bool, error) |
Check DIR existence using Might mode |
| Package | Function | Description |
|---|---|---|
| osexistpath | PATH(path) (string, error) |
Return path if exists, error otherwise |
| osexistpath | FILE(path) (string, error) |
Return file path if valid file, error otherwise |
| osexistpath | ROOT(path) (string, error) |
Return DIR path if valid DIR, error otherwise |
| Package | Function | Description |
|---|---|---|
| osexistpath | MustPath(path) |
Assert path exists, panic if not |
| osexistpath | MustFile(path) |
Assert file exists, panic if not |
| osexistpath | MustRoot(path) |
Assert DIR exists, panic if not |
The osmustexist package provides a clean API through auto handling errors with sure.Must(). This eliminates the need to check error returns, making code more concise and readable.
| Package | Function | Description |
|---|---|---|
| osmustexist | IsPathExists(path, verb) bool |
Check path existence with logging, panic on error |
| osmustexist | IsFileExists(path, verb) bool |
Check file existence with type validation, panic on error |
| osmustexist | IsRootExists(path, verb) bool |
Check DIR existence with type validation, panic on error |
| Package | Function | Description |
|---|---|---|
| osmustexist | IsPathExist(path) bool |
Check path existence in Quiet mode, panic on error |
| osmustexist | IsFileExist(path) bool |
Check file existence in Quiet mode, panic on error |
| osmustexist | IsRootExist(path) bool |
Check DIR existence in Quiet mode, panic on error |
| Package | Function | Description |
|---|---|---|
| osmustexist | IsPath(path) bool |
Check path existence using Might mode, panic on error |
| osmustexist | IsFile(path) bool |
Check file existence using Might mode, panic on error |
| osmustexist | IsRoot(path) bool |
Check DIR existence using Might mode, panic on error |
| Package | Function | Description |
|---|---|---|
| osmustexist | PATH(path) string |
Return path if exists, panic otherwise |
| osmustexist | FILE(path) string |
Return file path if valid file, panic otherwise |
| osmustexist | ROOT(path) string |
Return DIR path if valid DIR, panic otherwise |
| Package | Function | Description |
|---|---|---|
| osmustexist | MustPath(path) |
Assert path exists, panic if not |
| osmustexist | MustFile(path) |
Assert file exists, panic if not |
| osmustexist | MustRoot(path) |
Assert DIR exists, panic if not |
| Mode | Description |
|---|---|
Noisy |
Logs all errors and debug information |
Sweet |
Logs just unexpected errors |
Quiet |
No logging on errors |
Might |
Check mode, no error expectations |
Environment Setup (Recommended - osmustexist):
// Use osmustexist for clean setup code - no error handling needed
osmustexist.MustRoot("/project/config")
osmustexist.MustFile("/project/config/app.yaml")Runtime Validation (Recommended - osmustexist):
// Use osmustexist for clean boolean checks
if osmustexist.IsFileExist("config.txt") {
// Process config file - no need to handle error
}Check Operations (Recommended - osmustexist):
// Use osmustexist for simple probe checks
hasConfig := osmustexist.IsFile("config.json")
hasYaml := osmustexist.IsFile("config.yaml")Standard Error Handling (When needed):
// Use base package when you need explicit error handling
if exists, err := osexistpath.IsFileExist("config.txt"); err != nil {
return fmt.Errorf("failed to check file: %w", err)
} else if exists {
// Process config file
}Using osmustexist.PATH for clean validation (Recommended):
// No error handling needed - panics on failure
configPath := osmustexist.PATH("/app/config")Standard PATH function with explicit error handling:
configPath, err := osexistpath.PATH("/app/config")
if err != nil {
return fmt.Errorf("config DIR missing: %w", err)
}Chaining validations with osmustexist.ROOT (Recommended):
// Clean and readable - no error variables needed
dataPath := osmustexist.ROOT("/data")
logsPath := osmustexist.ROOT(filepath.Join(dataPath, "logs"))osmustexist Noisy mode for debugging (Recommended):
exists := osmustexist.IsFileExists("debug.log", osexistpath.Noisy)
// Logs detailed debug information, clean syntaxosmustexist Sweet mode for production (Recommended):
exists := osmustexist.IsRootExists("/var/lib/app", osexistpath.Sweet)
// Logs just unexpected errors, no error handling neededosmustexist: Ensure path is a file (Recommended):
if !osmustexist.IsFile("data.json") {
return errors.New("expected file, found DIR")
}osmustexist: Ensure path is a DIR (Recommended):
if !osmustexist.IsRoot("uploads") {
return errors.New("expected DIR, found file")
}osmustexist: Process config files (Recommended):
if osmustexist.IsFileExist("app-config.yaml") {
config := loadConfig("app-config.yaml")
applyConfig(config)
}osmustexist: Setup logging DIR if exists (Recommended):
if osmustexist.IsRootExist("/var/log/myapp") {
setupFileLogging("/var/log/myapp")
} else {
setupConsoleLogging()
}MIT License - see LICENSE.
Contributions are welcome! Report bugs, suggest features, and contribute code:
- π Mistake reports? Open an issue on GitHub with reproduction steps
- π‘ Fresh ideas? Create an issue to discuss
- π Documentation confusing? Report it so we can improve
- π Need new features? Share the use cases to help us understand requirements
- β‘ Performance issue? Help us optimize through reporting slow operations
- π§ Configuration problem? Ask questions about complex setups
- π’ Follow project progress? Watch the repo to get new releases and features
- π Success stories? Share how this package improved the workflow
- π¬ Feedback? We welcome suggestions and comments
New code contributions, follow this process:
- Fork: Fork the repo on GitHub (using the webpage UI).
- Clone: Clone the forked project (
git clone https://github.com/yourname/repo-name.git). - Navigate: Navigate to the cloned project (
cd repo-name) - Branch: Create a feature branch (
git checkout -b feature/xxx). - Code: Implement the changes with comprehensive tests
- Testing: (Golang project) Ensure tests pass (
go test ./...) and follow Go code style conventions - Documentation: Update documentation to support client-facing changes
- Stage: Stage changes (
git add .) - Commit: Commit changes (
git commit -m "Add feature xxx") ensuring backward compatible code - Push: Push to the branch (
git push origin feature/xxx). - PR: Open a merge request on GitHub (on the GitHub webpage) with detailed description.
Please ensure tests pass and include relevant documentation updates.
Welcome to contribute to this project via submitting merge requests and reporting issues.
Project Support:
- β Give GitHub stars if this project helps you
- π€ Share with teammates and (golang) programming friends
- π Write tech blogs about development tools and workflows - we provide content writing support
- π Join the ecosystem - committed to supporting open source and the (golang) development scene
Have Fun Coding with this package! πππ