github.com/waldiirawan/apm-agent-go/v2@v2.2.2/stacktrace/generate_library.bash (about) 1 #!/bin/bash 2 3 set -e 4 5 _PKGS=$(go list -f '{{printf "\t%q,\n" .ImportPath}}' "$@" | grep -v vendor/golang_org) 6 7 cat > library.go <<EOF 8 // Code generated by "go generate". DO NOT EDIT. 9 10 package stacktrace 11 12 import ( 13 "strings" 14 15 "github.com/armon/go-radix" 16 ) 17 18 var libraryPackages = newLibraryPackagesRadixTree( 19 "vendor/golang_org", 20 $_PKGS 21 ) 22 23 func newLibraryPackagesRadixTree(k ...string) *radix.Tree { 24 tree := radix.New() 25 for _, k := range k { 26 tree.Insert(k, true) 27 } 28 return tree 29 } 30 31 // RegisterLibraryPackage registers the given packages as being 32 // well-known library path prefixes. This must not be called 33 // concurrently with any other functions or methods in this 34 // package; it is expected to be used by init functions. 35 func RegisterLibraryPackage(pkg ...string) { 36 for _, pkg := range pkg { 37 libraryPackages.Insert(pkg, true) 38 } 39 } 40 41 // RegisterApplicationPackage registers the given packages as being 42 // an application path. This must not be called concurrently with 43 // any other functions or methods in this package; it is expected 44 // to be used by init functions. 45 // 46 // It is not typically necessary to register application paths. If 47 // a package does not match a registered *library* package path 48 // prefix, then the path is considered an application path. This 49 // function exists for the unusual case that an application exists 50 // within a library (e.g. an example program). 51 func RegisterApplicationPackage(pkg ...string) { 52 for _, pkg := range pkg { 53 libraryPackages.Insert(pkg, false) 54 } 55 } 56 57 // IsLibraryPackage reports whether or not the given package path is 58 // a library package. This includes known library packages 59 // (e.g. stdlib or apm-agent-go), vendored packages, and any packages 60 // with a prefix registered with RegisterLibraryPackage but not 61 // RegisterApplicationPackage. 62 func IsLibraryPackage(pkg string) bool { 63 if strings.HasSuffix(pkg, "_test") { 64 return false 65 } 66 if strings.Contains(pkg, "/vendor/") { 67 return true 68 } 69 prefix, v, ok := libraryPackages.LongestPrefix(pkg) 70 if !ok || v == false { 71 return false 72 } 73 return prefix == pkg || pkg[len(prefix)] == '/' 74 } 75 EOF 76 77 gofmt -w library.go