ignore

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

README

Ignore

A simple ignore file processor for Go.

This is a reimagining of jimschubert/iggy.

Usage

Given an ignore file, for example /your/directory/.gitignore:

# This is an example ignore file

# Match everything below this directory
path/to/dir/**

# Match all Test files with all extensions
**/*Test*.*

# Match files with one character filename and extension
**/?.?

# Match all files beginning with first or second
**/{first*,second*}

This file can be read and evaluated like this:

processor, _ := NewProcessor()
var allow bool
allow, _ = processor.AllowsFile("path/to/dir/ignored") // = false
allow, _ = processor.AllowsFile("other/path/to/dir/ignored") // = false
allow, _ = processor.AllowsFile("nested/test/SomeTest.java") // = false
allow, _ = processor.AllowsFile("nested/a.b") // = false
allow, _ = processor.AllowsFile("nested/abc.d") // = true
allow, _ = processor.AllowsFile("nested/first.txt") // = false
allow, _ = processor.AllowsFile("nested/second.txt") // = false
allow, _ = processor.AllowsFile("nested/third.txt") // = true

The NewProcessor function accepts functional parameters as defined in the ignore package.

To specify a custom path for the ignore file:

processor, _ := NewProcessor(
    WithIgnoreFilePath("/your/directory/.ignore"),
)

Currently only Git's ignore rules are supported. As new strategies are added they can be targeted like this:

processor, _ := NewProcessor(
    WithGitignoreStrategy(),
    WithIgnoreFilePath("/your/directory/.ignore"),
)

Patterns

File patterns of the default ignore strategy follow closely to that of .gitignore.

  • Rooted file pattern: /*.ext
    • Must exist in the root of the directory
    • Must begin with a forward slash /
    • Supports * or *.ext pattern
  • Directory Rule
    • Matches against directories (dir/) or directory contents (dir/**)
    • Must end in /
  • File Rule
    • Matches an individual filename or filename.ext

Similar to .gitignore processing, a double asterisk (**) can be used in place of a directory to indicate recursion.

For example:

path\to\**\file

matches both path\to\some\file and path\to\some\nested\file.

Single asterisks (*) match any characters within a pattern.

For example:

path\to\*file

matches both path\to\your_file and path\to\my_file, as well as path\to\file.

Why?

I mean… why not? Sometimes I want a simple way to ignore or force file processing in a directory, but I don't want to shell out to some other program to evaluate the logic.

License

Apache 2.0.

see License

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Processor

type Processor struct {
	// contains filtered or unexported fields
}

func NewProcessor

func NewProcessor(opts ...ProcessorOption) (*Processor, error)
Example
stdout(`Given:`)
paths := ls(`example/`)
stdout(`
With ignore file:`)
dump(`example/.ignore`)
stdout(`Results:`)

processor, _ := NewProcessor(
	WithGitignoreStrategy(),
	WithIgnoreFilePath(`example/.ignore`),
)

for _, path := range paths {
	if allowed, _ := processor.AllowsFile(path); allowed {
		stdout(fmt.Sprintf(`✓ %s`, strings.ReplaceAll(path, "\\", "/")))
	} else {
		stdout(fmt.Sprintf(`✘ %s`, strings.ReplaceAll(path, "\\", "/")))
	}
}
Output:
Given:
example/
example/.ignore
example/fileA.txt
example/fileB.txt
example/first
example/first/contents.md
example/other.txt
example/second
example/second/contents.md
example/third
example/third/contents.md

With ignore file:
**/file*.txt
!**/fileB.txt
**/contents.md
!**/second/contents.md

Results:
✓ example/
✓ example/.ignore
✘ example/fileA.txt
✓ example/fileB.txt
✓ example/first
✘ example/first/contents.md
✓ example/other.txt
✓ example/second
✓ example/second/contents.md
✓ example/third
✘ example/third/contents.md

func (*Processor) AllowsFile

func (p *Processor) AllowsFile(path string) (bool, error)

type ProcessorOption

type ProcessorOption func(*Processor) error

func WithGitignoreStrategy

func WithGitignoreStrategy() ProcessorOption

WithGitignoreStrategy is a functional option which applies the strategy for parsing .gitignore files

func WithIgnoreFilePath

func WithIgnoreFilePath(filePath string) ProcessorOption

WithIgnoreFilePath is a functional option which allows the user to parse a non-standard filename for a given strategy

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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