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