-
Notifications
You must be signed in to change notification settings - Fork 593
/
Copy pathlanguage.go
113 lines (104 loc) · 2.92 KB
/
language.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package pkg
import (
"strings"
"github.com/anchore/packageurl-go"
)
// Language represents a single programming language.
type Language string
const (
// the full set of supported programming languages
UnknownLanguage Language = ""
CPP Language = "c++"
Dart Language = "dart"
Dotnet Language = "dotnet"
Elixir Language = "elixir"
Erlang Language = "erlang"
Go Language = "go"
Haskell Language = "haskell"
Java Language = "java"
JavaScript Language = "javascript"
Lua Language = "lua"
OCaml Language = "ocaml"
PHP Language = "php"
Python Language = "python"
R Language = "R"
Ruby Language = "ruby"
Rust Language = "rust"
Swift Language = "swift"
Swipl Language = "swipl"
)
// AllLanguages is a set of all programming languages detected by syft.
var AllLanguages = []Language{
CPP,
Dart,
Dotnet,
Elixir,
Erlang,
Go,
Haskell,
Java,
JavaScript,
Lua,
OCaml,
PHP,
Python,
R,
Ruby,
Rust,
Swift,
Swipl,
}
// String returns the string representation of the language.
func (l Language) String() string {
return string(l)
}
func LanguageFromPURL(p string) Language {
purl, err := packageurl.FromString(p)
if err != nil {
return UnknownLanguage
}
return LanguageByName(purl.Type)
}
func LanguageByName(name string) Language {
switch strings.ToLower(name) {
case packageurl.TypeMaven, string(purlGradlePkgType), string(JavaPkg), string(Java):
return Java
case packageurl.TypeComposer, string(PhpComposerPkg), string(PHP):
return PHP
case packageurl.TypeGolang, string(GoModulePkg), string(Go):
return Go
case packageurl.TypeNPM, string(JavaScript), "nodejs", "node.js":
return JavaScript
case packageurl.TypeLuaRocks, string(Lua):
return Lua
case packageurl.TypePyPi, string(Python):
return Python
case packageurl.TypeGem, string(Ruby):
return Ruby
case purlCargoPkgType, string(RustPkg), string(Rust):
return Rust
case packageurl.TypePub, string(DartPubPkg), string(Dart):
return Dart
case string(Dotnet), ".net", packageurl.TypeNuget:
return Dotnet
case packageurl.TypeCocoapods, packageurl.TypeSwift, string(CocoapodsPkg):
return Swift
case "swipl", string(SwiplPackPkg):
return Swipl
case "ocaml", string(OpamPkg):
return OCaml
case packageurl.TypeConan, string(CPP):
return CPP
case packageurl.TypeHackage, string(Haskell):
return Haskell
case packageurl.TypeHex, packageurl.TypeOTP, "beam", "elixir", "erlang":
// should we support returning multiple languages to support this case?
// answer: no. We want this to definitively answer "which language does this package represent?"
// which might not be possible in all cases. See for more context: https://github.com/package-url/purl-spec/pull/178
return UnknownLanguage
case packageurl.TypeCran, "r":
return R
default:
return UnknownLanguage
}
}