union

package
Version: v0.0.0-...-8d4bc4b Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2019 License: MIT Imports: 6 Imported by: 17

Documentation

Overview

Package union offers a simple http.FileSystem that can unify multiple filesystems at various mount points.

Example
package main

import (
	"fmt"
	"log"
	"net/http"
	"os"

	"github.com/shurcooL/httpfs/union"
	"github.com/shurcooL/httpfs/vfsutil"
	"golang.org/x/tools/godoc/vfs/httpfs"
	"golang.org/x/tools/godoc/vfs/mapfs"
)

func walk(path string, fi os.FileInfo, err error) error {
	if err != nil {
		log.Printf("can't stat file %s: %v\n", path, err)
		return nil
	}
	fmt.Println(path)
	return nil
}

func main() {
	fs0 := httpfs.New(mapfs.New(map[string]string{
		"zzz-last-file.txt":   "It should be visited last.",
		"a-file.txt":          "It has stuff.",
		"another-file.txt":    "Also stuff.",
		"folderA/entry-A.txt": "Alpha.",
		"folderA/entry-B.txt": "Beta.",
	}))
	fs1 := httpfs.New(mapfs.New(map[string]string{
		"sample-file.txt":                "This file compresses well. Blaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaah!",
		"not-worth-compressing-file.txt": "Its normal contents are here.",
		"folderA/file1.txt":              "Stuff 1.",
		"folderA/file2.txt":              "Stuff 2.",
		"folderB/folderC/file3.txt":      "Stuff C-3.",
	}))

	fs := union.New(map[string]http.FileSystem{
		"/fs0": fs0,
		"/fs1": fs1,
	})

	err := vfsutil.Walk(fs, "/", walk)
	if err != nil {
		panic(err)
	}

}
Output:

/
/fs0
/fs0/a-file.txt
/fs0/another-file.txt
/fs0/folderA
/fs0/folderA/entry-A.txt
/fs0/folderA/entry-B.txt
/fs0/zzz-last-file.txt
/fs1
/fs1/folderA
/fs1/folderA/file1.txt
/fs1/folderA/file2.txt
/fs1/folderB
/fs1/folderB/folderC
/fs1/folderB/folderC/file3.txt
/fs1/not-worth-compressing-file.txt
/fs1/sample-file.txt
Example (Empty)
package main

import (
	"fmt"
	"log"
	"os"

	"github.com/shurcooL/httpfs/union"
	"github.com/shurcooL/httpfs/vfsutil"
)

func walk(path string, fi os.FileInfo, err error) error {
	if err != nil {
		log.Printf("can't stat file %s: %v\n", path, err)
		return nil
	}
	fmt.Println(path)
	return nil
}

func main() {
	empty := union.New(nil)

	err := vfsutil.Walk(empty, "/", walk)
	if err != nil {
		panic(err)
	}

}
Output:

/
Example (NotExist)
package main

import (
	"fmt"
	"os"

	"github.com/shurcooL/httpfs/union"
)

func main() {
	empty := union.New(nil)

	_, err := empty.Open("/does-not-exist")
	fmt.Println("os.IsNotExist:", os.IsNotExist(err))
	fmt.Println(err)

}
Output:

os.IsNotExist: true
open /does-not-exist: file does not exist

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(mapping map[string]http.FileSystem) http.FileSystem

New creates an union filesystem with the provided mapping of mount points to filesystems.

Each mount point must be of form "/mydir". It must start with a '/', and contain a single directory name.

Types

This section is empty.

Source Files

Jump to

Keyboard shortcuts

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