github.com/boyter/gocodewalker@v1.3.2/go-gitignore/example_test.go (about)

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