-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
⭐️ Add Purl to macOS and Windows systems #4996
Merged
+562
−99
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,23 @@ const ( | |
QualifierEpoch = "epoch" | ||
) | ||
|
||
// PackageURL is a helper struct that renters a package url based of an inventory | ||
// platform, purl type, and modifiers. | ||
type PackageURL struct { | ||
// Required: minimal attributes to render a PURL. | ||
Type Type | ||
Name string | ||
Version string | ||
|
||
// Optional: can be set via modifiers. | ||
Namespace string | ||
Arch string | ||
Epoch string | ||
|
||
// Used as metadata to fetch things like the architecture or linux distribution. | ||
platform *inventory.Platform | ||
} | ||
|
||
// NewQualifiers creates a new Qualifiers slice from a map of key/value pairs. | ||
// see https://github.com/package-url/purl-spec/blob/master/PURL-TYPES.rst for more information | ||
func NewQualifiers(qualifier map[string]string) packageurl.Qualifiers { | ||
|
@@ -43,44 +60,92 @@ func NewQualifiers(qualifier map[string]string) packageurl.Qualifiers { | |
return list | ||
} | ||
|
||
// NewPackageUrl creates a new package url for a given platform, name, version, arch, epoch and purlType | ||
// see https://github.com/package-url/purl-spec/blob/master/PURL-TYPES.rst for more information | ||
func NewPackageUrl(pf *inventory.Platform, name string, version string, arch string, epoch string, purlType string) string { | ||
qualifiers := map[string]string{} | ||
if arch != "" { | ||
qualifiers[QualifierArch] = arch | ||
// NewPackageURL creates a new package url for a given platform, name, version, and type. | ||
// | ||
// For more information, see: | ||
// https://github.com/package-url/purl-spec/blob/master/PURL-TYPES.rst | ||
func NewPackageURL(pf *inventory.Platform, t Type, name, version string, modifiers ...Modifier) *PackageURL { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That made me stumble.
🫤 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, we really can't 😭 |
||
purl := &PackageURL{ | ||
Type: t, | ||
Name: name, | ||
Version: version, | ||
platform: pf, | ||
} | ||
|
||
if epoch != "" && epoch != "0" { | ||
qualifiers[QualifierEpoch] = epoch | ||
// if a platform was provided | ||
if pf != nil { | ||
// use the platform architecture for the package | ||
purl.Arch = pf.Arch | ||
|
||
// and if the distro is set via labels, set it as a namespace | ||
if pf.Labels != nil && pf.Labels[detector.LabelDistroID] != "" { | ||
purl.Namespace = pf.Labels[detector.LabelDistroID] | ||
} | ||
} | ||
|
||
namespace := pf.Name | ||
if pf.Labels != nil && pf.Labels[detector.LabelDistroID] != "" { | ||
namespace = pf.Labels[detector.LabelDistroID] | ||
// apply modifiers | ||
for _, modifier := range modifiers { | ||
modifier(purl) | ||
} | ||
|
||
// generate distro qualifier | ||
distroQualifiers := []string{} | ||
distroQualifiers = append(distroQualifiers, namespace) | ||
if pf.Version != "" { | ||
distroQualifiers = append(distroQualifiers, pf.Version) | ||
} else if pf.Build != "" { | ||
distroQualifiers = append(distroQualifiers, pf.Build) | ||
return purl | ||
} | ||
|
||
func (purl PackageURL) String() string { | ||
qualifiers := map[string]string{} | ||
if purl.Arch != "" { | ||
qualifiers[QualifierArch] = purl.Arch | ||
} | ||
qualifiers[QualifierDistro] = strings.Join(distroQualifiers, "-") | ||
|
||
// Avoids creating purl's like: 'pkg:macos/macos/Package' | ||
if namespace == purlType { | ||
namespace = "" | ||
if purl.Epoch != "" && purl.Epoch != "0" { | ||
qualifiers[QualifierEpoch] = purl.Epoch | ||
} | ||
|
||
if distroQualifiers, ok := purl.distroQualifiers(); ok { | ||
qualifiers[QualifierDistro] = distroQualifiers | ||
} | ||
|
||
return packageurl.NewPackageURL( | ||
purlType, | ||
namespace, | ||
name, | ||
version, | ||
string(purl.Type), | ||
purl.Namespace, | ||
purl.Name, | ||
purl.Version, | ||
NewQualifiers(qualifiers), | ||
"", | ||
).ToString() | ||
} | ||
|
||
// generate distro qualifier | ||
func (purl PackageURL) distroQualifiers() (string, bool) { | ||
if purl.Namespace == "" { | ||
return "", false | ||
} | ||
|
||
distroQualifiers := []string{} | ||
distroQualifiers = append(distroQualifiers, purl.Namespace) | ||
if purl.platform.Version != "" { | ||
distroQualifiers = append(distroQualifiers, purl.platform.Version) | ||
} else if purl.platform.Build != "" { | ||
distroQualifiers = append(distroQualifiers, purl.platform.Build) | ||
} | ||
|
||
return strings.Join(distroQualifiers, "-"), true | ||
} | ||
|
||
type Modifier func(*PackageURL) | ||
|
||
func WithArch(arch string) Modifier { | ||
return func(purl *PackageURL) { | ||
purl.Arch = arch | ||
} | ||
} | ||
func WithEpoch(epoch string) Modifier { | ||
return func(purl *PackageURL) { | ||
purl.Epoch = epoch | ||
} | ||
} | ||
func WithNamespace(namespace string) Modifier { | ||
return func(purl *PackageURL) { | ||
purl.Namespace = namespace | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
renders
instead ofrenters