github.com/anchore/syft@v1.4.2-0.20240516191711-1bec1fc5d397/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  const (
    11  	// the full set of supported packages
    12  	UnknownPkg              Type = "UnknownPackage"
    13  	AlpmPkg                 Type = "alpm"
    14  	ApkPkg                  Type = "apk"
    15  	BinaryPkg               Type = "binary"
    16  	CocoapodsPkg            Type = "pod"
    17  	ConanPkg                Type = "conan"
    18  	DartPubPkg              Type = "dart-pub"
    19  	DebPkg                  Type = "deb"
    20  	DotnetPkg               Type = "dotnet"
    21  	ErlangOTPPkg            Type = "erlang-otp"
    22  	GemPkg                  Type = "gem"
    23  	GithubActionPkg         Type = "github-action"
    24  	GithubActionWorkflowPkg Type = "github-action-workflow"
    25  	GoModulePkg             Type = "go-module"
    26  	GraalVMNativeImagePkg   Type = "graalvm-native-image"
    27  	HackagePkg              Type = "hackage"
    28  	HexPkg                  Type = "hex"
    29  	JavaPkg                 Type = "java-archive"
    30  	JenkinsPluginPkg        Type = "jenkins-plugin"
    31  	KbPkg                   Type = "msrc-kb"
    32  	LinuxKernelPkg          Type = "linux-kernel"
    33  	LinuxKernelModulePkg    Type = "linux-kernel-module"
    34  	NixPkg                  Type = "nix"
    35  	NpmPkg                  Type = "npm"
    36  	PhpComposerPkg          Type = "php-composer"
    37  	PhpPeclPkg              Type = "php-pecl"
    38  	PortagePkg              Type = "portage"
    39  	PythonPkg               Type = "python"
    40  	Rpkg                    Type = "R-package"
    41  	RpmPkg                  Type = "rpm"
    42  	RustPkg                 Type = "rust-crate"
    43  	SwiftPkg                Type = "swift"
    44  	WordpressPluginPkg      Type = "wordpress-plugin"
    45  )
    46  
    47  // AllPkgs represents all supported package types
    48  var AllPkgs = []Type{
    49  	AlpmPkg,
    50  	ApkPkg,
    51  	BinaryPkg,
    52  	CocoapodsPkg,
    53  	ConanPkg,
    54  	DartPubPkg,
    55  	DebPkg,
    56  	DotnetPkg,
    57  	ErlangOTPPkg,
    58  	GemPkg,
    59  	GithubActionPkg,
    60  	GithubActionWorkflowPkg,
    61  	GoModulePkg,
    62  	HackagePkg,
    63  	HexPkg,
    64  	JavaPkg,
    65  	JenkinsPluginPkg,
    66  	KbPkg,
    67  	LinuxKernelPkg,
    68  	LinuxKernelModulePkg,
    69  	NixPkg,
    70  	NpmPkg,
    71  	PhpComposerPkg,
    72  	PhpPeclPkg,
    73  	PortagePkg,
    74  	PythonPkg,
    75  	Rpkg,
    76  	RpmPkg,
    77  	RustPkg,
    78  	SwiftPkg,
    79  	WordpressPluginPkg,
    80  }
    81  
    82  // PackageURLType returns the PURL package type for the current package.
    83  //
    84  //nolint:funlen
    85  func (t Type) PackageURLType() string {
    86  	switch t {
    87  	case AlpmPkg:
    88  		return "alpm"
    89  	case ApkPkg:
    90  		return packageurl.TypeAlpine
    91  	case CocoapodsPkg:
    92  		return packageurl.TypeCocoapods
    93  	case ConanPkg:
    94  		return packageurl.TypeConan
    95  	case DartPubPkg:
    96  		return packageurl.TypePub
    97  	case DebPkg:
    98  		return "deb"
    99  	case DotnetPkg:
   100  		return "dotnet"
   101  	case ErlangOTPPkg:
   102  		return packageurl.TypeOTP
   103  	case GemPkg:
   104  		return packageurl.TypeGem
   105  	case HexPkg:
   106  		return packageurl.TypeHex
   107  	case GithubActionPkg, GithubActionWorkflowPkg:
   108  		// note: this is not a real purl type, but it is the closest thing we have for now
   109  		return packageurl.TypeGithub
   110  	case GoModulePkg:
   111  		return packageurl.TypeGolang
   112  	case HackagePkg:
   113  		return packageurl.TypeHackage
   114  	case JavaPkg, JenkinsPluginPkg:
   115  		return packageurl.TypeMaven
   116  	case LinuxKernelPkg:
   117  		return "generic/linux-kernel"
   118  	case LinuxKernelModulePkg:
   119  		return packageurl.TypeGeneric
   120  	case PhpComposerPkg:
   121  		return packageurl.TypeComposer
   122  	case PhpPeclPkg:
   123  		return "pecl"
   124  	case PythonPkg:
   125  		return packageurl.TypePyPi
   126  	case PortagePkg:
   127  		return "portage"
   128  	case NixPkg:
   129  		return "nix"
   130  	case NpmPkg:
   131  		return packageurl.TypeNPM
   132  	case Rpkg:
   133  		return packageurl.TypeCran
   134  	case RpmPkg:
   135  		return packageurl.TypeRPM
   136  	case RustPkg:
   137  		return "cargo"
   138  	case SwiftPkg:
   139  		return packageurl.TypeSwift
   140  	case WordpressPluginPkg:
   141  		return "wordpress-plugin"
   142  	default:
   143  		// TODO: should this be a "generic" purl type instead?
   144  		return ""
   145  	}
   146  }
   147  
   148  func TypeFromPURL(p string) Type {
   149  	purl, err := packageurl.FromString(p)
   150  	if err != nil {
   151  		return UnknownPkg
   152  	}
   153  
   154  	ptype := purl.Type
   155  	if ptype == "generic" {
   156  		ptype = purl.Name
   157  	}
   158  	return TypeByName(ptype)
   159  }
   160  
   161  //nolint:funlen
   162  func TypeByName(name string) Type {
   163  	switch name {
   164  	case packageurl.TypeDebian:
   165  		return DebPkg
   166  	case packageurl.TypeRPM:
   167  		return RpmPkg
   168  	case "alpm":
   169  		return AlpmPkg
   170  	case packageurl.TypeAlpine, "alpine":
   171  		return ApkPkg
   172  	case packageurl.TypeMaven:
   173  		return JavaPkg
   174  	case packageurl.TypeComposer:
   175  		return PhpComposerPkg
   176  	case "pecl":
   177  		return PhpPeclPkg
   178  	case packageurl.TypeGolang:
   179  		return GoModulePkg
   180  	case packageurl.TypeNPM:
   181  		return NpmPkg
   182  	case packageurl.TypePyPi:
   183  		return PythonPkg
   184  	case packageurl.TypeGem:
   185  		return GemPkg
   186  	case "cargo", "crate":
   187  		return RustPkg
   188  	case packageurl.TypePub:
   189  		return DartPubPkg
   190  	case "dotnet": // here to support legacy use cases
   191  		return DotnetPkg
   192  	case packageurl.TypeCocoapods:
   193  		return CocoapodsPkg
   194  	case packageurl.TypeConan:
   195  		return ConanPkg
   196  	case packageurl.TypeHackage:
   197  		return HackagePkg
   198  	case "portage":
   199  		return PortagePkg
   200  	case packageurl.TypeHex:
   201  		return HexPkg
   202  	case packageurl.TypeOTP:
   203  		return ErlangOTPPkg
   204  	case "linux-kernel":
   205  		return LinuxKernelPkg
   206  	case "linux-kernel-module":
   207  		return LinuxKernelModulePkg
   208  	case "nix":
   209  		return NixPkg
   210  	case packageurl.TypeCran:
   211  		return Rpkg
   212  	case packageurl.TypeSwift:
   213  		return SwiftPkg
   214  	case "wordpress-plugin":
   215  		return WordpressPluginPkg
   216  	default:
   217  		return UnknownPkg
   218  	}
   219  }