github.com/ianlewis/go-gitignore@v0.1.1-0.20231110021210-4a0f15cbd56f/example_test.go (about) 1 // Copyright 2016 Denormal Limited 2 // Copyright 2023 Google LLC 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 package gitignore_test 17 18 import ( 19 "fmt" 20 21 "github.com/ianlewis/go-gitignore" 22 ) 23 24 func ExampleNewFromFile() { 25 ignore, err := gitignore.NewFromFile("/my/project/.gitignore") 26 if err != nil { 27 panic(err) 28 } 29 30 // attempt to match an absolute path 31 match := ignore.Match("/my/project/src/file.go") 32 if match != nil { 33 if match.Ignore() { 34 fmt.Println("ignore file.go") 35 } 36 } 37 38 // attempt to match a relative path 39 // - this is equivalent to the call above 40 match = ignore.Relative("src/file.go", false) 41 if match != nil { 42 if match.Include() { 43 fmt.Println("include file.go") 44 } 45 } 46 } // ExampleNewFromFile() 47 48 func ExampleNewRepository() { 49 ignore, err := gitignore.NewRepository("/my/project") 50 if err != nil { 51 panic(err) 52 } 53 54 // attempt to match a directory in the repository 55 match := ignore.Relative("src/examples", true) 56 if match != nil { 57 if match.Ignore() { 58 fmt.Printf( 59 "ignore src/examples because of pattern %q at %s", 60 match, match.Position(), 61 ) 62 } 63 } 64 65 // if we have an absolute path, or a path relative to the current 66 // working directory we can use the short-hand methods 67 if ignore.Include("/my/project/etc/service.conf") { 68 fmt.Println("include the service configuration") 69 } 70 } // ExampleNewRepository()