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