github.com/Wrdlbrnft/go-gitignore@v0.0.0-20201129201858-74ef740b8b77/example_test.go (about)

     1  package gitignore_test
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/denormal/go-gitignore"
     7  )
     8  
     9  func ExampleNewFromFile() {
    10  	ignore, err := gitignore.NewFromFile("/my/project/.gitignore")
    11  	if err != nil {
    12  		panic(err)
    13  	}
    14  
    15  	// attempt to match an absolute path
    16  	match := ignore.Match("/my/project/src/file.go")
    17  	if match != nil {
    18  		if match.Ignore() {
    19  			fmt.Println("ignore file.go")
    20  		}
    21  	}
    22  
    23  	// attempt to match a relative path
    24  	//		- this is equivalent to the call above
    25  	match = ignore.Relative("src/file.go", false)
    26  	if match != nil {
    27  		if match.Include() {
    28  			fmt.Println("include file.go")
    29  		}
    30  	}
    31  } // ExampleNewFromFile()
    32  
    33  func ExampleNewRepository() {
    34  	ignore, err := gitignore.NewRepository("/my/project")
    35  	if err != nil {
    36  		panic(err)
    37  	}
    38  
    39  	// attempt to match a directory in the repository
    40  	match := ignore.Relative("src/examples", true)
    41  	if match != nil {
    42  		if match.Ignore() {
    43  			fmt.Printf(
    44  				"ignore src/examples because of pattern %q at %s",
    45  				match, match.Position(),
    46  			)
    47  		}
    48  	}
    49  
    50  	// if we have an absolute path, or a path relative to the current
    51  	// working directory we can use the short-hand methods
    52  	if ignore.Include("/my/project/etc/service.conf") {
    53  		fmt.Println("include the service configuration")
    54  	}
    55  } // ExampleNewRepository()