finder

package module
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: May 31, 2025 License: BSD-3-Clause Imports: 8 Imported by: 2

README

Finder

Finder is a simple Go package for searching files and directories based on patterns in a specified directory. It also supports excluding certain files or directories and provides flexible configuration options for the search.

Static Badge

GoDoc GitHub stars Go Report Card codecov

Versions

Stable Version GitHub Release GitHub Release

Installation

If you are using Go modules, you can add this package to your project by running:

go get -u github.com/gouef/finder

Usage

package main

import (
	"fmt"
	"github.com/gouef/finder"
)

func main() {
	// Search for all files in the current directory and its subdirectories
	files := finder.New().In(".").FindFiles("*.go").Get()

	// Print the found files
	for _, file := range files {
		fmt.Println(file.Name())
	}
}

Filtering Files by Pattern

You can search for files or directories by specifying patterns using FindFiles or FindDirectories:

// Search for .txt files in the current directory and subdirectories
files := finder.New().In(".").FindFiles("*.txt").Get()
Excluding Files and Directories

You can exclude specific files or directories from the search results using the Exclude function:

// Search for .txt files, but exclude "test1.txt"
files := finder.New().In(".").FindFiles("*.txt").Exclude("test1.txt").Get()
Searching for Directories

If you want to search for directories instead of files:

// Search for directories in the current directory and subdirectories
dirs := finder.New().In(".").FindDirectories("*").Get()

Functions

  • New() – Creates a new instance of Finder.
  • In(dirs ...string) – Specifies the directories to search in.
  • Find(patterns ...string) – Searches for both files and directories based on the given patterns.
  • FindFiles(patterns ...string) – Searches only for files matching the given patterns.
  • FindDirectories(patterns ...string) – Searches only for directories matching the given patterns.
  • Exclude(patterns ...string) – Excludes files and directories matching the given patterns from the search results.
  • Get() – Retrieves the search results.

Example

package main

import (
	"fmt"
	"github.com/gouef/finder"
)

func main() {
	// Search for .go files and exclude "main.go"
	files := finder.FindFiles("*.go").In(".").Exclude("main.go").Get()

	// Print the found files
	for _, file := range files {
		fmt.Println(file.Name())
	}
}

Global Function

Match

Extending filepath.Match, but yout can use multiple patterns.

package main

import (
	"fmt"
	"github.com/gouef/finder"
)

func main() {
    files := finder.FindFiles("*.go").In(".").Exclude("main.go").Get()
}
DirectoryHash(path string) (string, error)

Get hash (md5) for directory, good for checking if something was changed.

package main

import (
	"fmt"
	"github.com/gouef/finder"
)

func main() {
	hash, err := finder.DirectoryHash("./directory")
	if err != nil {
		fmt.Println("Error:", err)
		return
	}
	fmt.Println("MD5 hash of directory:", hash)
}
DirectoryFilesHash(path string) (map[string]string, error)

Get hashes (md5) for every file in directory

package main

import (
	"fmt"
	"github.com/gouef/finder"
)

func main() {
	hashes, err := finder.DirectoryFilesHash("./directory")
	if err != nil {
		fmt.Println("Error:", err)
		return
	}
	
	for filePath, fileHash := range hashes {
		fmt.Println("MD5 hash of file:", filePath, " ", fileHash)
	}
}
FileHash(path string) (string, error)

Get hash (md5) for single file

package main

import (
	"fmt"
	"github.com/gouef/finder"
)

func main() {
	hash, err := finder.FileHash("./directory/file.txt")
	if err != nil {
		fmt.Println("Error:", err)
		return
	}
	
    fmt.Println("MD5 hash of file:", hash)
}

Contributing

Read Contributing

Contributors

JanGalek actions-user

Join our Discord Community! 🎉

Discord

Click above to join our community on Discord!

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DirectoryFilesHash added in v1.1.4

func DirectoryFilesHash(path string) (map[string]string, error)

func DirectoryHash added in v1.1.3

func DirectoryHash(path string) (string, error)

func FileHash added in v1.1.4

func FileHash(path string) (string, error)

func GetProjectRoot added in v1.2.1

func GetProjectRoot() string

func Match added in v1.1.1

func Match(name string, patterns ...string) bool

Types

type Finder

type Finder struct {
	Dirs             []string
	Patterns         []string
	Files            map[string]*Info
	Excludes         []string
	Mode             Mode
	EnabledRecursive bool
}

func Find

func Find(patterns ...string) *Finder

Find Searches for both Files and directories based on the given Patterns.

func FindDirectories

func FindDirectories(patterns ...string) *Finder

FindDirectories Searches only for directories matching the given Patterns.

func FindFiles

func FindFiles(patterns ...string) *Finder

FindFiles Searches only for Files matching the given Patterns.

func In

func In(dirs ...string) *Finder

In Specifies the directories to search in.

func New

func New() *Finder

New Creates a new instance of Finder.

func (*Finder) Exclude

func (f *Finder) Exclude(patterns ...string) *Finder

Exclude Excludes Files and directories matching the given Patterns from the search results.

func (*Finder) Find

func (f *Finder) Find(patterns ...string) *Finder

Find Searches for both Files and directories based on the given Patterns.

func (*Finder) FindDirectories

func (f *Finder) FindDirectories(patterns ...string) *Finder

FindDirectories Searches only for directories matching the given Patterns.

func (*Finder) FindFiles

func (f *Finder) FindFiles(patterns ...string) *Finder

FindFiles Searches only for Files matching the given Patterns.

func (*Finder) Get

func (f *Finder) Get() map[string]*Info

Get Retrieves the search results.

func (*Finder) In

func (f *Finder) In(dirs ...string) *Finder

In Specifies the directories to search in.

func (*Finder) Match added in v1.1.1

func (f *Finder) Match(patterns ...string) map[string]*Info

Match Retrieves the search results with match Patterns.

func (*Finder) NotRecursive added in v1.3.0

func (f *Finder) NotRecursive() *Finder

func (*Finder) Recursive added in v1.3.0

func (f *Finder) Recursive() *Finder

type Info added in v1.1.0

type Info struct {
	FileInfo os.FileInfo
	Path     string
	Ext      string
	Name     string
}

type Mode

type Mode string
const (
	ModeDir  Mode = "dir"
	ModeFile Mode = "file"
	ModeAll  Mode = "all"
)

Jump to

Keyboard shortcuts

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