vfscopy

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2020 License: MIT Imports: 8 Imported by: 1

README

vfscopy

Recursively copy a Virtual FileSystem, such as http.FileSystem, to a native file system destination.

Works with any file system that implements http.FileSystem, including vfsgen, fileb0x, gobindata and most others.

Example: native file system (os)

httpfs := http.Dir("/tmp/public/")
vfs := vfscopy.NewVFS(httpfs)

if err := vfscopy.CopyAll(vfs, ".", "/tmp/dst/"); nil != err {
    fmt.Fprintf(os.Stderr, "couldn't copy vfs: %v\n", err)
}

Example: vfsgen

Note: vfsgen does not support symlinks or file permissions.

package main

import (
    "fmt"

    "git.rootprojects.org/root/vfscopy"

    // vfsgen-generated file system
    "git.example.com/org/project/assets"
)

func main() {
    vfs := vfscopy.NewVFS(assets.Assets)

    if err := vfscopy.CopyAll(vfs, ".", "/tmp/dst/"); nil != err {
        fmt.Fprintf(os.Stderr, "couldn't copy vfs: %v\n", err)
    }
    fmt.Println("Done.")
}

Test

# Generate the test virtual file system
go generate ./...

# Run the tests
go test ./...

License

The MIT License (MIT)

We used the recursive native file system copy implementation at https://github.com/otiai10/copy as a starting point and added virtual file system support.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CopyAll

func CopyAll(vfs FileSystem, src, dest string, opt ...Options) error

CopyAll copies src to dest, doesn't matter if src is a directory or a file.

Types

type Dir

type Dir string

Dir is an implementation of a Virtual FileSystem

func (Dir) Open

func (d Dir) Open(name string) (File, error)

Open opens a file relative to a virtual filesystem

func (d Dir) Readlink(name string) (string, error)

Readlink returns the destination of the named symbolic link.

type File

type File interface {
	io.Closer
	io.Reader
	io.Seeker
	Readdir(count int) ([]os.FileInfo, error)
	Stat() (os.FileInfo, error)
}

File is copied from http.File

type FileSystem

type FileSystem interface {
	Open(name string) (File, error)
	Readlink(name string) (string, error)
}

FileSystem is a Virtual FileSystem with Symlink support

func NewVFS

func NewVFS(httpfs http.FileSystem) FileSystem

NewVFS gives an http.FileSystem (real) symlink support

type Options

type Options struct {
	// OnSymlink can specify what to do on symlink
	OnSymlink func(src string) SymlinkAction
	// Skip can specify which files should be skipped
	Skip func(src string) (bool, error)
	// AddPermission to every entities,
	// NO MORE THAN 0777
	AddPermission os.FileMode
	// Sync file after copy.
	// Useful in case when file must be on the disk
	// (in case crash happens, for example),
	// at the expense of some performance penalty
	Sync bool
}

Options specifies optional actions on copying.

type SymlinkAction

type SymlinkAction int

SymlinkAction represents what to do on symlink.

const (
	// Shallow creates new symlink to the dest of symlink.
	Shallow SymlinkAction = iota
	// Skip does nothing with symlink.
	Skip
)

type VFS

type VFS struct {
	FileSystem http.FileSystem
}

VFS is a virtual FileSystem with Symlink support

func (*VFS) Open

func (v *VFS) Open(name string) (File, error)

Open opens a file relative to a virtual filesystem

func (v *VFS) Readlink(name string) (string, error)

Readlink returns a "not implemented" error, which is okay because it is never called for http.FileSystem.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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