revhttpfs

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2021 License: MIT Imports: 6 Imported by: 1

README

RevHttpFs

The afero golang library already has an afero.HttpFs which goes from an afero Fs to http.FileSystem. This package implements the reverse, allowing use of an http.FileSystem as an afero.Fs.

Motivation

The reason for this is that many tools expose http.FileSystem interfaces, since it is the standard in golang for serving files.

In particular, tools to embed files in golang executables usually return http.FileSystems (see statik, packr, vfsgen, etc).

Allowing to use these with Afero leads to some really useful stuff, such as using afero.CopyOnWriteFs to "overlay" a configuration folder/memory map over the http.FileSystem returned from such tools.

Example

import (

    "github.com/rakyll/statik/fs"
    "github.com/spf13/afero"
    "github.com/dkumor/revhttpfs"
)

func OverwritableBuiltinFileSystem() (afero.Fs,error) {
    statikFS, err := fs.New()
    if err!=nil {
        return nil,err
    }

    aferoStatikFs := revhttpfs.NewReverseHttpFs(statikFS)
    overwritableStatikFs := afero.NewMemMapFs()

    return afero.NewCopyOnWriteFs(aferoStatikFs, pluginFs),nil
}

The above code uses resources embedded in the executable with statik, which come in the form of an http.FileSystem, uses ReverseHttpFs to convert it to an afero.Fs, and then creates a copy-on-write overlay over the static built-in files, using a memory map.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ReverseHttpFile

type ReverseHttpFile struct {
	http.File
}

ReverseHttpFile takes an http.File and matches it to File

func (ReverseHttpFile) Name

func (a ReverseHttpFile) Name() string

Name returns the file's name

func (ReverseHttpFile) ReadAt

func (a ReverseHttpFile) ReadAt(p []byte, off int64) (int, error)

ReadAt seeks, then reads

func (ReverseHttpFile) Readdirnames

func (a ReverseHttpFile) Readdirnames(n int) ([]string, error)

Readdirnames returns a list of names from Readdir

func (ReverseHttpFile) Sync

func (a ReverseHttpFile) Sync() error

Sync does nothing here

func (ReverseHttpFile) Truncate

func (a ReverseHttpFile) Truncate(size int64) error

Truncate always returns a permissions error, since an http.FileSystem is read-only

func (ReverseHttpFile) Write

func (a ReverseHttpFile) Write(n []byte) (int, error)

Write always returns a permissions error, since an http.FileSystem is read-only

func (ReverseHttpFile) WriteAt

func (a ReverseHttpFile) WriteAt(p []byte, off int64) (int, error)

WriteAt always returns a permissions error, since an http.FileSystem is read-only

func (ReverseHttpFile) WriteString

func (a ReverseHttpFile) WriteString(s string) (int, error)

WriteString always returns a permissions error, since an http.FileSystem is read-only

type ReverseHttpFs

type ReverseHttpFs struct {
	http.FileSystem
}

ReverseHttpFs converts an http.Filesystem into an afero Fs

func NewReverseHttpFs

func NewReverseHttpFs(fs http.FileSystem) ReverseHttpFs

Given an http.FileSystem, returns an Fs

func (ReverseHttpFs) Chmod

func (fs ReverseHttpFs) Chmod(n string, m os.FileMode) error

Chmod always returns a permissions error, since an http.FileSystem is read-only

func (ReverseHttpFs) Chown

func (fs ReverseHttpFs) Chown(name string, uid, gid int) error

Chown always returns a permissions error, since an http.FileSystem is read-only

func (ReverseHttpFs) Chtimes

func (fs ReverseHttpFs) Chtimes(n string, a, m time.Time) error

Chtimes always returns a permissions error, since an http.FileSystem is read-only

func (ReverseHttpFs) Create

func (fs ReverseHttpFs) Create(n string) (afero.File, error)

Create always returns a permissions error, since an http.FileSystem is read-only

func (ReverseHttpFs) LstatIfPossible

func (fs ReverseHttpFs) LstatIfPossible(name string) (os.FileInfo, bool, error)

LstatIfPossible always fails here

func (ReverseHttpFs) Mkdir

func (fs ReverseHttpFs) Mkdir(n string, p os.FileMode) error

Mkdir always returns a permissions error, since an http.FileSystem is read-only

func (ReverseHttpFs) MkdirAll

func (r ReverseHttpFs) MkdirAll(n string, p os.FileMode) error

MkdirAll always returns a permissions error, since an http.FileSystem is read-only

func (ReverseHttpFs) Name

func (fs ReverseHttpFs) Name() string

Name returns "ReverseHttpFs"

func (ReverseHttpFs) Open

func (fs ReverseHttpFs) Open(n string) (afero.File, error)

Open opens the given file (readonly)

func (ReverseHttpFs) OpenFile

func (fs ReverseHttpFs) OpenFile(name string, flag int, perm os.FileMode) (afero.File, error)

OpenFile opens the file (readonly)

func (ReverseHttpFs) ReadDir

func (fs ReverseHttpFs) ReadDir(name string) ([]os.FileInfo, error)

ReadDir reads the given file as a directory

func (ReverseHttpFs) Remove

func (fs ReverseHttpFs) Remove(n string) error

Remove always returns a permissions error, since an http.FileSystem is read-only

func (ReverseHttpFs) RemoveAll

func (fs ReverseHttpFs) RemoveAll(p string) error

RemoveAll always returns a permissions error, since an http.FileSystem is read-only

func (ReverseHttpFs) Rename

func (fs ReverseHttpFs) Rename(o, n string) error

Rename always returns a permissions error, since an http.FileSystem is read-only

func (ReverseHttpFs) Stat

func (fs ReverseHttpFs) Stat(name string) (os.FileInfo, error)

Stat runs Stat on the file

Jump to

Keyboard shortcuts

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