matchedfs

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2021 License: MIT Imports: 6 Imported by: 1

README

Matched Fs

Go Reference pipeline status coverage report goreport

This package implements an afero.Fs that allows matcher functions to hide underlying paths or alter permissions of underlying paths.

The interface adds two methods to the Fs:

package matchedfs_test

import (
	"fmt"
	"os"

	"github.com/spf13/afero"
	"gitlab.com/tbhartman/go-matchedfs"
)

type myMatcher struct {
	matchString []string
}
func (m *myMatcher) Match(name string) bool {
	for _, str := range m.matchString {
		if name == str {
			return true
		}
	}
	return false
}

func ExampleFs() {
	fs := matchedfs.New(afero.NewMemMapFs())
	fs.Mkdir("a", os.ModeDir | 0700)
	fs.Mkdir("b", os.ModeDir | 0700)

	fs.SetExcludeMatcher(&myMatcher{[]string{"a/"}})

	_, errA := fs.Stat("a")
	if errA == nil {
		fmt.Println("a exists")
	} else {
		fmt.Println("a does not exist")
	}
	_, errB := fs.Stat("b")
	if errB == nil {
		fmt.Println("b exists")
	} else {
		fmt.Println("b does not exist")
	}

	// Output:
	// a does not exist
	// b exists
}

Documentation

Overview

Package matchedfs allows to hide or alter permissions of an underlying afero.Fs.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Fs

type Fs interface {
	afero.Fs

	// SetExcludeMatcher sets the Matcher to be used for excluding/hiding
	// files from the filesystem.
	SetExcludeMatcher(Matcher)
	SetExcludePathMatcher(PathMatcher)

	// AddPermissionMatcher adds a Matcher which applies the os.FileMode mask
	// to the matched paths mode bits.  Specifically, a given file/directory
	// permissions are bitcleared by mode.
	AddPermissionMatcher(m Matcher, mode os.FileMode)
	AddPermissionPathMatcher(p PathMatcher, mode os.FileMode)
}

Fs exposes the underlying afero.Fs according to the exclude and permission matchers. If the exclude matcher matches a path, associated operations return an os.ErrNotExists. If one of the permission matchers masks in such a way as to not allow an operation, the associated operation returns os.ErrPermission.

Example
package main

import (
	"fmt"
	"os"

	"github.com/spf13/afero"
	"gitlab.com/tbhartman/go-matchedfs"
)

type myMatcher struct {
	matchString []string
}

func (m *myMatcher) Match(name string) bool {
	for _, str := range m.matchString {
		if name == str {
			return true
		}
	}
	return false
}

func main() {
	fs := matchedfs.New(afero.NewMemMapFs())
	fs.Mkdir("a", os.ModeDir|0700)
	fs.Mkdir("b", os.ModeDir|0700)

	fs.SetExcludeMatcher(&myMatcher{[]string{"a/"}})

	_, errA := fs.Stat("a")
	if errA == nil {
		fmt.Println("a exists")
	} else {
		fmt.Println("a does not exist")
	}
	_, errB := fs.Stat("b")
	if errB == nil {
		fmt.Println("b exists")
	} else {
		fmt.Println("b does not exist")
	}

}
Output:

a does not exist
b exists

func New

func New(underlyingFs afero.Fs) Fs

New creates a new matchedfs

type Matcher

type Matcher interface {
	Match(path string) bool
}

Matcher is the interface to match a path. The path is passed through filepath.Clean and filepath.ToSlash. If it matches an existing directory, a slash is appended. The resulting path is passed to Match.

type PathMatcher

type PathMatcher interface {
	MatchesPath(path string) bool
}

PathMatcher replicates the interface used by "github.com/sabhiram/go-gitignore" .

Jump to

Keyboard shortcuts

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