github.com/anchore/syft@v1.38.2/syft/pkg/type.go (about) 1 package pkg 2 3 import ( 4 "github.com/anchore/packageurl-go" 5 ) 6 7 // Type represents a Package Type for or within a language ecosystem (there may be multiple package types within a language ecosystem) 8 type Type string 9 10 func (t Type) String() string { 11 return string(t) 12 } 13 14 const ( 15 // the full set of supported packages 16 UnknownPkg Type = "UnknownPackage" 17 AlpmPkg Type = "alpm" 18 ApkPkg Type = "apk" 19 BinaryPkg Type = "binary" 20 BitnamiPkg Type = "bitnami" 21 CocoapodsPkg Type = "pod" 22 ConanPkg Type = "conan" 23 CondaPkg Type = "conda" 24 DartPubPkg Type = "dart-pub" 25 DebPkg Type = "deb" 26 DotnetPkg Type = "dotnet" 27 ErlangOTPPkg Type = "erlang-otp" 28 GemPkg Type = "gem" 29 GithubActionPkg Type = "github-action" 30 GithubActionWorkflowPkg Type = "github-action-workflow" 31 GoModulePkg Type = "go-module" 32 GraalVMNativeImagePkg Type = "graalvm-native-image" 33 HackagePkg Type = "hackage" 34 HexPkg Type = "hex" 35 JavaPkg Type = "java-archive" 36 JenkinsPluginPkg Type = "jenkins-plugin" 37 KbPkg Type = "msrc-kb" 38 LinuxKernelPkg Type = "linux-kernel" 39 LinuxKernelModulePkg Type = "linux-kernel-module" 40 NixPkg Type = "nix" 41 NpmPkg Type = "npm" 42 OpamPkg Type = "opam" 43 PhpComposerPkg Type = "php-composer" 44 PhpPeclPkg Type = "php-pecl" // Deprecated: will be removed in syft v2.0 45 PhpPearPkg Type = "php-pear" 46 PortagePkg Type = "portage" 47 PythonPkg Type = "python" 48 Rpkg Type = "R-package" 49 LuaRocksPkg Type = "lua-rocks" 50 RpmPkg Type = "rpm" 51 RustPkg Type = "rust-crate" 52 SwiftPkg Type = "swift" 53 SwiplPackPkg Type = "swiplpack" 54 TerraformPkg Type = "terraform" 55 WordpressPluginPkg Type = "wordpress-plugin" 56 HomebrewPkg Type = "homebrew" 57 ModelPkg Type = "model" 58 ) 59 60 // AllPkgs represents all supported package types 61 var AllPkgs = []Type{ 62 AlpmPkg, 63 ApkPkg, 64 BinaryPkg, 65 BitnamiPkg, 66 CocoapodsPkg, 67 ConanPkg, 68 CondaPkg, 69 DartPubPkg, 70 DebPkg, 71 DotnetPkg, 72 ErlangOTPPkg, 73 GemPkg, 74 GithubActionPkg, 75 GithubActionWorkflowPkg, 76 GraalVMNativeImagePkg, 77 GoModulePkg, 78 HackagePkg, 79 HexPkg, 80 JavaPkg, 81 JenkinsPluginPkg, 82 KbPkg, 83 LinuxKernelPkg, 84 LinuxKernelModulePkg, 85 NixPkg, 86 NpmPkg, 87 OpamPkg, 88 PhpComposerPkg, 89 PhpPeclPkg, 90 PhpPearPkg, 91 PortagePkg, 92 PythonPkg, 93 Rpkg, 94 LuaRocksPkg, 95 RpmPkg, 96 RustPkg, 97 SwiftPkg, 98 SwiplPackPkg, 99 TerraformPkg, 100 WordpressPluginPkg, 101 HomebrewPkg, 102 ModelPkg, 103 } 104 105 // PackageURLType returns the PURL package type for the current package. 106 // 107 //nolint:funlen, gocyclo 108 func (t Type) PackageURLType() string { 109 switch t { 110 case AlpmPkg: 111 return "alpm" 112 case ApkPkg: 113 return packageurl.TypeAlpine 114 case BitnamiPkg: 115 return packageurl.TypeBitnami 116 case CocoapodsPkg: 117 return packageurl.TypeCocoapods 118 case ConanPkg: 119 return packageurl.TypeConan 120 case CondaPkg: 121 return packageurl.TypeGeneric 122 case DartPubPkg: 123 return packageurl.TypePub 124 case DebPkg: 125 return "deb" 126 case DotnetPkg: 127 return "dotnet" 128 case ErlangOTPPkg: 129 return packageurl.TypeOTP 130 case GemPkg: 131 return packageurl.TypeGem 132 case HexPkg: 133 return packageurl.TypeHex 134 case GithubActionPkg, GithubActionWorkflowPkg: 135 // note: this is not a real purl type, but it is the closest thing we have for now 136 return packageurl.TypeGithub 137 case GoModulePkg: 138 return packageurl.TypeGolang 139 case HackagePkg: 140 return packageurl.TypeHackage 141 case JavaPkg, JenkinsPluginPkg: 142 return packageurl.TypeMaven 143 case LinuxKernelPkg: 144 return "generic/linux-kernel" 145 case LinuxKernelModulePkg: 146 return packageurl.TypeGeneric 147 case PhpComposerPkg: 148 return packageurl.TypeComposer 149 case PhpPearPkg, PhpPeclPkg: 150 return "pear" 151 case PythonPkg: 152 return packageurl.TypePyPi 153 case PortagePkg: 154 return "portage" 155 case LuaRocksPkg: 156 return packageurl.TypeLuaRocks 157 case NixPkg: 158 return "nix" 159 case NpmPkg: 160 return packageurl.TypeNPM 161 case OpamPkg: 162 return "opam" 163 case Rpkg: 164 return packageurl.TypeCran 165 case RpmPkg: 166 return packageurl.TypeRPM 167 case RustPkg: 168 return "cargo" 169 case SwiftPkg: 170 return packageurl.TypeSwift 171 case SwiplPackPkg: 172 return "swiplpack" 173 case TerraformPkg: 174 return "terraform" 175 case WordpressPluginPkg: 176 return "wordpress-plugin" 177 case HomebrewPkg: 178 return "homebrew" 179 default: 180 // TODO: should this be a "generic" purl type instead? 181 return "" 182 } 183 } 184 185 func TypeFromPURL(p string) Type { 186 purl, err := packageurl.FromString(p) 187 if err != nil { 188 return UnknownPkg 189 } 190 191 ptype := purl.Type 192 if ptype == "generic" { 193 ptype = purl.Name 194 } 195 return TypeByName(ptype) 196 } 197 198 //nolint:funlen,gocyclo 199 func TypeByName(name string) Type { 200 switch name { 201 case "alpm": 202 return AlpmPkg 203 case packageurl.TypeAlpine, "alpine": 204 return ApkPkg 205 case packageurl.TypeBitnami: 206 return BitnamiPkg 207 case packageurl.TypeDebian: 208 return DebPkg 209 case packageurl.TypeComposer: 210 return PhpComposerPkg 211 case "pear", "pecl": 212 return PhpPearPkg 213 case packageurl.TypeGolang: 214 return GoModulePkg 215 case packageurl.TypeGem: 216 return GemPkg 217 case "cargo", "crate": 218 return RustPkg 219 case "conda": 220 return CondaPkg 221 case packageurl.TypePub: 222 return DartPubPkg 223 case "dotnet": // here to support legacy use cases 224 return DotnetPkg 225 case packageurl.TypeCocoapods: 226 return CocoapodsPkg 227 case packageurl.TypeConan: 228 return ConanPkg 229 case packageurl.TypeHackage: 230 return HackagePkg 231 case packageurl.TypeHex: 232 return HexPkg 233 case packageurl.TypeLuaRocks: 234 return LuaRocksPkg 235 case packageurl.TypeMaven: 236 return JavaPkg 237 case packageurl.TypeNPM: 238 return NpmPkg 239 case packageurl.TypePyPi: 240 return PythonPkg 241 case "portage": 242 return PortagePkg 243 case packageurl.TypeOTP: 244 return ErlangOTPPkg 245 case "linux-kernel": 246 return LinuxKernelPkg 247 case "linux-kernel-module": 248 return LinuxKernelModulePkg 249 case "nix": 250 return NixPkg 251 case "opam": 252 return OpamPkg 253 case packageurl.TypeCran: 254 return Rpkg 255 case packageurl.TypeRPM: 256 return RpmPkg 257 case packageurl.TypeSwift: 258 return SwiftPkg 259 case "swiplpack": 260 return SwiplPackPkg 261 case "terraform": 262 return TerraformPkg 263 case "wordpress-plugin": 264 return WordpressPluginPkg 265 case "homebrew": 266 return HomebrewPkg 267 default: 268 return UnknownPkg 269 } 270 }