osfs

package
v0.0.0-...-7b20d7d Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Example
package main

import (
	"fmt"
	"os"
	"path/filepath"

	"github.com/halimath/fsx"
	"github.com/halimath/fsx/osfs"
)

func main() {
	// Create a temporary directory to use as a root
	dir, err := os.MkdirTemp("", "fsx_example_*")
	if err != nil {
		panic(err)
	}
	// Make sure the directory is removed at the end of the test.
	defer os.RemoveAll(dir)

	// Create a fsx.FS using the temp dir.
	fsys := osfs.DirFS(dir)

	// Create a file inside the fsys.
	f, err := fsx.Create(fsys, "test.md")
	if err != nil {
		panic(err)
	}

	// Write some content to the file.
	if _, err := f.Write([]byte("# fsx example test\n\nThis is just an example.")); err != nil {
		panic(err)
	}

	if err := f.Close(); err != nil {
		panic(err)
	}

	// Create a symlink inside the fsys
	if err := fsys.Symlink("test.md", "README.md"); err != nil {
		panic(err)
	}

	// Now try to read the symlinked file using os functions.
	content, err := os.ReadFile(filepath.Join(dir, "README.md"))
	if err != nil {
		panic(err)
	}

	fmt.Println(string(content))
}
Output:

# fsx example test

This is just an example.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalid     = os.ErrInvalid
	ErrInvalidRoot = errors.New("fsx: DirFS with empty root")
)

Functions

func DirFS

func DirFS(root string) fsx.LinkFS

DirFS returns an OS backed filesystem rooted at root. This function works as a counterpart to os.DirFS and behaves equivalent. Notice that any error is delayed until other methods of the returned FS are called.

Types

This section is empty.

Jump to

Keyboard shortcuts

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