| 1 | # packageurl-go |
| 2 | |
| 3 | [](https://github.com/package-url/packageurl-go/actions?query=workflow%3Atest) [](https://coveralls.io/github/package-url/packageurl-go) [](https://pkg.go.dev/github.com/package-url/packageurl-go) [](https://goreportcard.com/report/github.com/package-url/packageurl-go) |
| 4 | |
| 5 | Go implementation of the package url spec. |
| 6 | |
| 7 | |
| 8 | ## Install |
| 9 | ``` |
| 10 | go get -u github.com/package-url/packageurl-go |
| 11 | ``` |
| 12 | |
| 13 | ## Versioning |
| 14 | |
| 15 | The versions will follow the spec. So if the spec is released at ``1.0``. Then all versions in the ``1.x.y`` will follow the ``1.x`` spec. |
| 16 | |
| 17 | |
| 18 | ## Usage |
| 19 | |
| 20 | ### Create from parts |
| 21 | ```go |
| 22 | package main |
| 23 | |
| 24 | import ( |
| 25 | "fmt" |
| 26 | |
| 27 | "github.com/package-url/packageurl-go" |
| 28 | ) |
| 29 | |
| 30 | func main() { |
| 31 | instance := packageurl.NewPackageURL("test", "ok", "name", "version", nil, "") |
| 32 | fmt.Printf("%s", instance.ToString()) |
| 33 | } |
| 34 | ``` |
| 35 | |
| 36 | ### Parse from string |
| 37 | ```go |
| 38 | package main |
| 39 | |
| 40 | import ( |
| 41 | "fmt" |
| 42 | |
| 43 | "github.com/package-url/packageurl-go" |
| 44 | ) |
| 45 | |
| 46 | func main() { |
| 47 | instance, err := packageurl.FromString("test:ok/name@version") |
| 48 | if err != nil { |
| 49 | panic(err) |
| 50 | } |
| 51 | fmt.Printf("%#v", instance) |
| 52 | } |
| 53 | |
| 54 | ``` |
| 55 | |
| 56 | |
| 57 | ## Test |
| 58 | Testing using the normal ``go test`` command. Using ``make test`` will pull the test fixtures shared between all package-url projects and then execute the tests. |
| 59 | |
| 60 | ``` |
| 61 | curl -Ls https://raw.githubusercontent.com/package-url/purl-spec/master/test-suite-data.json -o testdata/test-suite-data.json |
| 62 | go test -v -cover ./... |
| 63 | === RUN TestFromStringExamples |
| 64 | --- PASS: TestFromStringExamples (0.00s) |
| 65 | === RUN TestToStringExamples |
| 66 | --- PASS: TestToStringExamples (0.00s) |
| 67 | === RUN TestStringer |
| 68 | --- PASS: TestStringer (0.00s) |
| 69 | === RUN TestQualifiersMapConversion |
| 70 | --- PASS: TestQualifiersMapConversion (0.00s) |
| 71 | PASS |
| 72 | github.com/package-url/packageurl-go coverage: 90.7% of statements |
| 73 | ok github.com/package-url/packageurl-go 0.004s coverage: 90.7% of statements |
| 74 | ``` |
| 75 | |
| 76 | ## Fuzzing |
| 77 | |
| 78 | Fuzzing is done with standard [Go fuzzing](https://go.dev/doc/fuzz/), introduced in Go 1.18. |
| 79 | |
| 80 | Fuzz tests check for inputs that cause `FromString` to panic. |
| 81 | |
| 82 | Using `make fuzz` will run fuzz tests for one minute. |
| 83 | |
| 84 | To run fuzz tests longer: |
| 85 | |
| 86 | ``` |
| 87 | go test -fuzztime=60m -fuzz . |
| 88 | ``` |
| 89 | |
| 90 | Or omit `-fuzztime` entirely to run indefinitely. |