🛠️ Convenience Functions
This module exposes a set of package-level helper functions that simplify common CPE operations: parsing, validation, matching, conversion and filtering.
🚀 MustParse
func MustParse(cpeStr string) *CPEParses a CPE string and panics on failure. Suited for constants or test code.
| Parameter | Type | Description |
|---|---|---|
cpeStr | string | The CPE string |
| Return | Type | Description |
|---|---|---|
| #1 | *CPE | The parsed CPE object |
cpe := cpeskills.MustParse("cpe:2.3:a:microsoft:windows:10:*:*:*:*:*:*:*")🔁 ParseOr
func ParseOr(cpeStr string, defaultCPE *CPE) *CPEParses a CPE string, returning defaultCPE on failure instead of an error.
| Parameter | Type | Description |
|---|---|---|
cpeStr | string | The CPE string |
defaultCPE | *CPE | Fallback value when parsing fails |
| Return | Type | Description |
|---|---|---|
| #1 | *CPE | The parsed result or the default |
cpe := cpeskills.ParseOr("bad-string", cpeskills.MustParse("cpe:2.3:a:*:*:*:*:*:*:*:*:*"))✅ IsCPE23String
func IsCPE23String(s string) boolReports whether the string conforms to the CPE 2.3 format.
| Parameter | Type | Description |
|---|---|---|
s | string | The string to test |
| Return | Type | Description |
|---|---|---|
| #1 | bool | true if it is a 2.3-formatted string |
fmt.Println(cpeskills.IsCPE23String("cpe:2.3:a:microsoft:windows:10:*:*:*:*:*:*:*"))✅ IsCPE22String
func IsCPE22String(s string) boolReports whether the string conforms to the CPE 2.2 format.
| Parameter | Type | Description |
|---|---|---|
s | string | The string to test |
| Return | Type | Description |
|---|---|---|
| #1 | bool | true if it is a 2.2-formatted string |
fmt.Println(cpeskills.IsCPE22String("cpe:/a:microsoft:windows:10"))⚡ QuickMatch
func QuickMatch(cpeStr1, cpeStr2 string) (bool, error)Quickly matches two CPE strings without manual parsing.
| Parameter | Type | Description |
|---|---|---|
cpeStr1 | string | The first CPE string |
cpeStr2 | string | The second CPE string |
| Return | Type | Description |
|---|---|---|
| #1 | bool | true if they match |
| #2 | error | An error if parsing fails |
ok, err := cpeskills.QuickMatch(
"cpe:2.3:a:microsoft:windows:*:*:*:*:*:*:*:*",
"cpe:2.3:a:microsoft:windows:10:*:*:*:*:*:*:*",
)🏷️ StringToPart
func StringToPart(s string) (*Part, error)Converts a string into a Part value.
| Parameter | Type | Description |
|---|---|---|
s | string | The part string (e.g. a/o/h) |
| Return | Type | Description |
|---|---|---|
| #1 | *Part | The converted part |
| #2 | error | An error for invalid values |
part, err := cpeskills.StringToPart("a")📝 FormatCPE
func FormatCPE(cpe *CPE, version string) (string, error)Formats a CPE object according to the given CPE version ("2.2" or "2.3").
| Parameter | Type | Description |
|---|---|---|
cpe | *CPE | The CPE object to format |
version | string | Target version |
| Return | Type | Description |
|---|---|---|
| #1 | string | The formatted result |
| #2 | error | An error for an invalid version |
cpe := cpeskills.MustParse("cpe:2.3:a:microsoft:windows:10:*:*:*:*:*:*:*")
s, _ := cpeskills.FormatCPE(cpe, "2.2")🧬 Clone
func Clone(cpe *CPE) *CPEDeep-copies a CPE object.
| Parameter | Type | Description |
|---|---|---|
cpe | *CPE | The CPE object to clone |
| Return | Type | Description |
|---|---|---|
| #1 | *CPE | A new cloned object |
cpe := cpeskills.MustParse("cpe:2.3:a:microsoft:windows:10:*:*:*:*:*:*:*")
clone := cpeskills.Clone(cpe)🔁 CPEsToStrings
func CPEsToStrings(cpes []*CPE) []stringConverts a slice of CPE objects into a slice of URI strings (2.3 format).
| Parameter | Type | Description |
|---|---|---|
cpes | []*CPE | The CPE slice |
| Return | Type | Description |
|---|---|---|
| #1 | []string | A slice of URI strings |
cpes := []*CPE{cpeskills.MustParse("cpe:2.3:a:microsoft:windows:10:*:*:*:*:*:*:*")}
strs := cpeskills.CPEsToStrings(cpes)🔁 StringsToCPEs
func StringsToCPEs(strs []string) []*CPEConverts a slice of strings into a slice of CPE objects. Entries that fail to parse are skipped.
| Parameter | Type | Description |
|---|---|---|
strs | []string | The CPE string slice |
| Return | Type | Description |
|---|---|---|
| #1 | []*CPE | The parsed CPE slice |
cpes := cpeskills.StringsToCPEs([]string{"cpe:2.3:a:microsoft:windows:10:*:*:*:*:*:*:*"})🔎 FilterByPart
func FilterByPart(cpes []*CPE, part *Part) []*CPEFilters a CPE list by part type.
| Parameter | Type | Description |
|---|---|---|
cpes | []*CPE | The list to filter |
part | *Part | The target part |
| Return | Type | Description |
|---|---|---|
| #1 | []*CPE | The matching subset |
part, _ := cpeskills.StringToPart("a")
apps := cpeskills.FilterByPart(cpes, part)🔎 FilterByVendor
func FilterByVendor(cpes []*CPE, vendor string) []*CPEFilters a CPE list by vendor.
| Parameter | Type | Description |
|---|---|---|
cpes | []*CPE | The list to filter |
vendor | string | The target vendor |
| Return | Type | Description |
|---|---|---|
| #1 | []*CPE | The matching subset |
ms := cpeskills.FilterByVendor(cpes, "microsoft")🔎 FilterByProduct
func FilterByProduct(cpes []*CPE, product string) []*CPEFilters a CPE list by product name.
| Parameter | Type | Description |
|---|---|---|
cpes | []*CPE | The list to filter |
product | string | The target product |
| Return | Type | Description |
|---|---|---|
| #1 | []*CPE | The matching subset |
wins := cpeskills.FilterByProduct(cpes, "windows")🏷️ GetPartName
func GetPartName(shortName string) stringConverts a part short name (a/o/h) into its full name.
| Parameter | Type | Description |
|---|---|---|
shortName | string | The short name |
| Return | Type | Description |
|---|---|---|
| #1 | string | The full name |
fmt.Println(cpeskills.GetPartName("a")) // application