file

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2019 License: MIT Imports: 9 Imported by: 0

README

file -- Extend filepath.Glob function

Build Status GitHub license GitHub release

Usage

Search with **/ wildcard (match all directories recursively).

matches, err := file.Glob("**/*.[ch]")
if err != nil {
    fmt.Fprintf(os.Stderr, "%+v\n", err)
    return
}
for _, path := range matches {
    fmt.Println(path)
}
// Output:
// testdata/include/source.h
// testdata/source.c
Glob with context.Context
matches, err := file.GlobWithContext(context.Background(), "**/*.[ch]")
if err != nil {
    fmt.Fprintf(os.Stderr, "%+v\n", err)
    return
}
for _, path := range matches {
    fmt.Println(path)
}
// Output:
// testdata/include/source.h
// testdata/source.c
Glob with flags
matches, err := file.Glob("**/*.[ch]", file.WithFlags(file.StdFlags|file.AbsolutePath))
if err != nil {
    fmt.Fprintf(os.Stderr, "%+v\n", err)
    return
}
for _, path := range matches {
    fmt.Println(path)
}
// Output:
// /home/username/go/src/github.com/spiegel-im-spiegel/file/testdata/include/source.h
// /home/username/go/src/github.com/spiegel-im-spiegel/file/testdata/source.c
Flag Note
ContainsFile contains file
ContainsDir contains directory
SeparatorSlash use slash character for separator character in path
AbsolutePath output absolute representation of path
StdFlags ContainsFile | ContainsDir (default)

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Glob

func Glob(pattern string, opts ...GlobOptFunc) ([]string, error)

Glob returns the names of all files matching pattern. This function expands filepath.Glob

Example
package main

import (
	"fmt"
	"os"

	"github.com/spiegel-im-spiegel/file"
)

func main() {
	matches, err := file.Glob("**/*.[ch]", file.WithFlags(file.StdFlags|file.SeparatorSlash))
	if err != nil {
		fmt.Fprintf(os.Stderr, "%+v\n", err)
		return
	}
	for _, path := range matches {
		fmt.Println(path)
	}
}
Output:
testdata/include/source.h
testdata/source.c

func GlobWithContext

func GlobWithContext(ctx context.Context, pattern string, opts ...GlobOptFunc) ([]string, error)

GlobWithContext returns the names of all files matching pattern with context.Context.

Example
package main

import (
	"context"
	"fmt"
	"os"

	"github.com/spiegel-im-spiegel/file"
)

func main() {
	matches, err := file.GlobWithContext(context.Background(), "**/*.[ch]", file.WithFlags(file.StdFlags|file.SeparatorSlash))
	if err != nil {
		fmt.Fprintf(os.Stderr, "%+v\n", err)
		return
	}
	for _, path := range matches {
		fmt.Println(path)
	}
}
Output:
testdata/include/source.h
testdata/source.c

Types

type Flags

type Flags uint

Flags is type of operation flag in Glob() function.

const (
	ContainsFile   Flags = 1 << iota //contains file
	ContainsDir                      //contains directory
	SeparatorSlash                   //use slash ('/') character for separator character in path
	AbsolutePath                     //output absolute representation of path
	StdFlags       = ContainsFile | ContainsDir
)

Operation flag in Glob() function.

func (Flags) AbsolutePath

func (f Flags) AbsolutePath() bool

AbsolutePath returns status of AbsolutePath flag.

func (Flags) ContainsDir

func (f Flags) ContainsDir() bool

ContainsDir returns status of ContainsDir flag.

func (Flags) ContainsFile

func (f Flags) ContainsFile() bool

ContainsFile returns status of ContainsFile flag.

func (Flags) SeparatorSlash

func (f Flags) SeparatorSlash() bool

SeparatorSlash returns status of SeparatorSlash flag.

type GlobOptFunc

type GlobOptFunc func(*glob)

GlobOptFunc is self-referential function for functional options pattern.

func WithFlags

func WithFlags(f Flags) GlobOptFunc

WithFlags returns function for setting glob instance.

Jump to

Keyboard shortcuts

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