fsdoublestar

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2021 License: MIT Imports: 6 Imported by: 2

README

fsdoublestar

build coverage report doc

This package implements recursive directory globbing via ** for Go's io/fs.

Example

package main

import (
	"fmt"
	"os"

	"github.com/forensicanalysis/fsdoublestar"
)

func main() {
	// get file system for this repository
	wd, _ := os.Getwd()
	fsys := os.DirFS(wd)

	// get all yml files
	matches, _ := fsdoublestar.Glob(fsys, "**/*.yml")

	// print matches
	fmt.Println(matches)
	// Output: [.github/workflows/ci.yml .github/.golangci.yml]
}

Acknowledgement

This repository is based on Bob Matcuk's great doublestar package.

Documentation

Overview

Package glob provides a globing function for io/fs.

Example
package main

import (
	"fmt"
	"os"

	"github.com/forensicanalysis/fsdoublestar"
)

func main() {
	// get file system for this repository
	wd, _ := os.Getwd()
	fsys := os.DirFS(wd)

	// get all yml files
	matches, _ := fsdoublestar.Glob(fsys, "**/*.yml")

	// print matches
	fmt.Println(matches)
}
Output:

[.github/workflows/ci.yml .github/.golangci.yml]

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrBadPattern = path.ErrBadPattern

ErrBadPattern indicates a pattern was malformed.

Functions

func Glob

func Glob(fsys fs.FS, pattern string) (matches []string, err error)

Glob returns the names of all files matching pattern or nil if there is no matching file. The syntax of pattern is the same as in Match. The pattern may describe hierarchical names such as /usr/*/bin/ed (assuming the Separator is '/').

Glob ignores file system errors such as I/O errors reading directories. The only possible returned error is ErrBadPattern, when pattern is malformed.

Your system path separator is automatically used. This means on systems where the separator is '\\' (Windows), escaping will be disabled.

Note: this is meant as a drop-in replacement for filepath.Glob().

func Match

func Match(pattern, name string) (bool, error)

Match returns true if name matches the shell file name pattern. The pattern syntax is:

pattern:
  { term }
term:
  '*'         matches any sequence of non-path-separators
  '**'        matches any sequence of characters, including
              path separators.
  '?'         matches any single non-path-separator character
  '[' [ '^' ] { character-range } ']'
        character class (must be non-empty)
  '{' { term } [ ',' { term } ... ] '}'
  c           matches character c (c != '*', '?', '\\', '[')
  '\\' c      matches character c

character-range:
  c           matches character c (c != '\\', '-', ']')
  '\\' c      matches character c
  lo '-' hi   matches character c for lo <= c <= hi

Match requires pattern to match all of name, not just a substring. The path-separator defaults to the '/' character. The only possible returned error is ErrBadPattern, when pattern is malformed.

Note: this is meant as a drop-in replacement for path.Match() which always uses '/' as the path separator. If you want to support systems which use a different path separator (such as Windows), what you want is the PathMatch() function below.

func PathMatch

func PathMatch(pattern, name string) (bool, error)

PathMatch is like Match except that it uses your system's path separator. For most systems, this will be '/'. However, for Windows, it would be '\\'. Note that for systems where the path separator is '\\', escaping is disabled.

Note: this is meant as a drop-in replacement for filepath.Match().

Types

This section is empty.

Jump to

Keyboard shortcuts

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