glob

package module
v0.0.0-...-68797d8 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2025 License: MIT Imports: 7 Imported by: 1

README

github.com/pgavlin/glob

Package glob provides utilities for matching patterns against filesystem trees.

The matcher is designed to read as few directories as possible while matching; only directories that may contain matches are considered.

package main

import (
    "fmt"
    "log"
    "os"

    "github.com/pgavlin/glob"
)

func main() {
    glob, err := glob.New([]string{os.Args[1]}, []string{os.Args[2]})
    if err != nil {
        log.Fatal(err)
    }
    for path, err := range glob.Match(os.DirFS("."), ".", true) {
        if err != nil {
            log.Fatalf("%v: %v", path, err)
        }
        fmt.Println(path)
    }
}

Documentation

Overview

Package glob provides utilities for matching patterns against filesystem trees.

The matcher is designed to read as few directories as possible while matching; only directories that may contain matches are considered.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Glob

type Glob interface {
	// Match returns a sequence of (string, error) pairs for paths under dir that match the glob's include and exclude
	// patterns. The error portion of a pair is only non-nil when the path portion is a directory and Match fails to
	// read the directory's entries. If includeDirs is true, matching directories will be included in the sequence prior
	// to their contents.
	Match(fsys fs.FS, dir string, includeDirs bool) iter.Seq2[string, error]

	// MatchPath returns true if the given path matches the glob's includes and excludes.
	MatchPath(path string) bool
}

A Glob matches paths in a directory against a set of include and exclude patterns.

func New

func New(includes, excludes []string) (Glob, error)

New creates a new Glob from the given lists of include and exclude patterns.

A Glob matches a particular path p if any of its include patterns matches p and none of its exclude patterns match p.

The pattern syntax is:

pattern:
	pathTerm { '/' pathTerm }

pathTerm:
	'**'        matches any sequence of directory names, including the empty sequence
	{ term }    matches a sequence of terms against a name

term:
	'*'         matches any sequence of non-/ characters
	'?'         matches any single non-/ character
	'[' [ '^' ] { character-range } ']'
	            character class (must be non-empty)
	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

Patterns require that path terms match all of name, not just a substring. If any error is returned, it will be a list of path.ErrBadPattern errors.

Jump to

Keyboard shortcuts

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